KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > jarpackager > JarOptionsPage


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.jarpackager;
12
13 import org.eclipse.core.resources.IFile;
14 import org.eclipse.core.resources.IResource;
15 import org.eclipse.core.resources.IWorkspace;
16 import org.eclipse.core.resources.ResourcesPlugin;
17 import org.eclipse.core.runtime.IPath;
18 import org.eclipse.core.runtime.IStatus;
19 import org.eclipse.core.runtime.Path;
20
21 import org.eclipse.swt.SWT;
22 import org.eclipse.swt.events.SelectionAdapter;
23 import org.eclipse.swt.events.SelectionEvent;
24 import org.eclipse.swt.layout.GridData;
25 import org.eclipse.swt.layout.GridLayout;
26 import org.eclipse.swt.widgets.Button;
27 import org.eclipse.swt.widgets.Composite;
28 import org.eclipse.swt.widgets.Event;
29 import org.eclipse.swt.widgets.Label;
30 import org.eclipse.swt.widgets.Listener;
31 import org.eclipse.swt.widgets.Text;
32
33 import org.eclipse.jface.dialogs.Dialog;
34 import org.eclipse.jface.dialogs.IDialogSettings;
35 import org.eclipse.jface.resource.JFaceResources;
36 import org.eclipse.jface.window.Window;
37 import org.eclipse.jface.wizard.IWizardPage;
38 import org.eclipse.jface.wizard.WizardPage;
39
40 import org.eclipse.ui.dialogs.SaveAsDialog;
41 import org.eclipse.ui.PlatformUI;
42
43 import org.eclipse.jdt.internal.corext.util.Messages;
44
45 import org.eclipse.jdt.ui.jarpackager.JarPackageData;
46
47 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
48 import org.eclipse.jdt.internal.ui.JavaPlugin;
49
50 import org.eclipse.jdt.internal.ui.util.SWTUtil;
51
52 /**
53  * Page 2 of the JAR Package wizard
54  */

