KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > wizards > buildpaths > ClasspathContainerWizard


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.wizards.buildpaths;
12
13 import org.eclipse.core.runtime.CoreException;
14
15 import org.eclipse.swt.widgets.Shell;
16
17 import org.eclipse.jface.resource.JFaceResources;
18 import org.eclipse.jface.wizard.IWizardPage;
19 import org.eclipse.jface.wizard.Wizard;
20 import org.eclipse.jface.wizard.WizardDialog;
21
22 import org.eclipse.jdt.core.IClasspathEntry;
23 import org.eclipse.jdt.core.IJavaProject;
24
25 import org.eclipse.jdt.ui.wizards.IClasspathContainerPage;
26 import org.eclipse.jdt.ui.wizards.IClasspathContainerPageExtension;
27 import org.eclipse.jdt.ui.wizards.IClasspathContainerPageExtension2;
28
29 import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
30 import org.eclipse.jdt.internal.ui.util.PixelConverter;
31 import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages;
32
33
34 /**
35   */

36 public class ClasspathContainerWizard extends Wizard {
37
38     private ClasspathContainerDescriptor fPageDesc;
39     private IClasspathEntry fEntryToEdit;
40
41     private IClasspathEntry[] fNewEntries;
42     private IClasspathContainerPage fContainerPage;
43     private IJavaProject fCurrProject;
44     private IClasspathEntry[] fCurrClasspath;
45     
46     private ClasspathContainerSelectionPage fSelectionWizardPage;
47
48     /**
49      * Constructor for ClasspathContainerWizard.
50      * @param entryToEdit entry to edit
51      * @param currProject current project
52      * @param currEntries entries currently in classpath
53      */

54     public ClasspathContainerWizard(IClasspathEntry entryToEdit, IJavaProject currProject, IClasspathEntry[] currEntries) {
55         this(entryToEdit, null, currProject, currEntries);
56     }
57     
58     /**
59      * Constructor for ClasspathContainerWizard.
60      * @param pageDesc page description
61      * @param currProject current project
62      * @param currEntries entries currently in classpath
63      */

64     public ClasspathContainerWizard(ClasspathContainerDescriptor pageDesc, IJavaProject currProject, IClasspathEntry[] currEntries) {
65         this(null, pageDesc, currProject, currEntries);
66     }
67
68     private ClasspathContainerWizard(IClasspathEntry entryToEdit, ClasspathContainerDescriptor pageDesc, IJavaProject currProject, IClasspathEntry[] currEntries) {
69         fEntryToEdit= entryToEdit;
70         fPageDesc= pageDesc;
71         fNewEntries= null;
72         
73         fCurrProject= currProject;
74         fCurrClasspath= currEntries;
75         
76         String JavaDoc title;
77         if (entryToEdit == null) {
78             title= NewWizardMessages.ClasspathContainerWizard_new_title;
79         } else {
80             title= NewWizardMessages.ClasspathContainerWizard_edit_title;
81         }
82         setWindowTitle(title);
83     }
84         
85     public IClasspathEntry[] getNewEntries() {
86         return fNewEntries;
87     }
88
89     /* (non-Javadoc)
90      * @see IWizard#performFinish()
91      */

92     public boolean performFinish() {
93         if (fContainerPage != null) {
94             if (fContainerPage.finish()) {
95                 if (fEntryToEdit == null && fContainerPage instanceof IClasspathContainerPageExtension2) {
96                     fNewEntries= ((IClasspathContainerPageExtension2) fContainerPage).getNewContainers();
97                 } else {
98                     IClasspathEntry entry= fContainerPage.getSelection();
99                     fNewEntries= (entry != null) ? new IClasspathEntry[] { entry } : null;
100                 }
101                 return true;
102             }
103         }
104         return false;
105     }
106
107     /* (non-Javadoc)
108      * @see IWizard#addPages()
109      */

110     public void addPages() {
111         if (fPageDesc != null) {
112             fContainerPage= getContainerPage(fPageDesc);
113             addPage(fContainerPage);
114         } else if (fEntryToEdit == null) { // new entry: show selection page as first page
115
ClasspathContainerDescriptor[] containers= ClasspathContainerDescriptor.getDescriptors();
116
117             fSelectionWizardPage= new ClasspathContainerSelectionPage(containers);
118             addPage(fSelectionWizardPage);
119
120             // add as dummy, will not be shown
121
fContainerPage= new ClasspathContainerDefaultPage();
122             addPage(fContainerPage);
123         } else { // fPageDesc == null && fEntryToEdit != null
124
ClasspathContainerDescriptor[] containers= ClasspathContainerDescriptor.getDescriptors();
125             ClasspathContainerDescriptor descriptor= findDescriptorPage(containers, fEntryToEdit);
126             fContainerPage= getContainerPage(descriptor);
127             addPage(fContainerPage);
128         }
129         super.addPages();
130     }
131     
132     private IClasspathContainerPage getContainerPage(ClasspathContainerDescriptor pageDesc) {
133         IClasspathContainerPage containerPage= null;
134         if (pageDesc != null) {
135             IClasspathContainerPage page= pageDesc.getPage();
136             if (page != null) {
137                 return page; // if page is already created, avoid double initialization
138
}
139             try {
140                 containerPage= pageDesc.createPage();
141             } catch (CoreException e) {
142                 handlePageCreationFailed(e);
143                 containerPage= null;
144             }
145         }
146
147         if (containerPage == null) {
148             containerPage= new ClasspathContainerDefaultPage();
149             if (pageDesc != null) {
150                 pageDesc.setPage(containerPage); // avoid creation next time
151
}
152         }
153
154         if (containerPage instanceof IClasspathContainerPageExtension) {
155             ((IClasspathContainerPageExtension) containerPage).initialize(fCurrProject, fCurrClasspath);
156         }
157
158         containerPage.setSelection(fEntryToEdit);
159         containerPage.setWizard(this);
160         return containerPage;
161     }
162     
163     /* (non-Javadoc)
164      * @see IWizard#getNextPage(IWizardPage)
165      */

166     public IWizardPage getNextPage(IWizardPage page) {
167         if (page == fSelectionWizardPage) {
168
169             ClasspathContainerDescriptor selected= fSelectionWizardPage.getSelected();
170             fContainerPage= getContainerPage(selected);
171             
172             return fContainerPage;
173         }
174         return super.getNextPage(page);
175     }
176     
177     private void handlePageCreationFailed(CoreException e) {
178         String JavaDoc title= NewWizardMessages.ClasspathContainerWizard_pagecreationerror_title;
179         String JavaDoc message= NewWizardMessages.ClasspathContainerWizard_pagecreationerror_message;
180         ExceptionHandler.handle(e, getShell(), title, message);
181     }
182     
183     
184     private ClasspathContainerDescriptor findDescriptorPage(ClasspathContainerDescriptor[] containers, IClasspathEntry entry) {
185         for (int i = 0; i < containers.length; i++) {
186             if (containers[i].canEdit(entry)) {
187                 return containers[i];
188             }
189         }
190         return null;
191     }
192     
193     /* (non-Javadoc)
194      * @see org.eclipse.jface.wizard.Wizard#dispose()
195      */

196     public void dispose() {
197         if (fSelectionWizardPage != null) {
198             ClasspathContainerDescriptor[] descriptors= fSelectionWizardPage.getContainers();
199             for (int i= 0; i < descriptors.length; i++) {
200                 descriptors[i].dispose();
201             }
202         }
203         super.dispose();
204     }
205
206     /* (non-Javadoc)
207      * @see IWizard#canFinish()
208      */

209     public boolean canFinish() {
210         if (fSelectionWizardPage != null) {
211             if (!fContainerPage.isPageComplete()) {
212                 return false;
213             }
214         }
215         if (fContainerPage != null) {
216             return fContainerPage.isPageComplete();
217         }
218         return false;
219     }
220     
221     public static int openWizard(Shell shell, ClasspathContainerWizard wizard) {
222         WizardDialog dialog= new WizardDialog(shell, wizard);
223         PixelConverter converter= new PixelConverter(JFaceResources.getDialogFont());
224         dialog.setMinimumPageSize(converter.convertWidthInCharsToPixels(70), converter.convertHeightInCharsToPixels(20));
225         dialog.create();
226         return dialog.open();
227     }
228     
229 }
230
Popular Tags