KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > preferences > NativeLibrariesConfigurationBlock


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation 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
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.ui.preferences;
12
13 import org.eclipse.core.runtime.IPath;
14 import org.eclipse.core.runtime.IStatus;
15 import org.eclipse.core.runtime.Path;
16
17 import org.eclipse.core.resources.IContainer;
18 import org.eclipse.core.resources.IFolder;
19 import org.eclipse.core.resources.IProject;
20 import org.eclipse.core.resources.IResource;
21 import org.eclipse.core.resources.IWorkspaceRoot;
22 import org.eclipse.core.resources.ResourcesPlugin;
23
24 import org.eclipse.swt.SWT;
25 import org.eclipse.swt.layout.GridData;
26 import org.eclipse.swt.layout.GridLayout;
27 import org.eclipse.swt.widgets.Composite;
28 import org.eclipse.swt.widgets.Control;
29 import org.eclipse.swt.widgets.DirectoryDialog;
30 import org.eclipse.swt.widgets.Label;
31 import org.eclipse.swt.widgets.Shell;
32
33 import org.eclipse.jface.viewers.ILabelProvider;
34 import org.eclipse.jface.viewers.ITreeContentProvider;
35 import org.eclipse.jface.viewers.ViewerFilter;
36 import org.eclipse.jface.window.Window;
37
38 import org.eclipse.ui.dialogs.ElementTreeSelectionDialog;
39 import org.eclipse.ui.model.WorkbenchContentProvider;
40 import org.eclipse.ui.model.WorkbenchLabelProvider;
41
42 import org.eclipse.ui.views.navigator.ResourceComparator;
43
44 import org.eclipse.jdt.core.IClasspathEntry;
45
46 import org.eclipse.jdt.internal.corext.util.Messages;
47
48 import org.eclipse.jdt.internal.ui.dialogs.StatusInfo;
49 import org.eclipse.jdt.internal.ui.util.PixelConverter;
50 import org.eclipse.jdt.internal.ui.wizards.IStatusChangeListener;
51 import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages;
52 import org.eclipse.jdt.internal.ui.wizards.TypedElementSelectionValidator;
53 import org.eclipse.jdt.internal.ui.wizards.TypedViewerFilter;
54 import org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField;
55 import org.eclipse.jdt.internal.ui.wizards.dialogfields.IDialogFieldListener;
56 import org.eclipse.jdt.internal.ui.wizards.dialogfields.LayoutUtil;
57 import org.eclipse.jdt.internal.ui.wizards.dialogfields.SelectionButtonDialogField;
58 import org.eclipse.jdt.internal.ui.wizards.dialogfields.StringDialogField;
59
60 public class NativeLibrariesConfigurationBlock {
61
62     private class NativeLibrariesAdapter implements IDialogFieldListener {
63         public void dialogFieldChanged(DialogField field) {
64             doFieldChanged(field);
65         }
66     }
67     
68     private StringDialogField fPathField;
69     private SelectionButtonDialogField fBrowseWorkspace;
70     private SelectionButtonDialogField fBrowseExternal;
71     private final IClasspathEntry fEntry;
72     private Shell fShell;
73     private final IStatusChangeListener fListener;
74     private final String JavaDoc fOrginalValue;
75     
76     public NativeLibrariesConfigurationBlock(IStatusChangeListener listener, Shell parent, String JavaDoc nativeLibPath, IClasspathEntry parentEntry) {
77         fListener= listener;
78         fEntry= parentEntry;
79         
80         NativeLibrariesAdapter adapter= new NativeLibrariesAdapter();
81         
82         fPathField= new StringDialogField();
83         fPathField.setLabelText(NewWizardMessages.NativeLibrariesDialog_location_label);
84         fPathField.setDialogFieldListener(adapter);
85         
86         fBrowseWorkspace= new SelectionButtonDialogField(SWT.PUSH);
87         fBrowseWorkspace.setLabelText(NewWizardMessages.NativeLibrariesDialog_workspace_browse);
88         fBrowseWorkspace.setDialogFieldListener(adapter);
89         
90         fBrowseExternal= new SelectionButtonDialogField(SWT.PUSH);
91         fBrowseExternal.setLabelText(NewWizardMessages.NativeLibrariesDialog_external_browse);
92         fBrowseExternal.setDialogFieldListener(adapter);
93     
94         if (nativeLibPath != null) {
95             fPathField.setText(Path.fromPortableString(nativeLibPath).toString());
96             fOrginalValue= nativeLibPath;
97         } else {
98             fOrginalValue= ""; //$NON-NLS-1$
99
}
100     }
101     
102     public Control createContents(Composite parent) {
103         fShell= parent.getShell();
104         
105         Composite inner= new Composite(parent, SWT.NONE);
106         inner.setFont(parent.getFont());
107         inner.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
108
109         int nColumns= 3;
110         
111         GridLayout layout= new GridLayout(nColumns, false);
112         layout.marginWidth= 0;
113         layout.marginWidth= 0;
114         inner.setLayout(layout);
115         
116         PixelConverter converter= new PixelConverter(parent);
117
118         Label desc= new Label(inner, SWT.WRAP);
119         desc.setFont(inner.getFont());
120         desc.setText(Messages.format(NewWizardMessages.NativeLibrariesDialog_description, new String JavaDoc[] { fEntry.getPath().lastSegment() }));
121         GridData gridData= new GridData(GridData.FILL, GridData.CENTER, false, false, 3, 1);
122         gridData.widthHint= converter.convertWidthInCharsToPixels(80);
123         desc.setLayoutData(gridData);
124         
125         fPathField.doFillIntoGrid(inner, 2);
126         LayoutUtil.setHorizontalGrabbing(fPathField.getTextControl(null));
127         LayoutUtil.setWidthHint(fPathField.getTextControl(null), converter.convertWidthInCharsToPixels(50));
128         
129         fBrowseExternal.doFillIntoGrid(inner, 1);
130         
131         DialogField.createEmptySpace(inner, 2);
132         fBrowseWorkspace.doFillIntoGrid(inner, 1);
133         
134         fPathField.setFocus();
135         
136         return parent;
137     }
138     
139     public String JavaDoc getNativeLibraryPath() {
140         String JavaDoc val= fPathField.getText();
141         if (val.length() == 0) {
142             return null;
143         }
144         return new Path(val).toPortableString();
145     }
146     
147     final void doFieldChanged(DialogField field) {
148         if (field == fBrowseExternal) {
149             String JavaDoc res= chooseExternal();
150             if (res != null) {
151                 fPathField.setText(res);
152             }
153         } else if (field == fBrowseWorkspace) {
154             String JavaDoc res= chooseInternal();
155             if (res != null) {
156                 fPathField.setText(res);
157             }
158         } else if (field == fPathField) {
159             fListener.statusChanged(validatePath());
160         }
161     }
162
163     private IStatus validatePath() {
164         StatusInfo status= new StatusInfo();
165         String JavaDoc val= fPathField.getText();
166         if (val.length() == 0) {
167             return status;
168         }
169         Path path= new Path(val);
170         if (path.isAbsolute()) {
171             if (!path.toFile().isDirectory()) {
172                 status.setWarning(NewWizardMessages.NativeLibrariesDialog_error_external_not_existing);
173                 return status;
174             }
175         } else {
176             if (!(ResourcesPlugin.getWorkspace().getRoot().findMember(path) instanceof IContainer)) {
177                 status.setWarning(NewWizardMessages.NativeLibrariesDialog_error_internal_not_existing);
178                 return status;
179             }
180         }
181         return status;
182     }
183
184     private String JavaDoc chooseExternal() {
185         IPath currPath= new Path(fPathField.getText());
186         if (currPath.isEmpty()) {
187             currPath= fEntry.getPath();
188         } else {
189             currPath= currPath.removeLastSegments(1);
190         }
191     
192         DirectoryDialog dialog= new DirectoryDialog(fShell);
193         dialog.setMessage(NewWizardMessages.NativeLibrariesDialog_external_message);
194         dialog.setText(NewWizardMessages.NativeLibrariesDialog_extfiledialog_text);
195         dialog.setFilterPath(currPath.toOSString());
196         String JavaDoc res= dialog.open();
197         if (res != null) {
198             return res;
199         }
200         return null;
201     }
202     
203     /*
204      * Opens a dialog to choose an internal file.
205      */

206     private String JavaDoc chooseInternal() {
207         String JavaDoc initSelection= fPathField.getText();
208         
209         ILabelProvider lp= new WorkbenchLabelProvider();
210         ITreeContentProvider cp= new WorkbenchContentProvider();
211         Class JavaDoc[] acceptedClasses= new Class JavaDoc[] { IProject.class, IFolder.class };
212         TypedElementSelectionValidator validator= new TypedElementSelectionValidator(acceptedClasses, true);
213         ViewerFilter filter= new TypedViewerFilter(acceptedClasses);
214
215         IResource initSel= null;
216         IWorkspaceRoot root= ResourcesPlugin.getWorkspace().getRoot();
217         if (initSelection.length() > 0) {
218             initSel= root.findMember(new Path(initSelection));
219         }
220         if (initSel == null) {
221             initSel= root.findMember(fEntry.getPath());
222         }
223
224         ElementTreeSelectionDialog dialog= new ElementTreeSelectionDialog(fShell, lp, cp);
225         dialog.setAllowMultiple(false);
226         dialog.setValidator(validator);
227         dialog.addFilter(filter);
228         dialog.setComparator(new ResourceComparator(ResourceComparator.NAME));
229         dialog.setTitle(NewWizardMessages.NativeLibrariesDialog_intfiledialog_title);
230         dialog.setMessage(NewWizardMessages.NativeLibrariesDialog_intfiledialog_message);
231         dialog.setInput(root);
232         dialog.setInitialSelection(initSel);
233         dialog.setHelpAvailable(false);
234         if (dialog.open() == Window.OK) {
235             IResource res= (IResource) dialog.getFirstResult();
236             return res.getFullPath().makeRelative().toString();
237         }
238         return null;
239     }
240
241     public void performDefaults() {
242         fPathField.setText(fOrginalValue);
243     }
244
245 }
246
Popular Tags