Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 02c9906a5d9f7eab71e219d0b2ffffa6addd63ee (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
/*******************************************************************************
 * Copyright (c) 2003, 2006 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
/*
 * Created on May 30, 2003
 *
 * To change this generated comment go to 
 * Window>Preferences>Java>Code Generation>Code Template
 */
package org.eclipse.pde.internal.ui.wizards.imports;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Preferences;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogSettings;
import org.eclipse.jface.dialogs.IMessageProvider;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.preference.IPreferenceNode;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.pde.core.plugin.IPluginModelBase;
import org.eclipse.pde.internal.core.ExternalModelManager;
import org.eclipse.pde.internal.core.ICoreConstants;
import org.eclipse.pde.internal.core.PDECore;
import org.eclipse.pde.internal.core.PDEState;
import org.eclipse.pde.internal.core.PluginPathFinder;
import org.eclipse.pde.internal.ui.IHelpContextIds;
import org.eclipse.pde.internal.ui.PDEPlugin;
import org.eclipse.pde.internal.ui.PDEUIMessages;
import org.eclipse.pde.internal.ui.preferences.PDEPreferencesUtil;
import org.eclipse.pde.internal.ui.preferences.SourceCodeLocationsPreferenceNode;
import org.eclipse.pde.internal.ui.preferences.TargetEnvironmentPreferenceNode;
import org.eclipse.pde.internal.ui.preferences.TargetPlatformPreferenceNode;
import org.eclipse.pde.internal.ui.util.SWTUtil;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.PlatformUI;

public class PluginImportWizardFirstPage extends WizardPage {
	
	private static String SETTINGS_IMPORTTYPE = "importType"; //$NON-NLS-1$
	private static String SETTINGS_DOOTHER = "doother"; //$NON-NLS-1$
	private static String SETTINGS_DROPLOCATION = "droplocation"; //$NON-NLS-1$
	private static String SETTINGS_SCAN_ALL = "scanAll"; //$NON-NLS-1$
	
	private Button runtimeLocationButton;
	private Button browseButton;
	private Label otherLocationLabel;
	private Combo dropLocation;
	private Button changeButton;
	
	private Button importButton;
	private Button scanButton;

	private Button binaryButton;
	private Button binaryWithLinksButton;
	private Button sourceButton;
	
	//private String currentLocation;
	public static String TARGET_PLATFORM = "targetPlatform"; //$NON-NLS-1$
	private IPluginModelBase[] models = new IPluginModelBase[0];
	
	public PluginImportWizardFirstPage(String name) {
		super(name);
		setTitle(PDEUIMessages.ImportWizard_FirstPage_title); 
		setMessage(PDEUIMessages.ImportWizard_FirstPage_desc); 
		PDEPlugin.getDefault().getLabelProvider().connect(this);
	}

	public void createControl(Composite parent) {
		Composite container = new Composite(parent, SWT.NONE);
		GridLayout layout = new GridLayout();
		layout.verticalSpacing = 15;
		container.setLayout(layout);
		
		createDirectoryGroup(container);
		createImportChoicesGroup(container);
		createImportOptionsGroup(container);
		
		Dialog.applyDialogFont(container);
		initialize();
		setControl(container);
		PlatformUI.getWorkbench().getHelpSystem().setHelp(container, IHelpContextIds.PLUGIN_IMPORT_FIRST_PAGE);		
	}
	
	private void createImportChoicesGroup(Composite container) {
		Group importChoices = new Group(container, SWT.NONE);
		importChoices.setText(PDEUIMessages.ImportWizard_FirstPage_importGroup); 
		importChoices.setLayout(new GridLayout());
		importChoices.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
				
		scanButton = new Button(importChoices, SWT.RADIO);
		scanButton.setText(PDEUIMessages.ImportWizard_FirstPage_scanAll);		 
		
		importButton = new Button(importChoices, SWT.RADIO);
		importButton.setText(PDEUIMessages.ImportWizard_FirstPage_importPrereqs); 
		
	}
	
	private void createImportOptionsGroup(Composite container) {
		Group options = new Group(container, SWT.NONE);
		options.setText(PDEUIMessages.ImportWizard_FirstPage_importAs); 
		options.setLayout(new GridLayout());
		options.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		
		binaryButton = new Button(options, SWT.RADIO);
		binaryButton.setText(PDEUIMessages.ImportWizard_FirstPage_binary); 
		
		binaryWithLinksButton = new Button(options, SWT.RADIO);
		binaryWithLinksButton.setText(PDEUIMessages.ImportWizard_FirstPage_binaryLinks); 
		
		sourceButton = new Button(options, SWT.RADIO);
		sourceButton.setText(PDEUIMessages.ImportWizard_FirstPage_source); 
	}
	
	
	private void initialize() {
		IDialogSettings settings = getDialogSettings();
		
		ArrayList items = new ArrayList();
		for (int i = 0; i < 6; i++) {
			String curr = settings.get(SETTINGS_DROPLOCATION + String.valueOf(i));
			if (curr != null && !items.contains(curr)) {
				items.add(curr);
			}
		}
		dropLocation.setItems((String[]) items.toArray(new String[items.size()]));
		
		if (settings.getBoolean(SETTINGS_DOOTHER)) {
			runtimeLocationButton.setSelection(false);
			changeButton.setEnabled(false);
			dropLocation.setText(items.get(0).toString());		
		} else {
			runtimeLocationButton.setSelection(true);
			otherLocationLabel.setEnabled(false);
			dropLocation.setEnabled(false);
			browseButton.setEnabled(false);
			dropLocation.setText(getTargetHome());
		}

		
		int importType = PluginImportOperation.IMPORT_BINARY;
		try {
			importType = settings.getInt(SETTINGS_IMPORTTYPE);
		} catch (NumberFormatException e) {
		}
		if (importType == PluginImportOperation.IMPORT_BINARY) {
			binaryButton.setSelection(true);
		} else if (importType == PluginImportOperation.IMPORT_BINARY_WITH_LINKS) {
			binaryWithLinksButton.setSelection(true);
		} else {
			sourceButton.setSelection(true);
		}
		
		boolean scan = true;
		if (settings.get(SETTINGS_SCAN_ALL) != null) {
			scan = settings.getBoolean(SETTINGS_SCAN_ALL);
		}
		scanButton.setSelection(scan);
		importButton.setSelection(!scan);
		
	}
	
	private void createDirectoryGroup(Composite parent) {
		Group composite = new Group(parent, SWT.NONE);
		composite.setText(PDEUIMessages.ImportWizard_FirstPage_importFrom); 

		GridLayout layout = new GridLayout();
		layout.numColumns = 3;
		composite.setLayout(layout);
		composite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

		runtimeLocationButton = new Button(composite, SWT.CHECK);
		GridData gd = new GridData();
		gd.horizontalSpan = 2;
		runtimeLocationButton.setLayoutData(gd);
		
		runtimeLocationButton.setText(PDEUIMessages.ImportWizard_FirstPage_target); 
		runtimeLocationButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				boolean selected = runtimeLocationButton.getSelection();
				if (selected) {
					dropLocation.setText(getTargetHome());
				}
				otherLocationLabel.setEnabled(!selected);
				dropLocation.setEnabled(!selected);
				browseButton.setEnabled(!selected);
				changeButton.setEnabled(selected);
				validateDropLocation();
			}
		});

		changeButton = new Button(composite, SWT.PUSH);
		changeButton.setText(PDEUIMessages.ImportWizard_FirstPage_goToTarget); 
		changeButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				handleChangeTargetPlatform();
			}
		});
		changeButton.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
		SWTUtil.setButtonDimensionHint(changeButton);

		otherLocationLabel = new Label(composite, SWT.NULL);
		otherLocationLabel.setText(PDEUIMessages.ImportWizard_FirstPage_otherFolder); 

		dropLocation = new Combo(composite, SWT.DROP_DOWN);
		dropLocation.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		dropLocation.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				validateDropLocation();
			}
		});

		browseButton = new Button(composite, SWT.PUSH);
		browseButton.setText(PDEUIMessages.ImportWizard_FirstPage_browse); 
		browseButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				IPath chosen = chooseDropLocation();
				if (chosen != null)
					dropLocation.setText(chosen.toOSString());
			}
		});
		browseButton.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
		SWTUtil.setButtonDimensionHint(browseButton);

		Label label = new Label(composite, SWT.NONE);
		label.setText(PDEUIMessages.ImportWizard_FirstPage_source_label); 
		gd = new GridData();
		gd.horizontalSpan = 2;
		label.setLayoutData(gd);
		
		Button sourceLocations = new Button(composite, SWT.PUSH);
		sourceLocations.setText(PDEUIMessages.ImportWizard_FirstPage_codeLocations); 
		sourceLocations.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END));
		sourceLocations.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				handleSourceLocations();
			}
		});
		SWTUtil.setButtonDimensionHint(sourceLocations);
		sourceLocations.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
		
		label = new Label(composite, SWT.WRAP);
		label.setText(PDEUIMessages.ImportWizard_FirstPage_variables); 
		gd = new GridData();
		gd.horizontalSpan = 2;
		label.setLayoutData(gd);

		Button envButton = new Button(composite, SWT.PUSH);
		envButton.setText(PDEUIMessages.ImportWizard_FirstPage_env); 
		envButton.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END| GridData.FILL_HORIZONTAL));
		envButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				handleEnvChange();
			}
		});
		SWTUtil.setButtonDimensionHint(envButton);
		envButton.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
		
	}
	
	
	private IPath chooseDropLocation() {
		DirectoryDialog dialog = new DirectoryDialog(getShell());
		dialog.setFilterPath(dropLocation.getText());
		dialog.setText(PDEUIMessages.ImportWizard_messages_folder_title); 
		dialog.setMessage(PDEUIMessages.ImportWizard_messages_folder_message); 
		String res = dialog.open();
		if (res != null) {
			return new Path(res);
		}
		return null;
	}
	
	private void handleChangeTargetPlatform() {
		IPreferenceNode targetNode = new TargetPlatformPreferenceNode();
		if (PDEPreferencesUtil.showPreferencePage(targetNode))
			dropLocation.setText(ExternalModelManager.getEclipseHome().toOSString());
	}
	
	private void handleSourceLocations() {
		PDEPreferencesUtil.showPreferencePage(new SourceCodeLocationsPreferenceNode());
	}
	
	private void handleEnvChange() {
		PDEPreferencesUtil.showPreferencePage(new TargetEnvironmentPreferenceNode());
	}

	private String getTargetHome() {
		Preferences preferences = PDECore.getDefault().getPluginPreferences();
		return preferences.getString(ICoreConstants.PLATFORM_PATH);
	}
	
	public boolean getScanAllPlugins() {
		return scanButton.getSelection();
	}
	
	public int getImportType() {
		if (binaryButton.getSelection()) {
			return PluginImportOperation.IMPORT_BINARY;
		}
		
		if (binaryWithLinksButton.getSelection()) {
			return PluginImportOperation.IMPORT_BINARY_WITH_LINKS;
		}
		
		return PluginImportOperation.IMPORT_WITH_SOURCE;
	}
	
	public String getDropLocation() {
		return runtimeLocationButton.getSelection()
			? TARGET_PLATFORM
			: dropLocation.getText().trim();
	}
	
	public void storeSettings() {
		IDialogSettings settings = getDialogSettings();
		boolean other = !runtimeLocationButton.getSelection();
		if (dropLocation.getText().length() > 0 && other) {
			settings.put(
				SETTINGS_DROPLOCATION + String.valueOf(0),
				dropLocation.getText().trim());
			String[] items = dropLocation.getItems();
			int nEntries = Math.min(items.length, 5);
			for (int i = 0; i < nEntries; i++) {
				settings.put(SETTINGS_DROPLOCATION + String.valueOf(i + 1), items[i]);
			}
		}
		settings.put(SETTINGS_DOOTHER, other);
		settings.put(SETTINGS_IMPORTTYPE, getImportType());
		settings.put(SETTINGS_SCAN_ALL, getScanAllPlugins());
	}
	
	public void dispose() {
		PDEPlugin.getDefault().getLabelProvider().disconnect(this);
	}
	
	private void validateDropLocation() {
		if (!runtimeLocationButton.getSelection()) {
			IPath curr = new Path(dropLocation.getText());
			if (curr.segmentCount() == 0 && curr.getDevice() == null) {
				setErrorMessage(PDEUIMessages.ImportWizard_errors_locationMissing); 
				setPageComplete(false);
				return;
			}
			if (!Path.ROOT.isValidPath(dropLocation.getText())) {
				setErrorMessage(PDEUIMessages.ImportWizard_errors_buildFolderInvalid); 
				setPageComplete(false);
				return;
			}

			if (!curr.toFile().isDirectory()) {
				setErrorMessage(PDEUIMessages.ImportWizard_errors_buildFolderMissing); 
				setPageComplete(false);
				return;
			}
			if (!curr.equals(new Path(getTargetHome()))) {
				setErrorMessage(null);
				setMessage(PDEUIMessages.ImportWizard_FirstPage_warning, IMessageProvider.WARNING); 
				setPageComplete(true);
				return;
			}
		}
		setErrorMessage(null);
		setPageComplete(true);
		setMessage(PDEUIMessages.ImportWizard_FirstPage_desc); 
	}
	
	private void resolveTargetPlatform() {
		IRunnableWithProgress op = new IRunnableWithProgress() {
			public void run(IProgressMonitor monitor) {
				models = PDECore.getDefault().getModelManager().getExternalModels();
				monitor.done();
			}
		};
		try {
			getContainer().run(true, false, op);
		} catch (InvocationTargetException e) {
			PDEPlugin.log(e);
		} catch (InterruptedException e) {
		}
	}
	
	private void resolveArbitraryLocation(final String location) {
		IRunnableWithProgress op = new IRunnableWithProgress() {
			public void run(IProgressMonitor monitor) {
				File[] files = new File[2];
				files[0] = new File(location);
				files[1] = new File(location, "plugins"); //$NON-NLS-1$
				URL[] urls = PluginPathFinder.scanLocations(files);
				URL[] all = new URL[urls.length + 1];
				try {
					all[0] = new URL("file:" + files[0].getAbsolutePath()); //$NON-NLS-1$
					System.arraycopy(urls, 0, all, 1, urls.length);
				} catch (MalformedURLException e) {
					all = urls; 
				}
				PDEState state = new PDEState(all, false, monitor);
				models = state.getTargetModels();
				monitor.done();
			}
		};
		try {
			getContainer().run(true, false, op);
		} catch (InvocationTargetException e) {
			PDEPlugin.log(e);
		} catch (InterruptedException e) {
		}
	}
		

	public IPluginModelBase[] getModels() {
		String dropLocation = getDropLocation();
		if (dropLocation.equals(TARGET_PLATFORM)) {
			resolveTargetPlatform();
		} else {
			resolveArbitraryLocation(dropLocation);
		}
		return models;
	}
	
	public boolean isCurrentPage() {
		return super.isCurrentPage();
	}
}

Back to the top