1 /*
2  * PreferencesPane.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 
18 import com.google.gwt.user.client.Command;
19 import com.google.gwt.user.client.ui.CheckBox;
20 
21 import org.rstudio.core.client.prefs.PreferencesDialogPaneBase;
22 import org.rstudio.core.client.prefs.RestartRequirement;
23 import org.rstudio.core.client.widget.NumericValueWidget;
24 import org.rstudio.studio.client.workbench.prefs.model.Prefs.PrefValue;
25 import org.rstudio.studio.client.workbench.prefs.model.UserPrefs;
26 
27 import java.util.ArrayList;
28 
29 public abstract class PreferencesPane extends PreferencesDialogPaneBase<UserPrefs>
30 {
31    @Override
onApply(UserPrefs rPrefs)32    public RestartRequirement onApply(UserPrefs rPrefs)
33    {
34       for (Command cmd : onApplyCommands_)
35          cmd.execute();
36       return new RestartRequirement();
37    }
38 
39    /**
40     * Create a checkbox bound to a user preference with `lessSpaced` margin style.
41     * @param label checkbox label
42     * @param prefValue boolean preference to bind to
43     * @return CheckBox
44     */
checkboxPref(String label, final PrefValue<Boolean> prefValue)45    protected CheckBox checkboxPref(String label,
46          final PrefValue<Boolean> prefValue)
47    {
48       return checkboxPref(label, prefValue, null /*title*/, true /*defaultSpaced*/);
49    }
50 
51    /**
52     * Create a checkbox bound to a user preference.
53     * @param label checkbox label
54     * @param prefValue bound boolean preference
55     * @param defaultSpaced if true apply `lessSpaced style`, otherwise no spacing
56     * style will be applied
57     * @return CheckBox
58     */
checkboxPref(String label, final PrefValue<Boolean> prefValue, boolean defaultSpaced)59    protected CheckBox checkboxPref(String label,
60                                    final PrefValue<Boolean> prefValue,
61                                    boolean defaultSpaced)
62    {
63       return checkboxPref(label, prefValue, null /*title*/, defaultSpaced);
64    }
65 
66    /**
67     * Create a checkbox bound to a user preference with `lessSpaced` margin style.
68     * @param label checkbox label
69     * @param prefValue bound boolean preference
70     * @param title checkbox title; typically shown via tooltip
71     * @return CheckBox
72     */
checkboxPref(String label, final PrefValue<Boolean> prefValue, String title)73    protected CheckBox checkboxPref(String label,
74                                    final PrefValue<Boolean> prefValue,
75                                    String title)
76    {
77       return checkboxPref(label, prefValue, title, true /*defaultSpaced*/);
78    }
79 
80    /**
81     * Create a checkbox bound to a user preference.
82     * @param label checkbox label
83     * @param prefValue bound boolean preference
84     * @param title checkbox title; typically shown via tooltip
85     * @param defaultSpaced if true apply `lessSpaced` style, otherwise no spacing
86     * style will be applied
87     * @return CheckBox
88     */
checkboxPref(String label, final PrefValue<Boolean> prefValue, String title, boolean defaultSpaced)89    protected CheckBox checkboxPref(String label,
90                                    final PrefValue<Boolean> prefValue,
91                                    String title,
92                                    boolean defaultSpaced)
93    {
94       final CheckBox checkBox = new CheckBox(label, false);
95       if (defaultSpaced)
96          lessSpaced(checkBox);
97       checkBox.setValue(prefValue.getGlobalValue());
98       if (title != null)
99          checkBox.setTitle(title);
100       onApplyCommands_.add(new Command()
101       {
102          public void execute()
103          {
104             prefValue.setGlobalValue(checkBox.getValue());
105          }
106       });
107       return checkBox;
108    }
109 
110    /**
111     * Prompt for integer preference value in range [0 - maxint]
112     */
numericPref(String label, final PrefValue<Integer> prefValue)113    protected NumericValueWidget numericPref(String label,
114                                             final PrefValue<Integer> prefValue)
115    {
116       return numericPref(label, NumericValueWidget.ZeroMinimum,
117             NumericValueWidget.NoMaximum,
118             prefValue);
119    }
120 
numericPref(String label, Integer minValue, Integer maxValue, final PrefValue<Integer> prefValue)121    protected NumericValueWidget numericPref(String label,
122                                             Integer minValue,
123                                             Integer maxValue,
124                                             final PrefValue<Integer> prefValue)
125    {
126       return numericPref(label, minValue, maxValue, prefValue, true);
127    }
128 
129    /**
130     * Prompt for integer preference value in range [min, max]
131     *
132     * @param label
133     * @param minValue minimum value or NumericValueWidget.ZeroMinimum
134     * @param maxValue maximum value or NumericValueWidget.NoMaximum
135     * @param prefValue
136     * @param defaultSpaced
137     * @return
138     */
numericPref(String label, Integer minValue, Integer maxValue, final PrefValue<Integer> prefValue, boolean defaultSpaced)139    protected NumericValueWidget numericPref(String label,
140                                             Integer minValue,
141                                             Integer maxValue,
142                                             final PrefValue<Integer> prefValue,
143                                             boolean defaultSpaced)
144    {
145       final NumericValueWidget widget = new NumericValueWidget(label, minValue, maxValue);
146       if (defaultSpaced)
147          lessSpaced(widget);
148       registerEnsureVisibleHandler(widget);
149       widget.setValue(prefValue.getGlobalValue() + "");
150       onApplyCommands_.add(new Command()
151       {
152          public void execute()
153          {
154             try
155             {
156                prefValue.setGlobalValue(Integer.parseInt(widget.getValue()));
157             }
158             catch (Exception e)
159             {
160                // It's OK for this to be invalid if we got past validation--
161                // that means the associated checkbox wasn't checked
162             }
163          }
164       });
165       return widget;
166    }
167 
168    protected final ArrayList<Command> onApplyCommands_ = new ArrayList<>();
169 }
170