55 class JarOptionsPage extends WizardPage implements IJarPackageWizardPage {
56
57     // Untyped listener
58
private class UntypedListener implements Listener {
59         /*
60          * Implements method from Listener
61          */

62         public void handleEvent(Event e) {
63             if (getControl() == null)
64                 return;
65             update();
66         }
67     }
68
69     private JarPackageData fJarPackage;
70
71     // widgets
72
private Button fExportErrorsCheckbox;
73     private Button fExportWarningsCheckbox;
74     private Button fUseSourceFoldersCheckbox;
75     private Composite fDescriptionFileGroup;
76     private Button fSaveDescriptionCheckbox;
77     private Label fDescriptionFileLabel;
78     private Text fDescriptionFileText;
79     private Button fDescriptionFileBrowseButton;
80     private Button fBuildIfNeededCheckbox;
81
82     // dialog store id constants
83
private final static String JavaDoc PAGE_NAME= "jarOptionsWizardPage"; //$NON-NLS-1$
84

85     private final static String JavaDoc STORE_EXPORT_WARNINGS= PAGE_NAME + ".EXPORT_WARNINGS"; //$NON-NLS-1$
86
private final static String JavaDoc STORE_EXPORT_ERRORS= PAGE_NAME + ".EXPORT_ERRORS"; //$NON-NLS-1$
87
private final static String JavaDoc STORE_SAVE_DESCRIPTION= PAGE_NAME + ".SAVE_DESCRIPTION"; //$NON-NLS-1$
88
private final static String JavaDoc STORE_DESCRIPTION_LOCATION= PAGE_NAME + ".DESCRIPTION_LOCATION"; //$NON-NLS-1$
89
private final static String JavaDoc STORE_USE_SRC_FOLDERS= PAGE_NAME + ".STORE_USE_SRC_FOLDERS"; //$NON-NLS-1$
90
private final static String JavaDoc STORE_BUILD_IF_NEEDED= PAGE_NAME + ".BUILD_IF_NEEDED"; //$NON-NLS-1$
91

92     /**
93      * Create an instance of this class
94      */

95     public JarOptionsPage(JarPackageData jarPackage) {
96         super(PAGE_NAME);
97         setTitle(JarPackagerMessages.JarOptionsPage_title);
98         setDescription(JarPackagerMessages.JarOptionsPage_description);
99         fJarPackage= jarPackage;
100     }
101
102     /*
103      * Method declared on IDialogPage.
104      */

105     public void createControl(Composite parent) {
106         Composite composite= new Composite(parent, SWT.NULL);
107         composite.setLayout(new GridLayout());
108         composite.setLayoutData(
109             new GridData(GridData.VERTICAL_ALIGN_FILL | GridData.HORIZONTAL_ALIGN_FILL));
110
111         createOptionsGroup(composite);
112
113         restoreWidgetValues();
114         setControl(composite);
115         update();
116
117         Dialog.applyDialogFont(composite);
118         PlatformUI.getWorkbench().getHelpSystem().setHelp(composite, IJavaHelpContextIds.JAROPTIONS_WIZARD_PAGE);
119     }
120
121     /**
122      * Create the export options specification widgets.
123      *
124      * @param parent org.eclipse.swt.widgets.Composite
125      */

126     protected void createOptionsGroup(Composite parent) {
127         
128         initializeDialogUnits(parent);
129         
130         Composite optionsGroup= new Composite(parent, SWT.NONE);
131         GridLayout layout= new GridLayout();
132         layout.marginHeight= 0;
133         optionsGroup.setLayout(layout);
134
135         createLabel(optionsGroup, JarPackagerMessages.JarOptionsPage_howTreatProblems_label, false);
136
137         UntypedListener selectionListener= new UntypedListener();
138
139         fExportErrorsCheckbox= new Button(optionsGroup, SWT.CHECK | SWT.LEFT);
140         fExportErrorsCheckbox.setText(JarPackagerMessages.JarOptionsPage_exportErrors_text);
141         fExportErrorsCheckbox.addListener(SWT.Selection, selectionListener);
142
143         fExportWarningsCheckbox= new Button(optionsGroup, SWT.CHECK | SWT.LEFT);
144         fExportWarningsCheckbox.setText(JarPackagerMessages.JarOptionsPage_exportWarnings_text);
145         fExportWarningsCheckbox.addListener(SWT.Selection, selectionListener);
146
147         createSpacer(optionsGroup);
148
149         fUseSourceFoldersCheckbox= new Button(optionsGroup, SWT.CHECK | SWT.LEFT);
150         fUseSourceFoldersCheckbox.setText(JarPackagerMessages.JarOptionsPage_useSourceFoldersHierarchy);
151         fUseSourceFoldersCheckbox.addListener(SWT.Selection, selectionListener);
152         fUseSourceFoldersCheckbox.setEnabled(fJarPackage.areJavaFilesExported() && !fJarPackage.areGeneratedFilesExported());
153
154         createSpacer(optionsGroup);
155
156         fBuildIfNeededCheckbox= new Button(optionsGroup, SWT.CHECK | SWT.LEFT);
157         fBuildIfNeededCheckbox.setText(JarPackagerMessages.JarOptionsPage_buildIfNeeded);
158         fBuildIfNeededCheckbox.addListener(SWT.Selection, selectionListener);
159
160         createSpacer(optionsGroup);
161         
162         fSaveDescriptionCheckbox= new Button(optionsGroup, SWT.CHECK | SWT.LEFT);
163         fSaveDescriptionCheckbox.setText(JarPackagerMessages.JarOptionsPage_saveDescription_text);
164         fSaveDescriptionCheckbox.addListener(SWT.Selection, selectionListener);
165         createDescriptionFileGroup(parent);
166     }
167
168     /**
169      * Persists resource specification control setting that are to be restored
170      * in the next instance of this page. Subclasses wishing to persist
171      * settings for their controls should extend the hook method
172      * <code>internalSaveWidgetValues</code>.
173      */

174     public final void saveWidgetValues() {
175         // update directory names history
176
IDialogSettings settings= getDialogSettings();
177         if (settings != null) {
178             settings.put(STORE_EXPORT_WARNINGS, fJarPackage.exportWarnings());
179             settings.put(STORE_EXPORT_ERRORS, fJarPackage.areErrorsExported());
180             settings.put(STORE_USE_SRC_FOLDERS, fJarPackage.useSourceFolderHierarchy());
181             settings.put(STORE_BUILD_IF_NEEDED, fJarPackage.isBuildingIfNeeded());
182             settings.put(STORE_SAVE_DESCRIPTION, fJarPackage.isDescriptionSaved());
183             settings.put(STORE_DESCRIPTION_LOCATION, fJarPackage.getDescriptionLocation().toString());
184         }
185         // Allow subclasses to save values
186
internalSaveWidgetValues();
187     }
188
189     /**
190      * Hook method for subclasses to persist their settings.
191      */

192     protected void internalSaveWidgetValues() {
193     }
194
195     /**
196      * Hook method for restoring widget values to the values that they held
197      * last time this wizard was used to completion.
198      */

199     protected void restoreWidgetValues() {
200         if (!((JarPackageWizard)getWizard()).isInitializingFromJarPackage())
201             initializeJarPackage();
202
203         fExportWarningsCheckbox.setSelection(fJarPackage.exportWarnings());
204         fExportErrorsCheckbox.setSelection(fJarPackage.areErrorsExported());
205         fBuildIfNeededCheckbox.setSelection(fJarPackage.isBuildingIfNeeded());
206         fUseSourceFoldersCheckbox.setSelection(fJarPackage.useSourceFolderHierarchy());
207         fSaveDescriptionCheckbox.setSelection(fJarPackage.isDescriptionSaved());
208         fDescriptionFileText.setText(fJarPackage.getDescriptionLocation().toString());
209     }
210
211     /**
212      * Initializes the JAR package from last used wizard page values.
213      */

214     protected void initializeJarPackage() {
215         IDialogSettings settings= getDialogSettings();
216         if (settings != null) {
217             fJarPackage.setExportWarnings(settings.getBoolean(STORE_EXPORT_WARNINGS));
218             fJarPackage.setExportErrors(settings.getBoolean(STORE_EXPORT_ERRORS));
219             fJarPackage.setUseSourceFolderHierarchy(settings.getBoolean(STORE_USE_SRC_FOLDERS));
220             fJarPackage.setSaveDescription(false); // bug 15877
221
String JavaDoc pathStr= settings.get(STORE_DESCRIPTION_LOCATION);
222             if (pathStr == null)
223                 pathStr= ""; //$NON-NLS-1$
224
fJarPackage.setDescriptionLocation(new Path(pathStr));
225             if (settings.get(STORE_BUILD_IF_NEEDED) != null)
226                 fJarPackage.setBuildIfNeeded(settings.getBoolean(STORE_BUILD_IF_NEEDED));
227         }
228     }
229
230     private void update() {
231         updateModel();
232         updateWidgetEnablements();
233         updatePageCompletion();
234     }
235
236     /**
237      * Stores the widget values in the JAR package.
238      */

239     protected void updateModel() {
240         if (getControl() == null)
241             return;
242         fJarPackage.setExportWarnings(fExportWarningsCheckbox.getSelection());
243         fJarPackage.setExportErrors(fExportErrorsCheckbox.getSelection());
244         fJarPackage.setBuildIfNeeded(fBuildIfNeededCheckbox.getSelection());
245         fJarPackage.setSaveDescription(fSaveDescriptionCheckbox.getSelection());
246         fJarPackage.setDescriptionLocation(new Path(fDescriptionFileText.getText()));
247         fJarPackage.setUseSourceFolderHierarchy(fUseSourceFoldersCheckbox.getSelection());
248     }
249
250     /**
251      * Open an appropriate destination browser so that the user can specify a source
252      * to import from
253      */

254     protected void handleDescriptionFileBrowseButtonPressed() {
255         SaveAsDialog dialog= new SaveAsDialog(getContainer().getShell());
256         dialog.create();
257         dialog.getShell().setText(JarPackagerMessages.JarOptionsPage_saveAsDialog_title);
258         dialog.setMessage(JarPackagerMessages.JarOptionsPage_saveAsDialog_message);
259         dialog.setOriginalFile(createFileHandle(fJarPackage.getDescriptionLocation()));
260         if (dialog.open() == Window.OK) {
261             IPath path= dialog.getResult();
262             path= path.removeFileExtension().addFileExtension(JarPackagerUtil.DESCRIPTION_EXTENSION);
263             fDescriptionFileText.setText(path.toString());
264         }
265     }
266
267     /**
268      * Updates the enablements of this page's controls. Subclasses may extend.
269      */

270     protected void updateWidgetEnablements() {
271         boolean saveDescription= fSaveDescriptionCheckbox.getSelection();
272         fDescriptionFileGroup.setEnabled(saveDescription);
273         fDescriptionFileBrowseButton.setEnabled(saveDescription);
274         fDescriptionFileText.setEnabled(saveDescription);
275         fDescriptionFileLabel.setEnabled(saveDescription);
276         
277         boolean exportClassFiles= fJarPackage.areClassFilesExported() && !fJarPackage.areOutputFoldersExported();
278         fExportWarningsCheckbox.setEnabled(exportClassFiles);
279         fExportErrorsCheckbox.setEnabled(exportClassFiles);
280         
281         boolean isAutobuilding= ResourcesPlugin.getWorkspace().isAutoBuilding();
282         fBuildIfNeededCheckbox.setEnabled(exportClassFiles && !isAutobuilding);
283         
284         fUseSourceFoldersCheckbox.setEnabled(fJarPackage.areJavaFilesExported() && !fJarPackage.areGeneratedFilesExported());
285     }
286
287     /*
288      * Implements method from IJarPackageWizardPage
289      */

290     public boolean isPageComplete() {
291         if (fJarPackage.isDescriptionSaved()){
292             if (fJarPackage.getDescriptionLocation().toString().length() == 0) {
293                 setErrorMessage(null);
294                 return false;
295             }
296             IPath location= fJarPackage.getDescriptionLocation();
297             if (!location.toString().startsWith("/")) { //$NON-NLS-1$
298
setErrorMessage(JarPackagerMessages.JarOptionsPage_error_descriptionMustBeAbsolute);
299                 return false;
300             }
301             IResource resource= findResource(location);
302             if (resource != null && resource.getType() != IResource.FILE) {
303                 setErrorMessage(JarPackagerMessages.JarOptionsPage_error_descriptionMustNotBeExistingContainer);
304                 return false;
305             }
306             resource= findResource(location.removeLastSegments(1));
307             if (resource == null || resource.getType() == IResource.FILE) {
308                 setErrorMessage(JarPackagerMessages.JarOptionsPage_error_descriptionContainerDoesNotExist);
309                 return false;
310             }
311             String JavaDoc fileExtension= fJarPackage.getDescriptionLocation().getFileExtension();
312             if (fileExtension == null || !fileExtension.equals(JarPackagerUtil.DESCRIPTION_EXTENSION)) {
313                 setErrorMessage(Messages.format(JarPackagerMessages.JarOptionsPage_error_invalidDescriptionExtension, JarPackagerUtil.DESCRIPTION_EXTENSION));
314                 return false;
315             }
316         }
317         setErrorMessage(null);
318         return true;
319     }
320     
321     public boolean canFlipToNextPage() {
322         return fJarPackage.areGeneratedFilesExported() && super.canFlipToNextPage();
323     }
324     
325     /*
326      * Overrides method from WizardDataTransferPage
327      */

328     protected void createDescriptionFileGroup(Composite parent) {
329         // destination specification group
330
fDescriptionFileGroup= new Composite(parent, SWT.NONE);
331         GridLayout layout= new GridLayout();
332         layout.numColumns= 3;
333         fDescriptionFileGroup.setLayout(layout);
334         fDescriptionFileGroup.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL));
335
336         fDescriptionFileLabel= new Label(fDescriptionFileGroup, SWT.NONE);
337         fDescriptionFileLabel.setText(JarPackagerMessages.JarOptionsPage_descriptionFile_label);
338
339         // destination name entry field
340
fDescriptionFileText= new Text(fDescriptionFileGroup, SWT.SINGLE | SWT.BORDER);
341         fDescriptionFileText.addListener(SWT.Modify, new UntypedListener());
342         GridData data= new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL);
343         data.widthHint= convertWidthInCharsToPixels(40);
344         fDescriptionFileText.setLayoutData(data);
345
346         // destination browse button
347
fDescriptionFileBrowseButton= new Button(fDescriptionFileGroup, SWT.PUSH);
348         fDescriptionFileBrowseButton.setText(JarPackagerMessages.JarOptionsPage_browseButton_text);
349         fDescriptionFileBrowseButton.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
350         SWTUtil.setButtonDimensionHint(fDescriptionFileBrowseButton);
351         fDescriptionFileBrowseButton.addSelectionListener(new SelectionAdapter() {
352             public void widgetSelected(SelectionEvent e) {
353                 handleDescriptionFileBrowseButtonPressed();
354             }
355         });
356     }
357
358     /**
359      * Creates a file resource handle for the file with the given workspace path.
360      * This method does not create the file resource; this is the responsibility
361      * of <code>createFile</code>.
362      *
363      * @param filePath the path of the file resource to create a handle for
364      * @return the new file resource handle
365      */

