KickJava   Java API By Example, From Geeks To Geeks.

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


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.jarpackager;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.Arrays JavaDoc;
16 import java.util.HashSet JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.List JavaDoc;
19 import java.util.Set JavaDoc;
20 import java.util.jar.Attributes JavaDoc;
21 import java.util.jar.Manifest JavaDoc;
22
23 import org.eclipse.core.runtime.CoreException;
24 import org.eclipse.core.runtime.IPath;
25 import org.eclipse.core.runtime.IStatus;
26 import org.eclipse.core.runtime.Path;
27
28 import org.eclipse.core.resources.IFile;
29 import org.eclipse.core.resources.IResource;
30 import org.eclipse.core.resources.IWorkspace;
31
32 import org.eclipse.swt.SWT;
33 import org.eclipse.swt.events.KeyAdapter;
34 import org.eclipse.swt.events.KeyEvent;
35 import org.eclipse.swt.events.SelectionAdapter;
36 import org.eclipse.swt.events.SelectionEvent;
37 import org.eclipse.swt.layout.GridData;
38 import org.eclipse.swt.layout.GridLayout;
39 import org.eclipse.swt.widgets.Button;
40 import org.eclipse.swt.widgets.Composite;
41 import org.eclipse.swt.widgets.Control;
42 import org.eclipse.swt.widgets.Event;
43 import org.eclipse.swt.widgets.Label;
44 import org.eclipse.swt.widgets.Listener;
45 import org.eclipse.swt.widgets.Text;
46
47 import org.eclipse.jface.dialogs.Dialog;
48 import org.eclipse.jface.dialogs.IDialogSettings;
49 import org.eclipse.jface.dialogs.IMessageProvider;
50 import org.eclipse.jface.resource.JFaceResources;
51 import org.eclipse.jface.viewers.DecoratingLabelProvider;
52 import org.eclipse.jface.window.Window;
53 import org.eclipse.jface.wizard.IWizardPage;
54 import org.eclipse.jface.wizard.WizardPage;
55
56 import org.eclipse.ui.PlatformUI;
57 import org.eclipse.ui.dialogs.ElementTreeSelectionDialog;
58 import org.eclipse.ui.dialogs.ISelectionStatusValidator;
59 import org.eclipse.ui.dialogs.SaveAsDialog;
60 import org.eclipse.ui.dialogs.SelectionDialog;
61
62 import org.eclipse.jdt.core.ICompilationUnit;
63 import org.eclipse.jdt.core.IPackageFragment;
64 import org.eclipse.jdt.core.IPackageFragmentRoot;
65 import org.eclipse.jdt.core.IType;
66 import org.eclipse.jdt.core.JavaCore;
67 import org.eclipse.jdt.core.JavaModelException;
68 import org.eclipse.jdt.core.search.IJavaSearchScope;
69
70 import org.eclipse.jdt.internal.corext.util.Messages;
71
72 import org.eclipse.jdt.ui.JavaElementComparator;
73 import org.eclipse.jdt.ui.JavaElementLabelProvider;
74 import org.eclipse.jdt.ui.JavaUI;
75 import org.eclipse.jdt.ui.ProblemsLabelDecorator;
76 import org.eclipse.jdt.ui.StandardJavaElementContentProvider;
77 import org.eclipse.jdt.ui.jarpackager.JarPackageData;
78
79 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
80 import org.eclipse.jdt.internal.ui.JavaPlugin;
81 import org.eclipse.jdt.internal.ui.dialogs.StatusInfo;
82 import org.eclipse.jdt.internal.ui.filters.EmptyInnerPackageFilter;
83 import org.eclipse.jdt.internal.ui.search.JavaSearchScopeFactory;
84 import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
85 import org.eclipse.jdt.internal.ui.util.MainMethodSearchEngine;
86 import org.eclipse.jdt.internal.ui.util.SWTUtil;
87 import org.eclipse.jdt.internal.ui.viewsupport.LibraryFilter;
88
89 /**
90  * Page 3 of the JAR Package wizard
91  */

