-
Notifications
You must be signed in to change notification settings - Fork 86
/
VariableBlock.java
444 lines (371 loc) · 13.8 KB
/
VariableBlock.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
437
438
439
440
441
442
443
444
/*******************************************************************************
* Copyright (c) 2000, 2011 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.jdt.internal.ui.wizards.buildpaths;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
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.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerComparator;
import org.eclipse.jface.window.Window;
import org.eclipse.ui.PlatformUI;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.IJavaModel;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.internal.ui.JavaPlugin;
import org.eclipse.jdt.internal.ui.util.CoreUtility;
import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
import org.eclipse.jdt.internal.ui.util.Progress;
import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages;
import org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField;
import org.eclipse.jdt.internal.ui.wizards.dialogfields.IDialogFieldListener;
import org.eclipse.jdt.internal.ui.wizards.dialogfields.IListAdapter;
import org.eclipse.jdt.internal.ui.wizards.dialogfields.LayoutUtil;
import org.eclipse.jdt.internal.ui.wizards.dialogfields.ListDialogField;
public class VariableBlock {
private final ListDialogField<CPVariableElement> fVariablesList;
private Control fControl;
private CLabel fWarning;
private boolean fHasChanges;
private List<CPVariableElement> fSelectedElements;
private boolean fAskToBuild;
private final boolean fEditOnDoubleclick;
public VariableBlock(boolean inPreferencePage, String initSelection) {
fSelectedElements= new ArrayList<>(0);
fEditOnDoubleclick= inPreferencePage;
fAskToBuild= true;
String[] buttonLabels= new String[] {
NewWizardMessages.VariableBlock_vars_add_button,
NewWizardMessages.VariableBlock_vars_edit_button,
NewWizardMessages.VariableBlock_vars_remove_button
};
VariablesAdapter adapter= new VariablesAdapter();
CPVariableElementLabelProvider labelProvider= new CPVariableElementLabelProvider(inPreferencePage);
fVariablesList= new ListDialogField<>(adapter, buttonLabels, labelProvider);
fVariablesList.setDialogFieldListener(adapter);
fVariablesList.setLabelText(NewWizardMessages.VariableBlock_vars_label);
fVariablesList.setRemoveButtonIndex(2);
fVariablesList.enableButton(1, false);
fVariablesList.setViewerComparator(new ViewerComparator() {
@Override
public int compare(Viewer viewer, Object e1, Object e2) {
if (e1 instanceof CPVariableElement && e2 instanceof CPVariableElement) {
return getComparator().compare(((CPVariableElement)e1).getName(), ((CPVariableElement)e2).getName());
}
return super.compare(viewer, e1, e2);
}
});
refresh(initSelection);
}
public boolean hasChanges() {
return fHasChanges;
}
public void setChanges(boolean hasChanges) {
fHasChanges= hasChanges;
}
public Control createContents(Composite parent) {
Composite composite= new Composite(parent, SWT.NONE);
composite.setFont(parent.getFont());
LayoutUtil.doDefaultLayout(composite, new DialogField[] { fVariablesList }, true, 0, 0);
LayoutUtil.setHorizontalGrabbing(fVariablesList.getListControl(null));
fWarning= new CLabel(composite, SWT.NONE);
fWarning.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false, fVariablesList.getNumberOfControls() - 1, 1));
fControl= composite;
updateDeprecationWarning();
return composite;
}
public void addDoubleClickListener(IDoubleClickListener listener) {
fVariablesList.getTableViewer().addDoubleClickListener(listener);
}
public void addSelectionChangedListener(ISelectionChangedListener listener) {
fVariablesList.getTableViewer().addSelectionChangedListener(listener);
}
private Shell getShell() {
if (fControl != null) {
return fControl.getShell();
}
return JavaPlugin.getActiveWorkbenchShell();
}
private class VariablesAdapter implements IDialogFieldListener, IListAdapter<CPVariableElement> {
// -------- IListAdapter --------
@Override
public void customButtonPressed(ListDialogField<CPVariableElement> field, int index) {
switch (index) {
case 0: /* add */
editEntries(null);
break;
case 1: /* edit */
List<CPVariableElement> selected= field.getSelectedElements();
editEntries(selected.get(0));
break;
}
}
@Override
public void selectionChanged(ListDialogField<CPVariableElement> field) {
doSelectionChanged(field);
}
@Override
public void doubleClicked(ListDialogField<CPVariableElement> field) {
if (fEditOnDoubleclick) {
List<CPVariableElement> selected= field.getSelectedElements();
if (canEdit(selected, containsReadOnly(selected))) {
editEntries(selected.get(0));
}
}
}
// ---------- IDialogFieldListener --------
@Override
public void dialogFieldChanged(DialogField field) {
}
}
private boolean containsReadOnly(List<CPVariableElement> selected) {
for (int i= selected.size()-1; i >= 0; i--) {
if (selected.get(i).isReadOnly()) {
return true;
}
}
return false;
}
private boolean canEdit(List<CPVariableElement> selected, boolean containsReadOnly) {
return selected.size() == 1 && !containsReadOnly;
}
/**
* @param field the dialog field
*/
private void doSelectionChanged(DialogField field) {
List<CPVariableElement> selected= fVariablesList.getSelectedElements();
boolean containsReadOnly= containsReadOnly(selected);
// edit
fVariablesList.enableButton(1, canEdit(selected, containsReadOnly));
// remove button
fVariablesList.enableButton(2, !containsReadOnly);
fSelectedElements= selected;
updateDeprecationWarning();
}
private void updateDeprecationWarning() {
if (fWarning == null || fWarning.isDisposed())
return;
for (CPVariableElement element : fSelectedElements) {
String deprecationMessage= element.getDeprecationMessage();
if (deprecationMessage != null) {
fWarning.setText(deprecationMessage);
fWarning.setImage(JFaceResources.getImage(Dialog.DLG_IMG_MESSAGE_WARNING));
return;
}
}
fWarning.setText(null);
fWarning.setImage(null);
}
private void editEntries(CPVariableElement entry) {
List<CPVariableElement> existingEntries= fVariablesList.getElements();
VariableCreationDialog dialog= new VariableCreationDialog(getShell(), entry, existingEntries);
if (dialog.open() != Window.OK) {
return;
}
CPVariableElement newEntry= dialog.getClasspathElement();
if (entry == null) {
fVariablesList.addElement(newEntry);
entry= newEntry;
fHasChanges= true;
} else {
boolean hasChanges= !entry.getName().equals(newEntry.getName())
|| !entry.getPath().equals(newEntry.getPath());
if (hasChanges) {
fHasChanges= true;
entry.setName(newEntry.getName());
entry.setPath(newEntry.getPath());
fVariablesList.refresh();
}
}
fVariablesList.selectElements(new StructuredSelection(entry));
}
public List<CPVariableElement> getSelectedElements() {
return fSelectedElements;
}
public boolean performOk() {
ArrayList<String> removedVariables= new ArrayList<>();
ArrayList<String> changedVariables= new ArrayList<>();
removedVariables.addAll(Arrays.asList(JavaCore.getClasspathVariableNames()));
// remove all unchanged
List<CPVariableElement> changedElements= fVariablesList.getElements();
for (int i= changedElements.size()-1; i >= 0; i--) {
CPVariableElement curr= changedElements.get(i);
if (curr.isReadOnly()) {
changedElements.remove(curr);
} else {
IPath path= curr.getPath();
IPath prevPath= JavaCore.getClasspathVariable(curr.getName());
if (prevPath != null && prevPath.equals(path)) {
changedElements.remove(curr);
} else {
changedVariables.add(curr.getName());
}
}
removedVariables.remove(curr.getName());
}
int steps= changedElements.size() + removedVariables.size();
if (steps > 0) {
boolean needsBuild= false;
if (fAskToBuild && doesChangeRequireFullBuild(removedVariables, changedVariables)) {
String title= NewWizardMessages.VariableBlock_needsbuild_title;
String message= NewWizardMessages.VariableBlock_needsbuild_message;
MessageDialog buildDialog= new MessageDialog(getShell(), title, null, message, MessageDialog.QUESTION, new String[] { IDialogConstants.YES_LABEL, IDialogConstants.NO_LABEL, IDialogConstants.CANCEL_LABEL }, 2);
int res= buildDialog.open();
if (res != 0 && res != 1) {
return false;
}
needsBuild= (res == 0);
}
final VariableBlockRunnable runnable= new VariableBlockRunnable(removedVariables, changedElements);
final ProgressMonitorDialog dialog= new ProgressMonitorDialog(getShell());
try {
PlatformUI.getWorkbench().getProgressService().runInUI(dialog, runnable, ResourcesPlugin.getWorkspace().getRoot());
} catch (InvocationTargetException e) {
ExceptionHandler.handle(new InvocationTargetException(new NullPointerException()), getShell(), NewWizardMessages.VariableBlock_variableSettingError_titel, NewWizardMessages.VariableBlock_variableSettingError_message);
return false;
} catch (InterruptedException e) {
return false;
}
if (needsBuild) {
CoreUtility.getBuildJob(null).schedule();
}
}
return true;
}
private boolean doesChangeRequireFullBuild(List<String> removed, List<String> changed) {
try {
IJavaModel model= JavaCore.create(ResourcesPlugin.getWorkspace().getRoot());
for (IJavaProject project : model.getJavaProjects()) {
for (IClasspathEntry curr : project.getRawClasspath()) {
if (curr.getEntryKind() == IClasspathEntry.CPE_VARIABLE) {
String var= curr.getPath().segment(0);
if (removed.contains(var) || changed.contains(var)) {
return true;
}
}
}
}
} catch (JavaModelException e) {
return true;
}
return false;
}
private static class VariableBlockRunnable implements IRunnableWithProgress {
private final List<String> fToRemove;
private final List<CPVariableElement> fToChange;
public VariableBlockRunnable(List<String> toRemove, List<CPVariableElement> toChange) {
fToRemove= toRemove;
fToChange= toChange;
}
/*
* @see IRunnableWithProgress#run(IProgressMonitor)
*/
@Override
public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
monitor.beginTask(NewWizardMessages.VariableBlock_operation_desc, 1);
try {
setVariables(monitor);
} catch (CoreException e) {
throw new InvocationTargetException(e);
} catch (OperationCanceledException e) {
throw new InterruptedException();
} finally {
monitor.done();
}
}
public void setVariables(IProgressMonitor monitor) throws JavaModelException, CoreException {
int nVariables= fToChange.size() + fToRemove.size();
String[] names= new String[nVariables];
IPath[] paths= new IPath[nVariables];
int k= 0;
for (CPVariableElement curr : fToChange) {
names[k]= curr.getName();
paths[k]= curr.getPath();
k++;
}
for (String element : fToRemove) {
names[k]= element;
paths[k]= null;
k++;
}
JavaCore.setClasspathVariables(names, paths, Progress.subMonitor(monitor, 1));
}
}
/**
* If set to true, a dialog will ask the user to build on variable changed
* @param askToBuild The askToBuild to set
*/
public void setAskToBuild(boolean askToBuild) {
fAskToBuild= askToBuild;
}
/**
* @param initSelection the initial selection
*/
public void refresh(String initSelection) {
CPVariableElement initSelectedElement= null;
String[] entries= JavaCore.getClasspathVariableNames();
ArrayList<CPVariableElement> elements= new ArrayList<>(entries.length);
for (String name : entries) {
CPVariableElement elem;
IPath entryPath= JavaCore.getClasspathVariable(name);
if (entryPath != null) {
elem= new CPVariableElement(name, entryPath);
elements.add(elem);
if (name.equals(initSelection)) {
initSelectedElement= elem;
}
}
}
fVariablesList.setElements(elements);
if (initSelectedElement != null) {
ISelection sel= new StructuredSelection(initSelectedElement);
fVariablesList.selectElements(sel);
} else {
fVariablesList.selectFirstElement();
}
fHasChanges= false;
}
public void setSelection(String elementName) {
for (int i= 0; i < fVariablesList.getSize(); i++) {
CPVariableElement elem= fVariablesList.getElement(i);
if (elem.getName().equals(elementName)) {
fVariablesList.selectElements(new StructuredSelection(elem));
return;
}
}
}
}