1
/*******************************************************************************
2
* Copyright (c) 2002, 2008 QNX Software Systems and others.
3
* All rights reserved. This program and the accompanying materials
4
* are made available under the terms of the Eclipse Public License v1.0
5
* which accompanies this distribution, and is available at
6
* http://www.eclipse.org/legal/epl-v10.html
9
* IBM Corporation - initial API and implementation
10
* QNX Software Systems - Initial API and implementation
11
*******************************************************************************/
13
package org.eclipse.cdt.internal.ui.preferences;
18
import org.eclipse.core.resources.IResource;
19
import org.eclipse.core.runtime.Path;
20
import org.eclipse.jface.dialogs.IDialogConstants;
21
import org.eclipse.jface.dialogs.IMessageProvider;
22
import org.eclipse.jface.dialogs.TitleAreaDialog;
23
import org.eclipse.swt.SWT;
24
import org.eclipse.swt.events.ModifyEvent;
25
import org.eclipse.swt.events.ModifyListener;
26
import org.eclipse.swt.events.SelectionAdapter;
27
import org.eclipse.swt.events.SelectionEvent;
28
import org.eclipse.swt.graphics.Font;
29
import org.eclipse.swt.layout.FormAttachment;
30
import org.eclipse.swt.layout.FormData;
31
import org.eclipse.swt.layout.FormLayout;
32
import org.eclipse.swt.widgets.Button;
33
import org.eclipse.swt.widgets.Composite;
34
import org.eclipse.swt.widgets.Control;
35
import org.eclipse.swt.widgets.DirectoryDialog;
36
import org.eclipse.swt.widgets.FileDialog;
37
import org.eclipse.swt.widgets.Label;
38
import org.eclipse.swt.widgets.Shell;
39
import org.eclipse.swt.widgets.Text;
42
* @deprecated in CDT 8.0. This class appears to be never used.
45
public class PathEntryVariableDialog extends TitleAreaDialog {
48
private Button okButton;
50
private Label variableNameLabel;
52
private Label variableValueLabel;
54
private Text variableNameField;
56
private Text variableValueField;
58
private Button fileButton;
60
private Button folderButton;
63
* This dialog type: <code>NEW_VARIABLE</code> or
64
* <code>EXISTING_VARIABLE</code>.
69
* The type of variable that can be edited in this dialog.
70
* <code>IResource.FILE</code> or <code>IResource.FOLDER</code>
72
private int variableType;
75
* The name of the variable being edited.
77
private String variableName;
80
* The value of the variable being edited.
82
private String variableValue;
85
* The original name of the variable being edited. It is used when testing
86
* if the current variable's name is already in use.
88
private String originalName;
91
* Used to select the proper message depending on the current mode
92
* (new/existing variable).
94
private boolean newVariable;
97
* Set of variable names currently in use. Used when warning the user that
98
* the currently selected name is already in use by another variable.
100
private Set<String> namesInUse;
103
* The current validation status. Its value can be one of the following:<ul>
104
* <li><code>IMessageProvider.NONE</code> (default);</li>
105
* <li><code>IMessageProvider.WARNING</code>;</li>
106
* <li><code>IMessageProvider.ERROR</code>;</li>
108
* Used when validating the user input.
110
private int validationStatus;
113
* The current validation message generated by the last
114
* call to a <code>validate</code> method.
116
private String validationMessage;
119
* Whether a variable name has been entered.
121
private boolean nameEntered = false;
124
* Whether a variable location has been entered.
126
private boolean locationEntered = false;
129
* The standard message to be shown when there are no problems being
132
final private String standardMessage;
135
* Constant for defining this dialog as intended to create a new variable
138
public final static int NEW_VARIABLE = 1;
141
* Constant for defining this dialog as intended to edit an existing
142
* variable (value = 2).
144
public final static int EXISTING_VARIABLE = 2;
147
* Constructs a dialog for editing a new/existing path variable.
149
* @param parentShell the parent shell
150
* @param type the dialog type: <code>NEW_VARIABLE</code> or
151
* <code>EXISTING_VARIABLE</code>
152
* @param variableType the type of variable that can be edited in
153
* this dialog. <code>IResource.FILE</code> or <code>IResource.FOLDER</code>
154
* @param namesInUse a set of variable names currently in use
156
public PathEntryVariableDialog(Shell parentShell, int type, int variableType, Set<String> namesInUse) {
159
this.newVariable = type == NEW_VARIABLE;
160
this.variableName = ""; //$NON-NLS-1$
161
this.variableValue = ""; //$NON-NLS-1$
162
this.variableType = variableType;
163
this.namesInUse = namesInUse;
166
this.standardMessage = PreferencesMessages.PathEntryVariableDialog_message_newVariable;
168
this.standardMessage = PreferencesMessages.PathEntryVariableDialog_message_existingVariable;
172
* Configures this dialog's shell, setting the shell's text.
174
* @see org.eclipse.jface.window.Window#configureShell(Shell)
177
protected void configureShell(Shell shell) {
178
super.configureShell(shell);
180
shell.setText(PreferencesMessages.PathEntryVariableDialog_shellTitle_newVariable);
183
.setText(PreferencesMessages.PathEntryVariableDialog_shellTitle_existingVariable);
187
* Creates and returns the contents of this dialog (except for the button bar).
189
* @see org.eclipse.jface.dialogs.TitleAreaDialog#createDialogArea
192
protected Control createDialogArea(Composite parent) {
193
// top level composite
194
Composite parentComposite = (Composite) super.createDialogArea(parent);
196
// creates dialog area composite
197
Composite contents = createComposite(parentComposite);
199
// creates and lay outs dialog area widgets
200
createWidgets(contents, parent.getFont());
202
// validate possibly already incorrect variable definitions
203
if (type == EXISTING_VARIABLE) {
204
nameEntered = locationEntered = true;
205
validateVariableValue();
212
* Creates and configures this dialog's main composite.
214
* @param parentComposite parent's composite
215
* @return this dialog's main composite
217
private Composite createComposite(Composite parentComposite) {
218
// creates a composite with standard margins and spacing
219
Composite contents = new Composite(parentComposite, SWT.NONE);
221
FormLayout layout = new FormLayout();
223
layout.marginHeight = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN);
224
layout.marginWidth = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN);
226
contents.setLayout(layout);
227
contents.setFont(parentComposite.getFont());
230
setTitle(PreferencesMessages.PathEntryVariableDialog_dialogTitle_newVariable);
232
setTitle(PreferencesMessages.PathEntryVariableDialog_dialogTitle_existingVariable);
233
setMessage(standardMessage);
238
* Creates widgets for this dialog.
240
* @param parent the parent composite where to create widgets
243
private void createWidgets(Composite contents, Font font) {
246
String nameLabelText = PreferencesMessages.PathEntryVariableDialog_variableName;
247
String valueLabelText = PreferencesMessages.PathEntryVariableDialog_variableValue;
249
// variable name label
250
variableNameLabel = new Label(contents, SWT.LEFT);
251
variableNameLabel.setText(nameLabelText);
253
data = new FormData();
254
variableNameLabel.setLayoutData(data);
255
variableNameLabel.setFont(font);
257
// variable value label
258
variableValueLabel = new Label(contents, SWT.LEFT);
259
variableValueLabel.setText(valueLabelText);
261
data = new FormData();
262
data.top = new FormAttachment(variableNameLabel,
263
convertVerticalDLUsToPixels(10));
264
variableValueLabel.setLayoutData(data);
265
variableValueLabel.setFont(font);
267
// the larger label will be used in the left attachments for the fields
268
Label largerLabel = nameLabelText.length() > valueLabelText.length() ? variableNameLabel
269
: variableValueLabel;
271
// variable name field
272
variableNameField = new Text(contents, SWT.SINGLE | SWT.BORDER);
273
variableNameField.setText(variableName);
275
data = new FormData();
276
data.width = convertWidthInCharsToPixels(50);
277
data.left = new FormAttachment(largerLabel,
278
convertHorizontalDLUsToPixels(5));
279
variableNameField.setLayoutData(data);
280
variableNameField.setFont(font);
281
variableNameField.setFocus();
283
variableNameField.addModifyListener(new ModifyListener() {
284
public void modifyText(ModifyEvent event) {
285
variableNameModified();
289
// variable value field
290
variableValueField = new Text(contents, SWT.SINGLE | SWT.BORDER);
291
variableValueField.setText(variableValue);
293
data = new FormData();
294
data.width = convertWidthInCharsToPixels(50);
295
data.left = new FormAttachment(largerLabel,
296
convertHorizontalDLUsToPixels(5));
297
data.top = new FormAttachment(variableNameLabel,
298
convertVerticalDLUsToPixels(10));
299
variableValueField.setLayoutData(data);
300
variableValueField.setFont(font);
302
variableValueField.addModifyListener(new ModifyListener() {
303
public void modifyText(ModifyEvent event) {
304
variableValueModified();
308
// select file path button
309
fileButton = new Button(contents, SWT.PUSH);
310
fileButton.setText(PreferencesMessages.PathEntryVariableDialog_file);
311
if ((variableType & IResource.FILE) == 0)
312
fileButton.setEnabled(false);
314
data = setButtonFormLayoutData(fileButton);
315
data.top = new FormAttachment(variableNameLabel,
316
convertVerticalDLUsToPixels(10));
317
data.left = new FormAttachment(variableValueField,
318
convertHorizontalDLUsToPixels(10));
319
data.right = new FormAttachment(100, -5);
320
fileButton.setLayoutData(data);
321
fileButton.setFont(font);
323
fileButton.addSelectionListener(new SelectionAdapter() {
325
public void widgetSelected(SelectionEvent e) {
330
// select folder path button
331
folderButton = new Button(contents, SWT.PUSH);
332
folderButton.setText(PreferencesMessages.PathEntryVariableDialog_folder);
333
if ((variableType & IResource.FOLDER) == 0)
334
folderButton.setEnabled(false);
336
data = setButtonFormLayoutData(folderButton);
337
data.top = new FormAttachment(variableValueLabel,
338
convertVerticalDLUsToPixels(10));
339
data.left = new FormAttachment(variableValueField,
340
convertHorizontalDLUsToPixels(10));
341
data.right = new FormAttachment(100, -5);
342
folderButton.setLayoutData(data);
343
folderButton.setFont(font);
345
folderButton.addSelectionListener(new SelectionAdapter() {
347
public void widgetSelected(SelectionEvent e) {
354
* Sets the <code>FormData</code> on the specified button to be one that is
355
* spaced for the current dialog page units. The method
356
* <code>initializeDialogUnits</code> must be called once before calling this
357
* method for the first time.
359
* @param button the button to set the <code>FormData</code>
360
* @return the <code>FormData</code> set on the specified button
362
private FormData setButtonFormLayoutData(Button button) {
363
FormData data = new FormData();
364
int widthHint = convertHorizontalDLUsToPixels(IDialogConstants.BUTTON_WIDTH);
365
data.width = Math.max(widthHint, button.computeSize(SWT.DEFAULT,
366
SWT.DEFAULT, true).x);
367
button.setLayoutData(data);
372
* Fires validations (variable name first) and updates enabled state for the
373
* "Ok" button accordingly.
375
protected void variableNameModified() {
376
// updates and validates the variable name
377
variableName = variableNameField.getText().trim();
378
validationStatus = IMessageProvider.NONE;
379
okButton.setEnabled(validateVariableName() && validateVariableValue());
384
* Fires validations (variable value first) and updates enabled state for the
385
* "Ok" button accordingly.
387
protected void variableValueModified() {
388
// updates and validates the variable value
389
variableValue = variableValueField.getText().trim();
390
validationStatus = IMessageProvider.NONE;
391
okButton.setEnabled(validateVariableValue() && validateVariableName());
392
locationEntered = true;
396
* Opens a dialog where the user can select a folder path.
398
protected void selectFolder() {
399
DirectoryDialog dialog = new DirectoryDialog(getShell());
400
dialog.setText(PreferencesMessages.PathEntryVariableDialog_selectFolderTitle);
401
dialog.setMessage(PreferencesMessages.PathEntryVariableDialog_selectFolderMessage);
402
dialog.setFilterPath(variableValue);
403
String res = dialog.open();
405
variableValue = new Path(res).makeAbsolute().toOSString();
406
variableValueField.setText(variableValue);
411
* Opens a dialog where the user can select a file path.
413
protected void selectFile() {
414
FileDialog dialog = new FileDialog(getShell());
415
dialog.setText(PreferencesMessages.PathEntryVariableDialog_selectFileTitle);
416
dialog.setFilterPath(variableValue);
417
String res = dialog.open();
419
variableValue = new Path(res).makeAbsolute().toOSString();
420
variableValueField.setText(variableValue);
425
* Adds buttons to this dialog's button bar.
427
* @see org.eclipse.jface.dialogs.Dialog#createButtonsForButtonBar
430
protected void createButtonsForButtonBar(Composite parent) {
431
okButton = createButton(parent, IDialogConstants.OK_ID,
432
IDialogConstants.OK_LABEL, true);
433
okButton.setEnabled(type == EXISTING_VARIABLE);
435
createButton(parent, IDialogConstants.CANCEL_ID,
436
IDialogConstants.CANCEL_LABEL, false);
440
* Validates the current variable name, and updates this dialog's message.
442
* @return true if the name is valid, false otherwise
444
private boolean validateVariableName() {
445
boolean allowFinish = false;
447
// if the current validationStatus is ERROR, no additional validation applies
448
if (validationStatus == IMessageProvider.ERROR)
451
// assumes everything will be ok
452
String message = standardMessage;
453
int newValidationStatus = IMessageProvider.NONE;
455
if (variableName.length() == 0) {
456
// the variable name is empty
458
// a name was entered before and is now empty
459
newValidationStatus = IMessageProvider.ERROR;
460
message = PreferencesMessages.PathEntryVariableDialog_variableNameEmptyMessage;
463
if (namesInUse.contains(variableName)
464
&& !variableName.equals(originalName)) {
465
// the variable name is already in use
466
message = PreferencesMessages.PathEntryVariableDialog_variableAlreadyExistsMessage;
467
newValidationStatus = IMessageProvider.ERROR;
473
// overwrite the current validation status / message only if everything is ok (clearing them)
474
// or if we have a more serious problem than the current one
475
if (validationStatus == IMessageProvider.NONE
476
|| newValidationStatus == IMessageProvider.ERROR) {
477
validationStatus = newValidationStatus;
478
validationMessage = message;
480
// only set the message here if it is not going to be set in
481
// validateVariableValue to avoid flashing.
482
if (allowFinish == false)
483
setMessage(validationMessage, validationStatus);
488
* Validates the current variable value, and updates this dialog's message.
490
* @return true if the value is valid, false otherwise
492
private boolean validateVariableValue() {
493
boolean allowFinish = false;
495
// if the current validationStatus is ERROR, no additional validation applies
496
if (validationStatus == IMessageProvider.ERROR)
499
// assumes everything will be ok
500
String message = standardMessage;
501
int newValidationStatus = IMessageProvider.NONE;
503
if (variableValue.length() == 0) {
504
// the variable value is empty
505
if (locationEntered) {
506
// a location value was entered before and is now empty
507
newValidationStatus = IMessageProvider.ERROR;
508
message = PreferencesMessages.PathEntryVariableDialog_variableValueEmptyMessage;
510
} else if (!Path.EMPTY.isValidPath(variableValue)) {
511
// the variable value is an invalid path
512
message = PreferencesMessages.PathEntryVariableDialog_variableValueInvalidMessage;
513
newValidationStatus = IMessageProvider.ERROR;
514
} else if (!new Path(variableValue).isAbsolute()) {
515
// the variable value is a relative path
516
message = PreferencesMessages.PathEntryVariableDialog_pathIsRelativeMessage;
517
newValidationStatus = IMessageProvider.ERROR;
518
} else if (!new File(variableValue).exists()) {
519
// the path does not exist (warning)
520
message = PreferencesMessages.PathEntryVariableDialog_pathDoesNotExistMessage;
521
newValidationStatus = IMessageProvider.WARNING;
527
// overwrite the current validation status / message only if everything is ok (clearing them)
528
// or if we have a more serious problem than the current one
529
if (validationStatus == IMessageProvider.NONE
530
|| newValidationStatus > validationStatus) {
531
validationStatus = newValidationStatus;
532
validationMessage = message;
534
setMessage(validationMessage, validationStatus);
539
* Returns the variable name.
541
* @return the variable name
543
public String getVariableName() {
548
* Returns the variable value.
550
* @return the variable value
552
public String getVariableValue() {
553
return variableValue;
557
* Sets the variable name.
559
* @param variableName the new variable name
561
public void setVariableName(String variableName) {
562
this.variableName = variableName.trim();
563
this.originalName = this.variableName;
567
* Sets the variable value.
569
* @param variableValue the new variable value
571
public void setVariableValue(String variableValue) {
572
this.variableValue = variableValue;