KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2003, 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 /*
12  * Created on May 30, 2003
13  *
14  * To change this generated comment go to
15  * Window>Preferences>Java>Code Generation>Code Template
16  */

17 package org.eclipse.pde.internal.ui.wizards.imports;
18
19 import java.io.File JavaDoc;
20 import java.lang.reflect.InvocationTargetException JavaDoc;
21 import java.net.MalformedURLException JavaDoc;
22 import java.net.URL JavaDoc;
23 import java.util.ArrayList JavaDoc;
24
25 import org.eclipse.core.runtime.IPath;
26 import org.eclipse.core.runtime.IProgressMonitor;
27 import org.eclipse.core.runtime.Path;
28 import org.eclipse.core.runtime.Preferences;
29 import org.eclipse.jface.dialogs.Dialog;
30 import org.eclipse.jface.dialogs.IDialogSettings;
31 import org.eclipse.jface.dialogs.IMessageProvider;
32 import org.eclipse.jface.operation.IRunnableWithProgress;
33 import org.eclipse.jface.preference.IPreferenceNode;
34 import org.eclipse.jface.wizard.WizardPage;
35 import org.eclipse.pde.core.plugin.IPluginModelBase;
36 import org.eclipse.pde.core.plugin.PluginRegistry;
37 import org.eclipse.pde.core.plugin.TargetPlatform;
38 import org.eclipse.pde.internal.core.ICoreConstants;
39 import org.eclipse.pde.internal.core.PDECore;
40 import org.eclipse.pde.internal.core.PDEState;
41 import org.eclipse.pde.internal.core.PluginPathFinder;
42 import org.eclipse.pde.internal.ui.IHelpContextIds;
43 import org.eclipse.pde.internal.ui.PDEPlugin;
44 import org.eclipse.pde.internal.ui.PDEUIMessages;
45 import org.eclipse.pde.internal.ui.preferences.PDEPreferencesUtil;
46 import org.eclipse.pde.internal.ui.preferences.SourceCodeLocationsPreferenceNode;
47 import org.eclipse.pde.internal.ui.preferences.TargetEnvironmentPreferenceNode;
48 import org.eclipse.pde.internal.ui.preferences.TargetPlatformPreferenceNode;
49 import org.eclipse.pde.internal.ui.util.SWTUtil;
50 import org.eclipse.swt.SWT;
51 import org.eclipse.swt.events.ModifyEvent;
52 import org.eclipse.swt.events.ModifyListener;
53 import org.eclipse.swt.events.SelectionAdapter;
54 import org.eclipse.swt.events.SelectionEvent;
55 import org.eclipse.swt.layout.GridData;
56 import org.eclipse.swt.layout.GridLayout;
57 import org.eclipse.swt.widgets.Button;
58 import org.eclipse.swt.widgets.Combo;
59 import org.eclipse.swt.widgets.Composite;
60 import org.eclipse.swt.widgets.DirectoryDialog;
61 import org.eclipse.swt.widgets.Group;
62 import org.eclipse.swt.widgets.Label;
63 import org.eclipse.ui.PlatformUI;
64
65 public class PluginImportWizardFirstPage extends WizardPage {
66     
67     private static String JavaDoc SETTINGS_IMPORTTYPE = "importType"; //$NON-NLS-1$
68
private static String JavaDoc SETTINGS_DOOTHER = "doother"; //$NON-NLS-1$
69
private static String JavaDoc SETTINGS_DROPLOCATION = "droplocation"; //$NON-NLS-1$
70
private static String JavaDoc SETTINGS_SCAN_ALL = "scanAll"; //$NON-NLS-1$
71

72     private Button runtimeLocationButton;
73     private Button browseButton;
74     private Label otherLocationLabel;
75     private Combo dropLocation;
76     private Button changeButton;
77     
78     private Button importButton;
79     private Button scanButton;
80
81     private Button binaryButton;
82     private Button binaryWithLinksButton;
83     private Button sourceButton;
84     
85     //private String currentLocation;
86
public static String JavaDoc TARGET_PLATFORM = "targetPlatform"; //$NON-NLS-1$
87
private IPluginModelBase[] models = new IPluginModelBase[0];
88     private boolean canceled = false;
89     
90     public PluginImportWizardFirstPage(String JavaDoc name) {
91         super(name);
92         setTitle(PDEUIMessages.ImportWizard_FirstPage_title);
93         setMessage(PDEUIMessages.ImportWizard_FirstPage_desc);
94         PDEPlugin.getDefault().getLabelProvider().connect(this);
95     }
96
97     public void createControl(Composite parent) {
98         Composite container = new Composite(parent, SWT.NONE);
99         GridLayout layout = new GridLayout();
100         layout.verticalSpacing = 15;
101         container.setLayout(layout);
102         
103         createDirectoryGroup(container);
104         createImportChoicesGroup(container);
105         createImportOptionsGroup(container);
106         
107         Dialog.applyDialogFont(container);
108         initialize();
109         setControl(container);
110         PlatformUI.getWorkbench().getHelpSystem().setHelp(container, IHelpContextIds.PLUGIN_IMPORT_FIRST_PAGE);
111     }
112     
113     private void createImportChoicesGroup(Composite container) {
114         Group importChoices = new Group(container, SWT.NONE);
115         importChoices.setText(PDEUIMessages.ImportWizard_FirstPage_importGroup);
116         importChoices.setLayout(new GridLayout());
117         importChoices.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
118                 
119         scanButton = new Button(importChoices, SWT.RADIO);
120         scanButton.setText(PDEUIMessages.ImportWizard_FirstPage_scanAll);
121         
122         importButton = new Button(importChoices, SWT.RADIO);
123         importButton.setText(PDEUIMessages.ImportWizard_FirstPage_importPrereqs);
124         
125     }
126     
127     private void createImportOptionsGroup(Composite container) {
128         Group options = new Group(container, SWT.NONE);
129         options.setText(PDEUIMessages.ImportWizard_FirstPage_importAs);
130         options.setLayout(new GridLayout());
131         options.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
132         
133         binaryButton = new Button(options, SWT.RADIO);
134         binaryButton.setText(PDEUIMessages.ImportWizard_FirstPage_binary);
135         
136         binaryWithLinksButton = new Button(options, SWT.RADIO);
137         binaryWithLinksButton.setText(PDEUIMessages.ImportWizard_FirstPage_binaryLinks);
138         
139         sourceButton = new Button(options, SWT.RADIO);
140         sourceButton.setText(PDEUIMessages.ImportWizard_FirstPage_source);
141     }
142     
143     
144     private void initialize() {
145         IDialogSettings settings = getDialogSettings();
146         
147         ArrayList JavaDoc items = new ArrayList JavaDoc();
148         for (int i = 0; i < 6; i++) {
149             String JavaDoc curr = settings.get(SETTINGS_DROPLOCATION + String.valueOf(i));
150             if (curr != null && !items.contains(curr)) {
151                 items.add(curr);
152             }
153         }
154         dropLocation.setItems((String JavaDoc[]) items.toArray(new String JavaDoc[items.size()]));
155         
156         if (settings.getBoolean(SETTINGS_DOOTHER)) {
157             runtimeLocationButton.setSelection(false);
158             changeButton.setEnabled(false);
159             dropLocation.setText(items.get(0).toString());
160         } else {
161             runtimeLocationButton.setSelection(true);
162             otherLocationLabel.setEnabled(false);
163             dropLocation.setEnabled(false);
164             browseButton.setEnabled(false);
165             dropLocation.setText(getTargetHome());
166         }
167
168         
169         int importType = PluginImportOperation.IMPORT_BINARY;
170         try {
171             importType = settings.getInt(SETTINGS_IMPORTTYPE);
172         } catch (NumberFormatException JavaDoc e) {
173         }
174         if (importType == PluginImportOperation.IMPORT_BINARY) {
175             binaryButton.setSelection(true);
176         } else if (importType == PluginImportOperation.IMPORT_BINARY_WITH_LINKS) {
177             binaryWithLinksButton.setSelection(true);
178         } else {
179             sourceButton.setSelection(true);
180         }
181         
182         boolean scan = true;
183         if (settings.get(SETTINGS_SCAN_ALL) != null) {
184             scan = settings.getBoolean(SETTINGS_SCAN_ALL);
185         }
186         scanButton.setSelection(scan);
187         importButton.setSelection(!scan);
188         
189     }
190     
191     private void createDirectoryGroup(Composite parent) {
192         Group composite = new Group(parent, SWT.NONE);
193         composite.setText(PDEUIMessages.ImportWizard_FirstPage_importFrom);
194
195         GridLayout layout = new GridLayout();
196         layout.numColumns = 3;
197         composite.setLayout(layout);
198         composite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
199
200         runtimeLocationButton = new Button(composite, SWT.CHECK);
201         GridData gd = new GridData();
202         gd.horizontalSpan = 2;
203         runtimeLocationButton.setLayoutData(gd);
204         
205         runtimeLocationButton.setText(PDEUIMessages.ImportWizard_FirstPage_target);
206         runtimeLocationButton.addSelectionListener(new SelectionAdapter() {
207             public void widgetSelected(SelectionEvent e) {
208                 boolean selected = runtimeLocationButton.getSelection();
209                 if (selected) {
210                     dropLocation.setText(getTargetHome());
211                 }
212                 otherLocationLabel.setEnabled(!selected);
213                 dropLocation.setEnabled(!selected);
214                 browseButton.setEnabled(!selected);
215                 changeButton.setEnabled(selected);
216                 validateDropLocation();
217             }
218         });
219
220         changeButton = new Button(composite, SWT.PUSH);
221         changeButton.setText(PDEUIMessages.ImportWizard_FirstPage_goToTarget);
222         changeButton.addSelectionListener(new SelectionAdapter() {
223             public void widgetSelected(SelectionEvent e) {
224                 handleChangeTargetPlatform();
225             }
226         });
227         changeButton.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
228         SWTUtil.setButtonDimensionHint(changeButton);
229
230         otherLocationLabel = new Label(composite, SWT.NULL);
231         otherLocationLabel.setText(PDEUIMessages.ImportWizard_FirstPage_otherFolder);
232
233         dropLocation = new Combo(composite, SWT.DROP_DOWN);
234         dropLocation.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
235         dropLocation.addModifyListener(new ModifyListener() {
236             public void modifyText(ModifyEvent e) {
237                 validateDropLocation();
238             }
239         });
240
241         browseButton = new Button(composite, SWT.PUSH);
242         browseButton.setText(PDEUIMessages.ImportWizard_FirstPage_browse);
243         browseButton.addSelectionListener(new SelectionAdapter() {
244             public void widgetSelected(SelectionEvent e) {
245                 IPath chosen = chooseDropLocation();
246                 if (chosen != null)
247                     dropLocation.setText(chosen.toOSString());
248             }
249         });
250         browseButton.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
251         SWTUtil.setButtonDimensionHint(browseButton);
252
253         Label label = new Label(composite, SWT.NONE);
254         label.setText(PDEUIMessages.ImportWizard_FirstPage_source_label);
255         gd = new GridData();
256         gd.horizontalSpan = 2;
257         label.setLayoutData(gd);
258         
259         Button sourceLocations = new Button(composite, SWT.PUSH);
260         sourceLocations.setText(PDEUIMessages.ImportWizard_FirstPage_codeLocations);
261         sourceLocations.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END));
262         sourceLocations.addSelectionListener(new SelectionAdapter() {
263             public void widgetSelected(SelectionEvent e) {
264                 handleSourceLocations();
265             }
266         });
267         SWTUtil.setButtonDimensionHint(sourceLocations);
268         sourceLocations.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
269         
270         label = new Label(composite, SWT.WRAP);
271         label.setText(PDEUIMessages.ImportWizard_FirstPage_variables);
272         gd = new GridData();
273         gd.horizontalSpan = 2;
274         label.setLayoutData(gd);
275
276         Button envButton = new Button(composite, SWT.PUSH);
277         envButton.setText(PDEUIMessages.ImportWizard_FirstPage_env);
278         envButton.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END| GridData.FILL_HORIZONTAL));
279         envButton.addSelectionListener(new SelectionAdapter() {
280             public void widgetSelected(SelectionEvent e) {
281                 handleEnvChange();
282             }
283         });
284         SWTUtil.setButtonDimensionHint(envButton);
285         envButton.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
286         
287     }
288     
289     
290     private IPath chooseDropLocation() {
291         DirectoryDialog dialog = new DirectoryDialog(getShell());
292         dialog.setFilterPath(dropLocation.getText());
293         dialog.setText(PDEUIMessages.ImportWizard_messages_folder_title);
294         dialog.setMessage(PDEUIMessages.ImportWizard_messages_folder_message);
295         String JavaDoc res = dialog.open();
296         if (res != null) {
297             return new Path(res);
298         }
299         return null;
300     }
301     
302     private void handleChangeTargetPlatform() {
303         IPreferenceNode targetNode = new TargetPlatformPreferenceNode();
304         if (PDEPreferencesUtil.showPreferencePage(targetNode))
305             dropLocation.setText(TargetPlatform.getLocation());
306     }
307     
308     private void handleSourceLocations() {
309         PDEPreferencesUtil.showPreferencePage(new SourceCodeLocationsPreferenceNode());
310     }
311     
312     private void handleEnvChange() {
313         PDEPreferencesUtil.showPreferencePage(new TargetEnvironmentPreferenceNode());
314     }
315
316     private String JavaDoc getTargetHome() {
317         Preferences preferences = PDECore.getDefault().getPluginPreferences();
318         return preferences.getString(ICoreConstants.PLATFORM_PATH);
319     }
320     
321     public boolean getScanAllPlugins() {
322         return scanButton.getSelection();
323     }
324     
325     public int getImportType() {
326         if (binaryButton.getSelection()) {
327             return PluginImportOperation.IMPORT_BINARY;
328         }
329         
330         if (binaryWithLinksButton.getSelection()) {
331             return PluginImportOperation.IMPORT_BINARY_WITH_LINKS;
332         }
333         
334         return PluginImportOperation.IMPORT_WITH_SOURCE;
335     }
336     
337     public String JavaDoc getDropLocation() {
338         return runtimeLocationButton.getSelection()
339             ? TARGET_PLATFORM
340             : dropLocation.getText().trim();
341     }
342     
343     public void storeSettings() {
344         IDialogSettings settings = getDialogSettings();
345         boolean other = !runtimeLocationButton.getSelection();
346         if (dropLocation.getText().length() > 0 && other) {
347             settings.put(
348                 SETTINGS_DROPLOCATION + String.valueOf(0),
349                 dropLocation.getText().trim());
350             String JavaDoc[] items = dropLocation.getItems();
351             int nEntries = Math.min(items.length, 5);
352             for (int i = 0; i < nEntries; i++) {
353                 settings.put(SETTINGS_DROPLOCATION + String.valueOf(i + 1), items[i]);
354             }
355         }
356         settings.put(SETTINGS_DOOTHER, other);
357         settings.put(SETTINGS_IMPORTTYPE, getImportType());
358         settings.put(SETTINGS_SCAN_ALL, getScanAllPlugins());
359     }
360     
361     public void dispose() {
362         PDEPlugin.getDefault().getLabelProvider().disconnect(this);
363     }
364     
365     private void validateDropLocation() {
366         if (!runtimeLocationButton.getSelection()) {
367             IPath curr = new Path(dropLocation.getText());
368             if (curr.segmentCount() == 0 && curr.getDevice() == null) {
369                 setErrorMessage(PDEUIMessages.ImportWizard_errors_locationMissing);
370                 setPageComplete(false);
371                 return;
372             }
373             if (!Path.ROOT.isValidPath(dropLocation.getText())) {
374                 setErrorMessage(PDEUIMessages.ImportWizard_errors_buildFolderInvalid);
375                 setPageComplete(false);
376                 return;
377             }
378
379             if (!curr.toFile().isDirectory()) {
380                 setErrorMessage(PDEUIMessages.ImportWizard_errors_buildFolderMissing);
381                 setPageComplete(false);
382                 return;
383             }
384             if (!curr.equals(new Path(getTargetHome()))) {
385                 setErrorMessage(null);
386                 setMessage(PDEUIMessages.ImportWizard_FirstPage_warning, IMessageProvider.WARNING);
387                 setPageComplete(true);
388                 return;
389             }
390         }
391         setErrorMessage(null);
392         setPageComplete(true);
393         setMessage(PDEUIMessages.ImportWizard_FirstPage_desc);
394     }
395     
396     private void resolveTargetPlatform() {
397         IRunnableWithProgress op = new IRunnableWithProgress() {
398             public void run(IProgressMonitor monitor) {
399                 models = PluginRegistry.getExternalModels();
400                 monitor.done();
401             }
402         };
403         try {
404             getContainer().run(true, false, op);
405         } catch (InvocationTargetException JavaDoc e) {
406             PDEPlugin.log(e);
407         } catch (InterruptedException JavaDoc e) {
408         }
409     }
410     
411     private void resolveArbitraryLocation(final String JavaDoc location) {
412         IRunnableWithProgress op = new IRunnableWithProgress() {
413             public void run(IProgressMonitor monitor) {
414                 File JavaDoc[] files = new File JavaDoc[2];
415                 files[0] = new File JavaDoc(location);
416                 files[1] = new File JavaDoc(location, "plugins"); //$NON-NLS-1$
417
URL JavaDoc[] urls = PluginPathFinder.scanLocations(files);
418                 URL JavaDoc[] all = new URL JavaDoc[urls.length + 1];
419                 try {
420                     all[0] = new URL JavaDoc("file:" + files[0].getAbsolutePath()); //$NON-NLS-1$
421
System.arraycopy(urls, 0, all, 1, urls.length);
422                 } catch (MalformedURLException JavaDoc e) {
423                     all = urls;
424                 }
425                 PDEState state = new PDEState(all, false, monitor);
426                 models = state.getTargetModels();
427                 canceled = monitor.isCanceled();
428                 monitor.done();
429             }
430         };
431         try {
432             getContainer().run(true, true, op);
433         } catch (InvocationTargetException JavaDoc e) {
434             PDEPlugin.log(e);
435         } catch (InterruptedException JavaDoc e) {
436         }
437     }
438         
439
440     public IPluginModelBase[] getModels() {
441         String JavaDoc dropLocation = getDropLocation();
442         if (dropLocation.equals(TARGET_PLATFORM)) {
443             resolveTargetPlatform();
444         } else {
445             resolveArbitraryLocation(dropLocation);
446         }
447         return models;
448     }
449     
450     public boolean isCurrentPage() {
451         return super.isCurrentPage();
452     }
453     
454     public boolean isRefreshNeeded() {
455         if (canceled) {
456             canceled = false;
457             return true;
458         }
459         return false;
460     }
461 }
462
Popular Tags