92 class JarManifestWizardPage extends WizardPage implements IJarPackageWizardPage {
93
94     // Untyped listener
95
private class UntypedListener implements Listener {
96         /*
97          * Implements method from Listener
98          */

99         public void handleEvent(Event e) {
100             if (getControl() == null)
101                 return;
102             update();
103         }
104     }
105     private UntypedListener fUntypedListener= new UntypedListener();
106
107     // Model
108
private JarPackageData fJarPackage;
109     
110     // Cache for main types
111
private IType[] fMainTypes;
112     
113     // Widgets
114
private Composite fManifestGroup;
115     private Button fGenerateManifestRadioButton;
116     private Button fSaveManifestCheckbox;
117     private Button fReuseManifestCheckbox;
118     private Text fNewManifestFileText;
119     private Label fNewManifestFileLabel;
120     private Button fNewManifestFileBrowseButton;
121     private Button fUseManifestRadioButton;
122     private Text fManifestFileText;
123     private Label fManifestFileLabel;
124     private Button fManifestFileBrowseButton;
125     
126     private Label fSealingHeaderLabel;
127     private Button fSealJarRadioButton;
128     private Label fSealJarLabel;
129     private Button fSealedPackagesDetailsButton;
130     private Button fSealPackagesRadioButton;
131     private Label fSealPackagesLabel;
132     private Button fUnSealedPackagesDetailsButton;
133     
134     private Label fMainClassHeaderLabel;
135     private Label fMainClassLabel;
136     private Text fMainClassText;
137     private Button fMainClassBrowseButton;
138     
139     // Dialog store id constants
140
private final static String JavaDoc PAGE_NAME= "JarManifestWizardPage"; //$NON-NLS-1$
141

142     // Manifest creation
143
private final static String JavaDoc STORE_GENERATE_MANIFEST= PAGE_NAME + ".GENERATE_MANIFEST"; //$NON-NLS-1$
144
private final static String JavaDoc STORE_SAVE_MANIFEST= PAGE_NAME + ".SAVE_MANIFEST"; //$NON-NLS-1$
145
private final static String JavaDoc STORE_REUSE_MANIFEST= PAGE_NAME + ".REUSE_MANIFEST"; //$NON-NLS-1$
146
private final static String JavaDoc STORE_MANIFEST_LOCATION= PAGE_NAME + ".MANIFEST_LOCATION"; //$NON-NLS-1$
147

148     // Sealing
149
private final static String JavaDoc STORE_SEAL_JAR= PAGE_NAME + ".SEAL_JAR"; //$NON-NLS-1$
150

151     /**
152      * Create an instance of this class
153      */

154     public JarManifestWizardPage(JarPackageData jarPackage) {
155         super(PAGE_NAME);
156         setTitle(JarPackagerMessages.JarManifestWizardPage_title);
157         setDescription(JarPackagerMessages.JarManifestWizardPage_description);
158         fJarPackage= jarPackage;
159     }
160
161     // ----------- Widget creation -----------
162

163     /*
164      * Method declared on IDialogPage.
165      */

166     public void createControl(Composite parent) {
167         
168         initializeDialogUnits(parent);
169         
170         Composite composite= new Composite(parent, SWT.NULL);
171         composite.setLayout(new GridLayout());
172         composite.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_FILL | GridData.HORIZONTAL_ALIGN_FILL));
173
174         createLabel(composite, JarPackagerMessages.JarManifestWizardPage_manifestSource_label, false);
175         createManifestGroup(composite);
176
177         createSpacer(composite);
178
179         fSealingHeaderLabel= createLabel(composite, JarPackagerMessages.JarManifestWizardPage_sealingHeader_label, false);
180         createSealingGroup(composite);
181
182         createSpacer(composite);
183
184         fMainClassHeaderLabel= createLabel(composite, JarPackagerMessages.JarManifestWizardPage_mainClassHeader_label, false);
185         createMainClassGroup(composite);
186
187         setEqualButtonSizes();
188         
189         restoreWidgetValues();
190
191         setControl(composite);
192         update();
193
194         Dialog.applyDialogFont(composite);
195         PlatformUI.getWorkbench().getHelpSystem().setHelp(composite, IJavaHelpContextIds.JARMANIFEST_WIZARD_PAGE);
196             
197     }
198     /**
199      * Create the export options specification widgets.
200      *
201      * @param parent org.eclipse.swt.widgets.Composite
202      */

203     protected void createManifestGroup(Composite parent) {
204         fManifestGroup= new Composite(parent, SWT.NONE);
205         GridLayout layout= new GridLayout();
206         fManifestGroup.setLayout(layout);
207         fManifestGroup.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL));
208
209         fGenerateManifestRadioButton= new Button(fManifestGroup, SWT.RADIO | SWT.LEFT);
210         fGenerateManifestRadioButton.setText(JarPackagerMessages.JarManifestWizardPage_genetateManifest_text);
211         fGenerateManifestRadioButton.addListener(SWT.Selection, fUntypedListener);
212
213             Composite saveOptions= new Composite(fManifestGroup, SWT.NONE);
214             GridLayout saveOptionsLayout= new GridLayout();
215             saveOptionsLayout.marginWidth= 0;
216             saveOptions.setLayout(saveOptionsLayout);
217
218             GridData data= new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL);
219             data.horizontalIndent=20;
220             saveOptions.setLayoutData(data);
221     
222             fSaveManifestCheckbox= new Button(saveOptions, SWT.CHECK | SWT.LEFT);
223             fSaveManifestCheckbox.setText(JarPackagerMessages.JarManifestWizardPage_saveManifest_text);
224             fSaveManifestCheckbox.addListener(SWT.MouseUp, fUntypedListener);
225     
226             fReuseManifestCheckbox= new Button(saveOptions, SWT.CHECK | SWT.LEFT);
227             fReuseManifestCheckbox.setText(JarPackagerMessages.JarManifestWizardPage_reuseManifest_text);
228             fReuseManifestCheckbox.addListener(SWT.MouseUp, fUntypedListener);
229             
230             createNewManifestFileGroup(saveOptions);
231
232         fUseManifestRadioButton= new Button(fManifestGroup, SWT.RADIO | SWT.LEFT);
233         fUseManifestRadioButton.setText(JarPackagerMessages.JarManifestWizardPage_useManifest_text);
234         
235         fUseManifestRadioButton.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
236             Composite existingManifestGroup= new Composite(fManifestGroup, SWT.NONE);
237             GridLayout existingManifestLayout= new GridLayout();
238             existingManifestLayout.marginWidth= 0;
239             existingManifestGroup.setLayout(existingManifestLayout);
240             data= new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL);
241             data.horizontalIndent=20;
242             existingManifestGroup.setLayoutData(data);
243             createManifestFileGroup(existingManifestGroup);
244     }
245
246     protected void createNewManifestFileGroup(Composite parent) {
247         // destination specification group
248
Composite manifestFileGroup= new Composite(parent, SWT.NONE);
249         GridLayout layout= new GridLayout();
250         layout.marginWidth= 0;
251         layout.numColumns= 3;
252         manifestFileGroup.setLayout(layout);
253         manifestFileGroup.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL));
254
255         fNewManifestFileLabel= new Label(manifestFileGroup, SWT.NONE);
256         fNewManifestFileLabel.setText(JarPackagerMessages.JarManifestWizardPage_newManifestFile_text);
257
258         // entry field
259
fNewManifestFileText= new Text(manifestFileGroup, SWT.SINGLE | SWT.BORDER);
260         fNewManifestFileText.addListener(SWT.Modify, fUntypedListener);
261         
262         GridData data= new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL);
263         data.widthHint= convertWidthInCharsToPixels(40);
264         fNewManifestFileText.setLayoutData(data);
265
266         // browse button
267
fNewManifestFileBrowseButton= new Button(manifestFileGroup, SWT.PUSH);
268         fNewManifestFileBrowseButton.setText(JarPackagerMessages.JarManifestWizardPage_newManifestFileBrowseButton_text);
269         fNewManifestFileBrowseButton.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
270         fNewManifestFileBrowseButton.addSelectionListener(new SelectionAdapter() {
271             public void widgetSelected(SelectionEvent e) {
272                 handleNewManifestFileBrowseButtonPressed();
273             }
274         });
275     }
276
277     protected void createManifestFileGroup(Composite parent) {
278         // destination specification group
279
Composite manifestFileGroup= new Composite(parent, SWT.NONE);
280         GridLayout layout= new GridLayout();
281         layout.numColumns= 3;
282         layout.marginWidth= 0;
283         
284         manifestFileGroup.setLayout(layout);
285         manifestFileGroup.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL));
286
287         fManifestFileLabel= new Label(manifestFileGroup, SWT.NONE);
288         fManifestFileLabel.setText(JarPackagerMessages.JarManifestWizardPage_manifestFile_text);
289
290         // entry field
291
fManifestFileText= new Text(manifestFileGroup, SWT.SINGLE | SWT.BORDER);
292         fManifestFileText.addListener(SWT.Modify, fUntypedListener);
293         GridData data= new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL);
294         data.widthHint= convertWidthInCharsToPixels(40);
295         fManifestFileText.setLayoutData(data);
296
297         // browse button
298
fManifestFileBrowseButton= new Button(manifestFileGroup, SWT.PUSH);
299         fManifestFileBrowseButton.setText(JarPackagerMessages.JarManifestWizardPage_manifestFileBrowse_text);
300         fManifestFileBrowseButton.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
301         fManifestFileBrowseButton.addSelectionListener(new SelectionAdapter() {
302             public void widgetSelected(SelectionEvent e) {
303                 handleManifestFileBrowseButtonPressed();
304             }
305         });
306     }
307     /**
308      * Creates the JAR sealing specification controls.
309      *
310      * @param parent the parent control
311      */

