001: /*******************************************************************************
002: * Copyright (c) 2003, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.ui.wizards.imports;
011:
012: import java.io.File;
013: import java.lang.reflect.InvocationTargetException;
014: import java.net.MalformedURLException;
015: import java.net.URL;
016: import java.util.ArrayList;
017:
018: import org.eclipse.core.runtime.IPath;
019: import org.eclipse.core.runtime.IProgressMonitor;
020: import org.eclipse.core.runtime.Path;
021: import org.eclipse.core.runtime.Preferences;
022: import org.eclipse.jface.dialogs.Dialog;
023: import org.eclipse.jface.dialogs.IDialogSettings;
024: import org.eclipse.jface.dialogs.IMessageProvider;
025: import org.eclipse.jface.operation.IRunnableWithProgress;
026: import org.eclipse.jface.preference.IPreferenceNode;
027: import org.eclipse.jface.wizard.WizardPage;
028: import org.eclipse.pde.core.plugin.IPluginModelBase;
029: import org.eclipse.pde.core.plugin.PluginRegistry;
030: import org.eclipse.pde.core.plugin.TargetPlatform;
031: import org.eclipse.pde.internal.core.ICoreConstants;
032: import org.eclipse.pde.internal.core.PDECore;
033: import org.eclipse.pde.internal.core.PDEState;
034: import org.eclipse.pde.internal.core.PluginPathFinder;
035: import org.eclipse.pde.internal.ui.IHelpContextIds;
036: import org.eclipse.pde.internal.ui.PDEPlugin;
037: import org.eclipse.pde.internal.ui.PDEUIMessages;
038: import org.eclipse.pde.internal.ui.preferences.PDEPreferencesUtil;
039: import org.eclipse.pde.internal.ui.preferences.SourceCodeLocationsPreferenceNode;
040: import org.eclipse.pde.internal.ui.preferences.TargetEnvironmentPreferenceNode;
041: import org.eclipse.pde.internal.ui.preferences.TargetPlatformPreferenceNode;
042: import org.eclipse.pde.internal.ui.util.SWTUtil;
043: import org.eclipse.swt.SWT;
044: import org.eclipse.swt.events.ModifyEvent;
045: import org.eclipse.swt.events.ModifyListener;
046: import org.eclipse.swt.events.SelectionAdapter;
047: import org.eclipse.swt.events.SelectionEvent;
048: import org.eclipse.swt.layout.GridData;
049: import org.eclipse.swt.layout.GridLayout;
050: import org.eclipse.swt.widgets.Button;
051: import org.eclipse.swt.widgets.Combo;
052: import org.eclipse.swt.widgets.Composite;
053: import org.eclipse.swt.widgets.DirectoryDialog;
054: import org.eclipse.swt.widgets.Group;
055: import org.eclipse.swt.widgets.Label;
056: import org.eclipse.ui.PlatformUI;
057:
058: public class PluginImportWizardFirstPage extends WizardPage {
059:
060: private static String SETTINGS_IMPORTTYPE = "importType"; //$NON-NLS-1$
061: private static String SETTINGS_DOOTHER = "doother"; //$NON-NLS-1$
062: private static String SETTINGS_DROPLOCATION = "droplocation"; //$NON-NLS-1$
063: private static String SETTINGS_SCAN_ALL = "scanAll"; //$NON-NLS-1$
064:
065: private Button runtimeLocationButton;
066: private Button browseButton;
067: private Label otherLocationLabel;
068: private Combo dropLocation;
069: private Button changeButton;
070:
071: private Button importButton;
072: private Button scanButton;
073:
074: private Button binaryButton;
075: private Button binaryWithLinksButton;
076: private Button sourceButton;
077:
078: //private String currentLocation;
079: public static String TARGET_PLATFORM = "targetPlatform"; //$NON-NLS-1$
080: private IPluginModelBase[] models = new IPluginModelBase[0];
081: private boolean canceled = false;
082:
083: public PluginImportWizardFirstPage(String name) {
084: super (name);
085: setTitle(PDEUIMessages.ImportWizard_FirstPage_title);
086: setMessage(PDEUIMessages.ImportWizard_FirstPage_desc);
087: PDEPlugin.getDefault().getLabelProvider().connect(this );
088: }
089:
090: public void createControl(Composite parent) {
091: Composite container = new Composite(parent, SWT.NONE);
092: GridLayout layout = new GridLayout();
093: layout.verticalSpacing = 15;
094: container.setLayout(layout);
095:
096: createDirectoryGroup(container);
097: createImportChoicesGroup(container);
098: createImportOptionsGroup(container);
099:
100: Dialog.applyDialogFont(container);
101: initialize();
102: setControl(container);
103: PlatformUI.getWorkbench().getHelpSystem().setHelp(container,
104: IHelpContextIds.PLUGIN_IMPORT_FIRST_PAGE);
105: }
106:
107: private void createImportChoicesGroup(Composite container) {
108: Group importChoices = new Group(container, SWT.NONE);
109: importChoices
110: .setText(PDEUIMessages.ImportWizard_FirstPage_importGroup);
111: importChoices.setLayout(new GridLayout());
112: importChoices.setLayoutData(new GridData(
113: GridData.FILL_HORIZONTAL));
114:
115: scanButton = new Button(importChoices, SWT.RADIO);
116: scanButton
117: .setText(PDEUIMessages.ImportWizard_FirstPage_scanAll);
118:
119: importButton = new Button(importChoices, SWT.RADIO);
120: importButton
121: .setText(PDEUIMessages.ImportWizard_FirstPage_importPrereqs);
122:
123: }
124:
125: private void createImportOptionsGroup(Composite container) {
126: Group options = new Group(container, SWT.NONE);
127: options.setText(PDEUIMessages.ImportWizard_FirstPage_importAs);
128: options.setLayout(new GridLayout());
129: options.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
130:
131: binaryButton = new Button(options, SWT.RADIO);
132: binaryButton
133: .setText(PDEUIMessages.ImportWizard_FirstPage_binary);
134:
135: binaryWithLinksButton = new Button(options, SWT.RADIO);
136: binaryWithLinksButton
137: .setText(PDEUIMessages.ImportWizard_FirstPage_binaryLinks);
138:
139: sourceButton = new Button(options, SWT.RADIO);
140: sourceButton
141: .setText(PDEUIMessages.ImportWizard_FirstPage_source);
142: }
143:
144: private void initialize() {
145: IDialogSettings settings = getDialogSettings();
146:
147: ArrayList items = new ArrayList();
148: for (int i = 0; i < 6; i++) {
149: String curr = settings.get(SETTINGS_DROPLOCATION
150: + String.valueOf(i));
151: if (curr != null && !items.contains(curr)) {
152: items.add(curr);
153: }
154: }
155: dropLocation.setItems((String[]) items.toArray(new String[items
156: .size()]));
157:
158: if (settings.getBoolean(SETTINGS_DOOTHER)) {
159: runtimeLocationButton.setSelection(false);
160: changeButton.setEnabled(false);
161: dropLocation.setText(items.get(0).toString());
162: } else {
163: runtimeLocationButton.setSelection(true);
164: otherLocationLabel.setEnabled(false);
165: dropLocation.setEnabled(false);
166: browseButton.setEnabled(false);
167: dropLocation.setText(getTargetHome());
168: }
169:
170: int importType = PluginImportOperation.IMPORT_BINARY;
171: try {
172: importType = settings.getInt(SETTINGS_IMPORTTYPE);
173: } catch (NumberFormatException e) {
174: }
175: if (importType == PluginImportOperation.IMPORT_BINARY) {
176: binaryButton.setSelection(true);
177: } else if (importType == PluginImportOperation.IMPORT_BINARY_WITH_LINKS) {
178: binaryWithLinksButton.setSelection(true);
179: } else {
180: sourceButton.setSelection(true);
181: }
182:
183: boolean scan = true;
184: if (settings.get(SETTINGS_SCAN_ALL) != null) {
185: scan = settings.getBoolean(SETTINGS_SCAN_ALL);
186: }
187: scanButton.setSelection(scan);
188: importButton.setSelection(!scan);
189:
190: }
191:
192: private void createDirectoryGroup(Composite parent) {
193: Group composite = new Group(parent, SWT.NONE);
194: composite
195: .setText(PDEUIMessages.ImportWizard_FirstPage_importFrom);
196:
197: GridLayout layout = new GridLayout();
198: layout.numColumns = 3;
199: composite.setLayout(layout);
200: composite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
201:
202: runtimeLocationButton = new Button(composite, SWT.CHECK);
203: GridData gd = new GridData();
204: gd.horizontalSpan = 2;
205: runtimeLocationButton.setLayoutData(gd);
206:
207: runtimeLocationButton
208: .setText(PDEUIMessages.ImportWizard_FirstPage_target);
209: runtimeLocationButton
210: .addSelectionListener(new SelectionAdapter() {
211: public void widgetSelected(SelectionEvent e) {
212: boolean selected = runtimeLocationButton
213: .getSelection();
214: if (selected) {
215: dropLocation.setText(getTargetHome());
216: }
217: otherLocationLabel.setEnabled(!selected);
218: dropLocation.setEnabled(!selected);
219: browseButton.setEnabled(!selected);
220: changeButton.setEnabled(selected);
221: validateDropLocation();
222: }
223: });
224:
225: changeButton = new Button(composite, SWT.PUSH);
226: changeButton
227: .setText(PDEUIMessages.ImportWizard_FirstPage_goToTarget);
228: changeButton.addSelectionListener(new SelectionAdapter() {
229: public void widgetSelected(SelectionEvent e) {
230: handleChangeTargetPlatform();
231: }
232: });
233: changeButton.setLayoutData(new GridData(
234: GridData.HORIZONTAL_ALIGN_FILL));
235: SWTUtil.setButtonDimensionHint(changeButton);
236:
237: otherLocationLabel = new Label(composite, SWT.NULL);
238: otherLocationLabel
239: .setText(PDEUIMessages.ImportWizard_FirstPage_otherFolder);
240:
241: dropLocation = new Combo(composite, SWT.DROP_DOWN);
242: dropLocation.setLayoutData(new GridData(
243: GridData.FILL_HORIZONTAL));
244: dropLocation.addModifyListener(new ModifyListener() {
245: public void modifyText(ModifyEvent e) {
246: validateDropLocation();
247: }
248: });
249:
250: browseButton = new Button(composite, SWT.PUSH);
251: browseButton
252: .setText(PDEUIMessages.ImportWizard_FirstPage_browse);
253: browseButton.addSelectionListener(new SelectionAdapter() {
254: public void widgetSelected(SelectionEvent e) {
255: IPath chosen = chooseDropLocation();
256: if (chosen != null)
257: dropLocation.setText(chosen.toOSString());
258: }
259: });
260: browseButton.setLayoutData(new GridData(
261: GridData.HORIZONTAL_ALIGN_FILL));
262: SWTUtil.setButtonDimensionHint(browseButton);
263:
264: Label label = new Label(composite, SWT.NONE);
265: label
266: .setText(PDEUIMessages.ImportWizard_FirstPage_source_label);
267: gd = new GridData();
268: gd.horizontalSpan = 2;
269: label.setLayoutData(gd);
270:
271: Button sourceLocations = new Button(composite, SWT.PUSH);
272: sourceLocations
273: .setText(PDEUIMessages.ImportWizard_FirstPage_codeLocations);
274: sourceLocations.setLayoutData(new GridData(
275: GridData.HORIZONTAL_ALIGN_END));
276: sourceLocations.addSelectionListener(new SelectionAdapter() {
277: public void widgetSelected(SelectionEvent e) {
278: handleSourceLocations();
279: }
280: });
281: SWTUtil.setButtonDimensionHint(sourceLocations);
282: sourceLocations.setLayoutData(new GridData(
283: GridData.HORIZONTAL_ALIGN_FILL));
284:
285: label = new Label(composite, SWT.WRAP);
286: label.setText(PDEUIMessages.ImportWizard_FirstPage_variables);
287: gd = new GridData();
288: gd.horizontalSpan = 2;
289: label.setLayoutData(gd);
290:
291: Button envButton = new Button(composite, SWT.PUSH);
292: envButton.setText(PDEUIMessages.ImportWizard_FirstPage_env);
293: envButton.setLayoutData(new GridData(
294: GridData.HORIZONTAL_ALIGN_END
295: | GridData.FILL_HORIZONTAL));
296: envButton.addSelectionListener(new SelectionAdapter() {
297: public void widgetSelected(SelectionEvent e) {
298: handleEnvChange();
299: }
300: });
301: SWTUtil.setButtonDimensionHint(envButton);
302: envButton.setLayoutData(new GridData(
303: GridData.HORIZONTAL_ALIGN_FILL));
304:
305: }
306:
307: private IPath chooseDropLocation() {
308: DirectoryDialog dialog = new DirectoryDialog(getShell());
309: dialog.setFilterPath(dropLocation.getText());
310: dialog
311: .setText(PDEUIMessages.ImportWizard_messages_folder_title);
312: dialog
313: .setMessage(PDEUIMessages.ImportWizard_messages_folder_message);
314: String res = dialog.open();
315: if (res != null) {
316: return new Path(res);
317: }
318: return null;
319: }
320:
321: private void handleChangeTargetPlatform() {
322: IPreferenceNode targetNode = new TargetPlatformPreferenceNode();
323: if (PDEPreferencesUtil.showPreferencePage(targetNode))
324: dropLocation.setText(TargetPlatform.getLocation());
325: }
326:
327: private void handleSourceLocations() {
328: PDEPreferencesUtil
329: .showPreferencePage(new SourceCodeLocationsPreferenceNode());
330: }
331:
332: private void handleEnvChange() {
333: PDEPreferencesUtil
334: .showPreferencePage(new TargetEnvironmentPreferenceNode());
335: }
336:
337: private String getTargetHome() {
338: Preferences preferences = PDECore.getDefault()
339: .getPluginPreferences();
340: return preferences.getString(ICoreConstants.PLATFORM_PATH);
341: }
342:
343: public boolean getScanAllPlugins() {
344: return scanButton.getSelection();
345: }
346:
347: public int getImportType() {
348: if (binaryButton.getSelection()) {
349: return PluginImportOperation.IMPORT_BINARY;
350: }
351:
352: if (binaryWithLinksButton.getSelection()) {
353: return PluginImportOperation.IMPORT_BINARY_WITH_LINKS;
354: }
355:
356: return PluginImportOperation.IMPORT_WITH_SOURCE;
357: }
358:
359: public String getDropLocation() {
360: return runtimeLocationButton.getSelection() ? TARGET_PLATFORM
361: : dropLocation.getText().trim();
362: }
363:
364: public void storeSettings() {
365: IDialogSettings settings = getDialogSettings();
366: boolean other = !runtimeLocationButton.getSelection();
367: if (dropLocation.getText().length() > 0 && other) {
368: settings.put(SETTINGS_DROPLOCATION + String.valueOf(0),
369: dropLocation.getText().trim());
370: String[] items = dropLocation.getItems();
371: int nEntries = Math.min(items.length, 5);
372: for (int i = 0; i < nEntries; i++) {
373: settings.put(SETTINGS_DROPLOCATION
374: + String.valueOf(i + 1), items[i]);
375: }
376: }
377: settings.put(SETTINGS_DOOTHER, other);
378: settings.put(SETTINGS_IMPORTTYPE, getImportType());
379: settings.put(SETTINGS_SCAN_ALL, getScanAllPlugins());
380: }
381:
382: public void dispose() {
383: PDEPlugin.getDefault().getLabelProvider().disconnect(this );
384: }
385:
386: private void validateDropLocation() {
387: if (!runtimeLocationButton.getSelection()) {
388: IPath curr = new Path(dropLocation.getText());
389: if (curr.segmentCount() == 0 && curr.getDevice() == null) {
390: setErrorMessage(PDEUIMessages.ImportWizard_errors_locationMissing);
391: setPageComplete(false);
392: return;
393: }
394: if (!Path.ROOT.isValidPath(dropLocation.getText())) {
395: setErrorMessage(PDEUIMessages.ImportWizard_errors_buildFolderInvalid);
396: setPageComplete(false);
397: return;
398: }
399:
400: if (!curr.toFile().isDirectory()) {
401: setErrorMessage(PDEUIMessages.ImportWizard_errors_buildFolderMissing);
402: setPageComplete(false);
403: return;
404: }
405: if (!curr.equals(new Path(getTargetHome()))) {
406: setErrorMessage(null);
407: setMessage(
408: PDEUIMessages.ImportWizard_FirstPage_warning,
409: IMessageProvider.WARNING);
410: setPageComplete(true);
411: return;
412: }
413: }
414: setErrorMessage(null);
415: setPageComplete(true);
416: setMessage(PDEUIMessages.ImportWizard_FirstPage_desc);
417: }
418:
419: private void resolveTargetPlatform() {
420: IRunnableWithProgress op = new IRunnableWithProgress() {
421: public void run(IProgressMonitor monitor) {
422: models = PluginRegistry.getExternalModels();
423: monitor.done();
424: }
425: };
426: try {
427: getContainer().run(true, false, op);
428: } catch (InvocationTargetException e) {
429: PDEPlugin.log(e);
430: } catch (InterruptedException e) {
431: }
432: }
433:
434: private void resolveArbitraryLocation(final String location) {
435: IRunnableWithProgress op = new IRunnableWithProgress() {
436: public void run(IProgressMonitor monitor) {
437: File[] files = new File[2];
438: files[0] = new File(location);
439: files[1] = new File(location, "plugins"); //$NON-NLS-1$
440: URL[] urls = PluginPathFinder.scanLocations(files);
441: URL[] all = new URL[urls.length + 1];
442: try {
443: all[0] = new URL(
444: "file:" + files[0].getAbsolutePath()); //$NON-NLS-1$
445: System.arraycopy(urls, 0, all, 1, urls.length);
446: } catch (MalformedURLException e) {
447: all = urls;
448: }
449: PDEState state = new PDEState(all, false, monitor);
450: models = state.getTargetModels();
451: canceled = monitor.isCanceled();
452: monitor.done();
453: }
454: };
455: try {
456: getContainer().run(true, true, op);
457: } catch (InvocationTargetException e) {
458: PDEPlugin.log(e);
459: } catch (InterruptedException e) {
460: }
461: }
462:
463: public IPluginModelBase[] getModels() {
464: String dropLocation = getDropLocation();
465: if (dropLocation.equals(TARGET_PLATFORM)) {
466: resolveTargetPlatform();
467: } else {
468: resolveArbitraryLocation(dropLocation);
469: }
470: return models;
471: }
472:
473: public boolean isCurrentPage() {
474: return super .isCurrentPage();
475: }
476:
477: public boolean isRefreshNeeded() {
478: if (canceled) {
479: canceled = false;
480: return true;
481: }
482: return false;
483: }
484: }
|