View | Details | Raw Unified | Return to bug 204347 | Differences between
and this patch

Collapse All | Expand All

(-)src/org/eclipse/equinox/internal/p2/target/EquinoxAdminWizard.java (-92 lines)
Removed Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2007 IBM Corporation and others. All rights reserved. This
3
 * program and the accompanying materials are made available under the terms of
4
 * the Eclipse Public License v1.0 which accompanies this distribution, and is
5
 * available at http://www.eclipse.org/legal/epl-v10.html
6
 * 
7
 * Contributors: IBM Corporation - initial API and implementation
8
 ******************************************************************************/
9
package org.eclipse.equinox.internal.p2.target;
10
11
import java.io.File;
12
import java.io.IOException;
13
import java.net.MalformedURLException;
14
import java.net.URL;
15
import java.util.ArrayList;
16
import java.util.List;
17
import org.eclipse.equinox.frameworkadmin.*;
18
import org.eclipse.jface.wizard.Wizard;
19
import org.eclipse.pde.ui.IProvisionerWizard;
20
import org.osgi.framework.Filter;
21
import org.osgi.framework.InvalidSyntaxException;
22
import org.osgi.util.tracker.ServiceTracker;
23
24
public class EquinoxAdminWizard extends Wizard implements IProvisionerWizard {
25
26
	private final static String FILTER_OBJECTCLASS = "(" + org.osgi.framework.Constants.OBJECTCLASS + "=" + FrameworkAdmin.class.getName() + ")";
27
	private final static String filterFwName = "(" + FrameworkAdmin.SERVICE_PROP_KEY_FW_NAME + "=Equinox)";
28
	private final static String filterLauncherName = "(" + FrameworkAdmin.SERVICE_PROP_KEY_LAUNCHER_NAME + "=Eclipse.exe)";
29
	private final static String filterFwAdmin = "(&" + FILTER_OBJECTCLASS + filterFwName + filterLauncherName + ")";
30
	private ServiceTracker fwAdminTracker;
31
32
	private DirectorySelectionPage fPage;
33
	private File[] files;
34
35
	public File[] getLocations() {
36
		return files;
37
	}
38
39
	public boolean performFinish() {
40
		File[] installFolders = fPage.getLocations();
41
		if (installFolders.length == 0)
42
			return true;
43
44
		FrameworkAdmin admin = getFrameworkAdmin();
45
		Manipulator manipulator = admin.getManipulator();
46
47
		LauncherData launcherData = manipulator.getLauncherData();
48
		launcherData.setLauncher(new File(installFolders[0], "eclipse")); //TODO This should likely be customized as part of the values entered in the wizard.
49
		launcherData.setFwConfigLocation(new File(installFolders[0], "configuration"));
50
		try {
51
			manipulator.load();
52
		} catch (IllegalStateException e2) {
53
			return false;
54
		} catch (FrameworkAdminRuntimeException e2) {
55
			return false;
56
		} catch (IOException e2) {
57
			return false;
58
		}
59
		BundleInfo[] bundles = manipulator.getConfigData().getBundles();
60
		List collectedValues = new ArrayList(bundles.length);
61
		for (int i = 0; i < bundles.length; i++) {
62
			try {
63
				collectedValues.add(new File(new URL(bundles[i].getLocation()).getFile()));
64
			} catch (MalformedURLException e) {
65
				//Ignore
66
			}
67
		}
68
		files = (File[]) collectedValues.toArray(new File[collectedValues.size()]);
69
		return true;
70
	}
71
72
	private FrameworkAdmin getFrameworkAdmin() {
73
		if (fwAdminTracker == null) {
74
			Filter filter;
75
			try {
76
				filter = Activator.getContext().createFilter(filterFwAdmin);
77
				fwAdminTracker = new ServiceTracker(Activator.getContext(), filter, null);
78
				fwAdminTracker.open();
79
			} catch (InvalidSyntaxException e) {
80
				// never happens
81
				e.printStackTrace();
82
			}
83
		}
84
		return (FrameworkAdmin) fwAdminTracker.getService();
85
	}
86
87
	public void addPages() {
88
		fPage = new DirectorySelectionPage("file system"); //$NON-NLS-1$
89
		addPage(fPage);
90
		super.addPages();
91
	}
92
}
(-)src/org/eclipse/equinox/internal/p2/target/DirectorySelectionPage.java (-171 lines)
Removed Link Here
1
/*******************************************************************************
2
 * Copyright (c) 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.equinox.internal.p2.target;
12
13
//THIS IS A COPY OF THE CLASS OF THE SAME NAME CONTAINED IN PDE UI
14
import java.io.File;
15
import java.util.ArrayList;
16
import org.eclipse.jface.dialogs.Dialog;
17
import org.eclipse.jface.viewers.*;
18
import org.eclipse.jface.wizard.WizardPage;
19
import org.eclipse.pde.internal.ui.IHelpContextIds;
20
import org.eclipse.pde.internal.ui.PDEUIMessages;
21
import org.eclipse.pde.internal.ui.util.SWTUtil;
22
import org.eclipse.pde.internal.ui.util.SharedLabelProvider;
23
import org.eclipse.swt.SWT;
24
import org.eclipse.swt.events.*;
25
import org.eclipse.swt.graphics.Image;
26
import org.eclipse.swt.layout.GridData;
27
import org.eclipse.swt.layout.GridLayout;
28
import org.eclipse.swt.widgets.*;
29
import org.eclipse.ui.ISharedImages;
30
import org.eclipse.ui.PlatformUI;
31
32
public class DirectorySelectionPage extends WizardPage {
33
34
	private static final Image fFolderImage = PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(ISharedImages.IMG_OBJ_FOLDER).createImage();
35
	private static final String LAST_LOCATION = "last_location"; //$NON-NLS-1$
36
37
	class FolderLabelProvider extends SharedLabelProvider {
38
39
		public Image getImage(Object obj) {
40
			return fFolderImage;
41
		}
42
	}
43
44
	Text fDir = null;
45
	private TableViewer fTableViewer = null;
46
	private ArrayList fElements = new ArrayList();
47
	private Button fAddButton = null;
48
	private Button fRemoveButton = null;
49
	private String fLastLocation = null;
50
51
	protected DirectorySelectionPage(String pageName) {
52
		super(pageName);
53
		setTitle(PDEUIMessages.DirectorySelectionPage_title);
54
		setDescription(PDEUIMessages.DirectorySelectionPage_description);
55
		setPageComplete(false);
56
//		Preferences pref = PDECore.getDefault().getPluginPreferences();
57
//		fLastLocation = pref.getString(LAST_LOCATION);
58
//		if (fLastLocation.length() == 0)
59
//			fLastLocation = pref.getString(ICoreConstants.PLATFORM_PATH);
60
	}
61
62
	public void createControl(Composite parent) {
63
		Composite client = new Composite(parent, SWT.NONE);
64
		GridLayout layout = new GridLayout();
65
		layout.marginWidth = 2;
66
		layout.numColumns = 2;
67
		client.setLayout(layout);
68
		client.setLayoutData(new GridData(GridData.FILL_BOTH));
69
70
		Label label = new Label(client, SWT.None);
71
		label.setText(PDEUIMessages.DirectorySelectionPage_label);
72
		GridData gd = new GridData();
73
		gd.horizontalSpan = 2;
74
		label.setLayoutData(gd);
75
76
		fTableViewer = new TableViewer(client);
77
		fTableViewer.setLabelProvider(new FolderLabelProvider());
78
		fTableViewer.setContentProvider(new ArrayContentProvider());
79
		fTableViewer.setInput(fElements);
80
		gd = new GridData(GridData.FILL_BOTH);
81
		gd.verticalSpan = 3;
82
		fTableViewer.getControl().setLayoutData(gd);
83
84
		fTableViewer.addSelectionChangedListener(new ISelectionChangedListener() {
85
86
			public void selectionChanged(SelectionChangedEvent event) {
87
				updateButtons();
88
			}
89
90
		});
91
		fTableViewer.getTable().addKeyListener(new KeyAdapter() {
92
			public void keyPressed(KeyEvent event) {
93
				if (event.character == SWT.DEL && event.stateMask == 0) {
94
					handleRemove();
95
				}
96
			}
97
		});
98
		Dialog.applyDialogFont(fTableViewer.getControl());
99
		Dialog.applyDialogFont(label);
100
101
		createButtons(client);
102
		PlatformUI.getWorkbench().getHelpSystem().setHelp(client, IHelpContextIds.FILE_SYSTEM_PROVISIONING_PAGE);
103
104
		setControl(client);
105
	}
106
107
	protected void createButtons(Composite parent) {
108
		fAddButton = new Button(parent, SWT.PUSH);
109
		fAddButton.setText(PDEUIMessages.DirectorySelectionPage_add);
110
		fAddButton.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_BEGINNING));
111
		SWTUtil.setButtonDimensionHint(fAddButton);
112
		fAddButton.addSelectionListener(new SelectionAdapter() {
113
			public void widgetSelected(SelectionEvent e) {
114
				handleAdd();
115
			}
116
		});
117
118
		fRemoveButton = new Button(parent, SWT.PUSH);
119
		fRemoveButton.setText(PDEUIMessages.DirectorySelectionPage_remove);
120
		fRemoveButton.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_BEGINNING));
121
		SWTUtil.setButtonDimensionHint(fRemoveButton);
122
		fRemoveButton.addSelectionListener(new SelectionAdapter() {
123
			public void widgetSelected(SelectionEvent e) {
124
				handleRemove();
125
			}
126
		});
127
		updateButtons();
128
	}
129
130
	private void handleAdd() {
131
		DirectoryDialog dialog = new DirectoryDialog(getShell());
132
		dialog.setMessage(PDEUIMessages.DirectorySelectionPage_message);
133
		dialog.setFilterPath(fLastLocation);
134
		String path = dialog.open();
135
		if (path != null) {
136
			fLastLocation = path;
137
			File newDirectory = new File(path);
138
			fElements.add(newDirectory);
139
			fTableViewer.add(newDirectory);
140
			setPageComplete(true);
141
		}
142
	}
143
144
	private void handleRemove() {
145
		Object[] elements = ((IStructuredSelection) fTableViewer.getSelection()).toArray();
146
		for (int i = 0; i < elements.length; i++)
147
			fElements.remove(elements[i]);
148
149
		Table table = fTableViewer.getTable();
150
		int index = table.getSelectionIndex() - fElements.size();
151
		if (index > fElements.size())
152
			index = fElements.size() - 1;
153
154
		fTableViewer.remove(elements);
155
		table.setSelection(index);
156
157
		updateButtons();
158
		setPageComplete(!fElements.isEmpty());
159
	}
160
161
	protected void updateButtons() {
162
		int num = fTableViewer.getTable().getSelectionCount();
163
		fRemoveButton.setEnabled(num > 0);
164
	}
165
166
	public File[] getLocations() {
167
//		Preferences pref = PDECore.getDefault().getPluginPreferences();
168
//		pref.setValue(LAST_LOCATION, fLastLocation);
169
		return (File[]) fElements.toArray(new File[fElements.size()]);
170
	}
171
}
(-)plugin.xml (-9 / +66 lines)
Lines 1-13 Link Here
1
<plugin>
1
<plugin>
2
   <extension
2
   <extension
3
         point="org.eclipse.pde.ui.targetProvisioners">
3
         point="org.eclipse.ui.commands">
4
      <provisioner
4
      <command
5
            class="org.eclipse.equinox.internal.p2.target.EquinoxAdminWizard"
5
            defaultHandler="org.eclipse.equinox.internal.p2.target.menu.CreateNewP2TargetDefinitionHandler"
6
            id="org.eclipse.equinox.p2.target.equinoxadmin"
6
            id="org.eclipse.equinox.p2.target.createTargetDefinition"
7
            name="Equinox Admin">
7
            name="Create new p2 target definition">
8
         <description>
8
      </command>
9
            blah blah
9
      <command
10
         </description>
10
            defaultHandler="org.eclipse.equinox.internal.p2.target.menu.LoadTargetDefinitionHandler"
11
      </provisioner>
11
            id="org.eclipse.equinox.p2.target.loadTargetDefinition"
12
            name="Load Target Definition">
13
      </command>
14
      <command
15
            defaultHandler="org.eclipse.equinox.internal.p2.target.menu.OpenTargetDefinitionHandler"
16
            id="org.eclipse.equinox.p2.target.openTargetDefinition"
17
            name="Open Target Definition">
18
      </command>
19
   </extension>
20
   <extension
21
         point="org.eclipse.ui.menus">
22
      <menuContribution
23
            locationURI="menu:org.eclipse.ui.main.menu">
24
         <menu
25
               id="org.eclipse.equinox.p2.target.p2SelfHostMenu"
26
               label="P2 Self Hosting"
27
               mnemonic="P">
28
            <command
29
                  commandId="org.eclipse.equinox.p2.target.createTargetDefinition"
30
                  label="Create new target definition..."
31
                  mnemonic="C">
32
            </command>
33
            <menu
34
                  id="org.eclipse.equinox.p2.target.p2LoadTargetMenu"
35
                  label="Load Target Definition">
36
               <dynamic
37
                     class="org.eclipse.equinox.internal.p2.target.menu.LoadTargetDefinitionSubMenu"
38
                     id="org.eclipse.equinox.p2.target.loadTargetFromDefinition">
39
               </dynamic>
40
               <separator
41
                     name="org.eclipse.equinox.p2.target.separator1"
42
                     visible="true">
43
               </separator>
44
               <command
45
                     commandId="org.eclipse.equinox.p2.target.loadTargetDefinition"
46
                     label="Load Target Definition..."
47
                     style="push">
48
               </command>
49
            </menu>
50
            <menu
51
                  id="org.eclipse.equinox.p2.target.p2OpenTargetDefinitionMenu"
52
                  label="Open Target Definition">
53
               <dynamic
54
                     class="org.eclipse.equinox.internal.p2.target.menu.OpenTargetDefinitionSubMenu"
55
                     id="org.eclipse.equinox.p2.target.openTargetDefinition">
56
               </dynamic>
57
               <separator
58
                     name="org.eclipse.equinox.p2.target.separator2"
59
                     visible="true">
60
               </separator>
61
               <command
62
                     commandId="org.eclipse.equinox.p2.target.openTargetDefinition"
63
                     label="Open Target Definition..."
64
                     style="push">
65
               </command>
66
            </menu>
67
         </menu>
68
      </menuContribution>
12
   </extension>
69
   </extension>
13
</plugin>
70
</plugin>
(-)META-INF/MANIFEST.MF (-1 / +4 lines)
Lines 9-15 Link Here
9
 org.eclipse.equinox.frameworkadmin,
9
 org.eclipse.equinox.frameworkadmin,
10
 org.eclipse.jface,
10
 org.eclipse.jface,
11
 org.eclipse.ui,
11
 org.eclipse.ui,
12
 org.eclipse.osgi
12
 org.eclipse.osgi,
13
 org.eclipse.core.runtime,
14
 org.eclipse.core.resources,
15
 org.eclipse.ui.ide
13
Bundle-Activator: org.eclipse.equinox.internal.p2.target.Activator
16
Bundle-Activator: org.eclipse.equinox.internal.p2.target.Activator
14
Eclipse-LazyStart: true
17
Eclipse-LazyStart: true
15
Export-Package: org.eclipse.equinox.internal.p2.target;x-internal:=true
18
Export-Package: org.eclipse.equinox.internal.p2.target;x-internal:=true
(-)src/org/eclipse/equinox/internal/p2/target/menu/LoadTargetDefinitionHandler.java (+99 lines)
Added Link Here
1
package org.eclipse.equinox.internal.p2.target.menu;
2
import java.lang.reflect.InvocationTargetException;
3
4
import org.eclipse.core.commands.AbstractHandler;
5
import org.eclipse.core.commands.ExecutionEvent;
6
import org.eclipse.core.commands.ExecutionException;
7
import org.eclipse.core.resources.IFile;
8
import org.eclipse.core.runtime.CoreException;
9
import org.eclipse.core.runtime.IProgressMonitor;
10
import org.eclipse.core.runtime.OperationCanceledException;
11
import org.eclipse.equinox.internal.p2.target.P2TargetDefinitionManager;
12
import org.eclipse.jface.dialogs.MessageDialog;
13
import org.eclipse.jface.operation.IRunnableWithProgress;
14
import org.eclipse.jface.viewers.ISelection;
15
import org.eclipse.jface.viewers.StructuredSelection;
16
import org.eclipse.jface.window.Window;
17
import org.eclipse.pde.internal.core.LoadTargetOperation;
18
import org.eclipse.pde.internal.core.itarget.ITargetModel;
19
import org.eclipse.pde.internal.core.target.WorkspaceTargetModel;
20
import org.eclipse.pde.internal.ui.IPDEUIConstants;
21
import org.eclipse.pde.internal.ui.PDEPlugin;
22
import org.eclipse.pde.internal.ui.PDEUIMessages;
23
import org.eclipse.pde.internal.ui.editor.target.TargetErrorDialog;
24
import org.eclipse.pde.internal.ui.util.FileExtensionFilter;
25
import org.eclipse.pde.internal.ui.util.FileValidator;
26
import org.eclipse.ui.IWorkbenchPage;
27
import org.eclipse.ui.IWorkbenchPart;
28
import org.eclipse.ui.IWorkbenchWindow;
29
import org.eclipse.ui.PartInitException;
30
import org.eclipse.ui.PlatformUI;
31
import org.eclipse.ui.dialogs.ElementTreeSelectionDialog;
32
import org.eclipse.ui.ide.IDE;
33
import org.eclipse.ui.model.WorkbenchContentProvider;
34
import org.eclipse.ui.model.WorkbenchLabelProvider;
35
import org.eclipse.ui.part.ISetSelectionTarget;
36
import org.eclipse.ui.progress.IProgressService;
37
38
39
public class LoadTargetDefinitionHandler extends AbstractHandler {
40
41
	public Object execute(ExecutionEvent arg0) throws ExecutionException {
42
		ElementTreeSelectionDialog dialog =
43
			new ElementTreeSelectionDialog(PDEPlugin.getActiveWorkbenchShell(),
44
				new WorkbenchLabelProvider(),
45
				new WorkbenchContentProvider());
46
				
47
		dialog.setValidator(new FileValidator());
48
		dialog.setAllowMultiple(false);
49
		dialog.setTitle(PDEUIMessages.TargetPlatformPreferencePage_FileSelectionTitle); 
50
		dialog.setMessage(PDEUIMessages.TargetPlatformPreferencePage_FileSelectionMessage); 
51
		dialog.addFilter(new FileExtensionFilter("target"));  //$NON-NLS-1$
52
		dialog.setInput(PDEPlugin.getWorkspace().getRoot());
53
54
		if (dialog.open() == Window.OK) {
55
			final IFile file = (IFile)dialog.getFirstResult();
56
			final ITargetModel targetModel = new WorkspaceTargetModel(file, false);
57
			try{	
58
				targetModel.load();
59
			} catch (CoreException e){
60
				MessageDialog.openError(PDEPlugin.getActiveWorkbenchShell(), PDEUIMessages.TargetPlatformPreferencePage_invalidTitle, "Could not load the target definition specified: " + e.getMessage());
61
			}
62
			IRunnableWithProgress run = new IRunnableWithProgress() {
63
				public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
64
					try {
65
						if (!targetModel.isLoaded()) {
66
							MessageDialog.openError(PDEPlugin.getActiveWorkbenchShell(), PDEUIMessages.TargetPlatformPreferencePage_invalidTitle, PDEUIMessages.TargetPlatformPreferencePage_invalidDescription);
67
							monitor.done();
68
							return;
69
						}
70
						LoadTargetOperation op = new LoadTargetOperation(targetModel.getTarget(), file.getFullPath());
71
						PDEPlugin.getWorkspace().run(op, monitor);
72
						Object[] features = op.getMissingFeatures();
73
						Object[] plugins = op.getMissingPlugins();
74
						if (plugins.length + features.length > 0)
75
							TargetErrorDialog.showDialog(PDEPlugin.getActiveWorkbenchShell(), features, plugins);
76
					} catch (CoreException e) {
77
						throw new InvocationTargetException(e);
78
					} catch (OperationCanceledException e) {
79
						throw new InterruptedException(e.getMessage());
80
					} finally {
81
						monitor.done();
82
					}
83
				}
84
			};
85
			IProgressService service = PlatformUI.getWorkbench().getProgressService();
86
			try {
87
				service.runInUI(service, run, PDEPlugin.getWorkspace().getRoot());
88
			} catch (InvocationTargetException e) {
89
			} catch (InterruptedException e) {
90
			}
91
			
92
			P2TargetDefinitionManager.getDefault().addTarget(file.getFullPath());
93
			
94
		}
95
		
96
		return null;
97
	}
98
99
}
(-)src/org/eclipse/equinox/internal/p2/target/P2TargetDefinitionCreationOperation.java (+73 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2005, 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
package org.eclipse.equinox.internal.p2.target;
12
13
import java.io.File;
14
15
import org.eclipse.core.resources.IFile;
16
import org.eclipse.pde.internal.core.itarget.IAdditionalLocation;
17
import org.eclipse.pde.internal.core.itarget.ILocationInfo;
18
import org.eclipse.pde.internal.core.itarget.ITarget;
19
import org.eclipse.pde.internal.core.itarget.ITargetModel;
20
import org.eclipse.pde.internal.core.itarget.ITargetPlugin;
21
import org.eclipse.pde.internal.ui.wizards.target.BaseTargetDefinitionOperation;
22
23
public class P2TargetDefinitionCreationOperation extends BaseTargetDefinitionOperation {
24
25
	private File[] fLocations;
26
	private String[] fBundles;
27
	private String fDefinitionName;
28
	
29
	public P2TargetDefinitionCreationOperation(IFile definitionFile, String definitionName, File[] locations, String[] bundles){
30
		super(definitionFile);
31
		fDefinitionName = definitionName;
32
		fLocations = locations;
33
		fBundles = bundles;
34
	}
35
	
36
	protected void initializeTarget(ITargetModel model) {
37
		ITarget target = model.getTarget();
38
		
39
		if (fDefinitionName != null && fDefinitionName.length() > 0){
40
			target.setName(fDefinitionName);
41
		}
42
		
43
		if (fLocations == null || fLocations.length <= 0){
44
			// TODO This is a problem, how to handle it?
45
		} else {
46
			ILocationInfo info = model.getFactory().createLocation();
47
			info.setPath(fLocations[0].getPath());
48
			info.setDefault(false);
49
			target.setLocationInfo(info);
50
			
51
			IAdditionalLocation[] locations = new IAdditionalLocation[fLocations.length-1];
52
			for (int i = 0; i < locations.length; i++) {
53
				IAdditionalLocation location = model.getFactory().createAdditionalLocation();
54
				location.setPath(fLocations[i+1].getPath());
55
				locations[i] = location;
56
			}
57
			target.addAdditionalDirectories(locations);
58
		}
59
		
60
		if (fBundles == null){
61
			target.setUseAllPlugins(true);
62
		} else if (fBundles.length > 0) {
63
			ITargetPlugin[] plugins = new ITargetPlugin[fBundles.length];
64
			for (int i = 0; i < plugins.length; i++) {
65
				ITargetPlugin plugin = model.getFactory().createPlugin();
66
				plugin.setId(fBundles[i]);
67
				plugins[i] = plugin;
68
			}
69
			target.addPlugins(plugins);
70
		}
71
	}
72
73
}
(-)src/org/eclipse/equinox/internal/p2/target/P2TargetDefinitionWizard.java (+68 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2007 IBM Corporation and others. All rights reserved. This
3
 * program and the accompanying materials are made available under the terms of
4
 * the Eclipse Public License v1.0 which accompanies this distribution, and is
5
 * available at http://www.eclipse.org/legal/epl-v10.html
6
 * 
7
 * Contributors: IBM Corporation - initial API and implementation
8
 ******************************************************************************/