312     protected void createSealingGroup(Composite parent) {
313         // destination specification group
314
Composite sealingGroup= new Composite(parent, SWT.NONE);
315         GridLayout layout= new GridLayout();
316         layout.numColumns= 3;
317         layout.horizontalSpacing += 3;
318         sealingGroup.setLayout(layout);
319         sealingGroup.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL));
320         createSealJarGroup(sealingGroup);
321         createSealPackagesGroup(sealingGroup);
322         
323     }
324     /**
325      * Creates the JAR sealing specification controls to seal the whole JAR.
326      *
327      * @param sealGroup the parent control
328      */

329     protected void createSealJarGroup(Composite sealGroup) {
330         fSealJarRadioButton= new Button(sealGroup, SWT.RADIO);
331         fSealJarRadioButton.setText(JarPackagerMessages.JarManifestWizardPage_sealJar_text);
332         fSealJarRadioButton.addListener(SWT.Selection, fUntypedListener);
333
334         fSealJarLabel= new Label(sealGroup, SWT.RIGHT);
335         fSealJarLabel.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL));
336         fSealJarLabel.setText(""); //$NON-NLS-1$
337

338         fUnSealedPackagesDetailsButton= new Button(sealGroup, SWT.PUSH);
339         fUnSealedPackagesDetailsButton.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
340         fUnSealedPackagesDetailsButton.setText(JarPackagerMessages.JarManifestWizardPage_unsealPackagesButton_text);
341         fUnSealedPackagesDetailsButton.addSelectionListener(new SelectionAdapter() {
342             public void widgetSelected(SelectionEvent e) {
343                 handleUnSealPackagesDetailsButtonPressed();
344             }
345         });
346         
347     }
348     /**
349      * Creates the JAR sealing specification controls to seal packages.
350      *
351      * @param sealGroup the parent control
352      */

353     protected void createSealPackagesGroup(Composite sealGroup) {
354         fSealPackagesRadioButton= new Button(sealGroup, SWT.RADIO);
355         fSealPackagesRadioButton.setText(JarPackagerMessages.JarManifestWizardPage_sealPackagesButton_text);
356
357         fSealPackagesLabel= new Label(sealGroup, SWT.RIGHT);
358         fSealPackagesLabel.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL));
359         fSealPackagesLabel.setText(""); //$NON-NLS-1$
360

