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