-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
PaneManager.java
436 lines (390 loc) · 15 KB
/
PaneManager.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
/*
* PaneManager.java
*
* Copyright (C) 2009-12 by RStudio, Inc.
*
* This program is licensed to you under the terms of version 3 of the
* GNU Affero General Public License. This program is distributed WITHOUT
* ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
* MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
* AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
*
*/
package org.rstudio.studio.client.workbench.ui;
import com.google.gwt.core.client.JsArrayString;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.name.Named;
import org.rstudio.core.client.Debug;
import org.rstudio.core.client.Triad;
import org.rstudio.core.client.dom.WindowEx;
import org.rstudio.core.client.events.WindowStateChangeEvent;
import org.rstudio.core.client.layout.DualWindowLayoutPanel;
import org.rstudio.core.client.layout.LogicalWindow;
import org.rstudio.core.client.layout.WindowState;
import org.rstudio.core.client.theme.MinimizedModuleTabLayoutPanel;
import org.rstudio.core.client.theme.MinimizedWindowFrame;
import org.rstudio.core.client.theme.PrimaryWindowFrame;
import org.rstudio.core.client.theme.WindowFrame;
import org.rstudio.core.client.theme.res.ThemeResources;
import org.rstudio.core.client.widget.ToolbarButton;
import org.rstudio.studio.client.application.events.EventBus;
import org.rstudio.studio.client.workbench.commands.Commands;
import org.rstudio.studio.client.workbench.model.ClientState;
import org.rstudio.studio.client.workbench.model.Session;
import org.rstudio.studio.client.workbench.model.WorkbenchServerOperations;
import org.rstudio.studio.client.workbench.model.helper.IntStateValue;
import org.rstudio.studio.client.workbench.prefs.model.UIPrefs;
import org.rstudio.studio.client.workbench.views.console.ConsoleInterruptButton;
import org.rstudio.studio.client.workbench.views.console.ConsolePane;
import org.rstudio.studio.client.workbench.views.output.find.FindOutputTab;
import org.rstudio.studio.client.workbench.views.source.SourceShim;
import java.util.ArrayList;
import java.util.HashMap;
/*
* TODO: Push client state when selected tab or layout changes
*/
public class PaneManager
{
public enum Tab {
Workspace, History, Files, Plots, Packages, Help, VCS, Build
}
class SelectedTabStateValue extends IntStateValue
{
SelectedTabStateValue(String name,
WorkbenchTabPanel tabPanel)
{
super("workbench-pane", name, ClientState.PROJECT_PERSISTENT,
session_.getSessionInfo().getClientState(), true);
tabPanel_ = tabPanel;
finishInit(session_.getSessionInfo().getClientState());
}
@Override
protected void onInit(Integer value)
{
if (value != null)
tabPanel_.selectTab(value);
}
@Override
protected Integer getValue() { return tabPanel_.getSelectedIndex(); }
private final WorkbenchTabPanel tabPanel_;
}
@Inject
public PaneManager(Provider<MainSplitPanel> pSplitPanel,
WorkbenchServerOperations server,
EventBus eventBus,
Session session,
Commands commands,
UIPrefs uiPrefs,
@Named("Console") final Widget consolePane,
ConsoleInterruptButton consoleInterrupt,
SourceShim source,
@Named("Workspace") final WorkbenchTab workspaceTab,
@Named("History") final WorkbenchTab historyTab,
@Named("Files") final WorkbenchTab filesTab,
@Named("Plots") final WorkbenchTab plotsTab,
@Named("Packages") final WorkbenchTab packagesTab,
@Named("Help") final WorkbenchTab helpTab,
@Named("VCS") final WorkbenchTab vcsTab,
@Named("Build") final WorkbenchTab buildTab,
@Named("Compile PDF") final WorkbenchTab compilePdfTab,
final FindOutputTab findOutputTab)
{
eventBus_ = eventBus;
session_ = session;
commands_ = commands;
consolePane_ = (ConsolePane)consolePane;
consoleInterrupt_ = consoleInterrupt;
source_ = source;
workspaceTab_ = workspaceTab;
historyTab_ = historyTab;
filesTab_ = filesTab;
plotsTab_ = plotsTab;
packagesTab_ = packagesTab;
helpTab_ = helpTab;
vcsTab_ = vcsTab;
buildTab_ = buildTab;
compilePdfTab_ = compilePdfTab;
findOutputTab_ = findOutputTab;
PaneConfig config = validateConfig(uiPrefs.paneConfig().getValue());
initPanes(config);
panes_ = createPanes(config);
left_ = createSplitWindow(panes_.get(0), panes_.get(1), "left");
right_ = createSplitWindow(panes_.get(2), panes_.get(3), "right");
panel_ = pSplitPanel.get();
panel_.initialize(left_, right_);
if (session_.getSessionInfo().getSourceDocuments().length() == 0
&& sourceLogicalWindow_.getState() != WindowState.HIDE)
{
sourceLogicalWindow_.onWindowStateChange(
new WindowStateChangeEvent(WindowState.HIDE));
}
else if (session_.getSessionInfo().getSourceDocuments().length() > 0
&& sourceLogicalWindow_.getState() == WindowState.HIDE)
{
sourceLogicalWindow_.onWindowStateChange(
new WindowStateChangeEvent(WindowState.NORMAL));
}
uiPrefs.paneConfig().addValueChangeHandler(new ValueChangeHandler<PaneConfig>()
{
public void onValueChange(ValueChangeEvent<PaneConfig> evt)
{
ArrayList<LogicalWindow> newPanes = createPanes(evt.getValue());
panes_ = newPanes;
left_.replaceWindows(newPanes.get(0), newPanes.get(1));
right_.replaceWindows(newPanes.get(2), newPanes.get(3));
tabSet1TabPanel_.clear();
tabSet2TabPanel_.clear();
populateTabPanel(tabNamesToTabs(evt.getValue().getTabSet1()),
tabSet1TabPanel_, tabSet1MinPanel_);
populateTabPanel(tabNamesToTabs(evt.getValue().getTabSet2()),
tabSet2TabPanel_, tabSet2MinPanel_);
}
});
}
private ArrayList<LogicalWindow> createPanes(PaneConfig config)
{
ArrayList<LogicalWindow> results = new ArrayList<LogicalWindow>();
JsArrayString panes = config.getPanes();
for (int i = 0; i < 4; i++)
{
results.add(panesByName_.get(panes.get(i)));
}
return results;
}
private void initPanes(PaneConfig config)
{
panesByName_ = new HashMap<String, LogicalWindow>();
panesByName_.put("Console", createConsole());
panesByName_.put("Source", createSource());
Triad<LogicalWindow, WorkbenchTabPanel, MinimizedModuleTabLayoutPanel> ts1 = createTabSet(
"TabSet1",
tabNamesToTabs(config.getTabSet1()));
panesByName_.put("TabSet1", ts1.first);
tabSet1TabPanel_ = ts1.second;
tabSet1MinPanel_ = ts1.third;
Triad<LogicalWindow, WorkbenchTabPanel, MinimizedModuleTabLayoutPanel> ts2 = createTabSet(
"TabSet2",
tabNamesToTabs(config.getTabSet2()));
panesByName_.put("TabSet2", ts2.first);
tabSet2TabPanel_ = ts2.second;
tabSet2MinPanel_ = ts2.third;
}
private ArrayList<Tab> tabNamesToTabs(JsArrayString tabNames)
{
ArrayList<Tab> tabList = new ArrayList<Tab>();
for (int j = 0; j < tabNames.length(); j++)
tabList.add(Enum.valueOf(Tab.class, tabNames.get(j)));
return tabList;
}
private PaneConfig validateConfig(PaneConfig config)
{
if (config == null)
config = PaneConfig.createDefault();
if (!config.validateAndAutoCorrect())
{
Debug.log("Pane config is not valid");
config = PaneConfig.createDefault();
}
return config;
}
public MainSplitPanel getPanel()
{
return panel_;
}
public WorkbenchTab getTab(Tab tab)
{
switch (tab)
{
case Workspace:
return workspaceTab_;
case History:
return historyTab_;
case Files:
return filesTab_;
case Plots:
return plotsTab_;
case Packages:
return packagesTab_;
case Help:
return helpTab_;
case VCS:
return vcsTab_;
case Build:
return buildTab_;
}
throw new IllegalArgumentException("Unknown tab");
}
public WorkbenchTab[] getAllTabs()
{
return new WorkbenchTab[] { workspaceTab_, historyTab_, filesTab_,
plotsTab_, packagesTab_, helpTab_,
vcsTab_, buildTab_};
}
public void activateTab(Tab tab)
{
WindowEx.get().focus();
tabToPanel_.get(tab).selectTab(tabToIndex_.get(tab));
}
public ConsolePane getConsole()
{
return consolePane_;
}
public WorkbenchTabPanel getOwnerTabPanel(Tab tab)
{
return tabToPanel_.get(tab);
}
public LogicalWindow getSourceLogicalWindow()
{
return sourceLogicalWindow_;
}
private DualWindowLayoutPanel createSplitWindow(LogicalWindow top,
LogicalWindow bottom,
String name)
{
return new DualWindowLayoutPanel(
eventBus_,
top,
bottom,
session_,
name,
WindowState.NORMAL,
(int) (Window.getClientHeight()*0.6));
}
private LogicalWindow createConsole()
{
PrimaryWindowFrame frame = new PrimaryWindowFrame("Console", null);
ToolbarButton goToWorkingDirButton =
commands_.goToWorkingDir().createToolbarButton();
goToWorkingDirButton.addStyleName(
ThemeResources.INSTANCE.themeStyles().windowFrameToolbarButton());
@SuppressWarnings("unused")
ConsoleTabPanel consoleTabPanel = new ConsoleTabPanel(frame,
consolePane_,
compilePdfTab_,
findOutputTab_,
eventBus_,
consoleInterrupt_,
goToWorkingDirButton);
return new LogicalWindow(frame, new MinimizedWindowFrame("Console"));
}
private LogicalWindow createSource()
{
WindowFrame sourceFrame = new WindowFrame();
sourceFrame.setFillWidget(source_.asWidget());
source_.forceLoad();
return sourceLogicalWindow_ = new LogicalWindow(
sourceFrame,
new MinimizedWindowFrame("Source"));
}
private
Triad<LogicalWindow, WorkbenchTabPanel, MinimizedModuleTabLayoutPanel>
createTabSet(String persisterName, ArrayList<Tab> tabs)
{
final WindowFrame frame = new WindowFrame();
final WorkbenchTabPanel tabPanel = new WorkbenchTabPanel(frame);
MinimizedModuleTabLayoutPanel minimized = new MinimizedModuleTabLayoutPanel();
populateTabPanel(tabs, tabPanel, minimized);
frame.setFillWidget(tabPanel);
minimized.addSelectionHandler(new SelectionHandler<Integer>()
{
public void onSelection(SelectionEvent<Integer> integerSelectionEvent)
{
int tab = integerSelectionEvent.getSelectedItem();
tabPanel.selectTab(tab);
}
});
tabPanel.addSelectionHandler(new SelectionHandler<Integer>()
{
public void onSelection(SelectionEvent<Integer> integerSelectionEvent)
{
session_.persistClientState();
}
});
new SelectedTabStateValue(persisterName, tabPanel);
return new Triad<LogicalWindow, WorkbenchTabPanel, MinimizedModuleTabLayoutPanel>(
new LogicalWindow(frame, minimized),
tabPanel,
minimized);
}
private void populateTabPanel(ArrayList<Tab> tabs,
WorkbenchTabPanel tabPanel,
MinimizedModuleTabLayoutPanel minimized)
{
ArrayList<WorkbenchTab> tabList = new ArrayList<WorkbenchTab>();
for (int i = 0; i < tabs.size(); i++)
{
Tab tab = tabs.get(i);
tabList.add(getTab(tab));
tabToPanel_.put(tab, tabPanel);
tabToIndex_.put(tab, i);
}
tabPanel.setTabs(tabList);
ArrayList<String> labels = new ArrayList<String>();
for (Tab tab : tabs)
{
if (!getTab(tab).isSuppressed())
labels.add(getTabLabel(tab));
}
minimized.setTabs(labels.toArray(new String[labels.size()]));
}
private String getTabLabel(Tab tab)
{
switch (tab)
{
case Workspace:
return "Workspace";
case History:
return "History";
case Files:
return "Files";
case Plots:
return "Plots";
case Packages:
return "Packages";
case Help:
return "Help";
case VCS:
return getTab(tab).getTitle();
case Build:
return "Build";
}
return "??";
}
private final EventBus eventBus_;
private final Session session_;
private final Commands commands_;
private final FindOutputTab findOutputTab_;
private final WorkbenchTab compilePdfTab_;
private final ConsolePane consolePane_;
private final ConsoleInterruptButton consoleInterrupt_;
private final SourceShim source_;
private final WorkbenchTab workspaceTab_;
private final WorkbenchTab historyTab_;
private final WorkbenchTab filesTab_;
private final WorkbenchTab plotsTab_;
private final WorkbenchTab packagesTab_;
private final WorkbenchTab helpTab_;
private final WorkbenchTab vcsTab_;
private final WorkbenchTab buildTab_;
private MainSplitPanel panel_;
private LogicalWindow sourceLogicalWindow_;
private final HashMap<Tab, WorkbenchTabPanel> tabToPanel_ =
new HashMap<Tab, WorkbenchTabPanel>();
private final HashMap<Tab, Integer> tabToIndex_ =
new HashMap<Tab, Integer>();
private HashMap<String, LogicalWindow> panesByName_;
private DualWindowLayoutPanel left_;
private DualWindowLayoutPanel right_;
private ArrayList<LogicalWindow> panes_;
private WorkbenchTabPanel tabSet1TabPanel_;
private MinimizedModuleTabLayoutPanel tabSet1MinPanel_;
private WorkbenchTabPanel tabSet2TabPanel_;
private MinimizedModuleTabLayoutPanel tabSet2MinPanel_;
}