361         fSealedPackagesDetailsButton= new Button(sealGroup, SWT.PUSH);
362         fSealedPackagesDetailsButton.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
363         fSealedPackagesDetailsButton.setText(JarPackagerMessages.JarManifestWizardPage_sealedPackagesDetailsButton_text);
364         fSealedPackagesDetailsButton.addSelectionListener(new SelectionAdapter() {
365             public void widgetSelected(SelectionEvent e) {
366                 handleSealPackagesDetailsButtonPressed();
367             }
368         });
369     }
370
371     protected void createMainClassGroup(Composite parent) {
372         // main type group
373
Composite mainClassGroup= new Composite(parent, SWT.NONE);
374         GridLayout layout= new GridLayout();
375         layout.numColumns= 3;
376         mainClassGroup.setLayout(layout);
377         mainClassGroup.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL));
378
379         fMainClassLabel= new Label(mainClassGroup, SWT.NONE);
380         fMainClassLabel.setText(JarPackagerMessages.JarManifestWizardPage_mainClass_label);
381
382         // entry field
383
fMainClassText= new Text(mainClassGroup, SWT.SINGLE | SWT.BORDER);
384         fMainClassText.addListener(SWT.Modify, fUntypedListener);
385         GridData data= new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL);
386         data.widthHint= convertWidthInCharsToPixels(40);
387         fMainClassText.setLayoutData(data);
388         fMainClassText.addKeyListener(new KeyAdapter() {
389             /*
390              * @see KeyListener#keyReleased(KeyEvent)
391              */

392             public void keyReleased(KeyEvent e) {
393                 fJarPackage.setManifestMainClass(findMainMethodByName(fMainClassText.getText()));
394                 update();
395             }
396         });
397
398         // browse button
399
fMainClassBrowseButton= new Button(mainClassGroup, SWT.PUSH);
400         fMainClassBrowseButton.setText(JarPackagerMessages.JarManifestWizardPage_mainClassBrowseButton_text);
401         fMainClassBrowseButton.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
402         fMainClassBrowseButton.addSelectionListener(new SelectionAdapter() {
403             public void widgetSelected(SelectionEvent e) {
404                 handleMainClassBrowseButtonPressed();
405             }
406         });
407     }
408     
409     // ----------- Event handlers -----------
410

411     private void update() {
412         updateModel();
413         updateEnableState();
414         updatePageCompletion();
415     }
416     
417     /**
418      * Open an appropriate dialog so that the user can specify a manifest
419      * to save
420      */

421     protected void handleNewManifestFileBrowseButtonPressed() {
422         // Use Save As dialog to select a new file inside the workspace
423
SaveAsDialog dialog= new SaveAsDialog(getContainer().getShell());
424         dialog.create();
425         dialog.getShell().setText(JarPackagerMessages.JarManifestWizardPage_saveAsDialog_title);
426         dialog.setMessage(JarPackagerMessages.JarManifestWizardPage_saveAsDialog_message);
427         dialog.setOriginalFile(createFileHandle(fJarPackage.getManifestLocation()));
428         if (dialog.open() == Window.OK) {
429             fJarPackage.setManifestLocation(dialog.getResult());
430             fNewManifestFileText.setText(dialog.getResult().toString());
431         }
432     }
433
434     protected void handleManifestFileBrowseButtonPressed() {
435         ElementTreeSelectionDialog dialog= createWorkspaceFileSelectionDialog(JarPackagerMessages.JarManifestWizardPage_manifestSelectionDialog_title, JarPackagerMessages.JarManifestWizardPage_manifestSelectionDialog_message);
436         if (fJarPackage.isManifestAccessible())
437             dialog.setInitialSelections(new IResource[] {fJarPackage.getManifestFile()});
438         if (dialog.open() == Window.OK) {
439             Object JavaDoc[] resources= dialog.getResult();
440             if (resources.length != 1)
441                 setErrorMessage(JarPackagerMessages.JarManifestWizardPage_error_onlyOneManifestMustBeSelected);
442             else {
443                 setErrorMessage(""); //$NON-NLS-1$
444
fJarPackage.setManifestLocation(((IResource)resources[0]).getFullPath());
445                 fManifestFileText.setText(fJarPackage.getManifestLocation().toString());
446             }
447         }
448     }
449
450     private IType findMainMethodByName(String JavaDoc name) {
451         if (fMainTypes == null) {
452             List JavaDoc resources= JarPackagerUtil.asResources(fJarPackage.getElements());
453             if (resources == null) {
454                 setErrorMessage(JarPackagerMessages.JarManifestWizardPage_error_noResourceSelected);
455                 return null;
456             }
457             IJavaSearchScope searchScope= JavaSearchScopeFactory.getInstance().createJavaSearchScope((IResource[])resources.toArray(new IResource[resources.size()]), true);
458             MainMethodSearchEngine engine= new MainMethodSearchEngine();
459             try {
460                 fMainTypes= engine.searchMainMethods(getContainer(), searchScope, 0);
461             } catch (InvocationTargetException JavaDoc ex) {
462                 // null
463
} catch (InterruptedException JavaDoc e) {
464                 // null
465
}
466         }
467         for (int i= 0; i < fMainTypes.length; i++) {
468             if (fMainTypes[i].getFullyQualifiedName().equals(name))
469              return fMainTypes[i];
470         }
471         return null;
472     }
473
474     protected void handleMainClassBrowseButtonPressed() {
475         List JavaDoc resources= JarPackagerUtil.asResources(fJarPackage.getElements());
476         if (resources == null) {
477             setErrorMessage(JarPackagerMessages.JarManifestWizardPage_error_noResourceSelected);
478             return;
479         }
480         IJavaSearchScope searchScope= JavaSearchScopeFactory.getInstance().createJavaSearchScope((IResource[])resources.toArray(new IResource[resources.size()]), true);
481         SelectionDialog dialog= JavaUI.createMainTypeDialog(getContainer().getShell(), getContainer(), searchScope, 0, false, ""); //$NON-NLS-1$
482
dialog.setTitle(JarPackagerMessages.JarManifestWizardPage_mainTypeSelectionDialog_title);
483         dialog.setMessage(JarPackagerMessages.JarManifestWizardPage_mainTypeSelectionDialog_message);
484         if (fJarPackage.getManifestMainClass() != null)
485             dialog.setInitialSelections(new Object JavaDoc[] {fJarPackage.getManifestMainClass()});
486
487         if (dialog.open() == Window.OK) {
488             fJarPackage.setManifestMainClass((IType)dialog.getResult()[0]);
489             fMainClassText.setText(JarPackagerUtil.getMainClassName(fJarPackage));
490         } else if (!fJarPackage.isMainClassValid(getContainer())) {
491             // user did not cancel: no types were found
492
fJarPackage.setManifestMainClass(null);
493             fMainClassText.setText(JarPackagerUtil.getMainClassName(fJarPackage));
494         }
495     }
496
497     protected void handleSealPackagesDetailsButtonPressed() {
498         SelectionDialog dialog= createPackageDialog(getPackagesForSelectedResources(fJarPackage));
499         dialog.setTitle(JarPackagerMessages.JarManifestWizardPage_sealedPackagesSelectionDialog_title);
500         dialog.setMessage(JarPackagerMessages.JarManifestWizardPage_sealedPackagesSelectionDialog_message);
501         dialog.setInitialSelections(fJarPackage.getPackagesToSeal());
502         if (dialog.open() == Window.OK)
503             fJarPackage.setPackagesToSeal(getPackagesFromDialog(dialog));
504         updateSealingInfo();
505     }
506
507     protected void handleUnSealPackagesDetailsButtonPressed() {
508         SelectionDialog dialog= createPackageDialog(getPackagesForSelectedResources(fJarPackage));
509         dialog.setTitle(JarPackagerMessages.JarManifestWizardPage_unsealedPackagesSelectionDialog_title);
510         dialog.setMessage(JarPackagerMessages.JarManifestWizardPage_unsealedPackagesSelectionDialog_message);
511         dialog.setInitialSelections(fJarPackage.getPackagesToUnseal());
512         if (dialog.open() == Window.OK)
513             fJarPackage.setPackagesToUnseal(getPackagesFromDialog(dialog));
514         updateSealingInfo();
515     }
516     /**
517      * Updates the enable state of this page's controls. Subclasses may extend.
518      */

