|
1 package com.nokia.carbide.internal.discovery.ui.view; |
|
2 |
|
3 import java.io.File; |
|
4 import java.io.FileInputStream; |
|
5 import java.io.IOException; |
|
6 import java.io.InputStream; |
|
7 import java.net.URI; |
|
8 import java.net.URISyntaxException; |
|
9 import java.net.URL; |
|
10 import java.text.MessageFormat; |
|
11 import java.util.ArrayList; |
|
12 import java.util.Collection; |
|
13 import java.util.HashSet; |
|
14 import java.util.List; |
|
15 import java.util.Properties; |
|
16 import java.util.Set; |
|
17 |
|
18 import org.eclipse.core.runtime.IPath; |
|
19 import org.eclipse.core.runtime.Path; |
|
20 import org.eclipse.core.runtime.Platform; |
|
21 import org.eclipse.equinox.internal.p2.discovery.Catalog; |
|
22 import org.eclipse.equinox.internal.p2.discovery.DiscoveryCore; |
|
23 import org.eclipse.equinox.internal.p2.discovery.compatibility.BundleDiscoveryStrategy; |
|
24 import org.eclipse.equinox.internal.p2.discovery.compatibility.RemoteBundleDiscoveryStrategy; |
|
25 import org.eclipse.equinox.internal.p2.discovery.model.CatalogItem; |
|
26 import org.eclipse.equinox.internal.p2.ui.discovery.DiscoveryUi; |
|
27 import org.eclipse.equinox.internal.p2.ui.discovery.wizards.CatalogConfiguration; |
|
28 import org.eclipse.equinox.internal.p2.ui.discovery.wizards.CatalogViewer; |
|
29 import org.eclipse.equinox.p2.core.IProvisioningAgent; |
|
30 import org.eclipse.equinox.p2.operations.ProvisioningSession; |
|
31 import org.eclipse.equinox.p2.repository.artifact.IArtifactRepositoryManager; |
|
32 import org.eclipse.equinox.p2.repository.metadata.IMetadataRepositoryManager; |
|
33 import org.eclipse.equinox.p2.ui.ProvisioningUI; |
|
34 import org.eclipse.jface.action.Action; |
|
35 import org.eclipse.jface.action.IAction; |
|
36 import org.eclipse.jface.dialogs.ProgressMonitorDialog; |
|
37 import org.eclipse.jface.layout.GridDataFactory; |
|
38 import org.eclipse.jface.layout.GridLayoutFactory; |
|
39 import org.eclipse.jface.resource.ImageDescriptor; |
|
40 import org.eclipse.jface.viewers.ISelectionChangedListener; |
|
41 import org.eclipse.jface.viewers.IStructuredSelection; |
|
42 import org.eclipse.jface.viewers.SelectionChangedEvent; |
|
43 import org.eclipse.jface.viewers.StructuredSelection; |
|
44 import org.eclipse.osgi.service.datalocation.Location; |
|
45 import org.eclipse.swt.SWT; |
|
46 import org.eclipse.swt.widgets.Composite; |
|
47 import org.eclipse.swt.widgets.Control; |
|
48 import org.eclipse.swt.widgets.Display; |
|
49 import org.eclipse.swt.widgets.Shell; |
|
50 import org.eclipse.ui.IActionBars; |
|
51 import org.eclipse.ui.IEditorPart; |
|
52 import org.eclipse.ui.PlatformUI; |
|
53 import org.eclipse.ui.actions.BaseSelectionListenerAction; |
|
54 |
|
55 import com.nokia.carbide.discovery.ui.Activator; |
|
56 import com.nokia.carbide.discovery.ui.Messages; |
|
57 import com.nokia.carbide.internal.discovery.ui.extension.IPortalPage; |
|
58 import com.nokia.cpp.internal.api.utils.ui.WorkbenchUtils; |
|
59 |
|
60 @SuppressWarnings("restriction") |
|
61 public class InstallExtensionsPage implements IPortalPage { |
|
62 |
|
63 private final class RunnableContextDialog extends ProgressMonitorDialog { |
|
64 private final String title; |
|
65 |
|
66 private RunnableContextDialog(Shell parent, String title) { |
|
67 super(parent); |
|
68 this.title = title; |
|
69 } |
|
70 |
|
71 @Override |
|
72 protected void configureShell(Shell shell) { |
|
73 super.configureShell(shell); |
|
74 shell.setText(title); |
|
75 } |
|
76 |
|
77 } |
|
78 |
|
79 private final class ActionBar implements IActionBar { |
|
80 private IAction[] actions; |
|
81 |
|
82 public ActionBar(IEditorPart part) { |
|
83 actions = makeActions(part); |
|
84 } |
|
85 |
|
86 @Override |
|
87 public String getTitle() { |
|
88 return "Install Extensions"; |
|
89 } |
|
90 |
|
91 @Override |
|
92 public IAction[] getActions() { |
|
93 return actions; |
|
94 } |
|
95 } |
|
96 |
|
97 private static final String DIRECTORY_KEY = "com.nokia.carbide.discovery.directory"; //$NON-NLS-1$ |
|
98 |
|
99 private CatalogViewer viewer; |
|
100 private List<ISelectionChangedListener> selectionListeners; |
|
101 private IActionBar actionBar; |
|
102 private IActionUIUpdater updater; |
|
103 |
|
104 public InstallExtensionsPage() { |
|
105 } |
|
106 |
|
107 @Override |
|
108 public String getText() { |
|
109 return "Install Extensions"; |
|
110 } |
|
111 |
|
112 @Override |
|
113 public ImageDescriptor getImageDescriptor() { |
|
114 return Activator.getImageDescriptor("icons/icon-discovery.png"); //$NON-NLS-1$ |
|
115 } |
|
116 |
|
117 @Override |
|
118 public Control createControl(Composite parent, IEditorPart part) { |
|
119 Composite c = new Composite(parent, SWT.NONE); |
|
120 GridLayoutFactory.swtDefaults().applyTo(c); |
|
121 viewer = new CatalogViewer(getCatalog(), part.getEditorSite(), |
|
122 new RunnableContextDialog(part.getEditorSite().getShell(), |
|
123 Messages.DiscoveryView_GatherExtensionsTitle), |
|
124 getConfiguration()); |
|
125 viewer.createControl(c); |
|
126 GridDataFactory.fillDefaults().grab(true, true).applyTo(viewer.getControl()); |
|
127 |
|
128 // Create the help context id for the viewer's control |
|
129 PlatformUI.getWorkbench().getHelpSystem().setHelp(viewer.getControl(), |
|
130 "com.nokia.carbide.discovery.ui.view.DiscoveryView.catalogviewer"); //$NON-NLS-1$ |
|
131 return c; |
|
132 } |
|
133 |
|
134 @Override |
|
135 public void init() { |
|
136 if (!WorkbenchUtils.isJUnitRunning()) { // do not initialize the catalog if JUnit is running |
|
137 Display.getDefault().asyncExec(new Runnable() { |
|
138 @Override |
|
139 public void run() { |
|
140 for (ISelectionChangedListener listener : selectionListeners) { |
|
141 viewer.addSelectionChangedListener(listener); |
|
142 } |
|
143 viewer.updateCatalog(); |
|
144 } |
|
145 }); |
|
146 } |
|
147 } |
|
148 |
|
149 @Override |
|
150 public IActionBar[] createCommandBars(IEditorPart part, IActionUIUpdater updater) { |
|
151 this.updater = updater; |
|
152 actionBar = new ActionBar(part); |
|
153 return new IActionBar[] { actionBar }; |
|
154 } |
|
155 |
|
156 private CatalogConfiguration getConfiguration() { |
|
157 CatalogConfiguration configuration = new CatalogConfiguration(); |
|
158 configuration.setShowTagFilter(false); |
|
159 return configuration; |
|
160 } |
|
161 |
|
162 private Catalog getCatalog() { |
|
163 Catalog catalog = new Catalog(); |
|
164 catalog.setEnvironment(DiscoveryCore.createEnvironment()); |
|
165 catalog.setVerifyUpdateSiteAvailability(false); |
|
166 |
|
167 // look for remote descriptor |
|
168 RemoteBundleDiscoveryStrategy remoteDiscoveryStrategy = new RemoteBundleDiscoveryStrategy(); |
|
169 String url = getFromServerProperties(DIRECTORY_KEY); |
|
170 if (url != null) { |
|
171 remoteDiscoveryStrategy.setDirectoryUrl(url); |
|
172 catalog.getDiscoveryStrategies().add(remoteDiscoveryStrategy); |
|
173 } |
|
174 else // look for descriptors from installed bundles |
|
175 catalog.getDiscoveryStrategies().add(new BundleDiscoveryStrategy()); |
|
176 |
|
177 return catalog; |
|
178 } |
|
179 |
|
180 public static String getFromServerProperties(String key) { |
|
181 Location installLocation = Platform.getInstallLocation(); |
|
182 URL url = installLocation.getURL(); |
|
183 IPath path = new Path(url.getPath()); |
|
184 path = path.append("configuration/server.properties"); //$NON-NLS-1$ |
|
185 File file = path.toFile(); |
|
186 Properties properties = new Properties(); |
|
187 try { |
|
188 InputStream is = new FileInputStream(file); |
|
189 properties.load(is); |
|
190 is.close(); |
|
191 } catch (IOException e) { |
|
192 String message = |
|
193 MessageFormat.format(Messages.DiscoveryView_MissingDirectoryURLError, key); |
|
194 Activator.logError(message, e); |
|
195 } |
|
196 return (String) properties.get(key); |
|
197 } |
|
198 |
|
199 private IAction[] makeActions(final IEditorPart part) { |
|
200 selectionListeners = new ArrayList<ISelectionChangedListener>(); |
|
201 List<IAction> actions = new ArrayList<IAction>(); |
|
202 IAction action = new Action(Messages.DiscoveryView_RefreshLabel) { |
|
203 public void run() { |
|
204 viewer.setSelection(StructuredSelection.EMPTY); |
|
205 viewer.updateCatalog(); |
|
206 viewer.refresh(); |
|
207 } |
|
208 }; |
|
209 actions.add(action); |
|
210 |
|
211 action = new BaseSelectionListenerAction(Messages.DiscoveryView_CheckAllLabel) { |
|
212 public void run() { |
|
213 viewer.setSelection(StructuredSelection.EMPTY); |
|
214 viewer.setSelection(getAllItemsSelection()); |
|
215 viewer.refresh(); |
|
216 } |
|
217 |
|
218 private IStructuredSelection getAllItemsSelection() { |
|
219 List<CatalogItem> catalogItems = new ArrayList<CatalogItem>(); |
|
220 for (CatalogItem catalogItem : viewer.getCatalog().getItems()) { |
|
221 if (!catalogItem.isInstalled()) |
|
222 catalogItems.add(catalogItem); |
|
223 } |
|
224 return new StructuredSelection(catalogItems); |
|
225 } |
|
226 |
|
227 protected boolean updateSelection(IStructuredSelection selection) { |
|
228 scheduleUpdateAllActionUIs(); |
|
229 return !getAllItemsSelection().equals(selection); |
|
230 } |
|
231 }; |
|
232 action.setId(getClass().getName() + ".checkAll"); |
|
233 selectionListeners.add((ISelectionChangedListener) action); |
|
234 actions.add(action); |
|
235 |
|
236 action = new BaseSelectionListenerAction(Messages.DiscoveryView_UncheckAllLabel) { |
|
237 public void run() { |
|
238 viewer.setSelection(StructuredSelection.EMPTY); |
|
239 viewer.refresh(); |
|
240 }; |
|
241 |
|
242 protected boolean updateSelection(IStructuredSelection selection) { |
|
243 scheduleUpdateAllActionUIs(); |
|
244 return !selection.isEmpty(); |
|
245 }; |
|
246 }; |
|
247 action.setId(getClass().getName() + ".uncheckAll"); |
|
248 selectionListeners.add((ISelectionChangedListener) action); |
|
249 actions.add(action); |
|
250 |
|
251 action = new BaseSelectionListenerAction(Messages.DiscoveryView_InstallLabel) { |
|
252 public void run() { |
|
253 DiscoveryUi.install(viewer.getCheckedItems(), |
|
254 new RunnableContextDialog(part.getEditorSite().getShell(), |
|
255 Messages.DiscoveryView_GatheringInstallInfoTitle)); |
|
256 }; |
|
257 |
|
258 protected boolean updateSelection(IStructuredSelection selection) { |
|
259 scheduleUpdateAllActionUIs(); |
|
260 return !selection.isEmpty(); |
|
261 }; |
|
262 }; |
|
263 action.setId(getClass().getName() + ".install"); |
|
264 selectionListeners.add((ISelectionChangedListener) action); |
|
265 actions.add(action); |
|
266 |
|
267 action = new Action(Messages.DiscoveryView_AdvancedInstallLabel) { |
|
268 public void run() { |
|
269 showInstallWizard(); |
|
270 } |
|
271 }; |
|
272 actions.add(action); |
|
273 |
|
274 ISelectionChangedListener selectionListener = new ISelectionChangedListener() { |
|
275 @Override |
|
276 public void selectionChanged(SelectionChangedEvent event) { |
|
277 IStructuredSelection selection = (IStructuredSelection) event.getSelection(); |
|
278 IActionBars bars = part.getEditorSite().getActionBars(); |
|
279 bars.getStatusLineManager().setMessage( |
|
280 selection.isEmpty() ? null : MessageFormat.format( |
|
281 Messages.DiscoveryView_StatusLineFmt, selection.size())); |
|
282 } |
|
283 }; |
|
284 selectionListeners.add(selectionListener); |
|
285 |
|
286 return (IAction[]) actions.toArray(new IAction[actions.size()]); |
|
287 } |
|
288 |
|
289 @Override |
|
290 public void dispose() { |
|
291 for (ISelectionChangedListener listener : selectionListeners) { |
|
292 viewer.removeSelectionChangedListener(listener); |
|
293 } |
|
294 } |
|
295 |
|
296 private void showInstallWizard() { |
|
297 ProvisioningUI defaultUI = ProvisioningUI.getDefaultUI(); |
|
298 ProvisioningSession session = defaultUI.getSession(); |
|
299 IProvisioningAgent agent = session.getProvisioningAgent(); |
|
300 IMetadataRepositoryManager metadataManager = (IMetadataRepositoryManager) agent.getService(IMetadataRepositoryManager.SERVICE_NAME); |
|
301 IArtifactRepositoryManager artifactManager = (IArtifactRepositoryManager) agent.getService(IArtifactRepositoryManager.SERVICE_NAME); |
|
302 for (URI uri : getCatalogURIs()) { |
|
303 metadataManager.addRepository(uri); |
|
304 artifactManager.addRepository(uri); |
|
305 } |
|
306 defaultUI.openInstallWizard(null, null, null); |
|
307 } |
|
308 |
|
309 private Collection<URI> getCatalogURIs() { |
|
310 Set<URI> uris = new HashSet<URI>(); |
|
311 for (CatalogItem catalogItem : viewer.getCatalog().getItems()) { |
|
312 try { |
|
313 uris.add(new URI(catalogItem.getSiteUrl())); |
|
314 } catch (URISyntaxException e) { |
|
315 // ignore bad URIs |
|
316 } |
|
317 } |
|
318 return uris; |
|
319 }; |
|
320 |
|
321 private void scheduleUpdateAllActionUIs() { |
|
322 Display.getDefault().asyncExec(new Runnable() { |
|
323 @Override |
|
324 public void run() { |
|
325 updater.updateAll(); |
|
326 } |
|
327 }); |
|
328 }; |
|
329 } |