001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.ui.wizards.buildpaths;
011:
012: import java.io.File;
013: import java.util.List;
014:
015: import org.eclipse.core.runtime.IPath;
016: import org.eclipse.core.runtime.Path;
017:
018: import org.eclipse.swt.SWT;
019: import org.eclipse.swt.layout.GridLayout;
020: import org.eclipse.swt.widgets.Composite;
021: import org.eclipse.swt.widgets.Control;
022: import org.eclipse.swt.widgets.DirectoryDialog;
023: import org.eclipse.swt.widgets.FileDialog;
024: import org.eclipse.swt.widgets.Shell;
025:
026: import org.eclipse.jface.dialogs.IDialogSettings;
027: import org.eclipse.jface.dialogs.StatusDialog;
028:
029: import org.eclipse.ui.PlatformUI;
030:
031: import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
032: import org.eclipse.jdt.internal.ui.IUIConstants;
033: import org.eclipse.jdt.internal.ui.JavaPlugin;
034: import org.eclipse.jdt.internal.ui.dialogs.StatusInfo;
035: import org.eclipse.jdt.internal.ui.dialogs.StatusUtil;
036: import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages;
037: import org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField;
038: import org.eclipse.jdt.internal.ui.wizards.dialogfields.IDialogFieldListener;
039: import org.eclipse.jdt.internal.ui.wizards.dialogfields.IStringButtonAdapter;
040: import org.eclipse.jdt.internal.ui.wizards.dialogfields.LayoutUtil;
041: import org.eclipse.jdt.internal.ui.wizards.dialogfields.SelectionButtonDialogField;
042: import org.eclipse.jdt.internal.ui.wizards.dialogfields.StringButtonDialogField;
043: import org.eclipse.jdt.internal.ui.wizards.dialogfields.StringDialogField;
044:
045: public class VariableCreationDialog extends StatusDialog {
046:
047: private IDialogSettings fDialogSettings;
048:
049: private StringDialogField fNameField;
050: private StatusInfo fNameStatus;
051:
052: private StringButtonDialogField fPathField;
053: private StatusInfo fPathStatus;
054: private SelectionButtonDialogField fDirButton;
055:
056: private CPVariableElement fElement;
057:
058: private List fExistingNames;
059:
060: public VariableCreationDialog(Shell parent,
061: CPVariableElement element, List existingNames) {
062: super (parent);
063: if (element == null) {
064: setTitle(NewWizardMessages.VariableCreationDialog_titlenew);
065: } else {
066: setTitle(NewWizardMessages.VariableCreationDialog_titleedit);
067: }
068:
069: fDialogSettings = JavaPlugin.getDefault().getDialogSettings();
070:
071: fElement = element;
072:
073: fNameStatus = new StatusInfo();
074: fPathStatus = new StatusInfo();
075:
076: NewVariableAdapter adapter = new NewVariableAdapter();
077: fNameField = new StringDialogField();
078: fNameField.setDialogFieldListener(adapter);
079: fNameField
080: .setLabelText(NewWizardMessages.VariableCreationDialog_name_label);
081:
082: fPathField = new StringButtonDialogField(adapter);
083: fPathField.setDialogFieldListener(adapter);
084: fPathField
085: .setLabelText(NewWizardMessages.VariableCreationDialog_path_label);
086: fPathField
087: .setButtonLabel(NewWizardMessages.VariableCreationDialog_path_file_button);
088:
089: fDirButton = new SelectionButtonDialogField(SWT.PUSH);
090: fDirButton.setDialogFieldListener(adapter);
091: fDirButton
092: .setLabelText(NewWizardMessages.VariableCreationDialog_path_dir_button);
093:
094: fExistingNames = existingNames;
095:
096: if (element != null) {
097: fNameField.setText(element.getName());
098: fPathField.setText(element.getPath().toString());
099: fExistingNames.remove(element.getName());
100: } else {
101: fNameField.setText(""); //$NON-NLS-1$
102: fPathField.setText(""); //$NON-NLS-1$
103: }
104: }
105:
106: /*
107: * @see Windows#configureShell
108: */
109: protected void configureShell(Shell newShell) {
110: super .configureShell(newShell);
111: PlatformUI.getWorkbench().getHelpSystem().setHelp(newShell,
112: IJavaHelpContextIds.VARIABLE_CREATION_DIALOG);
113: }
114:
115: public CPVariableElement getClasspathElement() {
116: return new CPVariableElement(fNameField.getText(), new Path(
117: fPathField.getText()));
118: }
119:
120: /*
121: * @see Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
122: */
123: protected Control createDialogArea(Composite parent) {
124: Composite composite = (Composite) super
125: .createDialogArea(parent);
126:
127: Composite inner = new Composite(composite, SWT.NONE);
128: inner.setFont(composite.getFont());
129:
130: GridLayout layout = new GridLayout();
131: layout.marginWidth = 0;
132: layout.marginHeight = 0;
133: layout.numColumns = 3;
134: inner.setLayout(layout);
135:
136: int fieldWidthHint = convertWidthInCharsToPixels(50);
137:
138: fNameField.doFillIntoGrid(inner, 2);
139: LayoutUtil.setWidthHint(fNameField.getTextControl(null),
140: fieldWidthHint);
141: LayoutUtil.setHorizontalGrabbing(fNameField
142: .getTextControl(null));
143:
144: DialogField.createEmptySpace(inner, 1);
145:
146: fPathField.doFillIntoGrid(inner, 3);
147: LayoutUtil.setWidthHint(fPathField.getTextControl(null),
148: fieldWidthHint);
149:
150: DialogField.createEmptySpace(inner, 2);
151: fDirButton.doFillIntoGrid(inner, 1);
152:
153: DialogField focusField = (fElement == null) ? fNameField
154: : fPathField;
155: focusField.postSetFocusOnDialogField(parent.getDisplay());
156: applyDialogFont(composite);
157: return composite;
158: }
159:
160: // -------- NewVariableAdapter --------
161:
162: private class NewVariableAdapter implements IDialogFieldListener,
163: IStringButtonAdapter {
164:
165: // -------- IDialogFieldListener
166: public void dialogFieldChanged(DialogField field) {
167: doFieldUpdated(field);
168: }
169:
170: // -------- IStringButtonAdapter
171: public void changeControlPressed(DialogField field) {
172: doChangeControlPressed(field);
173: }
174: }
175:
176: private void doChangeControlPressed(DialogField field) {
177: if (field == fPathField) {
178: IPath path = chooseExtJarFile();
179: if (path != null) {
180: fPathField.setText(path.toString());
181: }
182: }
183: }
184:
185: private void doFieldUpdated(DialogField field) {
186: if (field == fNameField) {
187: fNameStatus = nameUpdated();
188: } else if (field == fPathField) {
189: fPathStatus = pathUpdated();
190: } else if (field == fDirButton) {
191: IPath path = chooseExtDirectory();
192: if (path != null) {
193: fPathField.setText(path.toString());
194: }
195: }
196: updateStatus(StatusUtil.getMoreSevere(fPathStatus, fNameStatus));
197: }
198:
199: private StatusInfo nameUpdated() {
200: StatusInfo status = new StatusInfo();
201: String name = fNameField.getText();
202: if (name.length() == 0) {
203: status
204: .setError(NewWizardMessages.VariableCreationDialog_error_entername);
205: return status;
206: }
207: if (name.trim().length() != name.length()) {
208: status
209: .setError(NewWizardMessages.VariableCreationDialog_error_whitespace);
210: } else if (!Path.ROOT.isValidSegment(name)) {
211: status
212: .setError(NewWizardMessages.VariableCreationDialog_error_invalidname);
213: } else if (nameConflict(name)) {
214: status
215: .setError(NewWizardMessages.VariableCreationDialog_error_nameexists);
216: }
217: return status;
218: }
219:
220: private boolean nameConflict(String name) {
221: if (fElement != null && fElement.getName().equals(name)) {
222: return false;
223: }
224: for (int i = 0; i < fExistingNames.size(); i++) {
225: CPVariableElement elem = (CPVariableElement) fExistingNames
226: .get(i);
227: if (name.equals(elem.getName())) {
228: return true;
229: }
230: }
231: return false;
232: }
233:
234: private StatusInfo pathUpdated() {
235: StatusInfo status = new StatusInfo();
236:
237: String path = fPathField.getText();
238: if (path.length() > 0) { // empty path is ok
239: if (!Path.ROOT.isValidPath(path)) {
240: status
241: .setError(NewWizardMessages.VariableCreationDialog_error_invalidpath);
242: } else if (!new File(path).exists()) {
243: status
244: .setWarning(NewWizardMessages.VariableCreationDialog_warning_pathnotexists);
245: }
246: }
247: return status;
248: }
249:
250: private String getInitPath() {
251: String initPath = fPathField.getText();
252: if (initPath.length() == 0) {
253: initPath = fDialogSettings
254: .get(IUIConstants.DIALOGSTORE_LASTEXTJAR);
255: if (initPath == null) {
256: initPath = ""; //$NON-NLS-1$
257: }
258: } else {
259: IPath entryPath = new Path(initPath);
260: if (ArchiveFileFilter.isArchivePath(entryPath)) {
261: entryPath.removeLastSegments(1);
262: }
263: initPath = entryPath.toOSString();
264: }
265: return initPath;
266: }
267:
268: /*
269: * Open a dialog to choose a jar from the file system
270: */
271: private IPath chooseExtJarFile() {
272: String initPath = getInitPath();
273:
274: FileDialog dialog = new FileDialog(getShell());
275: dialog
276: .setText(NewWizardMessages.VariableCreationDialog_extjardialog_text);
277: dialog.setFilterExtensions(new String[] { "*.jar;*.zip" }); //$NON-NLS-1$
278: dialog.setFilterPath(initPath);
279: String res = dialog.open();
280: if (res != null) {
281: fDialogSettings.put(IUIConstants.DIALOGSTORE_LASTEXTJAR,
282: dialog.getFilterPath());
283: return Path.fromOSString(res).makeAbsolute();
284: }
285: return null;
286: }
287:
288: private IPath chooseExtDirectory() {
289: String initPath = getInitPath();
290:
291: DirectoryDialog dialog = new DirectoryDialog(getShell());
292: dialog
293: .setText(NewWizardMessages.VariableCreationDialog_extdirdialog_text);
294: dialog
295: .setMessage(NewWizardMessages.VariableCreationDialog_extdirdialog_message);
296: dialog.setFilterPath(initPath);
297: String res = dialog.open();
298: if (res != null) {
299: fDialogSettings.put(IUIConstants.DIALOGSTORE_LASTEXTJAR,
300: dialog.getFilterPath());
301: return Path.fromOSString(res);
302: }
303: return null;
304: }
305:
306: }
|