519     protected void updateEnableState() {
520         boolean generate= fGenerateManifestRadioButton.getSelection();
521
522         boolean save= generate && fSaveManifestCheckbox.getSelection();
523         fSaveManifestCheckbox.setEnabled(generate);
524         fReuseManifestCheckbox.setEnabled(fJarPackage.isDescriptionSaved() && save);
525         fNewManifestFileText.setEnabled(save);
526         fNewManifestFileLabel.setEnabled(save);
527         fNewManifestFileBrowseButton.setEnabled(save);
528
529         fManifestFileText.setEnabled(!generate);
530         fManifestFileLabel.setEnabled(!generate);
531         fManifestFileBrowseButton.setEnabled(!generate);
532
533         fSealingHeaderLabel.setEnabled(generate);
534         boolean sealState= fSealJarRadioButton.getSelection();
535         fSealJarRadioButton.setEnabled(generate);
536         fSealJarLabel.setEnabled(generate);
537         fUnSealedPackagesDetailsButton.setEnabled(sealState && generate);
538         fSealPackagesRadioButton.setEnabled(generate);
539         fSealPackagesLabel.setEnabled(generate);
540         fSealedPackagesDetailsButton.setEnabled(!sealState && generate);
541
542         fMainClassHeaderLabel.setEnabled(generate);
543         fMainClassLabel.setEnabled(generate);
544         fMainClassText.setEnabled(generate);
545         fMainClassBrowseButton.setEnabled(generate);
546
547         updateSealingInfo();
548     }
549         
550     protected void updateSealingInfo() {
551         if (fJarPackage.isJarSealed()) {
552             fSealPackagesLabel.setText(""); //$NON-NLS-1$
553
int i= fJarPackage.getPackagesToUnseal().length;
554             if (i == 0)
555                 fSealJarLabel.setText(JarPackagerMessages.JarManifestWizardPage_jarSealed);
556             else if (i == 1)
557                 fSealJarLabel.setText(JarPackagerMessages.JarManifestWizardPage_jarSealedExceptOne);
558             else
559                 fSealJarLabel.setText(Messages.format(JarPackagerMessages.JarManifestWizardPage_jarSealedExceptSome, new Integer JavaDoc(i)));
560                 
561         }
562         else {
563             fSealJarLabel.setText(""); //$NON-NLS-1$
564
int i= fJarPackage.getPackagesToSeal().length;
565             if (i == 0)
566                 fSealPackagesLabel.setText(JarPackagerMessages.JarManifestWizardPage_nothingSealed);
567             else if (i == 1)
568                 fSealPackagesLabel.setText(JarPackagerMessages.JarManifestWizardPage_onePackageSealed);
569             else
570                 fSealPackagesLabel.setText(Messages.format(JarPackagerMessages.JarManifestWizardPage_somePackagesSealed, new Integer JavaDoc(i)));
571         }
572     }
573     /*
574      * Implements method from IJarPackageWizardPage
575      */

