KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > ui > wizards > NewPackageWizardPage


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.ui.wizards;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14 import java.net.URI JavaDoc;
15
16 import org.eclipse.core.filesystem.EFS;
17 import org.eclipse.core.filesystem.IFileStore;
18
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.core.runtime.IPath;
21 import org.eclipse.core.runtime.IProgressMonitor;
22 import org.eclipse.core.runtime.IStatus;
23 import org.eclipse.core.runtime.NullProgressMonitor;
24
25 import org.eclipse.core.resources.IResource;
26
27 import org.eclipse.swt.SWT;
28 import org.eclipse.swt.layout.GridData;
29 import org.eclipse.swt.layout.GridLayout;
30 import org.eclipse.swt.widgets.Composite;
31 import org.eclipse.swt.widgets.Label;
32 import org.eclipse.swt.widgets.Text;
33
34 import org.eclipse.jface.dialogs.Dialog;
35 import org.eclipse.jface.operation.IRunnableWithProgress;
36 import org.eclipse.jface.viewers.IStructuredSelection;
37
38 import org.eclipse.ui.PlatformUI;
39
40 import org.eclipse.jdt.core.IJavaElement;
41 import org.eclipse.jdt.core.IJavaProject;
42 import org.eclipse.jdt.core.IPackageFragment;
43 import org.eclipse.jdt.core.IPackageFragmentRoot;
44 import org.eclipse.jdt.core.JavaConventions;
45 import org.eclipse.jdt.core.JavaCore;
46
47 import org.eclipse.jdt.internal.corext.util.Messages;
48
49 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
50 import org.eclipse.jdt.internal.ui.JavaPlugin;
51 import org.eclipse.jdt.internal.ui.dialogs.StatusInfo;
52 import org.eclipse.jdt.internal.ui.dialogs.TextFieldNavigationHandler;
53 import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages;
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.StringDialogField;
58
59 /**
60  * Wizard page to create a new package.
61  *
62  * <p>
63  * Note: This class is not intended to be subclassed, but clients can instantiate.
64  * To implement a different kind of a new package wizard page, extend <code>NewContainerWizardPage</code>.
65  * </p>
66  *
67  * @since 2.0
68  */

