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