576     public boolean isPageComplete() {
577         boolean isPageComplete= true;
578         setMessage(null);
579         
580         if (!fJarPackage.areGeneratedFilesExported())
581             return true;
582         
583         if (fJarPackage.isManifestGenerated() && fJarPackage.isManifestSaved()) {
584             if (fJarPackage.getManifestLocation().toString().length() == 0)
585                     isPageComplete= false;
586             else {
587                 IPath location= fJarPackage.getManifestLocation();
588                 if (!location.toString().startsWith("/")) { //$NON-NLS-1$
589
setErrorMessage(JarPackagerMessages.JarManifestWizardPage_error_manifestPathMustBeAbsolute);
590                     return false;
591                 }
592                 IResource resource= findResource(location);
593                 if (resource != null && resource.getType() != IResource.FILE) {
594                     setErrorMessage(JarPackagerMessages.JarManifestWizardPage_error_manifestMustNotBeExistingContainer);
595                     return false;
596                 }
597                 resource= findResource(location.removeLastSegments(1));
598                 if (resource == null || resource.getType() == IResource.FILE) {
599                     setErrorMessage(JarPackagerMessages.JarManifestWizardPage_error_manifestContainerDoesNotExist);
600                     return false;
601                 }
602             }
603         }
604         if (!fJarPackage.isManifestGenerated()) {
605             if (fJarPackage.isManifestAccessible()) {
606                 Manifest JavaDoc manifest= null;
607                 try {
608                     manifest= fJarPackage.getManifestProvider().create(fJarPackage);
609                 } catch (CoreException ex) {
610                     // nothing reported in the wizard
611
}
612                 if (manifest != null && manifest.getMainAttributes().getValue(Attributes.Name.MANIFEST_VERSION) == null)
613                     setMessage(JarPackagerMessages.JarManifestWizardPage_warning_noManifestVersion, IMessageProvider.WARNING);
614             } else {
615                 if (fJarPackage.getManifestLocation().toString().length() == 0)
616                     setErrorMessage(JarPackagerMessages.JarManifestWizardPage_error_noManifestFile);
617                 else
618                     setErrorMessage(JarPackagerMessages.JarManifestWizardPage_error_invalidManifestFile);
619                 return false;
620             }
621         }
622         Set JavaDoc selectedPackages= getPackagesForSelectedResources(fJarPackage);
623         if (fJarPackage.isJarSealed()
624                 && !selectedPackages.containsAll(Arrays.asList(fJarPackage.getPackagesToUnseal()))) {
625             setErrorMessage(JarPackagerMessages.JarManifestWizardPage_error_unsealedPackagesNotInSelection);
626             return false;
627         }
628         if (!fJarPackage.isJarSealed()
629                 && !selectedPackages.containsAll(Arrays.asList(fJarPackage.getPackagesToSeal()))) {
630             setErrorMessage(JarPackagerMessages.JarManifestWizardPage_error_sealedPackagesNotInSelection);
631             return false;
632         }
633         if (!fJarPackage.isMainClassValid(getContainer()) || (fJarPackage.getManifestMainClass() == null && fMainClassText.getText().length() > 0)) {
634             setErrorMessage(JarPackagerMessages.JarManifestWizardPage_error_invalidMainClass);
635             return false;
636         }
637
638         setErrorMessage(null);
639         return isPageComplete;
640     }
641
642     /*
643      * Implements method from IWizardPage.
644      */

645     public void setPreviousPage(IWizardPage page) {
646         super.setPreviousPage(page);
647         fMainTypes= null;
648         updateEnableState();
649         if (getContainer() != null)
650             updatePageCompletion();
651     }
652
653     /*
654      * Implements method from IJarPackageWizardPage.
655      */

656     public void finish() {
657         saveWidgetValues();
658     }
659
660     // ----------- Model handling -----------
661

662     /**
663      * Persists resource specification control setting that are to be restored
664      * in the next instance of this page. Subclasses wishing to persist
665      * settings for their controls should extend the hook method
666      * <code>internalSaveWidgetValues</code>.
667      */

668     public final void saveWidgetValues() {
669         IDialogSettings settings= getDialogSettings();
670         if (settings != null) {
671             // Manifest creation
672
settings.put(STORE_GENERATE_MANIFEST, fJarPackage.isManifestGenerated());
673             settings.put(STORE_SAVE_MANIFEST, fJarPackage.isManifestSaved());
674             settings.put(STORE_REUSE_MANIFEST, fJarPackage.isManifestReused());
675             settings.put(STORE_MANIFEST_LOCATION, fJarPackage.getManifestLocation().toString());
676
677             // Sealing
678
settings.put(STORE_SEAL_JAR, fJarPackage.isJarSealed());
679             }
680
681         // Allow subclasses to save values
682
internalSaveWidgetValues();
683     }
684     /**
685      * Hook method for subclasses to persist their settings.
686      */

687     protected void internalSaveWidgetValues() {
688     }
689     /**
690      * Hook method for restoring widget values to the values that they held
691      * last time this wizard was used to completion.
692      */

693     protected void restoreWidgetValues() {
694         if (!((JarPackageWizard)getWizard()).isInitializingFromJarPackage())
695             initializeJarPackage();
696
697         // Manifest creation
698
if (fJarPackage.isManifestGenerated())
699             fGenerateManifestRadioButton.setSelection(true);
700         else
701             fUseManifestRadioButton.setSelection(true);
702         fSaveManifestCheckbox.setSelection(fJarPackage.isManifestSaved());
703         fReuseManifestCheckbox.setSelection(fJarPackage.isManifestReused());
704         fManifestFileText.setText(fJarPackage.getManifestLocation().toString());
705         fNewManifestFileText.setText(fJarPackage.getManifestLocation().toString());
706
707         // Sealing
708
if (fJarPackage.isJarSealed())
709             fSealJarRadioButton.setSelection(true);
710         else
711             fSealPackagesRadioButton.setSelection(true);
712         
713         // Main-Class
714
fMainClassText.setText(JarPackagerUtil.getMainClassName(fJarPackage));
715     }
716     /**
717      * Initializes the JAR package from last used wizard page values.
718      */

