This repository has been archived by the owner on Feb 6, 2022. It is now read-only.
/
RunHelperDialog.java
219 lines (181 loc) · 7.72 KB
/
RunHelperDialog.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
/*
* Copyright 2012 Chris Sinjakli
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package uk.co.sinjakli.eclipserunhelper.ui;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.ILog;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchConfigurationType;
import org.eclipse.debug.core.ILaunchManager;
import org.eclipse.debug.ui.DebugUITools;
import org.eclipse.jface.dialogs.PopupDialog;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ColumnLabelProvider;
import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import uk.co.sinjakli.eclipserunhelper.RunHelperPlugin;
public class RunHelperDialog extends PopupDialog {
private final Map<String, ILaunchConfiguration> availableLaunches;
private final String launchType;
private final ILog logger;
private final Set<Image> disposableImages;
public RunHelperDialog(final Shell parent, final Map<String, ILaunchConfiguration> availableLaunches, final String launchType) {
super(parent, PopupDialog.INFOPOPUP_SHELLSTYLE, true, false,
false, false, false, null, null);
this.availableLaunches = availableLaunches;
this.launchType = launchType;
logger = RunHelperPlugin.getDefault().getLog();
disposableImages = new HashSet<Image>();
}
@Override
protected Control createDialogArea(final Composite parent) {
final Composite composite = (Composite) super.createDialogArea(parent);
final Table table = new Table(composite, SWT.SINGLE | SWT.FULL_SELECTION | SWT.BORDER);
table.setLinesVisible(true);
table.setHeaderVisible(false);
final GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
table.setLayoutData(data);
final TableColumn launchNameColumn = new TableColumn(table, SWT.NONE);
final TableColumn keyBindingColumn = new TableColumn(table, SWT.NONE);
final TableViewer tableViewer = new TableViewer(table);
tableViewer.setContentProvider(ArrayContentProvider.getInstance());
tableViewer.setInput(availableLaunches.keySet());
final TableViewerColumn launchNameColumnViewer = new TableViewerColumn(tableViewer, launchNameColumn);
launchNameColumnViewer.setLabelProvider(new ColumnLabelProvider() {
@Override
public String getText(final Object element) {
final String keyString = (String) element;
final ILaunchConfiguration launchConfiguration = availableLaunches.get(keyString);
final String launchPrefix;
if (launchType.equals(ILaunchManager.RUN_MODE)){
launchPrefix = "Run ";
} else {
launchPrefix = "Debug ";
}
return launchPrefix + launchConfiguration.getName();
}
@Override
public Image getImage(final Object element) {
final String keyString = (String) element;
final ILaunchConfiguration launchConfiguration = availableLaunches.get(keyString);
try {
final ILaunchConfigurationType launchConfigurationType = launchConfiguration.getType();
final Image launchImage = DebugUITools.getDefaultImageDescriptor(launchConfigurationType).createImage(true);
disposableImages.add(launchImage);
return launchImage;
} catch (final CoreException e) {
final IStatus errorStatus = RunHelperPlugin.errorStatus("Error getting launch configuration type.", e);
logger.log(errorStatus);
}
return super.getImage(element);
}
});
final TableViewerColumn keyBindingColumnViewer = new TableViewerColumn(tableViewer, keyBindingColumn);
keyBindingColumnViewer.setLabelProvider(new ColumnLabelProvider() {
@Override
public String getText(final Object element) {
final String keyString = (String) element;
return keyString;
}
});
tableViewer.addDoubleClickListener(new IDoubleClickListener() {
@Override
public void doubleClick(final DoubleClickEvent event) {
final IStructuredSelection selection = (IStructuredSelection) event.getSelection();
final String keyString = (String) selection.getFirstElement();
final ILaunchConfiguration launchConfiguration = availableLaunches.get(keyString);
launchAndCloseDialog(launchConfiguration);
}
});
table.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(final KeyEvent e) {
if (isEnterKey(e.keyCode)) {
final TableItem tableSelection = table.getSelection()[0];
final String keyString = (String) tableSelection.getData();
final ILaunchConfiguration launchConfiguration = availableLaunches.get(keyString);
launchAndCloseDialog(launchConfiguration);
} else if (availableLaunches.containsKey(String.valueOf(e.character))) {
launchByCharacter(e.character);
} else {
super.keyPressed(e);
}
}
private boolean isEnterKey(final int keyCode) {
return keyCode == SWT.CR || keyCode == SWT.LF;
}
});
// Needed so that eclipse updates table based on our label providers
tableViewer.refresh();
table.getColumn(0).pack();
table.getColumn(1).pack();
composite.pack();
table.setSelection(0);
return composite;
}
private void launchAndCloseDialog(final ILaunchConfiguration launchConfiguration) {
try {
launchConfiguration.launch(launchType, null);
} catch (final CoreException ex) {
final IStatus errorStatus = RunHelperPlugin.errorStatus("Error launching selected configuration.", ex);
logger.log(errorStatus);
}
RunHelperDialog.this.close();
}
private void launchByCharacter(final char keyCharacter) {
final String keyString = String.valueOf(keyCharacter);
final ILaunchConfiguration launchConfiguration = availableLaunches.get(keyString);
launchAndCloseDialog(launchConfiguration);
}
@Override
protected Point getInitialLocation(final Point initialSize) {
final Composite parent = getParentShell();
final Rectangle parentBounds = parent.getBounds();
final int padding = 10;
final int absoluteParentRightEdge = parentBounds.width + parent.getLocation().x;
final int absoluteParentBottomEdge = parentBounds.height + parent.getLocation().y;
final int initialX = absoluteParentRightEdge - initialSize.x - padding;
final int initialY = absoluteParentBottomEdge - initialSize.y - padding;
return new Point(initialX, initialY);
}
@Override
public boolean close() {
for (final Image image : disposableImages) {
image.dispose();
}
return super.close();
}
}