forked from hpcc-systems/EclipsePlugin
/
ECLEditorToolbar.java
257 lines (231 loc) · 8.19 KB
/
ECLEditorToolbar.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
package org.hpccsystems.eclide.editors;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchConfigurationListener;
import org.eclipse.debug.core.ILaunchManager;
import org.eclipse.debug.internal.ui.SWTFactory;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.hpccsystems.eclide.Activator;
import org.hpccsystems.eclide.resources.Messages;
import org.hpccsystems.internal.data.Cluster;
import org.hpccsystems.internal.data.Data;
import org.hpccsystems.internal.data.Platform;
import org.osgi.service.prefs.BackingStoreException;
import org.osgi.service.prefs.Preferences;
import de.ikoffice.widgets.SplitButton;
import de.ikoffice.widgets.SplitButtonSelectionListener;
public class ECLEditorToolbar extends Composite {
private IFile file;
private ILaunchConfiguration[] configurations;
private SplitButton submitButton;
private Combo comboServer;
private Combo comboTarget;
public ECLEditorToolbar(IFile sourceFile, Composite parent, int style) {
super(parent, style);
file = sourceFile;
GridLayout innerTopLayout = new GridLayout(5, false);
innerTopLayout.marginHeight = 0;
innerTopLayout.marginWidth = 0;
setLayout(innerTopLayout);
Display display = Display.getCurrent();
Color titleBackground = display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND);
setBackground(titleBackground);
FormData fData = new FormData();
fData.top = new FormAttachment(0);
fData.left = new FormAttachment(0);
fData.right = new FormAttachment(100);
setLayoutData(fData);
submitButton = new SplitButton(this, SWT.NONE);
submitButton.setText(Messages.Submit);
GridData gd = new GridData();
submitButton.setLayoutData(gd);
SWTFactory.setButtonDimensionHint(submitButton);
menuCreator(submitButton.getMenu());
submitButton.addSplitButtonSelectionListener(new SplitButtonSelectionListener() {
@Override
public boolean showMenu() {
return true;
}
@Override
public void buttonSelected() {
submit(false);
}
});
SWTFactory.createLabel(this, Messages.Server, 1);
comboServer = SWTFactory.createCombo(this, SWT.VERTICAL | SWT.DROP_DOWN | SWT.BORDER | SWT.READ_ONLY, 1, null);
comboServer.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
Preferences prefs = Activator.getPreferences("ECLEditorToolbar"); //$NON-NLS-1$
prefs.put("comboServer", comboServer.getText());
refreshTarget(true);
try {
prefs.flush();
} catch (BackingStoreException e1) {
}
}
});
SWTFactory.createLabel(this, Messages.Target, 1);
comboTarget = SWTFactory.createCombo(this, SWT.VERTICAL | SWT.DROP_DOWN | SWT.BORDER | SWT.READ_ONLY, 1, null);
comboTarget.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
Preferences prefs = Activator.getPreferences("ECLEditorToolbar"); //$NON-NLS-1$
prefs.put(comboServer.getText(), comboTarget.getText());
try {
prefs.flush();
} catch (BackingStoreException e1) {
}
}
});
ILaunchManager launchManager = DebugPlugin.getDefault().getLaunchManager();
launchManager.addLaunchConfigurationListener(new ILaunchConfigurationListener() {
@Override
public void launchConfigurationAdded(ILaunchConfiguration configuration) {
refreshLaunchConfigurations(false);
}
@Override
public void launchConfigurationChanged(ILaunchConfiguration configuration) {
refreshServers(false);
}
@Override
public void launchConfigurationRemoved(ILaunchConfiguration configuration) {
refreshLaunchConfigurations(false);
}
});
refreshLaunchConfigurations(true);
}
private void menuCreator(Menu menu) {
MenuItem[] items = menu.getItems();
if (items != null) {
for (MenuItem item : items) {
item.dispose();
}
}
MenuItem submit = new MenuItem(menu, SWT.PUSH);
submit.setText(Messages.Submit);
submit.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent arg0) {
submit(false);
}
});
MenuItem compile = new MenuItem(menu, SWT.PUSH);
compile.setText(Messages.Compile);
compile.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent arg0) {
submit(true);
}
});
MenuItem debug = new MenuItem(menu, SWT.PUSH);
debug.setText(Messages.Debug);
debug.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent arg0) {
}
});
debug.setEnabled(false);
}
ILaunchConfiguration getSelectedConfiguration() {
int selIndex = comboServer.getSelectionIndex();
if (selIndex >= 0 && selIndex < configurations.length) {
return configurations[comboServer.getSelectionIndex()];
}
return null;
}
void submit(boolean compileOnly) {
ILaunchConfiguration configuration = getSelectedConfiguration();
if (configuration != null) {
Platform platform = Data.get().GetPlatform(configuration);
platform.submit(configuration, file, comboTarget.getText(), compileOnly);
}
}
private void refreshLaunchConfigurations(boolean ignoreCurrent) {
configurations = new ILaunchConfiguration[0];
ILaunchManager launchManager = DebugPlugin.getDefault().getLaunchManager();
try {
configurations = launchManager.getLaunchConfigurations();
} catch (CoreException e) {
}
refreshServers(ignoreCurrent);
}
private void refreshServers(String defaultServer, boolean ignoreCurrent) {
String currentServer = comboServer.getText();
comboServer.removeAll();
int currentServerSel = -1;
int defaultServerSel = -1;
for (int i = 0; i < configurations.length; ++i) {
String name = configurations[i].getName();
comboServer.add(name);
if (name.equals(currentServer))
currentServerSel = i;
if (name.equals(defaultServer))
defaultServerSel = i;
}
if (currentServerSel >= 0)
comboServer.select(currentServerSel);
else if (defaultServerSel >= 0)
comboServer.select(defaultServerSel);
else
comboServer.select(0);
refreshTarget(ignoreCurrent);
}
private void refreshServers(boolean ignoreCurrent) {
Preferences prefs = Activator.getPreferences("ECLEditorToolbar"); //$NON-NLS-1$
refreshServers(prefs.get("comboServer", ""), ignoreCurrent); //$NON-NLS-1$ //$NON-NLS-2$
}
private void refreshTarget(String defaultTarget, boolean ignoreCurrent) {
String currentTarget = ignoreCurrent ? "" : comboServer.getText(); //$NON-NLS-1$
comboTarget.removeAll();
ILaunchConfiguration configuration = getSelectedConfiguration();
if (configuration != null) {
Platform platform = Data.get().GetPlatform(configuration);
if (platform != null) {
Cluster[] clusters = platform.getClusters();
int currentTargetSel = -1;
int defaultTargetSel = -1;
for (int i = 0; i < clusters.length; ++i) {
String name = clusters[i].getName();
comboTarget.add(name);
if (name.equals(currentTarget))
currentTargetSel = i;
if (name.equals(defaultTarget))
defaultTargetSel = i;
}
if (currentTargetSel >= 0)
comboTarget.select(currentTargetSel);
else if (defaultTargetSel >= 0)
comboTarget.select(defaultTargetSel);
}
}
}
private void refreshTarget(boolean ignoreCurrent) {
Preferences prefs = Activator.getPreferences("ECLEditorToolbar"); //$NON-NLS-1$
String defaultTarget = prefs.get(comboServer.getText(), ""); //$NON-NLS-1$
if (defaultTarget.isEmpty()) {
try {
ILaunchConfiguration configuration = getSelectedConfiguration();
if (configuration != null) {
defaultTarget = configuration.getAttribute(Platform.P_CLUSTER, ""); //$NON-NLS-1$
}
} catch (CoreException e) {
}
}
refreshTarget(defaultTarget, ignoreCurrent);
}
}