KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > wizards > imports > FeatureImportWizardDetailedPage


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 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.pde.internal.ui.wizards.imports;
12
13 import java.io.File JavaDoc;
14 import java.io.FileInputStream JavaDoc;
15 import java.io.IOException JavaDoc;
16 import java.io.InputStream JavaDoc;
17 import java.lang.reflect.InvocationTargetException JavaDoc;
18 import java.util.ArrayList JavaDoc;
19
20 import org.eclipse.core.runtime.CoreException;
21 import org.eclipse.core.runtime.IPath;
22 import org.eclipse.core.runtime.IProgressMonitor;
23 import org.eclipse.core.runtime.IStatus;
24 import org.eclipse.core.runtime.MultiStatus;
25 import org.eclipse.core.runtime.Status;
26 import org.eclipse.jface.dialogs.Dialog;
27 import org.eclipse.jface.operation.IRunnableWithProgress;
28 import org.eclipse.jface.viewers.CheckboxTableViewer;
29 import org.eclipse.jface.viewers.IStructuredContentProvider;
30 import org.eclipse.jface.viewers.StructuredViewer;
31 import org.eclipse.jface.wizard.WizardPage;
32 import org.eclipse.pde.internal.core.PDECore;
33 import org.eclipse.pde.internal.core.feature.ExternalFeatureModel;
34 import org.eclipse.pde.internal.core.ifeature.IFeatureModel;
35 import org.eclipse.pde.internal.ui.IHelpContextIds;
36 import org.eclipse.pde.internal.ui.PDEPlugin;
37 import org.eclipse.pde.internal.ui.PDEUIMessages;
38 import org.eclipse.pde.internal.ui.elements.DefaultContentProvider;
39 import org.eclipse.pde.internal.ui.parts.WizardCheckboxTablePart;
40 import org.eclipse.pde.internal.ui.wizards.ListUtil;
41 import org.eclipse.swt.SWT;
42 import org.eclipse.swt.layout.GridData;
43 import org.eclipse.swt.layout.GridLayout;
44 import org.eclipse.swt.widgets.Composite;
45 import org.eclipse.ui.PlatformUI;
46 import org.eclipse.ui.forms.widgets.FormToolkit;
47
48 public class FeatureImportWizardDetailedPage extends WizardPage {
49
50     private FeatureImportWizardFirstPage fFirstPage;
51     private boolean fIsOtherLocation;
52     private IPath fDropLocation;
53     private CheckboxTableViewer fFeatureViewer;
54     private TablePart fTablePart;
55     private IFeatureModel[] fModels;
56
57     public class ContentProvider
58     extends DefaultContentProvider
59     implements IStructuredContentProvider {
60         public Object JavaDoc[] getElements(Object JavaDoc parent) {
61             return getModels();
62         }
63     }
64     
65     class TablePart extends WizardCheckboxTablePart {
66         public TablePart(String JavaDoc mainLabel) {
67             super(mainLabel);
68         }
69
70         public void updateCounter(int count) {
71             super.updateCounter(count);
72             dialogChanged();
73         }
74         protected StructuredViewer createStructuredViewer(
75             Composite parent,
76             int style,
77             FormToolkit toolkit) {
78             StructuredViewer viewer =
79                 super.createStructuredViewer(parent, style, toolkit);
80             viewer.setSorter(ListUtil.FEATURE_SORTER);
81             return viewer;
82         }
83     }
84
85     public FeatureImportWizardDetailedPage(FeatureImportWizardFirstPage firstPage) {
86         super("FeatureImportWizardDetailedPage"); //$NON-NLS-1$
87
setTitle(PDEUIMessages.FeatureImportWizard_DetailedPage_title); //$NON-NLS-1$
88
setDescription(PDEUIMessages.FeatureImportWizard_DetailedPage_desc); //$NON-NLS-1$
89

90         fFirstPage = firstPage;
91         fIsOtherLocation = false;
92         fDropLocation = null;
93         fTablePart = new TablePart(PDEUIMessages.FeatureImportWizard_DetailedPage_featureList); //$NON-NLS-1$
94
PDEPlugin.getDefault().getLabelProvider().connect(this);
95     }
96
97     private void initializeFields(boolean isOtherLocation, IPath dropLocation) {
98         if (isOtherLocation != fIsOtherLocation
99                 || !dropLocation.equals(this.fDropLocation)) {
100             this.fIsOtherLocation = fFirstPage.isOtherLocation();
101             this.fDropLocation = dropLocation;
102             fModels = null;
103         }
104         if (fModels == null) {
105             getModels(); // force loading
106
IRunnableWithProgress op = new IRunnableWithProgress() {
107                 public void run(IProgressMonitor monitor) {
108                     monitor.beginTask(
109                         PDEUIMessages.FeatureImportWizard_messages_updating, //$NON-NLS-1$
110
IProgressMonitor.UNKNOWN);
111                     fFeatureViewer
112                         .getControl()
113                         .getDisplay()
114                         .asyncExec(new Runnable JavaDoc() {
115                         public void run() {
116                             fFeatureViewer.setInput(PDEPlugin.getDefault());
117                             if (getModels() != null)
118                                 fFeatureViewer.setCheckedElements(getModels());
119                                 fTablePart.updateCounter(getModels().length);
120                         }
121                     });
122                     monitor.done();
123                 }
124             };
125             try {
126                 getContainer().run(true, false, op);
127             } catch (InterruptedException JavaDoc e) {
128             } catch (InvocationTargetException JavaDoc e) {
129                 PDEPlugin.logException(e);
130             } finally {
131                 dialogChanged();
132             }
133             //treePart.updateCounter(0);
134
}
135     }
136
137     /*
138      * @see DialogPage#setVisible(boolean)
139      */

140     public void setVisible(boolean visible) {
141         super.setVisible(visible);
142         if (visible) {
143             initializeFields(fFirstPage.isOtherLocation(), fFirstPage.getDropLocation());
144         }
145     }
146
147     /*
148      * @see IDialogPage#createControl(Composite)
149      */

150     public void createControl(Composite parent) {
151         initializeDialogUnits(parent);
152
153         Composite container = new Composite(parent, SWT.NONE);
154         GridLayout layout = new GridLayout();
155         layout.numColumns = 2;
156         layout.marginHeight = 0;
157         layout.marginWidth = 5;
158         container.setLayout(layout);
159
160         fTablePart.createControl(container);
161         fFeatureViewer = fTablePart.getTableViewer();
162         fFeatureViewer.setContentProvider(new ContentProvider());
163         fFeatureViewer.setLabelProvider(PDEPlugin.getDefault().getLabelProvider());
164         GridData gd = (GridData) fTablePart.getControl().getLayoutData();
165         gd.heightHint = 300;
166         gd.widthHint = 300;
167         setControl(container);
168         dialogChanged();
169         Dialog.applyDialogFont(container);
170         PlatformUI.getWorkbench().getHelpSystem().setHelp(container, IHelpContextIds.FEATURE_IMPORT_SECOND_PAGE);
171     }
172
173     public void dispose() {
174         super.dispose();
175         PDEPlugin.getDefault().getLabelProvider().disconnect(this);
176     }
177
178     public IFeatureModel[] getModels() {
179         if (fModels != null)
180             return fModels;
181
182         final ArrayList JavaDoc result = new ArrayList JavaDoc();
183         final IPath home = fDropLocation;
184         if (home != null) {
185             IRunnableWithProgress op = new IRunnableWithProgress() {
186                 public void run(IProgressMonitor monitor)
187                     throws InvocationTargetException JavaDoc {
188                     monitor.beginTask(
189                         PDEUIMessages.FeatureImportWizard_messages_loadingFile, //$NON-NLS-1$
190
IProgressMonitor.UNKNOWN);
191
192                     try {
193                         if (!fIsOtherLocation) {
194                             IFeatureModel[] allModels = PDECore.getDefault()
195                                     .getFeatureModelManager().getModels();
196                             for (int i = 0; i < allModels.length; i++) {
197                                 if (allModels[i].getUnderlyingResource() == null) {
198                                     result.add(allModels[i]);
199                                 }
200                             }
201                         } else {
202                             MultiStatus errors = doLoadFeatures(result,
203                                     createPath(home), monitor);
204                             if (errors != null
205                                     && errors.getChildren().length > 0) {
206                                 PDEPlugin.log(errors);
207                             }
208                         }
209                         fModels =
210                             (IFeatureModel[]) result.toArray(
211                                 new IFeatureModel[result.size()]);
212
213                     } catch (CoreException e) {
214                         throw new InvocationTargetException JavaDoc(e);
215                     } finally {
216                         monitor.done();
217                     }
218                 }
219             };
220             try {
221                 getContainer().run(true, false, op);
222             } catch (InterruptedException JavaDoc e) {
223                 return null;
224             } catch (InvocationTargetException JavaDoc e) {
225                 PDEPlugin.logException(e);
226             }
227         }
228         return fModels;
229     }
230
231     private File JavaDoc createPath(IPath dropLocation) {
232         File JavaDoc featuresDir = new File JavaDoc(dropLocation.toFile(), "features"); //$NON-NLS-1$
233
if (featuresDir.exists())
234             return featuresDir;
235         return null;
236     }
237
238     private MultiStatus doLoadFeatures(
239         ArrayList JavaDoc result,
240         File JavaDoc path,
241         IProgressMonitor monitor)
242         throws CoreException {
243         if (path == null)
244             return null;
245         File JavaDoc[] dirs = path.listFiles();
246         if (dirs == null)
247             return null;
248         monitor.beginTask(PDEUIMessages.FeatureImportWizard_DetailedPage_loading, dirs.length); //$NON-NLS-1$
249
ArrayList JavaDoc resultStatus = new ArrayList JavaDoc();
250         for (int i = 0; i < dirs.length; i++) {
251             File JavaDoc dir = dirs[i];
252             if (dir.isDirectory()) {
253                 File JavaDoc manifest = new File JavaDoc(dir, "feature.xml"); //$NON-NLS-1$
254
if (manifest.exists()) {
255                     IStatus status = doLoadFeature(dir, manifest, result);
256                     if (status != null)
257                         resultStatus.add(status);
258                 }
259                 monitor.worked(1);
260             }
261         }
262         if (resultStatus != null) {
263             IStatus[] children =
264                 (IStatus[]) resultStatus.toArray(new IStatus[resultStatus.size()]);
265             MultiStatus multiStatus =
266                 new MultiStatus(
267                     PDEPlugin.PLUGIN_ID,
268                     IStatus.OK,
269                     children,
270                     PDEUIMessages.FeatureImportWizard_DetailedPage_problemsLoading, //$NON-NLS-1$
271
null);
272             return multiStatus;
273         }
274         return null;
275     }
276
277     private IStatus doLoadFeature(File JavaDoc dir, File JavaDoc manifest, ArrayList JavaDoc result) {
278         ExternalFeatureModel model = new ExternalFeatureModel();
279         model.setInstallLocation(dir.getAbsolutePath());
280         IStatus status = null;
281
282         InputStream JavaDoc stream = null;
283
284         try {
285             stream = new FileInputStream JavaDoc(manifest);
286             model.load(stream, false);
287             if(!model.isValid()){
288                 status =
289                     new Status(
290                         IStatus.WARNING,
291                         PDEPlugin.PLUGIN_ID,
292                         IStatus.OK,
293                         "Import location "+dir+" contains invalid feature.", //$NON-NLS-1$ //$NON-NLS-2$
294
null);
295             }
296         } catch (Exception JavaDoc e) {
297             // Errors in the file
298
status =
299                 new Status(
300                     IStatus.ERROR,
301                     PDEPlugin.PLUGIN_ID,
302                     IStatus.OK,
303                     e.getMessage(),
304                     e);
305         }
306         if (stream != null) {
307             try {
308                 stream.close();
309             } catch (IOException JavaDoc e) {
310             }
311         }
312         if (status == null)
313             result.add(model);
314         return status;
315     }
316
317     public IFeatureModel[] getSelectedModels() {
318         Object JavaDoc[] selected = fFeatureViewer.getCheckedElements();
319         IFeatureModel[] result = new IFeatureModel[selected.length];
320         System.arraycopy(selected, 0, result, 0, selected.length);
321         return result;
322     }
323
324     private void dialogChanged() {
325         String JavaDoc message = null;
326         if (fFeatureViewer != null && fFeatureViewer.getTable().getItemCount() == 0) {
327             message = PDEUIMessages.FeatureImportWizard_messages_noFeatures; //$NON-NLS-1$
328
}
329         setMessage(message, WizardPage.INFORMATION);
330         setPageComplete(fTablePart.getSelectionCount() > 0);
331     }
332
333     /* (non-Javadoc)
334      * @see org.eclipse.jface.wizard.WizardPage#isPageComplete()
335      */

336     public boolean isPageComplete() {
337         return fTablePart.getSelectionCount() > 0;
338     }
339
340 }
341
Popular Tags