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.preferences;
011:
012: import org.eclipse.core.runtime.IPath;
013: import org.eclipse.core.runtime.IStatus;
014: import org.eclipse.core.runtime.Path;
015:
016: import org.eclipse.core.resources.IContainer;
017: import org.eclipse.core.resources.IFolder;
018: import org.eclipse.core.resources.IProject;
019: import org.eclipse.core.resources.IResource;
020: import org.eclipse.core.resources.IWorkspaceRoot;
021: import org.eclipse.core.resources.ResourcesPlugin;
022:
023: import org.eclipse.swt.SWT;
024: import org.eclipse.swt.layout.GridData;
025: import org.eclipse.swt.layout.GridLayout;
026: import org.eclipse.swt.widgets.Composite;
027: import org.eclipse.swt.widgets.Control;
028: import org.eclipse.swt.widgets.DirectoryDialog;
029: import org.eclipse.swt.widgets.Label;
030: import org.eclipse.swt.widgets.Shell;
031:
032: import org.eclipse.jface.viewers.ILabelProvider;
033: import org.eclipse.jface.viewers.ITreeContentProvider;
034: import org.eclipse.jface.viewers.ViewerFilter;
035: import org.eclipse.jface.window.Window;
036:
037: import org.eclipse.ui.dialogs.ElementTreeSelectionDialog;
038: import org.eclipse.ui.model.WorkbenchContentProvider;
039: import org.eclipse.ui.model.WorkbenchLabelProvider;
040:
041: import org.eclipse.ui.views.navigator.ResourceComparator;
042:
043: import org.eclipse.jdt.core.IClasspathEntry;
044:
045: import org.eclipse.jdt.internal.corext.util.Messages;
046:
047: import org.eclipse.jdt.internal.ui.dialogs.StatusInfo;
048: import org.eclipse.jdt.internal.ui.util.PixelConverter;
049: import org.eclipse.jdt.internal.ui.wizards.IStatusChangeListener;
050: import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages;
051: import org.eclipse.jdt.internal.ui.wizards.TypedElementSelectionValidator;
052: import org.eclipse.jdt.internal.ui.wizards.TypedViewerFilter;
053: import org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField;
054: import org.eclipse.jdt.internal.ui.wizards.dialogfields.IDialogFieldListener;
055: import org.eclipse.jdt.internal.ui.wizards.dialogfields.LayoutUtil;
056: import org.eclipse.jdt.internal.ui.wizards.dialogfields.SelectionButtonDialogField;
057: import org.eclipse.jdt.internal.ui.wizards.dialogfields.StringDialogField;
058:
059: public class NativeLibrariesConfigurationBlock {
060:
061: private class NativeLibrariesAdapter implements
062: IDialogFieldListener {
063: public void dialogFieldChanged(DialogField field) {
064: doFieldChanged(field);
065: }
066: }
067:
068: private StringDialogField fPathField;
069: private SelectionButtonDialogField fBrowseWorkspace;
070: private SelectionButtonDialogField fBrowseExternal;
071: private final IClasspathEntry fEntry;
072: private Shell fShell;
073: private final IStatusChangeListener fListener;
074: private final String fOrginalValue;
075:
076: public NativeLibrariesConfigurationBlock(
077: IStatusChangeListener listener, Shell parent,
078: String nativeLibPath, IClasspathEntry parentEntry) {
079: fListener = listener;
080: fEntry = parentEntry;
081:
082: NativeLibrariesAdapter adapter = new NativeLibrariesAdapter();
083:
084: fPathField = new StringDialogField();
085: fPathField
086: .setLabelText(NewWizardMessages.NativeLibrariesDialog_location_label);
087: fPathField.setDialogFieldListener(adapter);
088:
089: fBrowseWorkspace = new SelectionButtonDialogField(SWT.PUSH);
090: fBrowseWorkspace
091: .setLabelText(NewWizardMessages.NativeLibrariesDialog_workspace_browse);
092: fBrowseWorkspace.setDialogFieldListener(adapter);
093:
094: fBrowseExternal = new SelectionButtonDialogField(SWT.PUSH);
095: fBrowseExternal
096: .setLabelText(NewWizardMessages.NativeLibrariesDialog_external_browse);
097: fBrowseExternal.setDialogFieldListener(adapter);
098:
099: if (nativeLibPath != null) {
100: fPathField.setText(Path.fromPortableString(nativeLibPath)
101: .toString());
102: fOrginalValue = nativeLibPath;
103: } else {
104: fOrginalValue = ""; //$NON-NLS-1$
105: }
106: }
107:
108: public Control createContents(Composite parent) {
109: fShell = parent.getShell();
110:
111: Composite inner = new Composite(parent, SWT.NONE);
112: inner.setFont(parent.getFont());
113: inner
114: .setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
115: true));
116:
117: int nColumns = 3;
118:
119: GridLayout layout = new GridLayout(nColumns, false);
120: layout.marginWidth = 0;
121: layout.marginWidth = 0;
122: inner.setLayout(layout);
123:
124: PixelConverter converter = new PixelConverter(parent);
125:
126: Label desc = new Label(inner, SWT.WRAP);
127: desc.setFont(inner.getFont());
128: desc.setText(Messages.format(
129: NewWizardMessages.NativeLibrariesDialog_description,
130: new String[] { fEntry.getPath().lastSegment() }));
131: GridData gridData = new GridData(GridData.FILL,
132: GridData.CENTER, false, false, 3, 1);
133: gridData.widthHint = converter.convertWidthInCharsToPixels(80);
134: desc.setLayoutData(gridData);
135:
136: fPathField.doFillIntoGrid(inner, 2);
137: LayoutUtil.setHorizontalGrabbing(fPathField
138: .getTextControl(null));
139: LayoutUtil.setWidthHint(fPathField.getTextControl(null),
140: converter.convertWidthInCharsToPixels(50));
141:
142: fBrowseExternal.doFillIntoGrid(inner, 1);
143:
144: DialogField.createEmptySpace(inner, 2);
145: fBrowseWorkspace.doFillIntoGrid(inner, 1);
146:
147: fPathField.setFocus();
148:
149: return parent;
150: }
151:
152: public String getNativeLibraryPath() {
153: String val = fPathField.getText();
154: if (val.length() == 0) {
155: return null;
156: }
157: return new Path(val).toPortableString();
158: }
159:
160: final void doFieldChanged(DialogField field) {
161: if (field == fBrowseExternal) {
162: String res = chooseExternal();
163: if (res != null) {
164: fPathField.setText(res);
165: }
166: } else if (field == fBrowseWorkspace) {
167: String res = chooseInternal();
168: if (res != null) {
169: fPathField.setText(res);
170: }
171: } else if (field == fPathField) {
172: fListener.statusChanged(validatePath());
173: }
174: }
175:
176: private IStatus validatePath() {
177: StatusInfo status = new StatusInfo();
178: String val = fPathField.getText();
179: if (val.length() == 0) {
180: return status;
181: }
182: Path path = new Path(val);
183: if (path.isAbsolute()) {
184: if (!path.toFile().isDirectory()) {
185: status
186: .setWarning(NewWizardMessages.NativeLibrariesDialog_error_external_not_existing);
187: return status;
188: }
189: } else {
190: if (!(ResourcesPlugin.getWorkspace().getRoot().findMember(
191: path) instanceof IContainer)) {
192: status
193: .setWarning(NewWizardMessages.NativeLibrariesDialog_error_internal_not_existing);
194: return status;
195: }
196: }
197: return status;
198: }
199:
200: private String chooseExternal() {
201: IPath currPath = new Path(fPathField.getText());
202: if (currPath.isEmpty()) {
203: currPath = fEntry.getPath();
204: } else {
205: currPath = currPath.removeLastSegments(1);
206: }
207:
208: DirectoryDialog dialog = new DirectoryDialog(fShell);
209: dialog
210: .setMessage(NewWizardMessages.NativeLibrariesDialog_external_message);
211: dialog
212: .setText(NewWizardMessages.NativeLibrariesDialog_extfiledialog_text);
213: dialog.setFilterPath(currPath.toOSString());
214: String res = dialog.open();
215: if (res != null) {
216: return res;
217: }
218: return null;
219: }
220:
221: /*
222: * Opens a dialog to choose an internal file.
223: */
224: private String chooseInternal() {
225: String initSelection = fPathField.getText();
226:
227: ILabelProvider lp = new WorkbenchLabelProvider();
228: ITreeContentProvider cp = new WorkbenchContentProvider();
229: Class[] acceptedClasses = new Class[] { IProject.class,
230: IFolder.class };
231: TypedElementSelectionValidator validator = new TypedElementSelectionValidator(
232: acceptedClasses, true);
233: ViewerFilter filter = new TypedViewerFilter(acceptedClasses);
234:
235: IResource initSel = null;
236: IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
237: if (initSelection.length() > 0) {
238: initSel = root.findMember(new Path(initSelection));
239: }
240: if (initSel == null) {
241: initSel = root.findMember(fEntry.getPath());
242: }
243:
244: ElementTreeSelectionDialog dialog = new ElementTreeSelectionDialog(
245: fShell, lp, cp);
246: dialog.setAllowMultiple(false);
247: dialog.setValidator(validator);
248: dialog.addFilter(filter);
249: dialog.setComparator(new ResourceComparator(
250: ResourceComparator.NAME));
251: dialog
252: .setTitle(NewWizardMessages.NativeLibrariesDialog_intfiledialog_title);
253: dialog
254: .setMessage(NewWizardMessages.NativeLibrariesDialog_intfiledialog_message);
255: dialog.setInput(root);
256: dialog.setInitialSelection(initSel);
257: dialog.setHelpAvailable(false);
258: if (dialog.open() == Window.OK) {
259: IResource res = (IResource) dialog.getFirstResult();
260: return res.getFullPath().makeRelative().toString();
261: }
262: return null;
263: }
264:
265: public void performDefaults() {
266: fPathField.setText(fOrginalValue);
267: }
268:
269: }
|