719     protected void initializeJarPackage() {
720         IDialogSettings settings= getDialogSettings();
721         if (settings != null) {
722             // Manifest creation
723
fJarPackage.setGenerateManifest(settings.getBoolean(STORE_GENERATE_MANIFEST));
724             fJarPackage.setSaveManifest(settings.getBoolean(STORE_SAVE_MANIFEST));
725             fJarPackage.setReuseManifest(settings.getBoolean(STORE_REUSE_MANIFEST));
726             String JavaDoc pathStr= settings.get(STORE_MANIFEST_LOCATION);
727             if (pathStr == null)
728                 pathStr= ""; //$NON-NLS-1$
729
fJarPackage.setManifestLocation(new Path(pathStr));
730
731             // Sealing
732
fJarPackage.setSealJar(settings.getBoolean(STORE_SEAL_JAR));
733         }
734     }
735     /**
736      * Stores the widget values in the JAR package.
737      */

738     protected void updateModel() {
739         if (getControl() == null)
740             return;
741         
742         // Manifest creation
743
fJarPackage.setGenerateManifest(fGenerateManifestRadioButton.getSelection());
744         fJarPackage.setSaveManifest(fSaveManifestCheckbox.getSelection());
745         fJarPackage.setReuseManifest(fReuseManifestCheckbox.getSelection());
746         String JavaDoc path;
747         if (fJarPackage.isManifestGenerated())
748             path= fNewManifestFileText.getText();
749         else
750             path= fManifestFileText.getText();
751         if (path == null)
752             path= ""; //$NON-NLS-1$
753
fJarPackage.setManifestLocation(new Path(path));
754
755         // Sealing
756
fJarPackage.setSealJar(fSealJarRadioButton.getSelection());
757     }
758     /**
759      * Determine if the page is complete and update the page appropriately.
760      */

761     protected void updatePageCompletion() {
762         boolean pageComplete= isPageComplete();
763         setPageComplete(pageComplete);
764         if (pageComplete) {
765             setErrorMessage(null);
766         }
767     }
768
769     // ----------- Utility methods -----------
770

771     /**
772      * Creates a file resource handle for the file with the given workspace path.
773      * This method does not create the file resource; this is the responsibility
774      * of <code>createFile</code>.
775      *
776      * @param filePath the path of the file resource to create a handle for
777      * @return the new file resource handle
778      */

779     protected IFile createFileHandle(IPath filePath) {
780         if (filePath.isValidPath(filePath.toString()) && filePath.segmentCount() >= 2)
781             return JavaPlugin.getWorkspace().getRoot().getFile(filePath);
782         else
783             return null;
784     }
785     /**
786      * Creates a new label with a bold font.
787      *
788      * @param parent the parent control
789      * @param text the label text
790      * @return the new label control
791      */

792     protected Label createLabel(Composite parent, String JavaDoc text, boolean bold) {
793         Label label= new Label(parent, SWT.NONE);
794         if (bold)
795             label.setFont(JFaceResources.getBannerFont());
796         label.setText(text);
797         GridData data= new GridData();
798         data.verticalAlignment= GridData.FILL;
799         data.horizontalAlignment= GridData.FILL;
800         label.setLayoutData(data);
801         return label;
802     }
803     /**
804      * Sets the size of a control.
805      *
806      * @param control the control for which to set the size
807      * @param width the new width of the control
808      * @param height the new height of the control
809      */

810     protected void setSize(Control control, int width, int height) {
811         GridData gd= new GridData(GridData.END);
812         gd.widthHint= width ;
813         gd.heightHint= height;
814         control.setLayoutData(gd);
815     }
816     /**
817      * Makes the size of all buttons equal.
818      */

819     protected void setEqualButtonSizes() {
820         int width= SWTUtil.getButtonWidthHint(fManifestFileBrowseButton);
821         int width2= SWTUtil.getButtonWidthHint(fNewManifestFileBrowseButton);
822         width= Math.max(width, width2);
823
824         width2= SWTUtil.getButtonWidthHint(fSealedPackagesDetailsButton);
825         width= Math.max(width, width2);
826
827         width2= SWTUtil.getButtonWidthHint(fUnSealedPackagesDetailsButton);
828         width= Math.max(width, width2);
829
830         width2= SWTUtil.getButtonWidthHint(fMainClassBrowseButton);
831         width= Math.max(width, width2);
832
833         setSize(fManifestFileBrowseButton, width, SWT.DEFAULT);
834         setSize(fNewManifestFileBrowseButton, width, SWT.DEFAULT);
835         setSize(fSealedPackagesDetailsButton, width, SWT.DEFAULT);
836         setSize(fUnSealedPackagesDetailsButton, width, SWT.DEFAULT);
837         setSize(fMainClassBrowseButton, width, SWT.DEFAULT);
838     }
839     
840     /**
841      * Creates a horizontal spacer line that fills the width of its container.
842      *
843      * @param parent the parent control
844      */

845     protected void createSpacer(Composite parent) {
846         Label spacer= new Label(parent, SWT.NONE);
847         GridData data= new GridData();
848         data.horizontalAlignment= GridData.FILL;
849         data.verticalAlignment= GridData.BEGINNING;
850         spacer.setLayoutData(data);
851     }
852     /**
853      * Returns the resource for the specified path.
854      *
855      * @param path the path for which the resource should be returned
856      * @return the resource specified by the path or <code>null</code>
857      */

858     protected IResource findResource(IPath path) {
859         IWorkspace workspace= JavaPlugin.getWorkspace();
860         IStatus result= workspace.validatePath(
861                             path.toString(),
862                             IResource.ROOT | IResource.PROJECT | IResource.FOLDER | IResource.FILE);
863         if (result.isOK() && workspace.getRoot().exists(path))
864             return workspace.getRoot().findMember(path);
865         return null;
866     }
867
868     protected IPath getPathFromString(String JavaDoc text) {
869         return new Path(text).makeAbsolute();
870     }
871     /**
872      * Creates a selection dialog that lists all packages under the given package
873      * fragment root.
874      * The caller is responsible for opening the dialog with <code>Window.open</code>,
875      * and subsequently extracting the selected packages (of type
876      * <code>IPackageFragment</code>) via <code>SelectionDialog.getResult</code>.
877      *
878      * @param packageFragments the package fragments
879      * @return a new selection dialog
880      */

