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.ArrayList;
014: import java.util.HashMap;
015: import java.util.Iterator;
016: import java.util.List;
017: import java.util.Map;
018:
019: import org.eclipse.core.runtime.IPath;
020: import org.eclipse.core.runtime.IStatus;
021: import org.eclipse.core.runtime.Path;
022:
023: import org.eclipse.swt.SWT;
024: import org.eclipse.swt.custom.CLabel;
025: import org.eclipse.swt.layout.GridData;
026: import org.eclipse.swt.layout.GridLayout;
027: import org.eclipse.swt.widgets.Button;
028: import org.eclipse.swt.widgets.Composite;
029: import org.eclipse.swt.widgets.Control;
030: import org.eclipse.swt.widgets.Shell;
031:
032: import org.eclipse.jface.dialogs.Dialog;
033: import org.eclipse.jface.dialogs.IDialogConstants;
034: import org.eclipse.jface.dialogs.IDialogSettings;
035: import org.eclipse.jface.dialogs.StatusDialog;
036: import org.eclipse.jface.resource.JFaceResources;
037: import org.eclipse.jface.viewers.StructuredSelection;
038: import org.eclipse.jface.viewers.Viewer;
039: import org.eclipse.jface.viewers.ViewerComparator;
040: import org.eclipse.jface.window.Window;
041:
042: import org.eclipse.ui.PlatformUI;
043: import org.eclipse.ui.dialogs.PreferencesUtil;
044:
045: import org.eclipse.jdt.core.JavaCore;
046:
047: import org.eclipse.jdt.internal.corext.util.Messages;
048:
049: import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
050: import org.eclipse.jdt.internal.ui.JavaPlugin;
051: import org.eclipse.jdt.internal.ui.dialogs.StatusInfo;
052: import org.eclipse.jdt.internal.ui.preferences.ClasspathVariablesPreferencePage;
053: import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages;
054: import org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField;
055: import org.eclipse.jdt.internal.ui.wizards.dialogfields.IDialogFieldListener;
056: import org.eclipse.jdt.internal.ui.wizards.dialogfields.IListAdapter;
057: import org.eclipse.jdt.internal.ui.wizards.dialogfields.LayoutUtil;
058: import org.eclipse.jdt.internal.ui.wizards.dialogfields.ListDialogField;
059: import org.eclipse.jdt.internal.ui.wizards.dialogfields.SelectionButtonDialogField;
060:
061: public class NewVariableEntryDialog extends StatusDialog {
062:
063: private class VariablesAdapter implements IDialogFieldListener,
064: IListAdapter {
065:
066: // -------- IListAdapter --------
067:
068: public void customButtonPressed(ListDialogField field, int index) {
069: switch (index) {
070: case IDX_EXTEND: /* extend */
071: extendButtonPressed();
072: break;
073: }
074: }
075:
076: public void selectionChanged(ListDialogField field) {
077: doSelectionChanged();
078: }
079:
080: public void doubleClicked(ListDialogField field) {
081: doDoubleClick();
082: }
083:
084: // ---------- IDialogFieldListener --------
085:
086: public void dialogFieldChanged(DialogField field) {
087: if (field == fConfigButton) {
088: configButtonPressed();
089: }
090:
091: }
092:
093: }
094:
095: private final int IDX_EXTEND = 0;
096:
097: private ListDialogField fVariablesList;
098: private boolean fCanExtend;
099: private boolean fIsValidSelection;
100:
101: private IPath[] fResultPaths;
102:
103: private SelectionButtonDialogField fConfigButton;
104:
105: private CLabel fWarning;
106:
107: public NewVariableEntryDialog(Shell parent) {
108: super (parent);
109: setTitle(NewWizardMessages.NewVariableEntryDialog_title);
110:
111: int shellStyle = getShellStyle();
112: setShellStyle(shellStyle | SWT.MAX | SWT.RESIZE);
113: updateStatus(new StatusInfo(IStatus.ERROR, "")); //$NON-NLS-1$
114:
115: String[] buttonLabels = new String[] { NewWizardMessages.NewVariableEntryDialog_vars_extend, };
116:
117: VariablesAdapter adapter = new VariablesAdapter();
118:
119: CPVariableElementLabelProvider labelProvider = new CPVariableElementLabelProvider(
120: false);
121:
122: fVariablesList = new ListDialogField(adapter, buttonLabels,
123: labelProvider);
124: fVariablesList.setDialogFieldListener(adapter);
125: fVariablesList
126: .setLabelText(NewWizardMessages.NewVariableEntryDialog_vars_label);
127:
128: fVariablesList.enableButton(IDX_EXTEND, false);
129:
130: fVariablesList.setViewerComparator(new ViewerComparator() {
131: public int compare(Viewer viewer, Object e1, Object e2) {
132: if (e1 instanceof CPVariableElement
133: && e2 instanceof CPVariableElement) {
134: return getComparator().compare(
135: ((CPVariableElement) e1).getName(),
136: ((CPVariableElement) e2).getName());
137: }
138: return super .compare(viewer, e1, e2);
139: }
140: });
141:
142: fConfigButton = new SelectionButtonDialogField(SWT.PUSH);
143: fConfigButton
144: .setLabelText(NewWizardMessages.NewVariableEntryDialog_configbutton_label);
145: fConfigButton.setDialogFieldListener(adapter);
146:
147: initializeElements();
148:
149: fCanExtend = false;
150: fIsValidSelection = false;
151: fResultPaths = null;
152:
153: fVariablesList.selectFirstElement();
154: }
155:
156: private void initializeElements() {
157: String[] entries = JavaCore.getClasspathVariableNames();
158: ArrayList elements = new ArrayList(entries.length);
159: for (int i = 0; i < entries.length; i++) {
160: String name = entries[i];
161: IPath entryPath = JavaCore.getClasspathVariable(name);
162: if (entryPath != null) {
163: elements.add(new CPVariableElement(name, entryPath));
164: }
165: }
166:
167: fVariablesList.setElements(elements);
168: }
169:
170: /* (non-Javadoc)
171: * @see Window#configureShell(Shell)
172: */
173: protected void configureShell(Shell shell) {
174: super .configureShell(shell);
175: PlatformUI.getWorkbench().getHelpSystem().setHelp(shell,
176: IJavaHelpContextIds.NEW_VARIABLE_ENTRY_DIALOG);
177: }
178:
179: /* (non-Javadoc)
180: * @see org.eclipse.jface.dialogs.Dialog#getDialogBoundsSettings()
181: */
182: protected IDialogSettings getDialogBoundsSettings() {
183: return JavaPlugin.getDefault().getDialogSettingsSection(
184: getClass().getName());
185: }
186:
187: /* (non-Javadoc)
188: * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
189: */
190: protected Control createDialogArea(Composite parent) {
191: initializeDialogUnits(parent);
192:
193: Composite composite = (Composite) super
194: .createDialogArea(parent);
195: GridLayout layout = (GridLayout) composite.getLayout();
196: layout.numColumns = 2;
197:
198: fVariablesList.doFillIntoGrid(composite, 3);
199:
200: LayoutUtil.setHorizontalSpan(fVariablesList
201: .getLabelControl(null), 2);
202:
203: GridData listData = (GridData) fVariablesList.getListControl(
204: null).getLayoutData();
205: listData.grabExcessHorizontalSpace = true;
206: listData.heightHint = convertHeightInCharsToPixels(10);
207: listData.widthHint = convertWidthInCharsToPixels(70);
208:
209: fWarning = new CLabel(composite, SWT.NONE);
210: fWarning.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true,
211: false, fVariablesList.getNumberOfControls() - 1, 1));
212:
213: Composite lowerComposite = new Composite(composite, SWT.NONE);
214: lowerComposite.setLayoutData(new GridData(
215: GridData.HORIZONTAL_ALIGN_FILL));
216:
217: layout = new GridLayout();
218: layout.marginHeight = 0;
219: layout.marginWidth = 0;
220: lowerComposite.setLayout(layout);
221:
222: fConfigButton.doFillIntoGrid(lowerComposite, 1);
223:
224: applyDialogFont(composite);
225: return composite;
226: }
227:
228: public IPath[] getResult() {
229: return fResultPaths;
230: }
231:
232: /*
233: * @see IDoubleClickListener#doubleClick(DoubleClickEvent)
234: */
235: private void doDoubleClick() {
236: if (fIsValidSelection) {
237: okPressed();
238: } else if (fCanExtend) {
239: extendButtonPressed();
240: }
241: }
242:
243: private void doSelectionChanged() {
244: boolean isValidSelection = true;
245: boolean canExtend = false;
246: StatusInfo status = new StatusInfo();
247:
248: List selected = fVariablesList.getSelectedElements();
249: int nSelected = selected.size();
250:
251: if (nSelected > 0) {
252: fResultPaths = new Path[nSelected];
253: for (int i = 0; i < nSelected; i++) {
254: CPVariableElement curr = (CPVariableElement) selected
255: .get(i);
256: fResultPaths[i] = new Path(curr.getName());
257: File file = curr.getPath().toFile();
258: if (!file.exists()) {
259: status
260: .setError(NewWizardMessages.NewVariableEntryDialog_info_notexists);
261: isValidSelection = false;
262: break;
263: }
264: if (file.isDirectory()) {
265: status
266: .setError(NewWizardMessages.NewVariableEntryDialog_info_isfolder);
267: canExtend = true;
268: isValidSelection = false;
269: break;
270: }
271: }
272: } else {
273: isValidSelection = false;
274: status
275: .setInfo(NewWizardMessages.NewVariableEntryDialog_info_noselection);
276: }
277: if (isValidSelection && nSelected > 1) {
278: String str = Messages
279: .format(
280: NewWizardMessages.NewVariableEntryDialog_info_selected,
281: String.valueOf(nSelected));
282: status.setInfo(str);
283: }
284: fCanExtend = nSelected == 1 && canExtend;
285: fVariablesList.enableButton(0, fCanExtend);
286:
287: updateStatus(status);
288: fIsValidSelection = isValidSelection;
289: Button okButton = getButton(IDialogConstants.OK_ID);
290: if (okButton != null && !okButton.isDisposed()) {
291: okButton.setEnabled(isValidSelection);
292: }
293: updateDeprecationWarning();
294: }
295:
296: private void updateDeprecationWarning() {
297: if (fWarning == null || fWarning.isDisposed())
298: return;
299:
300: for (Iterator iter = fVariablesList.getSelectedElements()
301: .iterator(); iter.hasNext();) {
302: CPVariableElement element = (CPVariableElement) iter.next();
303: String deprecationMessage = element.getDeprecationMessage();
304: if (deprecationMessage != null) {
305: fWarning.setText(deprecationMessage);
306: fWarning.setImage(JFaceResources
307: .getImage(Dialog.DLG_IMG_MESSAGE_WARNING));
308: return;
309: }
310: }
311: fWarning.setText(null);
312: fWarning.setImage(null);
313: }
314:
315: private IPath[] chooseExtensions(CPVariableElement elem) {
316: File file = elem.getPath().toFile();
317:
318: JARFileSelectionDialog dialog = new JARFileSelectionDialog(
319: getShell(), true, false);
320: dialog
321: .setTitle(NewWizardMessages.NewVariableEntryDialog_ExtensionDialog_title);
322: dialog
323: .setMessage(Messages
324: .format(
325: NewWizardMessages.NewVariableEntryDialog_ExtensionDialog_description,
326: elem.getName()));
327: dialog.setInput(file);
328: if (dialog.open() == Window.OK) {
329: Object[] selected = dialog.getResult();
330: IPath[] paths = new IPath[selected.length];
331: for (int i = 0; i < selected.length; i++) {
332: IPath filePath = Path.fromOSString(((File) selected[i])
333: .getPath());
334: IPath resPath = new Path(elem.getName());
335: for (int k = elem.getPath().segmentCount(); k < filePath
336: .segmentCount(); k++) {
337: resPath = resPath.append(filePath.segment(k));
338: }
339: paths[i] = resPath;
340: }
341: return paths;
342: }
343: return null;
344: }
345:
346: protected final void extendButtonPressed() {
347: List selected = fVariablesList.getSelectedElements();
348: if (selected.size() == 1) {
349: IPath[] extendedPaths = chooseExtensions((CPVariableElement) selected
350: .get(0));
351: if (extendedPaths != null) {
352: fResultPaths = extendedPaths;
353: super .buttonPressed(IDialogConstants.OK_ID);
354: }
355: }
356: }
357:
358: protected final void configButtonPressed() {
359: String id = ClasspathVariablesPreferencePage.ID;
360: Map options = new HashMap();
361: List selected = fVariablesList.getSelectedElements();
362: if (!selected.isEmpty()) {
363: String varName = ((CPVariableElement) selected.get(0))
364: .getName();
365: options
366: .put(
367: ClasspathVariablesPreferencePage.DATA_SELECT_VARIABLE,
368: varName);
369: }
370: PreferencesUtil.createPreferenceDialogOn(getShell(), id,
371: new String[] { id }, options).open();
372:
373: List oldElements = fVariablesList.getElements();
374: initializeElements();
375: List newElements = fVariablesList.getElements();
376: newElements.removeAll(oldElements);
377: if (!newElements.isEmpty()) {
378: fVariablesList.selectElements(new StructuredSelection(
379: newElements));
380: } else if (fVariablesList.getSelectedElements().isEmpty()) {
381: fVariablesList.selectFirstElement();
382: }
383: }
384:
385: }
|