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