881     protected SelectionDialog createPackageDialog(Set JavaDoc packageFragments) {
882         List JavaDoc packages= new ArrayList JavaDoc(packageFragments.size());
883         for (Iterator JavaDoc iter= packageFragments.iterator(); iter.hasNext();) {
884             IPackageFragment fragment= (IPackageFragment)iter.next();
885             boolean containsJavaElements= false;
886             int kind;
887             try {
888                 kind= fragment.getKind();
889                 containsJavaElements= fragment.getChildren().length > 0;
890             } catch (JavaModelException ex) {
891                 ExceptionHandler.handle(ex, getContainer().getShell(), JarPackagerMessages.JarManifestWizardPage_error_jarPackageWizardError_title, Messages.format(JarPackagerMessages.JarManifestWizardPage_error_jarPackageWizardError_message, fragment.getElementName()));
892                 continue;
893             }
894             if (kind != IPackageFragmentRoot.K_BINARY && containsJavaElements)
895                 packages.add(fragment);
896         }
897         StandardJavaElementContentProvider cp= new StandardJavaElementContentProvider() {
898             public boolean hasChildren(Object JavaDoc element) {
899                 // prevent the + from being shown in front of packages
900
return !(element instanceof IPackageFragment) && super.hasChildren(element);
901             }
902         };
903         final DecoratingLabelProvider provider= new DecoratingLabelProvider(new JavaElementLabelProvider(JavaElementLabelProvider.SHOW_DEFAULT), new ProblemsLabelDecorator(null));
904         ElementTreeSelectionDialog dialog= new ElementTreeSelectionDialog(getContainer().getShell(), provider, cp);
905         dialog.setDoubleClickSelects(false);
906         dialog.setComparator(new JavaElementComparator());
907         dialog.setInput(JavaCore.create(JavaPlugin.getWorkspace().getRoot()));
908         dialog.addFilter(new EmptyInnerPackageFilter());
909         dialog.addFilter(new LibraryFilter());
910         dialog.addFilter(new SealPackagesFilter(packages));
911         dialog.setValidator(new ISelectionStatusValidator() {
912             public IStatus validate(Object JavaDoc[] selection) {
913                 StatusInfo res= new StatusInfo();
914                 for (int i= 0; i < selection.length; i++) {
915                     if (!(selection[i] instanceof IPackageFragment)) {
916                         res.setError(JarPackagerMessages.JarManifestWizardPage_error_mustContainPackages);
917                         return res;
918                     }
919                 }
920                 res.setOK();
921                 return res;
922             }
923         });
924         return dialog;
925     }
926     /**
927      * Converts selection dialog results into an array of IPackageFragments.
928      * An empty array is returned in case of errors.
929      * @throws ClassCastException if results are not IPackageFragments
930      */

931     protected IPackageFragment[] getPackagesFromDialog(SelectionDialog dialog) {
932         if (dialog.getReturnCode() == Window.OK && dialog.getResult().length > 0)
933             return (IPackageFragment[])Arrays.asList(dialog.getResult()).toArray(new IPackageFragment[dialog.getResult().length]);
934         else
935             return new IPackageFragment[0];
936     }
937     /**
938      * Creates and returns a dialog to choose an existing workspace file.
939      */

940     protected ElementTreeSelectionDialog createWorkspaceFileSelectionDialog(String JavaDoc title, String JavaDoc message) {
941         int labelFlags= JavaElementLabelProvider.SHOW_BASICS
942                         | JavaElementLabelProvider.SHOW_OVERLAY_ICONS
943                         | JavaElementLabelProvider.SHOW_SMALL_ICONS;
944         final DecoratingLabelProvider provider= new DecoratingLabelProvider(new JavaElementLabelProvider(labelFlags), new ProblemsLabelDecorator(null));
945         ElementTreeSelectionDialog dialog= new ElementTreeSelectionDialog(getShell(), provider, new StandardJavaElementContentProvider());
946         dialog.setComparator(new JavaElementComparator());
947         dialog.setAllowMultiple(false);
948         dialog.setValidator(new ISelectionStatusValidator() {
949             public IStatus validate(Object JavaDoc[] selection) {
950                 StatusInfo res= new StatusInfo();
951                 // only single selection
952
if (selection.length == 1 && (selection[0] instanceof IFile))
953                     res.setOK();
954                 else
955                     res.setError(""); //$NON-NLS-1$
956
return res;
957             }
958         });
959         dialog.addFilter(new EmptyInnerPackageFilter());
960         dialog.addFilter(new LibraryFilter());
961         dialog.setTitle(title);
962         dialog.setMessage(message);
963         dialog.setStatusLineAboveButtons(true);
964         dialog.setInput(JavaCore.create(JavaPlugin.getWorkspace().getRoot()));
965         return dialog;
966     }
967
968     /**
969      * Returns the minimal set of packages which contain all the selected Java resources.
970      * @return the Set of IPackageFragments which contain all the selected resources
971      */

972     private Set JavaDoc getPackagesForSelectedResources(JarPackageData jarPackage) {
973         Set JavaDoc packages= new HashSet JavaDoc();
974         int n= fJarPackage.getElements().length;
975         for (int i= 0; i < n; i++) {
976             Object JavaDoc element= fJarPackage.getElements()[i];
977             if (element instanceof ICompilationUnit) {
978                 packages.add(((ICompilationUnit) element).getParent());
979             }
980         }
981         return packages;
982     }
983 }
984
Popular Tags