KickJava   Java API By Example, From Geeks To Geeks.

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


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.pde.internal.ui.wizards.imports;
12
13 import java.io.BufferedInputStream JavaDoc;
14 import java.io.File JavaDoc;
15 import java.io.FileInputStream JavaDoc;
16 import java.io.IOException JavaDoc;
17 import java.io.InputStream JavaDoc;
18 import java.lang.reflect.InvocationTargetException JavaDoc;
19 import java.util.ArrayList JavaDoc;
20
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.Path;
26 import org.eclipse.core.runtime.Preferences;
27 import org.eclipse.core.runtime.Status;
28 import org.eclipse.jface.dialogs.Dialog;
29 import org.eclipse.jface.dialogs.IDialogSettings;
30 import org.eclipse.jface.dialogs.IMessageProvider;
31 import org.eclipse.jface.operation.IRunnableWithProgress;
32 import org.eclipse.jface.viewers.CheckboxTableViewer;
33 import org.eclipse.jface.viewers.IStructuredContentProvider;
34 import org.eclipse.jface.viewers.StructuredViewer;
35 import org.eclipse.jface.wizard.WizardPage;
36 import org.eclipse.osgi.util.NLS;
37 import org.eclipse.pde.internal.core.ICoreConstants;
38 import org.eclipse.pde.internal.core.PDECore;
39 import org.eclipse.pde.internal.core.feature.ExternalFeatureModel;
40 import org.eclipse.pde.internal.core.ifeature.IFeatureModel;
41 import org.eclipse.pde.internal.ui.IHelpContextIds;
42 import org.eclipse.pde.internal.ui.IPDEUIConstants;
43 import org.eclipse.pde.internal.ui.PDEPlugin;
44 import org.eclipse.pde.internal.ui.PDEUIMessages;
45 import org.eclipse.pde.internal.ui.elements.DefaultContentProvider;
46 import org.eclipse.pde.internal.ui.parts.WizardCheckboxTablePart;
47 import org.eclipse.pde.internal.ui.util.SWTUtil;
48 import org.eclipse.pde.internal.ui.wizards.ListUtil;
49 import org.eclipse.swt.SWT;
50 import org.eclipse.swt.events.ModifyEvent;
51 import org.eclipse.swt.events.ModifyListener;
52 import org.eclipse.swt.events.SelectionAdapter;
53 import org.eclipse.swt.events.SelectionEvent;
54 import org.eclipse.swt.layout.GridData;
55 import org.eclipse.swt.layout.GridLayout;
56 import org.eclipse.swt.widgets.Button;
57 import org.eclipse.swt.widgets.Combo;
58 import org.eclipse.swt.widgets.Composite;
59 import org.eclipse.swt.widgets.Control;
60 import org.eclipse.swt.widgets.DirectoryDialog;
61 import org.eclipse.swt.widgets.Label;
62 import org.eclipse.ui.PlatformUI;
63 import org.eclipse.ui.forms.widgets.FormToolkit;
64 import org.eclipse.ui.progress.IProgressService;
65
66 public class FeatureImportWizardPage extends WizardPage {
67
68     private static final String JavaDoc SETTINGS_DROPLOCATION = "droplocation"; //$NON-NLS-1$
69
private static final String JavaDoc SETTINGS_DOOTHER = "doother"; //$NON-NLS-1$
70
private static final String JavaDoc SETTINGS_NOT_BINARY = "notbinary"; //$NON-NLS-1$
71

72     private Label fOtherLocationLabel;
73     private Button fRuntimeLocationButton;
74     private Button fBrowseButton;
75     private Combo fDropLocation;
76     private boolean fSelfSetLocation;
77     private String JavaDoc fCurrDropLocation;
78
79     private CheckboxTableViewer fFeatureViewer;
80     private TablePart fTablePart;
81     private IFeatureModel[] fModels;
82     
83     private Button fBinaryButton;
84
85     class ContentProvider extends DefaultContentProvider implements IStructuredContentProvider {
86         public Object JavaDoc[] getElements(Object JavaDoc parent) {
87             if (fModels != null)
88                 return fModels;
89             return new Object JavaDoc[0];
90         }
91     }
92     
93     class TablePart extends WizardCheckboxTablePart {
94         public TablePart() {
95             super(null, new String JavaDoc[] {
96                     PDEUIMessages.FeatureImportWizardPage_reload,
97                     PDEUIMessages.WizardCheckboxTablePart_selectAll,
98                     PDEUIMessages.WizardCheckboxTablePart_deselectAll});
99             setSelectAllIndex(1);
100             setDeselectAllIndex(2);
101         }
102         public void updateCounter(int count) {
103             super.updateCounter(count);
104             dialogChanged();
105         }
106         protected StructuredViewer createStructuredViewer(Composite parent, int style, FormToolkit toolkit) {
107             StructuredViewer viewer = super.createStructuredViewer(parent, style, toolkit);
108             viewer.setComparator(ListUtil.FEATURE_COMPARATOR);
109             return viewer;
110         }
111         protected void buttonSelected(Button button, int index) {
112             if (index == 0)
113                 loadFeatureTable();
114             else
115                 super.buttonSelected(button, index);
116         }
117     }
118     
119     public FeatureImportWizardPage() {
120         super("FeatureImportWizardPage"); //$NON-NLS-1$
121
setTitle(PDEUIMessages.FeatureImportWizard_FirstPage_title);
122         setDescription(PDEUIMessages.FeatureImportWizard_FirstPage_desc);
123         fTablePart = new TablePart();
124         PDEPlugin.getDefault().getLabelProvider().connect(this);
125     }
126
127     /*
128      * @see IDialogPage#createControl(Composite)
129      */

130     public void createControl(Composite parent) {
131         initializeDialogUnits(parent);
132
133         Composite composite = new Composite(parent, SWT.NONE);
134         composite.setLayout(new GridLayout(3, false));
135
136         fRuntimeLocationButton = new Button(composite, SWT.CHECK);
137         fillHorizontal(fRuntimeLocationButton, 3, false);
138         fRuntimeLocationButton.setText(PDEUIMessages.FeatureImportWizard_FirstPage_runtimeLocation);
139         
140         fOtherLocationLabel = new Label(composite, SWT.NULL);
141         fOtherLocationLabel.setText(PDEUIMessages.FeatureImportWizard_FirstPage_otherFolder);
142
143         fDropLocation = new Combo(composite, SWT.DROP_DOWN);
144         fillHorizontal(fDropLocation, 1, true);
145
146         fBrowseButton = new Button(composite, SWT.PUSH);
147         fBrowseButton.setText(PDEUIMessages.FeatureImportWizard_FirstPage_browse);
148         fBrowseButton.setLayoutData(new GridData());
149         SWTUtil.setButtonDimensionHint(fBrowseButton);
150
151         creatFeatureTable(composite);
152         
153         fBinaryButton = new Button(composite, SWT.CHECK);
154         GridData gd = fillHorizontal(fBinaryButton, 3, false);
155         gd.verticalIndent = 5;
156         fBinaryButton.setText(PDEUIMessages.FeatureImportWizard_FirstPage_binaryImport);
157         
158         initializeFields(getDialogSettings());
159         hookListeners();
160
161         setControl(composite);
162         dialogChanged();
163         Dialog.applyDialogFont(composite);
164         PlatformUI.getWorkbench().getHelpSystem().setHelp(composite, IHelpContextIds.FEATURE_IMPORT_FIRST_PAGE);
165     }
166
167     private String JavaDoc getTargetHome() {
168         Preferences preferences = PDECore.getDefault().getPluginPreferences();
169         return preferences.getString(ICoreConstants.PLATFORM_PATH);
170     }
171
172     private void hookListeners() {
173         fRuntimeLocationButton.addSelectionListener(new SelectionAdapter() {
174             public void widgetSelected(SelectionEvent e) {
175                 setOtherEnabled(!fRuntimeLocationButton.getSelection());
176                 setLocation(fRuntimeLocationButton.getSelection() ?
177                         getTargetHome() : fCurrDropLocation);
178                 loadFeatureTable();
179             }
180         });
181         fDropLocation.addModifyListener(new ModifyListener() {
182             public void modifyText(ModifyEvent e) {
183                 validateDropLocation();
184                 if (!fRuntimeLocationButton.getSelection()) {
185                     String JavaDoc newLoc = fDropLocation.getText();
186                     if (getMessageType() != WARNING &&
187                             !newLoc.equals(fCurrDropLocation) &&
188                             !fSelfSetLocation)
189                         setMessage(PDEUIMessages.FeatureImportWizardPage_reloadLocation, WARNING);
190                     fCurrDropLocation = newLoc;
191                 }
192             }
193         });
194         fBrowseButton.addSelectionListener(new SelectionAdapter() {
195             public void widgetSelected(SelectionEvent e) {
196                 IPath chosen = chooseDropLocation();
197                 if (chosen != null) {
198                     setLocation(chosen.toOSString());
199                     loadFeatureTable();
200                 }
201             }
202         });
203     }
204
205     private GridData fillHorizontal(Control control, int span, boolean grab) {
206         GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
207         gd.horizontalSpan = span;
208         gd.grabExcessHorizontalSpace = grab;
209         control.setLayoutData(gd);
210         return gd;
211     }
212
213     private void initializeFields(IDialogSettings initialSettings) {
214         String JavaDoc[] dropItems = new String JavaDoc[0];
215         boolean doOther = false;
216         boolean binary = true;
217
218         if (initialSettings != null) {
219             doOther = initialSettings.getBoolean(SETTINGS_DOOTHER);
220             binary = !initialSettings.getBoolean(SETTINGS_NOT_BINARY);
221
222             ArrayList JavaDoc items = new ArrayList JavaDoc();
223             for (int i = 0; i < 6; i++) {
224                 String JavaDoc curr = initialSettings.get(SETTINGS_DROPLOCATION + String.valueOf(i));
225                 if (curr != null && !items.contains(curr))
226                     items.add(curr);
227             }
228             if (items.size() == 0)
229                 items.add(""); //$NON-NLS-1$
230
dropItems = (String JavaDoc[]) items.toArray(new String JavaDoc[items.size()]);
231         }
232         fDropLocation.setItems(dropItems);
233         fRuntimeLocationButton.setSelection(!doOther);
234         setOtherEnabled(doOther);
235         fCurrDropLocation = doOther ? dropItems[0] : getTargetHome();
236         setLocation(fCurrDropLocation);
237         fBinaryButton.setSelection(binary);
238
239         validateDropLocation();
240         
241         loadFeatureTable();
242     }
243
244     private void setOtherEnabled(boolean enabled) {
245         fOtherLocationLabel.setEnabled(enabled);
246         fDropLocation.setEnabled(enabled);
247         fBrowseButton.setEnabled(enabled);
248     }
249
250     public void storeSettings(boolean finishPressed) {
251         IDialogSettings settings = getDialogSettings();
252         boolean other = !fRuntimeLocationButton.getSelection();
253         boolean binary = fBinaryButton.getSelection();
254         if (finishPressed || fDropLocation.getText().length() > 0 && other) {
255             settings.put(SETTINGS_DROPLOCATION + String.valueOf(0), fDropLocation.getText());
256             String JavaDoc[] items = fDropLocation.getItems();
257             int nEntries = Math.min(items.length, 5);
258             for (int i = 0; i < nEntries; i++)
259                 settings.put(SETTINGS_DROPLOCATION + String.valueOf(i + 1), items[i]);
260         }
261         if (finishPressed) {
262             settings.put(SETTINGS_DOOTHER, other);
263             settings.put(SETTINGS_NOT_BINARY, !binary);
264         }
265     }
266
267     /**
268      * Browses for a drop location.
269      */

270     private IPath chooseDropLocation() {
271         DirectoryDialog dialog = new DirectoryDialog(getShell());
272         dialog.setFilterPath(fDropLocation.getText());
273         dialog.setText(PDEUIMessages.FeatureImportWizard_messages_folder_title);
274         dialog.setMessage(PDEUIMessages.FeatureImportWizard_messages_folder_message);
275         String JavaDoc res = dialog.open();
276         return res != null ? new Path(res) : null;
277     }
278
279     private void validateDropLocation() {
280         String JavaDoc errorMessage = null;
281         if (isOtherLocation()) {
282             IPath curr = getDropLocation();
283             if (curr.segmentCount() == 0)
284                 errorMessage = PDEUIMessages.FeatureImportWizard_errors_locationMissing;
285             else if (!Path.ROOT.isValidPath(fDropLocation.getText()))
286                 errorMessage = PDEUIMessages.FeatureImportWizard_errors_buildFolderInvalid;
287             else {
288                 File JavaDoc file = curr.toFile();
289                 if (!file.exists() || !file.isDirectory())
290                     errorMessage = PDEUIMessages.FeatureImportWizard_errors_buildFolderMissing;
291             }
292         }
293         setErrorMessage(errorMessage);
294         setPageComplete(errorMessage == null);
295         fTablePart.setButtonEnabled(0, errorMessage == null);
296     }
297     
298     public boolean isBinary(){
299         return fBinaryButton.getSelection();
300     }
301
302     /**
303      * Returns the drop location.
304      */

305     public IPath getDropLocation() {
306         return new Path(fDropLocation.getText().trim());
307     }
308
309     public boolean isOtherLocation() {
310         return !fRuntimeLocationButton.getSelection();
311     }
312
313     private void loadFeatureTable() {
314         IFeatureModel[] models = getModels();
315         fFeatureViewer.setInput(PDEPlugin.getDefault());
316         if (models != null) {
317             // warning had been issued for unreloaded location
318
if (getMessageType() == WARNING)
319                 setMessage(null, WARNING);
320             if (!fRuntimeLocationButton.getSelection()) {
321                 String JavaDoc currItem = fDropLocation.getText();
322                 if (fDropLocation.indexOf(currItem) == -1) {
323                     fDropLocation.add(currItem, 0);
324                     if (fDropLocation.getItemCount() > 6)
325                         fDropLocation.remove(6);
326                     storeSettings(false);
327                 }
328             }
329             fFeatureViewer.setCheckedElements(models);
330         }
331         fTablePart.updateCounter(models != null ? models.length : 0);
332         fTablePart.getTableViewer().refresh();
333         
334         fTablePart.setButtonEnabled(1, models != null && models.length > 0);
335         fTablePart.setButtonEnabled(2, models != null && models.length > 0);
336         
337         dialogChanged();
338     }
339
340     public void creatFeatureTable(Composite parent) {
341         Composite container = new Composite(parent, SWT.NONE);
342         GridLayout layout = new GridLayout();
343         layout.numColumns = 2;
344         layout.marginHeight = layout.marginWidth = 0;
345         container.setLayout(layout);
346         GridData gd = new GridData(GridData.FILL_HORIZONTAL);
347         gd.horizontalSpan = 3;
348         gd.verticalIndent = 5;
349         gd.heightHint = gd.widthHint = 300;
350         container.setLayoutData(gd);
351         
352         fTablePart.createControl(container);
353         fFeatureViewer = fTablePart.getTableViewer();
354         fFeatureViewer.setContentProvider(new ContentProvider());
355         fFeatureViewer.setLabelProvider(PDEPlugin.getDefault().getLabelProvider());
356     }
357
358     public void dispose() {
359         super.dispose();
360         PDEPlugin.getDefault().getLabelProvider().disconnect(this);
361     }
362
363     public IFeatureModel[] getModels() {
364         final IPath home = getDropLocation();
365         final boolean useRuntimeLocation = fRuntimeLocationButton.getSelection() ||
366             getTargetHome().equals(fDropLocation.getText().trim());
367         IRunnableWithProgress runnable = new IRunnableWithProgress() {
368             public void run(IProgressMonitor monitor) throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
369                 monitor.beginTask(PDEUIMessages.FeatureImportWizard_messages_updating, IProgressMonitor.UNKNOWN);
370                 ArrayList JavaDoc result = new ArrayList JavaDoc();
371                 if (useRuntimeLocation) {
372                     IFeatureModel[] allModels = PDECore.getDefault().getFeatureModelManager().getModels();
373                     for (int i = 0; i < allModels.length; i++)
374                         if (allModels[i].getUnderlyingResource() == null)
375                             result.add(allModels[i]);
376                 } else {
377                     MultiStatus errors = doLoadFeatures(result, createPath(home));
378                     if (errors != null && errors.getChildren().length > 0)
379                         PDEPlugin.log(errors);
380                 }
381                 fModels = (IFeatureModel[])result.toArray(new IFeatureModel[result.size()]);
382                 monitor.done();
383             }
384         };
385         IProgressService pservice = PlatformUI.getWorkbench().getProgressService();
386         try {
387             pservice.busyCursorWhile(runnable);
388         } catch (InvocationTargetException JavaDoc e) {
389         } catch (InterruptedException JavaDoc e) {
390         }
391         return fModels;
392     }
393
394     private File JavaDoc createPath(IPath dropLocation) {
395         File JavaDoc featuresDir = new File JavaDoc(dropLocation.toFile(), "features"); //$NON-NLS-1$
396
if (featuresDir.exists())
397             return featuresDir;
398         return null;
399     }
400
401     private MultiStatus doLoadFeatures(ArrayList JavaDoc result, File JavaDoc path) {
402         if (path == null)
403             return null;
404         File JavaDoc[] dirs = path.listFiles();
405         if (dirs == null)
406             return null;
407         ArrayList JavaDoc resultStatus = new ArrayList JavaDoc();
408         for (int i = 0; i < dirs.length; i++) {
409             File JavaDoc dir = dirs[i];
410             if (dir.isDirectory()) {
411                 File JavaDoc manifest = new File JavaDoc(dir, "feature.xml"); //$NON-NLS-1$
412
if (manifest.exists()) {
413                     IStatus status = doLoadFeature(dir, manifest, result);
414                     if (status != null)
415                         resultStatus.add(status);
416                 }
417             }
418         }
419         return new MultiStatus(
420                 IPDEUIConstants.PLUGIN_ID, IStatus.OK,
421                 (IStatus[]) resultStatus.toArray(new IStatus[resultStatus.size()]),
422                 PDEUIMessages.FeatureImportWizard_DetailedPage_problemsLoading,
423                 null);
424     }
425
426     private IStatus doLoadFeature(File JavaDoc dir, File JavaDoc manifest, ArrayList JavaDoc result) {
427         ExternalFeatureModel model = new ExternalFeatureModel();
428         model.setInstallLocation(dir.getAbsolutePath());
429         IStatus status = null;
430
431         InputStream JavaDoc stream = null;
432
433         try {
434             stream = new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(manifest));
435             model.load(stream, false);
436             if(!model.isValid()){
437                 status = new Status(
438                         IStatus.WARNING, IPDEUIConstants.PLUGIN_ID, IStatus.OK,
439                         NLS.bind(PDEUIMessages.FeatureImportWizardPage_importHasInvalid, dir), null);
440             }
441         } catch (Exception JavaDoc e) {
442             // Errors in the file
443
status = new Status(IStatus.ERROR, IPDEUIConstants.PLUGIN_ID, IStatus.OK, e.getMessage(), e);
444         }
445         if (stream != null) {
446             try {
447                 stream.close();
448             } catch (IOException JavaDoc e) {
449             }
450         }
451         if (status == null)
452             result.add(model);
453         return status;
454     }
455
456     public IFeatureModel[] getSelectedModels() {
457         Object JavaDoc[] selected = fFeatureViewer.getCheckedElements();
458         IFeatureModel[] result = new IFeatureModel[selected.length];
459         System.arraycopy(selected, 0, result, 0, selected.length);
460         return result;
461     }
462
463     private void dialogChanged() {
464         String JavaDoc message = null;
465         if (fFeatureViewer != null && fFeatureViewer.getTable().getItemCount() == 0)
466             message = PDEUIMessages.FeatureImportWizard_messages_noFeatures;
467         
468         setMessage(message, IMessageProvider.INFORMATION);
469         setPageComplete(fTablePart.getSelectionCount() > 0);
470     }
471     
472     /* (non-Javadoc)
473      * @see org.eclipse.jface.wizard.WizardPage#isPageComplete()
474      */

475     public boolean isPageComplete() {
476         return fTablePart.getSelectionCount() > 0;
477     }
478     
479     private void setLocation(String JavaDoc location) {
480         fSelfSetLocation = true;
481         fDropLocation.setText(location);
482         fSelfSetLocation = false;
483     }
484 }
485
Popular Tags