9
package org.eclipse.equinox.internal.p2.target;
10
11
import java.io.File;
12
import java.lang.reflect.InvocationTargetException;
13
14
import org.eclipse.core.runtime.IPath;
15
import org.eclipse.jface.viewers.StructuredSelection;
16
import org.eclipse.jface.wizard.Wizard;
17
import org.eclipse.pde.internal.ui.PDEPlugin;
18
import org.eclipse.pde.internal.ui.PDEPluginImages;
19
import org.eclipse.pde.internal.ui.PDEUIMessages;
20
import org.eclipse.pde.internal.ui.wizards.PDEWizardNewFileCreationPage;
21
22
public class P2TargetDefinitionWizard extends Wizard {
23
24
	private PDEWizardNewFileCreationPage fPage1;
25
	private P2TargetDefinitionWizardPage fPage2;
26
	private IPath fFilePath;
27
28
	public P2TargetDefinitionWizard(){
29
		setWindowTitle("New P2 self-hosting target definition"); 
30
		setDefaultPageImageDescriptor(PDEPluginImages.DESC_TARGET_WIZ);
31
		setNeedsProgressMonitor(true);
32
	}
33
		
34
	public void addPages() {
35
		fPage1 = new PDEWizardNewFileCreationPage("Target Definition Location Page", StructuredSelection.EMPTY);
36
		fPage1.setFileExtension("target");
37
		fPage1.setTitle("p2 Target Definition");
38
		fPage1.setDescription("Choose a location for the target definition");
39
		addPage(fPage1);
40
		fPage2 = new P2TargetDefinitionWizardPage("p2 Target Definition Page"); //$NON-NLS-1$
41
		fPage2.setTitle("p2 Target Definition");
42
		fPage2.setDescription("Choose a p2 install for the target definition");
43
		addPage(fPage2);
44
		super.addPages();
45
	}
46
	
47
	public boolean canFinish() {
48
		return fPage1.isPageComplete() && fPage2.isPageComplete();
49
	}
50
		
51
	public boolean performFinish() {
52
		try {
53
			getContainer().run(false, true, new P2TargetDefinitionCreationOperation(fPage1.createNewFile(), fPage2.getDefinitionName(), fPage2.getDirectories(), fPage2.getBundles()));
54
			fFilePath = fPage1.getContainerFullPath().append(fPage1.getFileName());
55
		} catch (InvocationTargetException e) {
56
			PDEPlugin.logException(e);
57
			return false;
58
		} catch (InterruptedException e) {
59
			return false;
60
		}
61
		return true;
62
	}
63
		
64
	public IPath getTargetDefinition() {
65
		return fFilePath;
66
	}
67
68
}
(-)src/org/eclipse/equinox/internal/p2/target/P2TargetDefinitionManager.java (+77 lines)
Added Link Here
1
package org.eclipse.equinox.internal.p2.target;
2
3
import java.util.ArrayList;
4
import java.util.Iterator;
5
import java.util.List;
6
7
import org.eclipse.core.resources.IFile;
8
import org.eclipse.core.runtime.IPath;
9
import org.eclipse.core.runtime.Path;
10
import org.eclipse.core.runtime.Preferences;
11
import org.eclipse.pde.internal.core.PDECore;
12
import org.eclipse.pde.internal.ui.PDEPlugin;
13
14
public class P2TargetDefinitionManager {
15
16
	private static P2TargetDefinitionManager fDefault;
17
	private List fTargets = new ArrayList();
18
	
19
	public final String KNOWN_P2_TARGET_DEFINITIONS = "target.knownTargetDefinitions";
20
	
21
	protected P2TargetDefinitionManager() {}
22
	
23
	public static P2TargetDefinitionManager getDefault(){
24
		if (fDefault == null){
25
			fDefault = new P2TargetDefinitionManager();
26
			fDefault.initializeFromPreferences();
27
		}
28
		return fDefault;
29
	}
30
	
31
	public List getTargets(){
32
		return fTargets;
33
	}
34
	
35
	public void addTarget(IPath targetDefinitionFile){
36
		if (!fTargets.contains(targetDefinitionFile)){
37
			fTargets.add(targetDefinitionFile);
38
			persistTargets();
39
		}
40
	}
41
	
42
	private void initializeFromPreferences(){
43
		Preferences prefs = PDECore.getDefault().getPluginPreferences();
44
		String locationString = prefs.getString(KNOWN_P2_TARGET_DEFINITIONS);
45
		String[] locations = locationString.split(",");
46
		boolean needToWriteOutPreferences = false;
47
		for (int i = 0; i < locations.length; i++) {
48
			if (locations[i].length() > 0){
49
				IPath path = Path.fromPortableString(locations[i]);
50
				IFile file = PDEPlugin.getWorkspace().getRoot().getFile(path);
51
				if (file.exists() && !fTargets.contains(path)){
52
					fTargets.add(path);
53
				} else {
54
					needToWriteOutPreferences = true;
55
				}
56
			}
57
		}
58
		if (needToWriteOutPreferences){
59
			persistTargets();
60
		}
61
	}
62
	
63
	private void persistTargets(){
64
		Preferences prefs = PDECore.getDefault().getPluginPreferences();
65
		StringBuffer locationString = new StringBuffer();
66
		for (Iterator iterator = fTargets.iterator(); iterator.hasNext();) {
67
			IPath target = (IPath) iterator.next();
68
			locationString.append(target.toPortableString());
69
			if (iterator.hasNext()){
70
				locationString.append(',');
71
			}
72
		}
73
		prefs.setValue(KNOWN_P2_TARGET_DEFINITIONS, locationString.toString());
74
	}
75
	
76
	
77
}
(-)src/org/eclipse/equinox/internal/p2/target/menu/CreateNewP2TargetDefinitionHandler.java (+28 lines)
Added Link Here
1
package org.eclipse.equinox.internal.p2.target.menu;
2
import java.io.File;
3
4
import org.eclipse.core.commands.AbstractHandler;
5
import org.eclipse.core.commands.ExecutionEvent;
6
import org.eclipse.core.commands.ExecutionException;
7
import org.eclipse.equinox.internal.p2.target.P2TargetDefinitionManager;
8
import org.eclipse.equinox.internal.p2.target.P2TargetDefinitionWizard;
9
import org.eclipse.jface.wizard.WizardDialog;
10
import org.eclipse.pde.internal.ui.PDEPlugin;
11
import org.eclipse.pde.internal.ui.util.SWTUtil;
12
import org.eclipse.pde.internal.ui.wizards.provisioner.AddTargetPluginsWizard;
13
14
15
public class CreateNewP2TargetDefinitionHandler extends AbstractHandler {
16
17
	public Object execute(ExecutionEvent event) throws ExecutionException {
18
		P2TargetDefinitionWizard wizard = new P2TargetDefinitionWizard();
19
		WizardDialog dialog = new WizardDialog(PDEPlugin.getActiveWorkbenchShell(), wizard);
20
		dialog.create();
21
		dialog.open();
22
		
23
		P2TargetDefinitionManager.getDefault().addTarget(wizard.getTargetDefinition());
24
				
25
		return null;
26
	}
27
28
}
(-)src/org/eclipse/equinox/internal/p2/target/P2TargetDefinitionWizardPage.java (+348 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 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
package org.eclipse.equinox.internal.p2.target;
12
13
import java.io.File;
14
import java.io.IOException;
15
import java.net.MalformedURLException;
16
import java.net.URL;
17
import java.util.HashSet;
18
import java.util.Set;
19
import java.util.Stack;
20
21
import org.eclipse.core.resources.IFile;
22
import org.eclipse.core.runtime.IStatus;
23
import org.eclipse.core.runtime.Status;
24
import org.eclipse.equinox.frameworkadmin.BundleInfo;
25
import org.eclipse.equinox.frameworkadmin.FrameworkAdmin;
26
import org.eclipse.equinox.frameworkadmin.FrameworkAdminRuntimeException;
27
import org.eclipse.equinox.frameworkadmin.LauncherData;
28
import org.eclipse.equinox.frameworkadmin.Manipulator;
29
import org.eclipse.jface.dialogs.IMessageProvider;
30
import org.eclipse.jface.wizard.WizardPage;
31
import org.eclipse.pde.internal.ui.PDEPlugin;
32
import org.eclipse.swt.SWT;
33
import org.eclipse.swt.events.ModifyEvent;
34
import org.eclipse.swt.events.ModifyListener;
35
import org.eclipse.swt.events.SelectionEvent;
36
import org.eclipse.swt.events.SelectionListener;
37
import org.eclipse.swt.layout.GridData;
38
import org.eclipse.swt.layout.GridLayout;
39
import org.eclipse.swt.widgets.Button;
40
import org.eclipse.swt.widgets.Composite;
41
import org.eclipse.swt.widgets.DirectoryDialog;
42
import org.eclipse.swt.widgets.Label;
43
import org.eclipse.swt.widgets.Table;
44
import org.eclipse.swt.widgets.TableItem;
45
import org.eclipse.swt.widgets.Text;
46
import org.osgi.framework.Filter;
47
import org.osgi.framework.InvalidSyntaxException;
48
import org.osgi.util.tracker.ServiceTracker;
49
50
/**
51
 * Page used to select an equinox install configuration to load bundles from.
52
 * 
53
 * @since 3.4
54
 */
55
public class P2TargetDefinitionWizardPage extends WizardPage {
56
	
57
	// TODO Get this from the plugin class
58
	private static final String PLUGIN_ID = "org.eclipse.equinox.p2.target";
59
	
60
	private final static String FILTER_OBJECTCLASS = "(" + org.osgi.framework.Constants.OBJECTCLASS + "=" + FrameworkAdmin.class.getName() + ")";
61
	private final static String filterFwName = "(" + FrameworkAdmin.SERVICE_PROP_KEY_FW_NAME + "=Equinox)";
62
	private final static String filterLauncherName = "(" + FrameworkAdmin.SERVICE_PROP_KEY_LAUNCHER_NAME + "=Eclipse.exe)";
63
	private final static String filterFwAdmin = "(&" + FILTER_OBJECTCLASS + filterFwName + filterLauncherName + ")";
64
	private ServiceTracker fwAdminTracker;
65
66
	private Text fInstallLocation;
67
	private Text fTargetDefinitionName;
68
	private Table fBundleTable;
69
	
70
	private File[] fBundleDirs;
71
	private String[] fBundleIDs;
72
	
73
//	private IStatus fNameStatus;
74
	private IStatus fInstallStatus;
75
	
76
	public P2TargetDefinitionWizardPage(String pageName) {
77
		super(pageName);
78
		setPageComplete(false);
79
		// TODO Determine this from entries in wizard
80
	}
81
	
82
//	/* (non-Javadoc)
83
//	 * @see org.eclipse.jface.dialogs.IDialogPage#getImage()
84
//	 */
85
//	public Image getImage() {
86
//		// TODO Get an image for this wizard
87
//		return null;
88
//	}	
89
	
90
	public File[] getDirectories(){
91
		return fBundleDirs;
92
	}
93
	
94
	public String[] getBundles(){
95
		return fBundleIDs;
96
	}
97
	
98
	public String getDefinitionName(){
99
		return fTargetDefinitionName.getText().trim();
100
	}
101
102
	/* (non-Javadoc)
103
	 * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
104
	 */
105
	public void createControl(Composite parent) {
106
		// create a composite with standard margins and spacing
107
		Composite composite = new Composite(parent, SWT.NONE);
108
		GridLayout layout = new GridLayout();
109
		layout.numColumns = 3;
110
		composite.setLayout(layout);
111
		composite.setLayoutData(new GridData(GridData.FILL_BOTH));
112
		
113
		Label label = new Label(composite, SWT.NONE);
114
		label.setText("Target Name:");
115
		GridData gd = new GridData(GridData.FILL_HORIZONTAL);
116
		gd.horizontalSpan = 1;
117
		gd.grabExcessHorizontalSpace = false;
118
		label.setLayoutData(gd);
119
		
120
		fTargetDefinitionName = new Text(composite, SWT.SINGLE | SWT.BORDER);
121
		fTargetDefinitionName.setFont(composite.getFont());
122
    	gd = new GridData(GridData.FILL_HORIZONTAL);
123
    	gd.horizontalSpan = 2;
124
    	fTargetDefinitionName.setLayoutData(gd);
125
		
126
    	label = new Label(composite, SWT.NONE);
127
		label.setText("Install Location:");
128
		gd = new GridData(GridData.FILL_HORIZONTAL);
129
		gd.horizontalSpan = 1;
130
		gd.grabExcessHorizontalSpace = false;
131
		label.setLayoutData(gd);
132
    	
133
		fInstallLocation = new Text(composite, SWT.SINGLE | SWT.BORDER);
134
		fInstallLocation.setFont(composite.getFont());
135
    	gd = new GridData(GridData.FILL_HORIZONTAL);
136
    	gd.horizontalSpan = 1;
137
    	fInstallLocation.setLayoutData(gd);
138
    	
139
    	Button folders = new Button(composite, SWT.PUSH);
140
    	folders.setFont(composite.getFont());
141
    	folders.setText("Browse...");
142
		gd = new GridData();
143
		gd.horizontalSpan = 1;
144
		gd.horizontalAlignment = GridData.END;
145
		folders.setLayoutData(gd);	
146
    	
147
		label = new Label(composite, SWT.NONE);
148
		label.setText("Target Bundles:");
149
		gd = new GridData(GridData.FILL_HORIZONTAL);
150
		gd.horizontalSpan = 1;
151
		gd.grabExcessHorizontalSpace = false;
152
		label.setLayoutData(gd);
153
		
154
    	fBundleTable = new Table(composite, SWT.BORDER);
155
    	gd = new GridData(GridData.FILL_BOTH);
156
    	gd.horizontalSpan = 3;
157
    	fBundleTable.setLayoutData(gd);
158
	
159
	
160
	//add the listeners now to prevent them from monkeying with initialized settings
161
//    	fTargetDefinitionName.addModifyListener(new ModifyListener() {
162
//			public void modifyText(ModifyEvent e) {
163
//				validateTargetName();
164
//			}
165
//		});
166
    	fInstallLocation.addModifyListener(new ModifyListener() {
167
			public void modifyText(ModifyEvent e) {
168
				validateEquinoxInstallLocation();
169
			}
170
		});
171
		folders.addSelectionListener(new SelectionListener() {
172
			public void widgetDefaultSelected(SelectionEvent e) {}
173
			public void widgetSelected(SelectionEvent e) {
174
				DirectoryDialog dialog = new DirectoryDialog(getShell());
175
				File file = new File(fInstallLocation.getText());
176
				String text = fInstallLocation.getText();
177
				if (file.isFile()) {
178
					text = file.getParentFile().getAbsolutePath();
179
				}
180
				dialog.setFilterPath(text);
181
				dialog.setMessage("Select a p2 install directory"); 
182
				String newPath = dialog.open();
183
				if (newPath != null) {
184
					fInstallLocation.setText(newPath);
185
				}
186
			}
187
		});
188
			
189
		setControl(composite);
190
//		fNameStatus = new Status(IStatus.WARNING, PLUGIN_ID, "Enter a name for this target definition");
191
		fInstallStatus = new Status(IStatus.WARNING, PLUGIN_ID, "Enter a valid p2 install directory"); 
192
		
193
	}
194
	
195
//	private void validateTargetName() {
196
//		if (fTargetDefinitionName.getText().trim().length() > 0){
197
//			fNameStatus = Status.OK_STATUS;
198
//		} else {
199
//			fNameStatus = new Status(IStatus.WARNING, PLUGIN_ID, "Enter a name for this target definition");
200
//		}
201
//		updatePageStatus();
202
//	}
203
	
204
	private void validateEquinoxInstallLocation() {
205
		String locationName = fInstallLocation.getText().trim();
206
		fBundleTable.removeAll();
207
		fInstallStatus = Status.OK_STATUS;
208
		File installDir = null;
209
		File configurationFolder = null;
210
		File eclipseLauncher = null;
211
		if (locationName.length() == 0) {
212
			fInstallStatus = new Status(IStatus.WARNING, PLUGIN_ID, "Enter a valid p2 install directory"); 
213
		} 
214
		else {
215
			installDir = new File(locationName);
216
			if (!installDir.exists()) {
217
				fInstallStatus = new Status(IStatus.WARNING, PLUGIN_ID, "The entered location does not exist"); 
218
			} 
219
			else {
220
				// TODO We should let the user choose these values
221
				configurationFolder = new File(installDir, "configuration");
222
				if (!configurationFolder.exists()){
223
					fInstallStatus = new Status(IStatus.WARNING, PLUGIN_ID, "A configuration folder could not be found at: " + configurationFolder); 
224
				} else{
225
					eclipseLauncher = new File(installDir, "eclipse");
226
//					if (!eclipseLauncher.exists()){
227
//						s = new Status(IStatus.WARNING, PLUGIN_ID, "The launcher could not be found at: " + eclipseLauncher);
228
//					} else {
229
//					}
230
				}
231
			}
232
		}
233
		if (fInstallStatus.isOK()) {
234
			
235
			FrameworkAdmin admin = getFrameworkAdmin();
236
			
237
			if (admin != null){
238
			
239
				Manipulator manipulator = admin.getManipulator();
240
	
241
				LauncherData launcherData = manipulator.getLauncherData();
242
				launcherData.setLauncher(eclipseLauncher); 
243
				launcherData.setFwConfigLocation(configurationFolder);
244
				try {
245
					manipulator.load();
246
				} catch (IllegalStateException e) {
247
					fInstallStatus = new Status(IStatus.WARNING, PLUGIN_ID, "Problem loading manipulator: " + e);
248
				} catch (FrameworkAdminRuntimeException e) {
249
					fInstallStatus = new Status(IStatus.WARNING, PLUGIN_ID, "Problem loading manipulator: " + e);
250
				} catch (IOException e) {
251
					fInstallStatus = new Status(IStatus.WARNING, PLUGIN_ID, "Problem loading manipulator: " + e);
252
				}
253
				if (fInstallStatus.isOK()){
254
					BundleInfo[] bundles = manipulator.getConfigData().getBundles();
255
					Set bundleNames = new HashSet(bundles.length);
256
					Set bundleDirs = new HashSet();
257
					for (int i = 0; i < bundles.length; i++) {
258
						try {
259
							String bundleID = bundles[i].getSymbolicName();
260
							File bundleLocation = new File(new URL(bundles[i].getLocation()).getFile());
261
							File parentDir = bundleLocation.getParentFile();
262
							if (parentDir.isDirectory()){
263
								bundleDirs.add(parentDir);
264
							}
265
							bundleNames.add(bundleID);
266
						} catch (MalformedURLException e) {
267
							//Ignore
268
						}
269
					}
270
					fBundleDirs = (File[]) bundleDirs.toArray(new File[bundleDirs.size()]);
271
					fBundleIDs = (String[]) bundleNames.toArray(new String[bundleNames.size()]);
272
					System.out.println("Success! " + fBundleIDs);
273
					for (int i = 0; i < fBundleIDs.length; i++) {
274
						TableItem tableItem= new TableItem(fBundleTable, SWT.NONE);
275
						tableItem.setText(0, fBundleIDs[i]);
276
					}
277
				}
278
			} else {
279
				fInstallStatus = new Status(IStatus.WARNING, PLUGIN_ID, "Framework Admin is not available, unable to load install"); 
280
			}
281
		}
282
		updatePageStatus();
283
	}
284
	
285
	private FrameworkAdmin getFrameworkAdmin() {
286
		if (fwAdminTracker == null) {
287
			Filter filter;
288
			try {
289
				filter = PDEPlugin.getDefault().getBundleContext().createFilter(filterFwAdmin);
290
				fwAdminTracker = new ServiceTracker(PDEPlugin.getDefault().getBundleContext(), filter, null);
291
				fwAdminTracker.open();
292
			} catch (InvalidSyntaxException e) {
293
				// never happens
294
				e.printStackTrace();
295
			}
296
		}
297
		return (FrameworkAdmin) fwAdminTracker.getService();
298
	}
299
	
300
	/**
301
	 * Updates the status message on the page, based on the status of the VM and other
302
	 * status provided by the page.
303
	 */
304
	protected void updatePageStatus() {
305
		IStatus max = Status.OK_STATUS;
306
		if (/*fNameStatus == null ||*/ fInstallStatus == null){
307
			return;
308
		}
309
//		if (fNameStatus.getSeverity() > max.getSeverity()) {
310
//			max = fNameStatus;
311
//		}
312
		if (fInstallStatus.getSeverity() > max.getSeverity()) {
313
			max = fInstallStatus;
314
		}
315
		if (max.isOK()) {
316
			setMessage(null, IMessageProvider.NONE);
317
		} else {
318
			setStatusMessage(max);
319
		}
320
		setPageComplete(max.isOK() || max.getSeverity() == IStatus.INFO);
321
	}	
322
	
323
	/**
324
	 * Sets this page's message based on the status severity.
325
	 * 
326
	 * @param status status with message and severity
327
	 */
328
	protected void setStatusMessage(IStatus status) {
329
		if (status.isOK()) {
330
			setMessage(status.getMessage());
331
		} else {
332
			switch (status.getSeverity()) {
333
			case IStatus.ERROR:
334
				setMessage(status.getMessage(), IMessageProvider.ERROR);
335
				break;
336
			case IStatus.INFO:
337
				setMessage(status.getMessage(), IMessageProvider.INFORMATION);
338
				break;
339
			case IStatus.WARNING:
340
				setMessage(status.getMessage(), IMessageProvider.WARNING);
341
				break;
342
			default:
343
				break;
344
			}
345
		}
346
	}	
347
	
348
}
(-)src/org/eclipse/equinox/internal/p2/target/menu/LoadTargetDefinitionSubMenu.java (+107 lines)
Added Link Here
1
package org.eclipse.equinox.internal.p2.target.menu;
2
3
import java.lang.reflect.InvocationTargetException;
4
import java.util.ArrayList;
5
import java.util.Iterator;
6
import java.util.List;
7
8
import org.eclipse.core.resources.IFile;
9
import org.eclipse.core.runtime.CoreException;
10
import org.eclipse.core.runtime.IPath;
11
import org.eclipse.core.runtime.IProgressMonitor;
12
import org.eclipse.core.runtime.OperationCanceledException;
13
import org.eclipse.equinox.internal.p2.target.P2TargetDefinitionManager;
14
import org.eclipse.jface.action.Action;
15
import org.eclipse.jface.action.ActionContributionItem;
16
import org.eclipse.jface.action.IContributionItem;
17
import org.eclipse.jface.dialogs.MessageDialog;
18
import org.eclipse.jface.operation.IRunnableWithProgress;
19
import org.eclipse.jface.viewers.ISelection;
20
import org.eclipse.jface.viewers.StructuredSelection;
21
import org.eclipse.pde.internal.core.LoadTargetOperation;
22
import org.eclipse.pde.internal.core.itarget.ITargetModel;
23
import org.eclipse.pde.internal.core.target.WorkspaceTargetModel;
24
import org.eclipse.pde.internal.ui.IPDEUIConstants;
25
import org.eclipse.pde.internal.ui.PDEPlugin;
26
import org.eclipse.pde.internal.ui.PDEUIMessages;
27
import org.eclipse.pde.internal.ui.editor.target.TargetErrorDialog;
28
import org.eclipse.ui.IWorkbenchPage;
29
import org.eclipse.ui.IWorkbenchPart;
30
import org.eclipse.ui.IWorkbenchWindow;
31
import org.eclipse.ui.PartInitException;
32
import org.eclipse.ui.PlatformUI;
33
import org.eclipse.ui.actions.CompoundContributionItem;
34
import org.eclipse.ui.ide.IDE;
35
import org.eclipse.ui.part.ISetSelectionTarget;
36
import org.eclipse.ui.progress.IProgressService;
37
38
public class LoadTargetDefinitionSubMenu extends CompoundContributionItem {
39
40
	@Override
41
	protected IContributionItem[] getContributionItems() {
42
		List targetList = P2TargetDefinitionManager.getDefault().getTargets();
43
		List contributionItems = new ArrayList(targetList.size());
44
		for (Iterator targetIter = targetList.iterator(); targetIter.hasNext();) {
45
			IPath currentTarget = (IPath) targetIter.next();
46
			if (currentTarget != null){
47
				LoadTargetDefinitionAction action = new LoadTargetDefinitionAction(currentTarget);
48
				contributionItems.add(new ActionContributionItem(action));
49
			}
50
		}
51
		return (IContributionItem[])contributionItems.toArray(new IContributionItem[contributionItems.size()]);
52
	}
53
	
54
	class LoadTargetDefinitionAction extends Action {
55
		
56
		private IPath fTargetDefinition;
57
		
58
		public LoadTargetDefinitionAction(IPath targetDefinitionPath){
59
			fTargetDefinition = targetDefinitionPath;
60
		}
61
		
62
		public void run() {
63
			final IFile file = PDEPlugin.getWorkspace().getRoot().getFile(fTargetDefinition);
64
			final ITargetModel targetModel = new WorkspaceTargetModel(file, false);
65
			try{	
66
				targetModel.load();
67
			} catch (CoreException e){
68
				MessageDialog.openError(PDEPlugin.getActiveWorkbenchShell(), PDEUIMessages.TargetPlatformPreferencePage_invalidTitle, "Could not load the target definition specified: " + e.getMessage());
69
			}
70
			IRunnableWithProgress run = new IRunnableWithProgress() {
71
				public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
72
					try {
73
						if (!targetModel.isLoaded()) {
74
							MessageDialog.openError(PDEPlugin.getActiveWorkbenchShell(), PDEUIMessages.TargetPlatformPreferencePage_invalidTitle, PDEUIMessages.TargetPlatformPreferencePage_invalidDescription);
75
							monitor.done();
76
							return;
77
						}
78
						LoadTargetOperation op = new LoadTargetOperation(targetModel.getTarget(), fTargetDefinition);
79
						PDEPlugin.getWorkspace().run(op, monitor);
80
						Object[] features = op.getMissingFeatures();
81
						Object[] plugins = op.getMissingPlugins();
82
						if (plugins.length + features.length > 0)
83
							TargetErrorDialog.showDialog(PDEPlugin.getActiveWorkbenchShell(), features, plugins);
84
					} catch (CoreException e) {
85
						throw new InvocationTargetException(e);
86
					} catch (OperationCanceledException e) {
87
						throw new InterruptedException(e.getMessage());
88
					} finally {
89
						monitor.done();
90
					}
91
				}
92
			};
93
			IProgressService service = PlatformUI.getWorkbench().getProgressService();
94
			try {
95
				service.runInUI(service, run, PDEPlugin.getWorkspace().getRoot());
96
			} catch (InvocationTargetException e) {
97
			} catch (InterruptedException e) {
98
			}
99
		}
100
		
101
		public String getText() {
102
			return "Load " + fTargetDefinition;
103
		}
104
		
105
	}
106
107
}
(-)src/org/eclipse/equinox/internal/p2/target/menu/OpenTargetDefinitionHandler.java (+71 lines)
Added Link Here
1
package org.eclipse.equinox.internal.p2.target.menu;
2
import org.eclipse.core.commands.AbstractHandler;
3
import org.eclipse.core.commands.ExecutionEvent;
4
import org.eclipse.core.commands.ExecutionException;
5
import org.eclipse.core.resources.IFile;
6
import org.eclipse.core.runtime.CoreException;
7
import org.eclipse.equinox.internal.p2.target.P2TargetDefinitionManager;
8
import org.eclipse.jface.viewers.ISelection;
9
import org.eclipse.jface.viewers.StructuredSelection;
10
import org.eclipse.jface.window.Window;
11
import org.eclipse.pde.internal.ui.IPDEUIConstants;
12
import org.eclipse.pde.internal.ui.PDEPlugin;
13
import org.eclipse.pde.internal.ui.PDEUIMessages;
14
import org.eclipse.pde.internal.ui.util.FileExtensionFilter;
15
import org.eclipse.pde.internal.ui.util.FileValidator;
16
import org.eclipse.swt.SWT;
17
import org.eclipse.swt.widgets.Event;
18
import org.eclipse.swt.widgets.Listener;
19
import org.eclipse.ui.IWorkbenchPage;
20
import org.eclipse.ui.IWorkbenchPart;
21
import org.eclipse.ui.IWorkbenchWindow;
22
import org.eclipse.ui.PartInitException;
23
import org.eclipse.ui.dialogs.ElementTreeSelectionDialog;
24
import org.eclipse.ui.ide.IDE;
25
import org.eclipse.ui.model.WorkbenchContentProvider;
26
import org.eclipse.ui.model.WorkbenchLabelProvider;
27
import org.eclipse.ui.part.ISetSelectionTarget;
28
29
30
public class OpenTargetDefinitionHandler extends AbstractHandler {
31
32
	public Object execute(ExecutionEvent arg0) throws ExecutionException {
33
		ElementTreeSelectionDialog dialog =
34
			new ElementTreeSelectionDialog(PDEPlugin.getActiveWorkbenchShell(),
35
				new WorkbenchLabelProvider(),
36
				new WorkbenchContentProvider());
37
				
38
		dialog.setValidator(new FileValidator());
39
		dialog.setAllowMultiple(false);
40
		dialog.setTitle(PDEUIMessages.TargetPlatformPreferencePage_FileSelectionTitle); 
41
		dialog.setMessage(PDEUIMessages.TargetPlatformPreferencePage_FileSelectionMessage); 
42
		dialog.addFilter(new FileExtensionFilter("target"));  //$NON-NLS-1$
43
		dialog.setInput(PDEPlugin.getWorkspace().getRoot());
44
45
		if (dialog.open() == Window.OK) {
46
			IFile file = (IFile)dialog.getFirstResult();
47
			IWorkbenchWindow ww = PDEPlugin.getActiveWorkbenchWindow();
48
			if (ww == null) {
49
				return null;
50
			}
51
			IWorkbenchPage page = ww.getActivePage();
52
			if (page == null || !file.exists())
53
				return null;
54
			IWorkbenchPart focusPart = page.getActivePart();
55
			if (focusPart instanceof ISetSelectionTarget) {
56
				ISelection selection = new StructuredSelection(file);
57
				((ISetSelectionTarget) focusPart).selectReveal(selection);
58
			}
59
			try {
60
				IDE.openEditor(page, file, IPDEUIConstants.TARGET_EDITOR_ID);
61
			} catch (PartInitException e) {
62
			}
63
			
64
			P2TargetDefinitionManager.getDefault().addTarget(file.getFullPath());
65
			
66
		}
67
		
68
		return null;
69
	}
70
71
}
(-)src/org/eclipse/equinox/internal/p2/target/menu/OpenTargetDefinitionSubMenu.java (+76 lines)
Added Link Here
1
package org.eclipse.equinox.internal.p2.target.menu;
2
3
import java.util.ArrayList;
4
import java.util.Iterator;
5
import java.util.List;
6
7
import org.eclipse.core.resources.IFile;
8
import org.eclipse.core.runtime.CoreException;
9
import org.eclipse.core.runtime.IPath;
10
import org.eclipse.equinox.internal.p2.target.P2TargetDefinitionManager;
11
import org.eclipse.jface.action.Action;
12
import org.eclipse.jface.action.ActionContributionItem;
13
import org.eclipse.jface.action.IContributionItem;
14
import org.eclipse.jface.viewers.ISelection;
15
import org.eclipse.jface.viewers.StructuredSelection;
16
import org.eclipse.pde.internal.ui.IPDEUIConstants;
17
import org.eclipse.pde.internal.ui.PDEPlugin;
18
import org.eclipse.ui.IWorkbenchPage;
19
import org.eclipse.ui.IWorkbenchPart;
20
import org.eclipse.ui.IWorkbenchWindow;
21
import org.eclipse.ui.PartInitException;
22
import org.eclipse.ui.actions.CompoundContributionItem;
23
import org.eclipse.ui.ide.IDE;
24
import org.eclipse.ui.part.ISetSelectionTarget;
25
26
public class OpenTargetDefinitionSubMenu extends CompoundContributionItem {
27
28
	@Override
29
	protected IContributionItem[] getContributionItems() {
30
		List targetList = P2TargetDefinitionManager.getDefault().getTargets();
31
		List contributionItems = new ArrayList(targetList.size());
32
		for (Iterator targetIter = targetList.iterator(); targetIter.hasNext();) {
33
			IPath currentTarget = (IPath) targetIter.next();
34
			if (currentTarget != null){
35
				OpenTargetDefinitionAction action = new OpenTargetDefinitionAction(currentTarget);
36
				contributionItems.add(new ActionContributionItem(action));
37
			}
38
		}
39
		return (IContributionItem[])contributionItems.toArray(new IContributionItem[contributionItems.size()]);
40
	}
41
	
42
	class OpenTargetDefinitionAction extends Action {
43
		
44
		private IPath fTargetDefinition;
45
		
46
		public OpenTargetDefinitionAction(IPath targetDefinitionPath){
47
			fTargetDefinition = targetDefinitionPath;
48
		}
49
		
50
		public void run() {
51
			IFile file = PDEPlugin.getWorkspace().getRoot().getFile(fTargetDefinition);
52
			IWorkbenchWindow ww = PDEPlugin.getActiveWorkbenchWindow();
53
			if (ww == null) {
54
				return;
55
			}
56
			IWorkbenchPage page = ww.getActivePage();
57
			if (page == null || !file.exists())
58
				return;
59
			IWorkbenchPart focusPart = page.getActivePart();
60
			if (focusPart instanceof ISetSelectionTarget) {
61
				ISelection selection = new StructuredSelection(file);
62
				((ISetSelectionTarget) focusPart).selectReveal(selection);
63
			}
64
			try {
65
				IDE.openEditor(page, file, IPDEUIConstants.TARGET_EDITOR_ID);
66
			} catch (PartInitException e) {
67
			}
68
		}
69
		
70
		public String getText() {
71
			return "Open " + fTargetDefinition;
72
		}
73
		
74
	}
75
76
}

Return to bug 204347