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 |
} |