Skip to main content
summaryrefslogtreecommitdiffstats
blob: 08f9ecf65284adb3a755ab9ce921f7d22fe805e4 (plain) (blame)
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
/*******************************************************************************
 * Copyright (c) 2000, 2006 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.debug.internal.ui.preferences;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.variables.IStringVariableManager;
import org.eclipse.core.variables.IValueVariable;
import org.eclipse.core.variables.VariablesPlugin;
import org.eclipse.debug.internal.ui.DebugUIPlugin;
import org.eclipse.debug.internal.ui.IDebugHelpContextIds;
import org.eclipse.debug.internal.ui.MultipleInputDialog;
import org.eclipse.debug.internal.ui.SWTUtil;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.preference.PreferencePage;
import org.eclipse.jface.viewers.ColumnLayoutData;
import org.eclipse.jface.viewers.ColumnWeightData;
import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.IColorProvider;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.TableLayout;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerComparator;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;
import org.eclipse.ui.PlatformUI;

import com.ibm.icu.text.MessageFormat;

/**
 * Preference page for creating and configuring simple
 * launch variables.
 * 
 * @see org.eclipse.debug.core.variables.IValueVariable
 * @see org.eclipse.debug.core.variables.ISimpleVariableRegistry
 */
public class StringVariablePreferencePage extends PreferencePage implements IWorkbenchPreferencePage {
	
	private TableViewer variableTable;
	protected Button envAddButton;
	protected Button envEditButton;
	protected Button envRemoveButton;
	
	protected SimpleVariableContentProvider variableContentProvider= new SimpleVariableContentProvider();
	
	protected static final String NAME_LABEL= DebugPreferencesMessages.SimpleVariablePreferencePage_10; 
	protected static final String VALUE_LABEL = DebugPreferencesMessages.SimpleVariablePreferencePage_11; 
	protected static final String DESCRIPTION_LABEL = DebugPreferencesMessages.SimpleVariablePreferencePage_12; 
	
	protected static String[] variableTableColumnProperties= {
		"variable", //$NON-NLS-1$
		"value", //$NON-NLS-1$
		"description" //$NON-NLS-1$
	};
	protected String[] variableTableColumnHeaders= {
		DebugPreferencesMessages.SimpleVariablePreferencePage_3, 
		DebugPreferencesMessages.SimpleVariablePreferencePage_4, 
		DebugPreferencesMessages.SimpleVariablePreferencePage_5
	};
	protected ColumnLayoutData[] variableTableColumnLayouts= {
		new ColumnWeightData(33),
		new ColumnWeightData(33),
		new ColumnWeightData(34)
	};
	
	public StringVariablePreferencePage() {
		setDescription(DebugPreferencesMessages.SimpleVariablePreferencePage_6); 
	}

