1 /*
2  * PythonPreferencesPane.java
3  *
4  * Copyright (C) 2021 by RStudio, PBC
5  *
6  * Unless you have received this program directly from RStudio pursuant
7  * to the terms of a commercial license agreement with RStudio, then
8  * this program is licensed to you under the terms of version 3 of the
9  * GNU Affero General Public License. This program is distributed WITHOUT
10  * ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
11  * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
12  * AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
13  *
14  */
15 package org.rstudio.studio.client.workbench.prefs.views;
16 
17 import org.rstudio.core.client.StringUtil;
18 import org.rstudio.core.client.prefs.RestartRequirement;
19 import org.rstudio.studio.client.workbench.prefs.model.UserPrefs;
20 
21 import com.google.gwt.user.client.ui.Label;
22 import com.google.inject.Inject;
23 
24 public class PythonPreferencesPane extends PythonPreferencesPaneBase<UserPrefs>
25 {
26    @Inject
PythonPreferencesPane(PythonDialogResources res, PythonServerOperations server)27    public PythonPreferencesPane(PythonDialogResources res,
28                                 PythonServerOperations server)
29    {
30       super("420px", "(No interpreter selected)", false);
31 
32       overrideLabel_ = new Label();
33       add(overrideLabel_);
34    }
35 
36    @Override
initialize(UserPrefs prefs)37    protected void initialize(UserPrefs prefs)
38    {
39       String pythonPath = prefs.pythonPath().getGlobalValue();
40       initialize(pythonPath);
41 
42       // notify user if project pref is overriding global pref
43       String projectPythonPath = prefs.pythonPath().getProjectValue();
44       boolean hasProjectOverride = !StringUtil.isNullOrEmpty(projectPythonPath);
45       if (hasProjectOverride)
46       {
47          String text =
48                "(NOTE: This project has already been configured with " +
49                "its own Python interpreter. Use the Project Options " +
50                "dialog to change the version of Python used in this project.)";
51 
52          overrideLabel_.setText(text);
53          overrideLabel_.setVisible(true);
54          overrideLabel_.addStyleName(RES.styles().overrideLabel());
55       }
56       else
57       {
58          overrideLabel_.setVisible(false);
59          overrideLabel_.removeStyleName(RES.styles().overrideLabel());
60       }
61    }
62 
63    @Override
onApply(UserPrefs prefs)64    public RestartRequirement onApply(UserPrefs prefs)
65    {
66       return onApply(false, (PythonInterpreter interpreter) ->
67       {
68          if (interpreter.isValid())
69          {
70             prefs.pythonType().setGlobalValue(interpreter.getType());
71             prefs.pythonVersion().setGlobalValue(interpreter.getVersion());
72             prefs.pythonPath().setGlobalValue(interpreter.getPath());
73          }
74          else
75          {
76             prefs.pythonType().removeGlobalValue(true);
77             prefs.pythonVersion().removeGlobalValue(true);
78             prefs.pythonPath().removeGlobalValue(true);
79          }
80       });
81    }
82 
83    private final Label overrideLabel_;
84 }
85