69 public class NewPackageWizardPage extends NewContainerWizardPage {
70     
71     private static final String JavaDoc PAGE_NAME= "NewPackageWizardPage"; //$NON-NLS-1$
72

73     private static final String JavaDoc PACKAGE= "NewPackageWizardPage.package"; //$NON-NLS-1$
74

75     private StringDialogField fPackageDialogField;
76     
77     /*
78      * Status of last validation of the package field
79      */

80     private IStatus fPackageStatus;
81     
82     private IPackageFragment fCreatedPackageFragment;
83     
84     /**
85      * Creates a new <code>NewPackageWizardPage</code>
86      */

87     public NewPackageWizardPage() {
88         super(PAGE_NAME);
89         
90         setTitle(NewWizardMessages.NewPackageWizardPage_title);
91         setDescription(NewWizardMessages.NewPackageWizardPage_description);
92         
93         fCreatedPackageFragment= null;
94
95         PackageFieldAdapter adapter= new PackageFieldAdapter();
96         
97         fPackageDialogField= new StringDialogField();
98         fPackageDialogField.setDialogFieldListener(adapter);
99         fPackageDialogField.setLabelText(NewWizardMessages.NewPackageWizardPage_package_label);
100         
101         fPackageStatus= new StatusInfo();
102     }
103
104     // -------- Initialization ---------
105

106     /**
107      * The wizard owning this page is responsible for calling this method with the
108      * current selection. The selection is used to initialize the fields of the wizard
109      * page.
110      *
111      * @param selection used to initialize the fields
112      */

113     public void init(IStructuredSelection selection) {
114         IJavaElement jelem= getInitialJavaElement(selection);
115         
116         initContainerPage(jelem);
117         String JavaDoc pName= ""; //$NON-NLS-1$
118
if (jelem != null) {
119             IPackageFragment pf= (IPackageFragment) jelem.getAncestor(IJavaElement.PACKAGE_FRAGMENT);
120             if (pf != null && !pf.isDefaultPackage())
121                 pName= pf.getElementName();
122         }
123         setPackageText(pName, true);
124         updateStatus(new IStatus[] { fContainerStatus, fPackageStatus });
125     }
126     
127     // -------- UI Creation ---------
128

129     /*
130      * @see WizardPage#createControl
131      */

132     public void createControl(Composite parent) {
133         initializeDialogUnits(parent);
134             
135         Composite composite= new Composite(parent, SWT.NONE);
136         composite.setFont(parent.getFont());
137         int nColumns= 3;
138         
139         GridLayout layout= new GridLayout();
140         layout.numColumns= 3;
141         composite.setLayout(layout);
142         
143         Label label= new Label(composite, SWT.WRAP);
144         label.setText(NewWizardMessages.NewPackageWizardPage_info);
145         GridData gd= new GridData();
146         gd.widthHint= convertWidthInCharsToPixels(60);
147         gd.horizontalSpan= 3;
148         label.setLayoutData(gd);
149         
150         createContainerControls(composite, nColumns);
151         createPackageControls(composite, nColumns);
152         
153         setControl(composite);
154         Dialog.applyDialogFont(composite);
155         PlatformUI.getWorkbench().getHelpSystem().setHelp(composite, IJavaHelpContextIds.NEW_PACKAGE_WIZARD_PAGE);
156     }
157     
158     /**
159      * @see org.eclipse.jface.dialogs.IDialogPage#setVisible(boolean)
160      */

161     public void setVisible(boolean visible) {
162         super.setVisible(visible);
163         if (visible) {
164             setFocus();
165         }
166     }
167     
168     /**
169      * Sets the focus to the package name input field.
170      */

171     protected void setFocus() {
172         fPackageDialogField.setFocus();
173     }
174     
175
176     private void createPackageControls(Composite composite, int nColumns) {
177         fPackageDialogField.doFillIntoGrid(composite, nColumns - 1);
178         Text text= fPackageDialogField.getTextControl(null);
179         LayoutUtil.setWidthHint(text, getMaxFieldWidth());
180         LayoutUtil.setHorizontalGrabbing(text);
181         DialogField.createEmptySpace(composite);
182         
183         TextFieldNavigationHandler.install(text);
184     }
185                 
186     // -------- PackageFieldAdapter --------
187

188     private class PackageFieldAdapter implements IDialogFieldListener {
189         
190         // --------- IDialogFieldListener
191

192         public void dialogFieldChanged(DialogField field) {
193             fPackageStatus= packageChanged();
194             // tell all others
195
handleFieldChanged(PACKAGE);
196         }
197     }
198         
199     // -------- update message ----------------
200

201     /*
202      * @see org.eclipse.jdt.ui.wizards.NewContainerWizardPage#handleFieldChanged(String)
203      */

204     protected void handleFieldChanged(String JavaDoc fieldName) {
205         super.handleFieldChanged(fieldName);
206         if (fieldName == CONTAINER) {
207             fPackageStatus= packageChanged();
208         }
209         // do status line update
210
updateStatus(new IStatus[] { fContainerStatus, fPackageStatus });
211     }
212             
213     // ----------- validation ----------
214

215     private IStatus validatePackageName(String JavaDoc text) {
216         IJavaProject project= getJavaProject();
217         if (project == null || !project.exists()) {
218             return JavaConventions.validatePackageName(text, JavaCore.VERSION_1_3, JavaCore.VERSION_1_3);
219         }
220         String JavaDoc sourceLevel= project.getOption(JavaCore.COMPILER_SOURCE, true);
221         String JavaDoc compliance= project.getOption(JavaCore.COMPILER_COMPLIANCE, true);
222         return JavaConventions.validatePackageName(text, sourceLevel, compliance);
223     }
224     
225     /*
226      * Verifies the input for the package field.
227      */

228     private IStatus packageChanged() {
229         StatusInfo status= new StatusInfo();
230         String JavaDoc packName= getPackageText();
231         if (packName.length() > 0) {
232             IStatus val= validatePackageName(packName);
233             if (val.getSeverity() == IStatus.ERROR) {
234                 status.setError(Messages.format(NewWizardMessages.NewPackageWizardPage_error_InvalidPackageName, val.getMessage()));
235                 return status;
236             } else if (val.getSeverity() == IStatus.WARNING) {
237                 status.setWarning(Messages.format(NewWizardMessages.NewPackageWizardPage_warning_DiscouragedPackageName, val.getMessage()));
238             }
239         } else {
240             status.setError(NewWizardMessages.NewPackageWizardPage_error_EnterName);
241             return status;
242         }
243
244         IPackageFragmentRoot root= getPackageFragmentRoot();
245         if (root != null && root.getJavaProject().exists()) {
246             IPackageFragment pack= root.getPackageFragment(packName);
247             try {
248                 IPath rootPath= root.getPath();
249                 IPath outputPath= root.getJavaProject().getOutputLocation();
250                 if (rootPath.isPrefixOf(outputPath) && !rootPath.equals(outputPath)) {
251                     // if the bin folder is inside of our root, don't allow to name a package
252
// like the bin folder
253
IPath packagePath= pack.getPath();
254                     if (outputPath.isPrefixOf(packagePath)) {
255                         status.setError(NewWizardMessages.NewPackageWizardPage_error_IsOutputFolder);
256                         return status;
257                     }
258                 }
259                 if (pack.exists()) {
260                     if (pack.containsJavaResources() || !pack.hasSubpackages()) {
261                         status.setError(NewWizardMessages.NewPackageWizardPage_error_PackageExists);
262                     } else {
263                         status.setError(NewWizardMessages.NewPackageWizardPage_error_PackageNotShown);
264                     }
265                 } else {
266                     URI JavaDoc location= pack.getResource().getLocationURI();
267                     if (location != null) {
268                         IFileStore store= EFS.getStore(location);
269                         if (store.fetchInfo().exists()) {
270                             status.setError(NewWizardMessages.NewPackageWizardPage_error_PackageExistsDifferentCase);
271                         }
272                     }
273                 }
274             } catch (CoreException e) {
275                 JavaPlugin.log(e);
276             }
277         }
278         return status;
279     }
280
281     /**
282      * Returns the content of the package input field.
283      *
284      * @return the content of the package input field
285      */

286     public String JavaDoc getPackageText() {
287         return fPackageDialogField.getText();
288     }
289
290     /**
291      * Sets the content of the package input field to the given value.
292      *
293      * @param str the new package input field text
294      * @param canBeModified if <code>true</code> the package input
295      * field can be modified; otherwise it is read-only.
296      */

297     public void setPackageText(String JavaDoc str, boolean canBeModified) {
298         fPackageDialogField.setText(str);
299         
300         fPackageDialogField.setEnabled(canBeModified);
301     }
302     
303     /**
304      * Returns the resource handle that corresponds to the element to was created or
305      * will be created.
306      * @return A resource or null if the page contains illegal values.
307      * @since 3.0
308      */

309     public IResource getModifiedResource() {
310         IPackageFragmentRoot root= getPackageFragmentRoot();
311         if (root != null) {
312             return root.getPackageFragment(getPackageText()).getResource();
313         }
314         return null;
315     }
316     
317         
318     // ---- creation ----------------
319

320     /**
321      * Returns a runnable that creates a package using the current settings.
322      *
323      * @return the runnable that creates the new package
324      */

325     public IRunnableWithProgress getRunnable() {
326         return new IRunnableWithProgress() {
327             public void run(IProgressMonitor monitor) throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
328                 try {
329                     createPackage(monitor);
330                 } catch (CoreException e) {
331                     throw new InvocationTargetException JavaDoc(e);
332                 }
333             }
334         };
335     }
336
337     /**
338      * Returns the created package fragment. This method only returns a valid value
339      * after <code>getRunnable</code> or <code>createPackage</code> have been
340      * executed.
341      *
342      * @return the created package fragment
343      */

344     public IPackageFragment getNewPackageFragment() {
345         return fCreatedPackageFragment;
346     }
347     
348     /**
349      * Creates the new package using the entered field values.
350      *
351      * @param monitor a progress monitor to report progress. The progress
352      * monitor must not be <code>null</code>
353      * @throws CoreException Thrown if creating the package failed.
354      * @throws InterruptedException Thrown when the operation has been canceled.
355      * @since 2.1
356      */

357     public void createPackage(IProgressMonitor monitor) throws CoreException, InterruptedException JavaDoc {
358         if (monitor == null) {
359             monitor= new NullProgressMonitor();
360         }
361
362         IPackageFragmentRoot root= getPackageFragmentRoot();
363         String JavaDoc packName= getPackageText();
364         fCreatedPackageFragment= root.createPackageFragment(packName, true, monitor);
365         
366         if (monitor.isCanceled()) {
367             throw new InterruptedException JavaDoc();
368         }
369     }
370 }
371
Popular Tags