1 /*
2  * ManipulatorPopupPanel.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.views.plots.ui.manipulator;
16 
17 import org.rstudio.core.client.Debug;
18 import org.rstudio.core.client.widget.FocusHelper;
19 import org.rstudio.core.client.widget.MiniDialogPopupPanel;
20 import org.rstudio.studio.client.workbench.views.plots.model.Manipulator;
21 
22 import com.google.gwt.core.client.JsArrayString;
23 import com.google.gwt.dom.client.NativeEvent;
24 import com.google.gwt.event.dom.client.KeyCodes;
25 
26 import com.google.gwt.user.client.Event;
27 import com.google.gwt.user.client.ui.VerticalPanel;
28 import com.google.gwt.user.client.ui.Widget;
29 
30 public class ManipulatorPopupPanel extends MiniDialogPopupPanel
31 
32 {
ManipulatorPopupPanel(final ManipulatorChangedHandler changedHandler)33    public ManipulatorPopupPanel(final ManipulatorChangedHandler changedHandler)
34    {
35       super(true, false);
36 
37       changedHandler_ = changedHandler;
38 
39 
40       setCaption("Manipulate");
41 
42 
43 
44 
45    }
46 
47    @Override
createMainWidget()48    protected Widget createMainWidget()
49    {
50       mainPanel_ = new VerticalPanel();
51       mainPanel_.setWidth("250px");
52       return mainPanel_;
53    }
54 
update(Manipulator manipulator)55    public void update(Manipulator manipulator)
56    {
57       mainPanel_.clear();
58 
59       if (manipulator != null && manipulator.getVariables() != null)
60       {
61          // iterate over the variables
62          JsArrayString variables = manipulator.getVariables();
63          for (int i=0; i<variables.length(); i++)
64          {
65             String variable = variables.get(i);
66             try
67             {
68                ManipulatorControl addedControl = null;
69                Manipulator.Control control = manipulator.getControl(variable);
70                switch(control.getType())
71                {
72                case Manipulator.Control.SLIDER:
73                   Manipulator.Slider slider = control.cast();
74                   addedControl = addSliderControl(
75                                           variable,
76                                           manipulator.getDoubleValue(variable),
77                                           slider);
78                   break;
79                case Manipulator.Control.PICKER:
80                   Manipulator.Picker picker = control.cast();
81                   addedControl = addPickerControl(
82                                           variable,
83                                           manipulator.getStringValue(variable),
84                                           picker);
85                   break;
86                case Manipulator.Control.CHECKBOX:
87                   Manipulator.CheckBox checkBox = control.cast();
88                   addedControl = addCheckBoxControl(
89                                           variable,
90                                           manipulator.getBooleanValue(variable),
91                                           checkBox);
92                   break;
93                case Manipulator.Control.BUTTON:
94                   Manipulator.Button button = control.cast();
95                   addedControl = addButtonControl(variable, button);
96                   break;
97                }
98 
99                // save reference to first control (for setting focus)
100                if (i == 0)
101                   firstControl_ = addedControl;
102             }
103             catch(Throwable e)
104             {
105                Debug.log("WARNING: exception occurred during addition of " +
106                          "variable " + variable + ", " + e.getMessage());
107             }
108 
109          }
110       }
111    }
112 
focusFirstControl()113    public void focusFirstControl()
114    {
115       if (firstControl_ != null) // defensive
116       {
117          FocusHelper.setFocusDeferred(firstControl_);
118       }
119    }
120 
addSliderControl(String variable, double value, Manipulator.Slider slider)121    private ManipulatorControl addSliderControl(String variable,
122                                                double value,
123                                                Manipulator.Slider slider)
124    {
125       ManipulatorControlSlider sliderControl =
126          new ManipulatorControlSlider(variable, value, slider, changedHandler_);
127       mainPanel_.add(sliderControl);
128       return sliderControl;
129    }
130 
addPickerControl(String variable, String value, Manipulator.Picker picker)131    private ManipulatorControl addPickerControl(String variable,
132                                                String value,
133                                                Manipulator.Picker picker)
134    {
135       ManipulatorControlPicker pickerControl =
136          new ManipulatorControlPicker(variable, value, picker, changedHandler_);
137       mainPanel_.add(pickerControl);
138       return pickerControl;
139    }
140 
addCheckBoxControl(String variable, boolean value, Manipulator.CheckBox checkBox)141    private ManipulatorControl addCheckBoxControl(String variable,
142                                                  boolean value,
143                                                  Manipulator.CheckBox checkBox)
144    {
145       ManipulatorControlCheckBox checkBoxControl =
146          new ManipulatorControlCheckBox(variable, value, checkBox, changedHandler_);
147       mainPanel_.add(checkBoxControl);
148       return checkBoxControl;
149    }
150 
addButtonControl(String variable, Manipulator.Button button)151    private ManipulatorControl addButtonControl(String variable,
152                                                Manipulator.Button button)
153 {
154       ManipulatorControlButton buttonControl =
155          new ManipulatorControlButton(variable, button, changedHandler_);
156       mainPanel_.add(buttonControl);
157       return buttonControl;
158 }
159 
160 
161 
162    @Override
onPreviewNativeEvent(Event.NativePreviewEvent event)163    public void onPreviewNativeEvent(Event.NativePreviewEvent event)
164    {
165 
166       if (event.getTypeInt() == Event.ONKEYDOWN)
167       {
168          NativeEvent nativeEvent = event.getNativeEvent();
169          switch (nativeEvent.getKeyCode())
170          {
171             case KeyCodes.KEY_ESCAPE:
172                nativeEvent.preventDefault();
173                nativeEvent.stopPropagation();
174                event.cancel();
175                hideMiniDialog();
176                break;
177          }
178       }
179    }
180 
181    private VerticalPanel mainPanel_;
182    private ManipulatorControl firstControl_;
183    private final ManipulatorChangedHandler changedHandler_;
184 
185 }
186