366     protected IFile createFileHandle(IPath filePath) {
367         if (filePath.isValidPath(filePath.toString()) && filePath.segmentCount() >= 2)
368             return JavaPlugin.getWorkspace().getRoot().getFile(filePath);
369         else
370             return null;
371     }
372     /*
373      * Method declared on IWizardPage.
374      */

375     public void setPreviousPage(IWizardPage page) {
376         super.setPreviousPage(page);
377         updateWidgetEnablements();
378         if (getControl() != null)
379             updatePageCompletion();
380     }
381
382     /*
383      * Implements method from IJarPackageWizardPage.
384      */

385     public void finish() {
386         saveWidgetValues();
387     }
388
389     /**
390      * Creates a new label with a bold font.
391      *
392      * @param parent the parent control
393      * @param text the label text
394      * @return the new label control
395      */

396     protected Label createLabel(Composite parent, String JavaDoc text, boolean bold) {
397         Label label= new Label(parent, SWT.NONE);
398         if (bold)
399             label.setFont(JFaceResources.getBannerFont());
400         label.setText(text);
401         GridData data= new GridData();
402         data.verticalAlignment= GridData.FILL;
403         data.horizontalAlignment= GridData.FILL;
404         label.setLayoutData(data);
405         return label;
406     }
407
408     /**
409      * Creates a horizontal spacer line that fills the width of its container.
410      *
411      * @param parent the parent control
412      */

413     protected void createSpacer(Composite parent) {
414         Label spacer= new Label(parent, SWT.NONE);
415         GridData data= new GridData();
416         data.horizontalAlignment= GridData.FILL;
417         data.verticalAlignment= GridData.BEGINNING;
418         spacer.setLayoutData(data);
419     }
420
421     /**
422      * Determine if the page is complete and update the page appropriately.
423      */

424     protected void updatePageCompletion() {
425         boolean pageComplete= isPageComplete();
426         setPageComplete(pageComplete);
427         if (pageComplete) {
428             setErrorMessage(null);
429         }
430     }
431
432     /**
433      * Returns the resource for the specified path.
434      *
435      * @param path the path for which the resource should be returned
436      * @return the resource specified by the path or <code>null</code>
437      */

438     protected IResource findResource(IPath path) {
439         IWorkspace workspace= JavaPlugin.getWorkspace();
440         IStatus result= workspace.validatePath(
441                             path.toString(),
442                             IResource.ROOT | IResource.PROJECT | IResource.FOLDER | IResource.FILE);
443         if (result.isOK() && workspace.getRoot().exists(path))
444             return workspace.getRoot().findMember(path);
445         return null;
446     }
447 }
448
Popular Tags