	/**
	 * @see PreferencePage#createControl(Composite)
	 */
	public void createControl(Composite parent) {
		super.createControl(parent);
		PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(), IDebugHelpContextIds.SIMPLE_VARIABLE_PREFERENCE_PAGE);
	}

	protected Control createContents(Composite parent) {
		noDefaultAndApplyButton();
		Font font= parent.getFont();
		//The main composite
		Composite composite = new Composite(parent, SWT.NONE);
		GridLayout layout = new GridLayout();
		layout.marginHeight=0;
		layout.marginWidth=0;
		layout.numColumns= 2;
		composite.setLayout(layout);
		composite.setFont(font);
		
		createTable(composite);
		createButtons(composite);
		return composite;
	}
	
	/**
	 * Creates and configures the table containing launch configuration variables
	 * and their associated value.
	 */
	private void createTable(Composite parent) {
		Font font= parent.getFont();
		// Create table composite
		Composite tableComposite = new Composite(parent, SWT.NONE);
		GridLayout layout = new GridLayout();
		layout.marginHeight = 0;
		layout.marginWidth = 0;
		layout.numColumns = 1;
		GridData gridData = new GridData(GridData.FILL_BOTH);
		gridData.heightHint = 150;
		gridData.widthHint = 400;
		tableComposite.setLayout(layout);
		tableComposite.setLayoutData(gridData);
		tableComposite.setFont(font);
		// Create table
		variableTable = new TableViewer(tableComposite, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL | SWT.MULTI | SWT.FULL_SELECTION);
		Table table = variableTable.getTable();
		TableLayout tableLayout = new TableLayout();
		table.setLayout(tableLayout);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		table.setFont(font);
		gridData = new GridData(GridData.FILL_BOTH);
		variableTable.getControl().setLayoutData(gridData);
		variableTable.setContentProvider(variableContentProvider);
		variableTable.setColumnProperties(variableTableColumnProperties);
		variableTable.setComparator(new ViewerComparator() {
			public int compare(Viewer iViewer, Object e1, Object e2) {
				if (e1 == null) {
					return -1;
				} else if (e2 == null) {
					return 1;
				} else {
					return ((IValueVariable)e1).getName().compareToIgnoreCase(((IValueVariable)e2).getName());
				}
			}
		});
		
		variableTable.addSelectionChangedListener(new ISelectionChangedListener() {
			public void selectionChanged(SelectionChangedEvent event) {
				handleTableSelectionChanged(event);
			}
		});
		
		variableTable.addDoubleClickListener(new IDoubleClickListener() {
			public void doubleClick(DoubleClickEvent event) {
				if (!variableTable.getSelection().isEmpty()) {
					handleEditButtonPressed();
				}
			}
		});
		variableTable.getTable().addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent event) {
				if (event.character == SWT.DEL && event.stateMask == 0) {
					handleRemoveButtonPressed();
				}
			}
		});
				
		// Create columns
		for (int i = 0; i < variableTableColumnHeaders.length; i++) {
			tableLayout.addColumnData(variableTableColumnLayouts[i]);
			TableColumn tc = new TableColumn(table, SWT.NONE, i);
			tc.setResizable(variableTableColumnLayouts[i].resizable);
			tc.setText(variableTableColumnHeaders[i]);
		}
		variableTable.setInput(getVariableManager());
		variableTable.setLabelProvider(new SimpleVariableLabelProvider());
	}
	
	/**
	 * Creates the new/edit/remove buttons for the variable table
	 * @param parent the composite in which the buttons should be created
	 */
	private void createButtons(Composite parent) {
		// Create button composite
		Composite buttonComposite = new Composite(parent, SWT.NONE);
		GridLayout glayout = new GridLayout();
		glayout.marginHeight = 0;
		glayout.marginWidth = 0;
		glayout.numColumns = 1;
		GridData gdata = new GridData(GridData.VERTICAL_ALIGN_BEGINNING);
		buttonComposite.setLayout(glayout);
		buttonComposite.setLayoutData(gdata);
		buttonComposite.setFont(parent.getFont());
		
		// Create buttons
		envAddButton = SWTUtil.createPushButton(buttonComposite, DebugPreferencesMessages.SimpleVariablePreferencePage_7, null); 
		envAddButton.addSelectionListener(new SelectionAdapter()
		{
			public void widgetSelected(SelectionEvent event) {
				handleAddButtonPressed();
			}
		});
		envEditButton = SWTUtil.createPushButton(buttonComposite, DebugPreferencesMessages.SimpleVariablePreferencePage_8, null); 
		envEditButton.addSelectionListener(new SelectionAdapter()
		{
			public void widgetSelected(SelectionEvent event) {
				handleEditButtonPressed();
			}
		});
		envEditButton.setEnabled(false);
		envRemoveButton = SWTUtil.createPushButton(buttonComposite, DebugPreferencesMessages.SimpleVariablePreferencePage_9, null); 
		envRemoveButton.addSelectionListener(new SelectionAdapter()
		{
			public void widgetSelected(SelectionEvent event) {
				handleRemoveButtonPressed();
			}
		});
		envRemoveButton.setEnabled(false);
	}
	
	private void handleAddButtonPressed() {
		MultipleInputDialog dialog= new MultipleInputDialog(getShell(), DebugPreferencesMessages.SimpleVariablePreferencePage_13); 
		dialog.addTextField(NAME_LABEL, null, false);
		dialog.addBrowseField(VALUE_LABEL, null, true);
		dialog.addTextField(DESCRIPTION_LABEL, null, true);

		if (dialog.open() != Window.OK) {
			return;
		}

		String name= dialog.getStringValue(NAME_LABEL).trim();
		if (name != null && name.length() > 0) {
			String description= dialog.getStringValue(DESCRIPTION_LABEL);
			IValueVariable variable= getVariableManager().newValueVariable(name, description);
			variable.setValue(dialog.getStringValue(VALUE_LABEL));	
			addVariable(variable);
		}
	}
	
	/**
	 * Attempts to add the given variable. Returns whether the variable
	 * was added or not (as when the user answers not to overwrite an
	 * existing variable).
	 * @param variable the variable to add
	 * @return whether the variable was added
	 */
	public boolean addVariable(IValueVariable variable) {
		String name= variable.getName();
		List editedVariables= variableContentProvider.getVariables();
		Iterator iter= editedVariables.iterator();
		while (iter.hasNext()) {
			IValueVariable currentVariable = (IValueVariable) iter.next();
			String variableName = currentVariable.getName();
			if (variableName.equals(name)) {
				boolean overWrite= MessageDialog.openQuestion(getShell(), DebugPreferencesMessages.SimpleVariablePreferencePage_15, MessageFormat.format(DebugPreferencesMessages.SimpleVariablePreferencePage_16, new String[] {name}));  // 
				if (!overWrite) {
					return false;
				}
				variableContentProvider.removeVariables(new IValueVariable[]{currentVariable});
				break;
			}
		}
		variableContentProvider.addVariables(new IValueVariable[]{variable});
		variableTable.refresh();
		return true;
	}
	
	private void handleEditButtonPressed() {
		IStructuredSelection selection= (IStructuredSelection) variableTable.getSelection();
		IValueVariable variable= (IValueVariable) selection.getFirstElement();
		if (variable == null) {
			return;
		}
		String value= variable.getValue();
		if (value == null) {
			value= ""; //$NON-NLS-1$
		}
		String description= variable.getDescription();
		if (description == null) {
			description= ""; //$NON-NLS-1$
		}
		String originalName= variable.getName();
		MultipleInputDialog dialog= new MultipleInputDialog(getShell(), DebugPreferencesMessages.SimpleVariablePreferencePage_14); 
		dialog.addTextField(NAME_LABEL, originalName, false);
		dialog.addBrowseField(VALUE_LABEL, value, true);
		dialog.addTextField(DESCRIPTION_LABEL, description, true);
	
		if (dialog.open() == Window.OK) {
			String name= dialog.getStringValue(NAME_LABEL);
			value= dialog.getStringValue(VALUE_LABEL);
			description= dialog.getStringValue(DESCRIPTION_LABEL);
			if (!name.equals(originalName)) {
				IValueVariable newVariable = getVariableManager().newValueVariable(name, description);
				newVariable.setValue(value);
				if (addVariable(newVariable)) {
					variableContentProvider.removeVariables(new IValueVariable[]{variable});
					variableTable.refresh();
				}
			} else {
				if (value != null) {
					variable.setValue(value);
				}
				if (description != null) {
					variable.setDescription(description);
				}
				variableTable.update(variable, null);
			}
		}
	}
	
	/**
	 * Remove the selection variables.
	 */
	private void handleRemoveButtonPressed() {
		IStructuredSelection selection= (IStructuredSelection) variableTable.getSelection();
		List variablesToRemove= selection.toList();
		StringBuffer contributedVariablesToRemove= new StringBuffer();
		Iterator iter= variablesToRemove.iterator();
		while (iter.hasNext()) {
			IValueVariable variable = (IValueVariable) iter.next();
			if (variable.isContributed()) {
				contributedVariablesToRemove.append('\t').append(variable.getName()).append('\n');
			}
		}
		if (contributedVariablesToRemove.length() > 0) {
			boolean remove= MessageDialog.openQuestion(getShell(), DebugPreferencesMessages.SimpleLaunchVariablePreferencePage_21, MessageFormat.format(DebugPreferencesMessages.SimpleLaunchVariablePreferencePage_22, new String[] {contributedVariablesToRemove.toString()})); // 
			if (!remove) {
				return;
			}
		}
		IValueVariable[] variables= (IValueVariable[]) variablesToRemove.toArray(new IValueVariable[0]);
		variableContentProvider.removeVariables(variables); 
		variableTable.refresh();
	}
	
	/**
	 * Responds to a selection changed event in the variable table
	 * @param event the selection change event
	 */
	protected void handleTableSelectionChanged(SelectionChangedEvent event) {
		int size = ((IStructuredSelection)event.getSelection()).size();
		envEditButton.setEnabled(size == 1);
		envRemoveButton.setEnabled(size > 0);
	}

	public void init(IWorkbench workbench) {
	}

	/**
	 * Clear the variables.
	 */
	protected void performDefaults() {
		variableContentProvider.discardChanges();
		variableTable.refresh();
		super.performDefaults();
	}
	
	/**
	 * Sets the saved state for reversion.
	 */
	public boolean performOk() {
		variableContentProvider.saveChanges();
		return super.performOk();
	}

	/**
	 * Returns the DebugPlugin's singleton instance of the
	 * launch variable manager
	 * @return the singleton instance of the simple variable registry.
	 */
	private IStringVariableManager getVariableManager() {
		return VariablesPlugin.getDefault().getStringVariableManager();
	}
	
	private class SimpleVariableContentProvider implements IStructuredContentProvider {
		/**
		 * The content provider stores a copy of the variables for use during editing.
		 * The edited variables are saved to the launch manager when saveChanges()
		 * is called.
		 */
		private List fVariables = new ArrayList();
		
		public Object[] getElements(Object inputElement) {
			return fVariables.toArray();
		}
		
		/**
		 * Removes the given variables from the 'copied list'
		 * 
		 * @param variables variables to remove
		 */
		public void removeVariables(IValueVariable[] variables) {
			for (int i = 0; i < variables.length; i++) {
				fVariables.remove(variables[i]);
			}			
		}
		
		/**
		 * Adds the given variables to the 'copied list'
		 * 
		 * @param variables variables to add
		 */
		public void addVariables(IValueVariable[] variables) {
			for (int i = 0; i < variables.length; i++) {
				fVariables.add(variables[i]);
			}			
		}		

		public void dispose() {
		}
		
		public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
			if (newInput == null || !(newInput instanceof IStringVariableManager)){
				return;
			}
			discardChanges();
		}

		/**
		 * Saves the edited variable state to the variable manager.
		 */
		public void saveChanges() {
			IStringVariableManager manager = getVariableManager();
			manager.removeVariables(manager.getValueVariables());
			try {
				manager.addVariables((IValueVariable[]) fVariables.toArray(new IValueVariable[0]));
			} catch (CoreException e) {
				DebugUIPlugin.errorDialog(getShell(), DebugPreferencesMessages.StringVariablePreferencePage_24, DebugPreferencesMessages.StringVariablePreferencePage_25, e.getStatus()); // 
			}
		}
		
		/**
		 * Re-initializes to the variables currently stored in the manager.
		 */
		public void discardChanges() {
			IStringVariableManager manager = getVariableManager();
			IValueVariable[] variables = manager.getValueVariables();
			for (int i = 0; i < variables.length; i++) {
				IValueVariable variable = variables[i];
				IValueVariable copy = manager.newValueVariable(variable.getName(), variable.getDescription());
				copy.setValue(variable.getValue());
				fVariables.add(copy);
			}			
		}
		
		/**
		 * Returns the 'working set' of variables
		 * 
		 * @return the working set of variables (not yet saved)
		 */
		public List getVariables() {
			return fVariables;
		}
	}
	
	private class SimpleVariableLabelProvider extends LabelProvider implements ITableLabelProvider, IColorProvider {
		public Image getColumnImage(Object element, int columnIndex) {
			return null;
		}
		public String getColumnText(Object element, int columnIndex) {
			if (element instanceof IValueVariable) {
				IValueVariable variable= (IValueVariable) element;
				switch (columnIndex) {
					case 0 :
						StringBuffer buffer= new StringBuffer(variable.getName());
						if (variable.isContributed()) {
                            String pluginId = getVariableManager().getContributingPluginId(variable);
                            if (pluginId != null) {
                                buffer.append(MessageFormat.format(DebugPreferencesMessages.StringVariablePreferencePage_0, new String[] {pluginId})); 
                            } else {
                                buffer.append(DebugPreferencesMessages.SimpleLaunchVariablePreferencePage_23); 
                            }
						}
						return buffer.toString();
					case 1:
						String value= variable.getValue(); 
						if (value == null) {
							value= ""; //$NON-NLS-1$
						}
						return value;
					case 2:
						String description= variable.getDescription();
						if (description == null) {
							description= ""; //$NON-NLS-1$
						}
						return description;
				}
			}
			return null;
		}
		public Color getForeground(Object element) {
			return null;
		}
		public Color getBackground(Object element) {
			if (element instanceof IValueVariable) {
				if (((IValueVariable) element).isContributed()) {
					Display display= Display.getCurrent();
					return display.getSystemColor(SWT.COLOR_INFO_BACKGROUND);		
				}
			}
			return null;
		}
	}
}

Back to the top