author | dadubrow |
Mon, 09 Aug 2010 09:15:52 -0500 | |
changeset 1793 | b3fa776e81bd |
parent 1734 | bf670d38123a |
child 1810 | 2ccd3660a736 |
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.operations.ProvisioningSession; |
|
38 |
import org.eclipse.equinox.p2.repository.artifact.IArtifactRepositoryManager; |
|
39 |
import org.eclipse.equinox.p2.repository.metadata.IMetadataRepositoryManager; |
|
40 |
import org.eclipse.equinox.p2.ui.ProvisioningUI; |
|
41 |
import org.eclipse.jface.action.Action; |
|
42 |
import org.eclipse.jface.action.IAction; |
|
43 |
import org.eclipse.jface.dialogs.ProgressMonitorDialog; |
|
44 |
import org.eclipse.jface.layout.GridDataFactory; |
|
45 |
import org.eclipse.jface.layout.GridLayoutFactory; |
|
46 |
import org.eclipse.jface.viewers.ISelectionChangedListener; |
|
47 |
import org.eclipse.jface.viewers.IStructuredSelection; |
|
48 |
import org.eclipse.jface.viewers.SelectionChangedEvent; |
|
49 |
import org.eclipse.jface.viewers.StructuredSelection; |
|
50 |
import org.eclipse.swt.SWT; |
|
51 |
import org.eclipse.swt.widgets.Composite; |
|
52 |
import org.eclipse.swt.widgets.Control; |
|
53 |
import org.eclipse.swt.widgets.Display; |
|
54 |
import org.eclipse.swt.widgets.Shell; |
|
55 |
import org.eclipse.ui.IActionBars; |
|
56 |
import org.eclipse.ui.IEditorPart; |
|
57 |
import org.eclipse.ui.actions.BaseSelectionListenerAction; |
|
58 |
||
59 |
import com.nokia.carbide.discovery.ui.Activator; |
|
60 |
import com.nokia.carbide.discovery.ui.Messages; |
|
61 |
import com.nokia.cpp.internal.api.utils.ui.WorkbenchUtils; |
|
62 |
||
63 |
@SuppressWarnings("restriction") |
|
64 |
public abstract class AbstractDiscoveryPortalPageLayer implements IPortalPageLayer { |
|
65 |
||
66 |
private class RunnableContextDialog extends ProgressMonitorDialog { |
|
67 |
private final String title; |
|
68 |
||
69 |
private RunnableContextDialog(Shell parent, String title) { |
|
70 |
super(parent); |
|
71 |
this.title = title; |
|
72 |
} |
|
73 |
||
74 |
@Override |
|
75 |
protected void configureShell(Shell shell) { |
|
76 |
super.configureShell(shell); |
|
77 |
shell.setText(title); |
|
78 |
} |
|
79 |
||
80 |
} |
|
81 |
||
82 |
protected class ActionBar implements IActionBar { |
|
83 |
private IAction[] actions; |
|
84 |
||
85 |
public ActionBar(IEditorPart part) { |
|
86 |
actions = makeActions(part); |
|
87 |
} |
|
88 |
||
89 |
@Override |
|
90 |
public String getTitle() { |
|
91 |
return Messages.AbstractDiscoveryPortalPageLayer_Title; |
|
92 |
} |
|
93 |
||
94 |
@Override |
|
95 |
public IAction[] getActions() { |
|
96 |
return actions; |
|
97 |
} |
|
98 |
||
99 |
@Override |
|
100 |
public String[] getHighlightedActionIds() { |
|
101 |
return new String[] {INSTALL_ACTION_ID}; |
|
102 |
} |
|
103 |
} |
|
104 |
||
105 |
protected static final String INSTALL_ACTION_ID = |
|
106 |
AbstractDiscoveryPortalPageLayer.class.getName() + ".install"; //$NON-NLS-1$ |
|
107 |
protected static final String UNCHECK_ALL_ACTION_ID = |
|
108 |
AbstractDiscoveryPortalPageLayer.class.getName() + ".uncheckAll"; //$NON-NLS-1$ |
|
109 |
protected static final String CHECK_ALL_ACTION_ID = |
|
110 |
AbstractDiscoveryPortalPageLayer.class.getName() + ".checkAll"; //$NON-NLS-1$ |
|
111 |
protected static final String ADV_INSTALL_ACTION_ID = |
|
112 |
AbstractDiscoveryPortalPageLayer.class.getName() + ".advancedInstall"; //$NON-NLS-1$ |
|
113 |
protected static final String REFRESH_ACTION_ID = |
|
114 |
AbstractDiscoveryPortalPageLayer.class.getName() + ".refresh"; //$NON-NLS-1$ |
|
115 |
||
1793
b3fa776e81bd
make discovery layer more amenable to customization
dadubrow
parents:
1734
diff
changeset
|
116 |
private IEditorPart part; |
1734 | 117 |
private CatalogViewer viewer; |
118 |
private List<ISelectionChangedListener> selectionListeners; |
|
119 |
private IActionUIUpdater updater; |
|
120 |
||
121 |
@Override |
|
122 |
public Control createControl(Composite parent, IEditorPart part) { |
|
123 |
Composite c = new Composite(parent, SWT.NONE); |
|
124 |
GridLayoutFactory.swtDefaults().applyTo(c); |
|
125 |
viewer = new CatalogViewer(getCatalog(), part.getEditorSite(), |
|
126 |
new RunnableContextDialog(part.getEditorSite().getShell(), |
|
127 |
Messages.AbstractDiscoveryPortalPageLayer_GatheringExtensionsDesc), |
|
128 |
getConfiguration()); |
|
129 |
viewer.createControl(c); |
|
130 |
GridDataFactory.fillDefaults().grab(true, true).applyTo(viewer.getControl()); |
|
131 |
||
132 |
return c; |
|
133 |
} |
|
134 |
||
135 |
@Override |
|
136 |
public void init() { |
|
137 |
if (!WorkbenchUtils.isJUnitRunning()) { // do not initialize the catalog if JUnit is running |
|
138 |
Display.getDefault().asyncExec(new Runnable() { |
|
139 |
@Override |
|
140 |
public void run() { |
|
141 |
for (ISelectionChangedListener listener : selectionListeners) { |
|
142 |
viewer.addSelectionChangedListener(listener); |
|
143 |
} |
|
144 |
viewer.updateCatalog(); |
|
145 |
} |
|
146 |
}); |
|
147 |
} |
|
148 |
} |
|
149 |
||
150 |
@Override |
|
151 |
public IActionBar[] createCommandBars(IEditorPart part, IActionUIUpdater updater) { |
|
152 |
this.updater = updater; |
|
153 |
return new IActionBar[] { new ActionBar(part) }; |
|
154 |
} |
|
155 |
||
156 |
protected CatalogConfiguration getConfiguration() { |
|
157 |
CatalogConfiguration configuration = new CatalogConfiguration(); |
|
158 |
configuration.setShowTagFilter(false); |
|
159 |
return configuration; |
|
160 |
} |
|
161 |
||
162 |
protected 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 = getDirectoryURL(); |
|
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 |
protected String getDirectoryURL() { |
|
181 |
return Activator.getFromServerProperties(getClass().getName()); |
|
182 |
} |
|
183 |
||
184 |
protected IAction[] makeActions(final IEditorPart part) { |
|
1793
b3fa776e81bd
make discovery layer more amenable to customization
dadubrow
parents:
1734
diff
changeset
|
185 |
this.part = part; |
1734 | 186 |
selectionListeners = new ArrayList<ISelectionChangedListener>(); |
187 |
List<IAction> actions = new ArrayList<IAction>(); |
|
188 |
IAction action; |
|
189 |
||
190 |
// install |
|
191 |
action = new BaseSelectionListenerAction(Messages.AbstractDiscoveryPortalPageLayer_InstallActionLabel) { |
|
192 |
public void run() { |
|
1793
b3fa776e81bd
make discovery layer more amenable to customization
dadubrow
parents:
1734
diff
changeset
|
193 |
installCatalogItems(viewer.getCheckedItems()); |
1734 | 194 |
}; |
195 |
||
196 |
protected boolean updateSelection(IStructuredSelection selection) { |
|
197 |
scheduleUpdateAllActionUIs(); |
|
198 |
return !selection.isEmpty(); |
|
199 |
}; |
|
200 |
}; |
|
201 |
action.setToolTipText(Messages.AbstractDiscoveryPortalPageLayer_InstallActionTooltip); |
|
202 |
action.setId(INSTALL_ACTION_ID); |
|
203 |
selectionListeners.add((ISelectionChangedListener) action); |
|
204 |
actions.add(action); |
|
205 |
||
206 |
// refresh |
|
207 |
action = new Action(Messages.AbstractDiscoveryPortalPageLayer_RefreshActionLabel) { |
|
208 |
public void run() { |
|
209 |
viewer.setSelection(StructuredSelection.EMPTY); |
|
210 |
viewer.updateCatalog(); |
|
211 |
viewer.refresh(); |
|
212 |
} |
|
213 |
}; |
|
214 |
action.setId(REFRESH_ACTION_ID); |
|
215 |
actions.add(action); |
|
216 |
||
217 |
// check all |
|
218 |
action = new BaseSelectionListenerAction(Messages.AbstractDiscoveryPortalPageLayer_CheckAllActionLabel) { |
|
219 |
public void run() { |
|
220 |
viewer.setSelection(StructuredSelection.EMPTY); |
|
221 |
viewer.setSelection(getAllItemsSelection()); |
|
222 |
viewer.refresh(); |
|
223 |
} |
|
224 |
||
225 |
private IStructuredSelection getAllItemsSelection() { |
|
226 |
List<CatalogItem> catalogItems = new ArrayList<CatalogItem>(); |
|
227 |
for (CatalogItem catalogItem : viewer.getCatalog().getItems()) { |
|
228 |
if (!catalogItem.isInstalled()) |
|
229 |
catalogItems.add(catalogItem); |
|
230 |
} |
|
231 |
return new StructuredSelection(catalogItems); |
|
232 |
} |
|
233 |
||
234 |
protected boolean updateSelection(IStructuredSelection selection) { |
|
235 |
scheduleUpdateAllActionUIs(); |
|
236 |
return !getAllItemsSelection().equals(selection); |
|
237 |
} |
|
238 |
}; |
|
239 |
action.setId(CHECK_ALL_ACTION_ID); |
|
240 |
selectionListeners.add((ISelectionChangedListener) action); |
|
241 |
actions.add(action); |
|
242 |
||
243 |
// uncheck all |
|
244 |
action = new BaseSelectionListenerAction(Messages.AbstractDiscoveryPortalPageLayer_UncheckAllActionLabel) { |
|
245 |
public void run() { |
|
246 |
viewer.setSelection(StructuredSelection.EMPTY); |
|
247 |
viewer.refresh(); |
|
248 |
}; |
|
249 |
||
250 |
protected boolean updateSelection(IStructuredSelection selection) { |
|
251 |
scheduleUpdateAllActionUIs(); |
|
252 |
return !selection.isEmpty(); |
|
253 |
}; |
|
254 |
}; |
|
255 |
action.setId(UNCHECK_ALL_ACTION_ID); |
|
256 |
selectionListeners.add((ISelectionChangedListener) action); |
|
257 |
actions.add(action); |
|
258 |
||
259 |
// advanced install |
|
260 |
action = new Action(Messages.AbstractDiscoveryPortalPageLayer_AdvancedInstallActionLabel) { |
|
261 |
public void run() { |
|
262 |
showInstallWizard(); |
|
263 |
} |
|
264 |
}; |
|
265 |
action.setId(ADV_INSTALL_ACTION_ID); |
|
266 |
actions.add(action); |
|
267 |
||
268 |
ISelectionChangedListener selectionListener = new ISelectionChangedListener() { |
|
269 |
@Override |
|
270 |
public void selectionChanged(SelectionChangedEvent event) { |
|
271 |
IStructuredSelection selection = (IStructuredSelection) event.getSelection(); |
|
272 |
IActionBars bars = part.getEditorSite().getActionBars(); |
|
273 |
bars.getStatusLineManager().setMessage( |
|
274 |
selection.isEmpty() ? null : MessageFormat.format( |
|
275 |
Messages.AbstractDiscoveryPortalPageLayer_CheckedItemsStatusMessage, selection.size())); |
|
276 |
} |
|
277 |
}; |
|
278 |
selectionListeners.add(selectionListener); |
|
279 |
||
280 |
return (IAction[]) actions.toArray(new IAction[actions.size()]); |
|
281 |
} |
|
282 |
||
1793
b3fa776e81bd
make discovery layer more amenable to customization
dadubrow
parents:
1734
diff
changeset
|
283 |
protected void installCatalogItems(List<CatalogItem> items) { |
b3fa776e81bd
make discovery layer more amenable to customization
dadubrow
parents:
1734
diff
changeset
|
284 |
DiscoveryUi.install(items, |
b3fa776e81bd
make discovery layer more amenable to customization
dadubrow
parents:
1734
diff
changeset
|
285 |
new RunnableContextDialog(part.getEditorSite().getShell(), |
b3fa776e81bd
make discovery layer more amenable to customization
dadubrow
parents:
1734
diff
changeset
|
286 |
Messages.AbstractDiscoveryPortalPageLayer_GatheringInstallInfoDesc)); |
b3fa776e81bd
make discovery layer more amenable to customization
dadubrow
parents:
1734
diff
changeset
|
287 |
} |
b3fa776e81bd
make discovery layer more amenable to customization
dadubrow
parents:
1734
diff
changeset
|
288 |
|
1734 | 289 |
@Override |
290 |
public void dispose() { |
|
291 |
for (ISelectionChangedListener listener : selectionListeners) { |
|
292 |
viewer.removeSelectionChangedListener(listener); |
|
293 |
} |
|
294 |
} |
|
295 |
||
296 |
protected 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); |
|
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 |
} |
|
306 |
defaultUI.openInstallWizard(null, null, null); |
|
307 |
} |
|
308 |
||
1793
b3fa776e81bd
make discovery layer more amenable to customization
dadubrow
parents:
1734
diff
changeset
|
309 |
protected static Collection<URI> getSiteURIs(List<CatalogItem> catalogItems) { |
1734 | 310 |
Set<URI> uris = new HashSet<URI>(); |
1793
b3fa776e81bd
make discovery layer more amenable to customization
dadubrow
parents:
1734
diff
changeset
|
311 |
for (CatalogItem catalogItem : catalogItems) { |
1734 | 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 |
protected void scheduleUpdateAllActionUIs() { |
|
322 |
Display.getDefault().asyncExec(new Runnable() { |
|
323 |
@Override |
|
324 |
public void run() { |
|
325 |
updater.updateAll(); |
|
326 |
} |
|
327 |
}); |
|
328 |
} |
|
329 |
||
330 |
} |