author | dadubrow |
Fri, 13 Aug 2010 08:37:37 -0500 | |
changeset 1822 | 8fdfd1446ad7 |
parent 1810 | 2ccd3660a736 |
child 1824 | 7e34f628583a |
permissions | -rw-r--r-- |
1734 | 1 |
/* |
2 |
* Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies). |
|
3 |
* All rights reserved. |
|
4 |
* This component and the accompanying materials are made available |
|
5 |
* under the terms of the License "Eclipse Public License v1.0" |
|
6 |
* which accompanies this distribution, and is available |
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 |
* |
|
9 |
* Initial Contributors: |
|
10 |
* Nokia Corporation - initial contribution. |
|
11 |
* |
|
12 |
* Contributors: |
|
13 |
* |
|
14 |
* Description: |
|
15 |
* |
|
16 |
*/ |
|
17 |
package com.nokia.carbide.internal.discovery.ui.extension; |
|
18 |
||
19 |
import java.net.URI; |
|
20 |
import java.net.URISyntaxException; |
|
21 |
import java.text.MessageFormat; |
|
22 |
import java.util.ArrayList; |
|
23 |
import java.util.Collection; |
|
24 |
import java.util.HashSet; |
|
25 |
import java.util.List; |
|
26 |
import java.util.Set; |
|
27 |
||
28 |
import org.eclipse.equinox.internal.p2.discovery.Catalog; |
|
29 |
import org.eclipse.equinox.internal.p2.discovery.DiscoveryCore; |
|
30 |
import org.eclipse.equinox.internal.p2.discovery.compatibility.BundleDiscoveryStrategy; |
|
31 |
import org.eclipse.equinox.internal.p2.discovery.compatibility.RemoteBundleDiscoveryStrategy; |
|
32 |
import org.eclipse.equinox.internal.p2.discovery.model.CatalogItem; |
|
33 |
import org.eclipse.equinox.internal.p2.ui.discovery.DiscoveryUi; |
|
34 |
import org.eclipse.equinox.internal.p2.ui.discovery.wizards.CatalogConfiguration; |
|
35 |
import org.eclipse.equinox.internal.p2.ui.discovery.wizards.CatalogViewer; |
|
36 |
import org.eclipse.equinox.p2.core.IProvisioningAgent; |
|
37 |
import org.eclipse.equinox.p2.repository.artifact.IArtifactRepositoryManager; |
|
38 |
import org.eclipse.equinox.p2.repository.metadata.IMetadataRepositoryManager; |
|
39 |
import org.eclipse.equinox.p2.ui.ProvisioningUI; |
|
40 |
import org.eclipse.jface.action.Action; |
|
41 |
import org.eclipse.jface.action.IAction; |
|
42 |
import org.eclipse.jface.dialogs.ProgressMonitorDialog; |
|
43 |
import org.eclipse.jface.layout.GridDataFactory; |
|
44 |
import org.eclipse.jface.layout.GridLayoutFactory; |
|
45 |
import org.eclipse.jface.viewers.ISelectionChangedListener; |
|
46 |
import org.eclipse.jface.viewers.IStructuredSelection; |
|
47 |
import org.eclipse.jface.viewers.SelectionChangedEvent; |
|
48 |
import org.eclipse.jface.viewers.StructuredSelection; |
|
49 |
import org.eclipse.swt.SWT; |
|
50 |
import org.eclipse.swt.widgets.Composite; |
|
51 |
import org.eclipse.swt.widgets.Control; |
|
52 |
import org.eclipse.swt.widgets.Display; |
|
53 |
import org.eclipse.swt.widgets.Shell; |
|
54 |
import org.eclipse.ui.IActionBars; |
|
55 |
import org.eclipse.ui.IEditorPart; |
|
1810
2ccd3660a736
feature config exporter + non-functional importer
dadubrow
parents:
1793
diff
changeset
|
56 |
import org.eclipse.ui.PlatformUI; |
1734 | 57 |
import org.eclipse.ui.actions.BaseSelectionListenerAction; |
1810
2ccd3660a736
feature config exporter + non-functional importer
dadubrow
parents:
1793
diff
changeset
|
58 |
import org.eclipse.ui.handlers.IHandlerService; |
1734 | 59 |
|
60 |
import com.nokia.carbide.discovery.ui.Activator; |
|
61 |
import com.nokia.carbide.discovery.ui.Messages; |
|
62 |
import com.nokia.cpp.internal.api.utils.ui.WorkbenchUtils; |
|
63 |
||
64 |
@SuppressWarnings("restriction") |
|
65 |
public abstract class AbstractDiscoveryPortalPageLayer implements IPortalPageLayer { |
|
66 |
||
67 |
private class RunnableContextDialog extends ProgressMonitorDialog { |
|
68 |
private final String title; |
|
69 |
||
70 |
private RunnableContextDialog(Shell parent, String title) { |
|
71 |
super(parent); |
|
72 |
this.title = title; |
|
73 |
} |
|
74 |
||
75 |
@Override |
|
76 |
protected void configureShell(Shell shell) { |
|
77 |
super.configureShell(shell); |
|
78 |
shell.setText(title); |
|
79 |
} |
|
80 |
||
81 |
} |
|
82 |
||
83 |
protected class ActionBar implements IActionBar { |
|
84 |
private IAction[] actions; |
|
85 |
||
86 |
public ActionBar(IEditorPart part) { |
|
87 |
actions = makeActions(part); |
|
88 |
} |
|
89 |
||
90 |
@Override |
|
91 |
public String getTitle() { |
|
92 |
return Messages.AbstractDiscoveryPortalPageLayer_Title; |
|
93 |
} |
|
94 |
||
95 |
@Override |
|
96 |
public IAction[] getActions() { |
|
97 |
return actions; |
|
98 |
} |
|
99 |
||
100 |
@Override |
|
101 |
public String[] getHighlightedActionIds() { |
|
102 |
return new String[] {INSTALL_ACTION_ID}; |
|
103 |
} |
|
104 |
} |
|
105 |
||
106 |
protected static final String INSTALL_ACTION_ID = |
|
107 |
AbstractDiscoveryPortalPageLayer.class.getName() + ".install"; //$NON-NLS-1$ |
|
108 |
protected static final String UNCHECK_ALL_ACTION_ID = |
|
109 |
AbstractDiscoveryPortalPageLayer.class.getName() + ".uncheckAll"; //$NON-NLS-1$ |
|
110 |
protected static final String CHECK_ALL_ACTION_ID = |
|
111 |
AbstractDiscoveryPortalPageLayer.class.getName() + ".checkAll"; //$NON-NLS-1$ |
|
112 |
protected static final String ADV_INSTALL_ACTION_ID = |
|
113 |
AbstractDiscoveryPortalPageLayer.class.getName() + ".advancedInstall"; //$NON-NLS-1$ |
|
114 |
protected static final String REFRESH_ACTION_ID = |
|
115 |
AbstractDiscoveryPortalPageLayer.class.getName() + ".refresh"; //$NON-NLS-1$ |
|
116 |
||
1810
2ccd3660a736
feature config exporter + non-functional importer
dadubrow
parents:
1793
diff
changeset
|
117 |
protected IEditorPart part; |
1734 | 118 |
private CatalogViewer viewer; |
119 |
private List<ISelectionChangedListener> selectionListeners; |
|
120 |
private IActionUIUpdater updater; |
|
121 |
||
122 |
@Override |
|
123 |
public Control createControl(Composite parent, IEditorPart part) { |
|
124 |
Composite c = new Composite(parent, SWT.NONE); |
|
125 |
GridLayoutFactory.swtDefaults().applyTo(c); |
|
126 |
viewer = new CatalogViewer(getCatalog(), part.getEditorSite(), |
|
127 |
new RunnableContextDialog(part.getEditorSite().getShell(), |
|
128 |
Messages.AbstractDiscoveryPortalPageLayer_GatheringExtensionsDesc), |
|
129 |
getConfiguration()); |
|
130 |
viewer.createControl(c); |
|
131 |
GridDataFactory.fillDefaults().grab(true, true).applyTo(viewer.getControl()); |
|
132 |
||
133 |
return c; |
|
134 |
} |
|
135 |
||
136 |
@Override |
|
137 |
public void init() { |
|
138 |
if (!WorkbenchUtils.isJUnitRunning()) { // do not initialize the catalog if JUnit is running |
|
139 |
Display.getDefault().asyncExec(new Runnable() { |
|
140 |
@Override |
|
141 |
public void run() { |
|
142 |
for (ISelectionChangedListener listener : selectionListeners) { |
|
143 |
viewer.addSelectionChangedListener(listener); |
|
144 |
} |
|
145 |
viewer.updateCatalog(); |
|
1822 | 146 |
viewer.getControl().setFocus(); |
1734 | 147 |
} |
148 |
}); |
|
149 |
} |
|
150 |
} |
|
151 |
||
152 |
@Override |
|
153 |
public IActionBar[] createCommandBars(IEditorPart part, IActionUIUpdater updater) { |
|
154 |
this.updater = updater; |
|
155 |
return new IActionBar[] { new ActionBar(part) }; |
|
156 |
} |
|
157 |
||
158 |
protected CatalogConfiguration getConfiguration() { |
|
159 |
CatalogConfiguration configuration = new CatalogConfiguration(); |
|
160 |
configuration.setShowTagFilter(false); |
|
161 |
return configuration; |
|
162 |
} |
|
163 |
||
164 |
protected Catalog getCatalog() { |
|
165 |
Catalog catalog = new Catalog(); |
|
166 |
catalog.setEnvironment(DiscoveryCore.createEnvironment()); |
|
167 |
catalog.setVerifyUpdateSiteAvailability(false); |
|
168 |
||
169 |
// look for remote descriptor |
|
170 |
RemoteBundleDiscoveryStrategy remoteDiscoveryStrategy = new RemoteBundleDiscoveryStrategy(); |
|
171 |
String url = getDirectoryURL(); |
|
172 |
if (url != null) { |
|
173 |
remoteDiscoveryStrategy.setDirectoryUrl(url); |
|
174 |
catalog.getDiscoveryStrategies().add(remoteDiscoveryStrategy); |
|
175 |
} |
|
176 |
else // look for descriptors from installed bundles |
|
177 |
catalog.getDiscoveryStrategies().add(new BundleDiscoveryStrategy()); |
|
178 |
||
179 |
return catalog; |
|
180 |
} |
|
181 |
||
182 |
protected String getDirectoryURL() { |
|
183 |
return Activator.getFromServerProperties(getClass().getName()); |
|
184 |
} |
|
185 |
||
186 |
protected IAction[] makeActions(final IEditorPart part) { |
|
1793
b3fa776e81bd
make discovery layer more amenable to customization
dadubrow
parents:
1734
diff
changeset
|
187 |
this.part = part; |
1734 | 188 |
selectionListeners = new ArrayList<ISelectionChangedListener>(); |
189 |
List<IAction> actions = new ArrayList<IAction>(); |
|
190 |
IAction action; |
|
191 |
||
192 |
// install |
|
193 |
action = new BaseSelectionListenerAction(Messages.AbstractDiscoveryPortalPageLayer_InstallActionLabel) { |
|
194 |
public void run() { |
|
1793
b3fa776e81bd
make discovery layer more amenable to customization
dadubrow
parents:
1734
diff
changeset
|
195 |
installCatalogItems(viewer.getCheckedItems()); |
1734 | 196 |
}; |
197 |
||
198 |
protected boolean updateSelection(IStructuredSelection selection) { |
|
199 |
scheduleUpdateAllActionUIs(); |
|
200 |
return !selection.isEmpty(); |
|
201 |
}; |
|
202 |
}; |
|
203 |
action.setToolTipText(Messages.AbstractDiscoveryPortalPageLayer_InstallActionTooltip); |
|
204 |
action.setId(INSTALL_ACTION_ID); |
|
205 |
selectionListeners.add((ISelectionChangedListener) action); |
|
206 |
actions.add(action); |
|
207 |
||
208 |
// refresh |
|
209 |
action = new Action(Messages.AbstractDiscoveryPortalPageLayer_RefreshActionLabel) { |
|
210 |
public void run() { |
|
211 |
viewer.setSelection(StructuredSelection.EMPTY); |
|
212 |
viewer.updateCatalog(); |
|
213 |
viewer.refresh(); |
|
214 |
} |
|
215 |
}; |
|
216 |
action.setId(REFRESH_ACTION_ID); |
|
217 |
actions.add(action); |
|
218 |
||
219 |
// check all |
|
220 |
action = new BaseSelectionListenerAction(Messages.AbstractDiscoveryPortalPageLayer_CheckAllActionLabel) { |
|
221 |
public void run() { |
|
222 |
viewer.setSelection(StructuredSelection.EMPTY); |
|
223 |
viewer.setSelection(getAllItemsSelection()); |
|
224 |
viewer.refresh(); |
|
225 |
} |
|
226 |
||
227 |
private IStructuredSelection getAllItemsSelection() { |
|
228 |
List<CatalogItem> catalogItems = new ArrayList<CatalogItem>(); |
|
229 |
for (CatalogItem catalogItem : viewer.getCatalog().getItems()) { |
|
230 |
if (!catalogItem.isInstalled()) |
|
231 |
catalogItems.add(catalogItem); |
|
232 |
} |
|
233 |
return new StructuredSelection(catalogItems); |
|
234 |
} |
|
235 |
||
236 |
protected boolean updateSelection(IStructuredSelection selection) { |
|
237 |
scheduleUpdateAllActionUIs(); |
|
238 |
return !getAllItemsSelection().equals(selection); |
|
239 |
} |
|
240 |
}; |
|
241 |
action.setId(CHECK_ALL_ACTION_ID); |
|
242 |
selectionListeners.add((ISelectionChangedListener) action); |
|
243 |
actions.add(action); |
|
244 |
||
245 |
// uncheck all |
|
246 |
action = new BaseSelectionListenerAction(Messages.AbstractDiscoveryPortalPageLayer_UncheckAllActionLabel) { |
|
247 |
public void run() { |
|
248 |
viewer.setSelection(StructuredSelection.EMPTY); |
|
249 |
viewer.refresh(); |
|
250 |
}; |
|
251 |
||
252 |
protected boolean updateSelection(IStructuredSelection selection) { |
|
253 |
scheduleUpdateAllActionUIs(); |
|
254 |
return !selection.isEmpty(); |
|
255 |
}; |
|
256 |
}; |
|
257 |
action.setId(UNCHECK_ALL_ACTION_ID); |
|
258 |
selectionListeners.add((ISelectionChangedListener) action); |
|
259 |
actions.add(action); |
|
260 |
||
261 |
// advanced install |
|
262 |
action = new Action(Messages.AbstractDiscoveryPortalPageLayer_AdvancedInstallActionLabel) { |
|
263 |
public void run() { |
|
264 |
showInstallWizard(); |
|
265 |
} |
|
266 |
}; |
|
267 |
action.setId(ADV_INSTALL_ACTION_ID); |
|
268 |
actions.add(action); |
|
269 |
||
270 |
ISelectionChangedListener selectionListener = new ISelectionChangedListener() { |
|
271 |
@Override |
|
272 |
public void selectionChanged(SelectionChangedEvent event) { |
|
273 |
IStructuredSelection selection = (IStructuredSelection) event.getSelection(); |
|
274 |
IActionBars bars = part.getEditorSite().getActionBars(); |
|
275 |
bars.getStatusLineManager().setMessage( |
|
276 |
selection.isEmpty() ? null : MessageFormat.format( |
|
277 |
Messages.AbstractDiscoveryPortalPageLayer_CheckedItemsStatusMessage, selection.size())); |
|
278 |
} |
|
279 |
}; |
|
280 |
selectionListeners.add(selectionListener); |
|
281 |
||
282 |
return (IAction[]) actions.toArray(new IAction[actions.size()]); |
|
283 |
} |
|
284 |
||
1793
b3fa776e81bd
make discovery layer more amenable to customization
dadubrow
parents:
1734
diff
changeset
|
285 |
protected void installCatalogItems(List<CatalogItem> items) { |
b3fa776e81bd
make discovery layer more amenable to customization
dadubrow
parents:
1734
diff
changeset
|
286 |
DiscoveryUi.install(items, |
b3fa776e81bd
make discovery layer more amenable to customization
dadubrow
parents:
1734
diff
changeset
|
287 |
new RunnableContextDialog(part.getEditorSite().getShell(), |
b3fa776e81bd
make discovery layer more amenable to customization
dadubrow
parents:
1734
diff
changeset
|
288 |
Messages.AbstractDiscoveryPortalPageLayer_GatheringInstallInfoDesc)); |
b3fa776e81bd
make discovery layer more amenable to customization
dadubrow
parents:
1734
diff
changeset
|
289 |
} |
b3fa776e81bd
make discovery layer more amenable to customization
dadubrow
parents:
1734
diff
changeset
|
290 |
|
1734 | 291 |
@Override |
292 |
public void dispose() { |
|
293 |
for (ISelectionChangedListener listener : selectionListeners) { |
|
294 |
viewer.removeSelectionChangedListener(listener); |
|
295 |
} |
|
296 |
} |
|
297 |
||
298 |
protected void showInstallWizard() { |
|
1810
2ccd3660a736
feature config exporter + non-functional importer
dadubrow
parents:
1793
diff
changeset
|
299 |
IProvisioningAgent agent = ProvisioningUI.getDefaultUI().getSession().getProvisioningAgent(); |
1734 | 300 |
IMetadataRepositoryManager metadataManager = (IMetadataRepositoryManager) agent.getService(IMetadataRepositoryManager.SERVICE_NAME); |
301 |
IArtifactRepositoryManager artifactManager = (IArtifactRepositoryManager) agent.getService(IArtifactRepositoryManager.SERVICE_NAME); |
|
1793
b3fa776e81bd
make discovery layer more amenable to customization
dadubrow
parents:
1734
diff
changeset
|
302 |
for (URI uri : getSiteURIs(viewer.getCatalog().getItems())) { |
1734 | 303 |
metadataManager.addRepository(uri); |
304 |
artifactManager.addRepository(uri); |
|
305 |
} |
|
1810
2ccd3660a736
feature config exporter + non-functional importer
dadubrow
parents:
1793
diff
changeset
|
306 |
IHandlerService handlerService = |
2ccd3660a736
feature config exporter + non-functional importer
dadubrow
parents:
1793
diff
changeset
|
307 |
(IHandlerService) PlatformUI.getWorkbench().getService(IHandlerService.class); |
2ccd3660a736
feature config exporter + non-functional importer
dadubrow
parents:
1793
diff
changeset
|
308 |
try { |
2ccd3660a736
feature config exporter + non-functional importer
dadubrow
parents:
1793
diff
changeset
|
309 |
handlerService.executeCommand("org.eclipse.equinox.p2.ui.sdk.install", null); |
2ccd3660a736
feature config exporter + non-functional importer
dadubrow
parents:
1793
diff
changeset
|
310 |
} catch (Exception e) { |
2ccd3660a736
feature config exporter + non-functional importer
dadubrow
parents:
1793
diff
changeset
|
311 |
Activator.logError("Could not open install wizard", e); |
2ccd3660a736
feature config exporter + non-functional importer
dadubrow
parents:
1793
diff
changeset
|
312 |
} |
1734 | 313 |
} |
314 |
||
1793
b3fa776e81bd
make discovery layer more amenable to customization
dadubrow
parents:
1734
diff
changeset
|
315 |
protected static Collection<URI> getSiteURIs(List<CatalogItem> catalogItems) { |
1734 | 316 |
Set<URI> uris = new HashSet<URI>(); |
1793
b3fa776e81bd
make discovery layer more amenable to customization
dadubrow
parents:
1734
diff
changeset
|
317 |
for (CatalogItem catalogItem : catalogItems) { |
1734 | 318 |
try { |
319 |
uris.add(new URI(catalogItem.getSiteUrl())); |
|
320 |
} catch (URISyntaxException e) { |
|
321 |
// ignore bad URIs |
|
322 |
} |
|
323 |
} |
|
324 |
return uris; |
|
325 |
} |
|
326 |
||
327 |
protected void scheduleUpdateAllActionUIs() { |
|
328 |
Display.getDefault().asyncExec(new Runnable() { |
|
329 |
@Override |
|
330 |
public void run() { |
|
331 |
updater.updateAll(); |
|
332 |
} |
|
333 |
}); |
|
334 |
} |
|
335 |
||
336 |
} |