author | dadubrow |
Wed, 19 May 2010 15:35:14 -0500 | |
changeset 1374 | 67b625db1866 |
parent 1366 | c61bb9dcfe74 |
child 1385 | e626d1981906 |
permissions | -rw-r--r-- |
1360 | 1 |
package com.nokia.carbide.discovery.ui.view; |
2 |
||
3 |
||
1363 | 4 |
import java.io.File; |
5 |
import java.io.FileInputStream; |
|
6 |
import java.io.IOException; |
|
7 |
import java.io.InputStream; |
|
8 |
import java.net.URL; |
|
9 |
import java.text.MessageFormat; |
|
1360 | 10 |
import java.util.List; |
1363 | 11 |
import java.util.Properties; |
1360 | 12 |
|
1363 | 13 |
import org.eclipse.core.runtime.IPath; |
14 |
import org.eclipse.core.runtime.Path; |
|
15 |
import org.eclipse.core.runtime.Platform; |
|
1360 | 16 |
import org.eclipse.equinox.internal.p2.discovery.Catalog; |
17 |
import org.eclipse.equinox.internal.p2.discovery.DiscoveryCore; |
|
1366
c61bb9dcfe74
fix potential NPE + add bundle discovery for testing
dadubrow
parents:
1363
diff
changeset
|
18 |
import org.eclipse.equinox.internal.p2.discovery.compatibility.BundleDiscoveryStrategy; |
1360 | 19 |
import org.eclipse.equinox.internal.p2.discovery.compatibility.RemoteBundleDiscoveryStrategy; |
20 |
import org.eclipse.equinox.internal.p2.discovery.model.CatalogItem; |
|
21 |
import org.eclipse.equinox.internal.p2.ui.discovery.DiscoveryUi; |
|
22 |
import org.eclipse.equinox.internal.p2.ui.discovery.wizards.CatalogConfiguration; |
|
23 |
import org.eclipse.equinox.internal.p2.ui.discovery.wizards.CatalogViewer; |
|
24 |
import org.eclipse.jface.action.Action; |
|
25 |
import org.eclipse.jface.action.IMenuManager; |
|
26 |
import org.eclipse.jface.action.IToolBarManager; |
|
1361 | 27 |
import org.eclipse.jface.dialogs.ProgressMonitorDialog; |
1360 | 28 |
import org.eclipse.jface.layout.GridDataFactory; |
29 |
import org.eclipse.jface.layout.GridLayoutFactory; |
|
1374
67b625db1866
fix bugs, externalize strings, change some labels, add status line
dadubrow
parents:
1366
diff
changeset
|
30 |
import org.eclipse.jface.viewers.ISelectionChangedListener; |
1360 | 31 |
import org.eclipse.jface.viewers.IStructuredSelection; |
1374
67b625db1866
fix bugs, externalize strings, change some labels, add status line
dadubrow
parents:
1366
diff
changeset
|
32 |
import org.eclipse.jface.viewers.SelectionChangedEvent; |
1360 | 33 |
import org.eclipse.jface.viewers.StructuredSelection; |
1363 | 34 |
import org.eclipse.osgi.service.datalocation.Location; |
1360 | 35 |
import org.eclipse.swt.SWT; |
36 |
import org.eclipse.swt.widgets.Composite; |
|
37 |
import org.eclipse.swt.widgets.Display; |
|
38 |
import org.eclipse.ui.IActionBars; |
|
39 |
import org.eclipse.ui.PlatformUI; |
|
40 |
import org.eclipse.ui.actions.BaseSelectionListenerAction; |
|
41 |
import org.eclipse.ui.part.ViewPart; |
|
42 |
||
43 |
import com.nokia.carbide.discovery.ui.Activator; |
|
1374
67b625db1866
fix bugs, externalize strings, change some labels, add status line
dadubrow
parents:
1366
diff
changeset
|
44 |
import com.nokia.carbide.discovery.ui.Messages; |
1360 | 45 |
|
46 |
@SuppressWarnings("restriction") |
|
1361 | 47 |
public class DiscoveryView extends ViewPart { |
1363 | 48 |
|
49 |
private static final String DIRECTORY_KEY = "com.nokia.carbide.discovery.directory"; //$NON-NLS-1$ |
|
1360 | 50 |
|
51 |
private CatalogViewer viewer; |
|
52 |
private Action refreshAction; |
|
1374
67b625db1866
fix bugs, externalize strings, change some labels, add status line
dadubrow
parents:
1366
diff
changeset
|
53 |
private BaseSelectionListenerAction checkAllAction; |
67b625db1866
fix bugs, externalize strings, change some labels, add status line
dadubrow
parents:
1366
diff
changeset
|
54 |
private BaseSelectionListenerAction checkNoneAction; |
1361 | 55 |
private BaseSelectionListenerAction installAction; |
1360 | 56 |
|
1361 | 57 |
private boolean initialized; |
1360 | 58 |
|
1374
67b625db1866
fix bugs, externalize strings, change some labels, add status line
dadubrow
parents:
1366
diff
changeset
|
59 |
private ISelectionChangedListener selectionListener; |
67b625db1866
fix bugs, externalize strings, change some labels, add status line
dadubrow
parents:
1366
diff
changeset
|
60 |
|
1360 | 61 |
public DiscoveryView() { |
62 |
} |
|
63 |
||
64 |
/** |
|
65 |
* This is a callback that will allow us |
|
66 |
* to create the viewer and initialize it. |
|
67 |
*/ |
|
68 |
public void createPartControl(Composite parent) { |
|
69 |
Composite c = new Composite(parent, SWT.NONE); |
|
70 |
GridLayoutFactory.fillDefaults().applyTo(c); |
|
71 |
viewer = new CatalogViewer(getCatalog(), getSite(), getSite().getWorkbenchWindow(), getConfiguration()); |
|
72 |
viewer.createControl(c); |
|
73 |
GridDataFactory.fillDefaults().grab(true, true).applyTo(viewer.getControl()); |
|
74 |
||
75 |
// Create the help context id for the viewer's control |
|
1374
67b625db1866
fix bugs, externalize strings, change some labels, add status line
dadubrow
parents:
1366
diff
changeset
|
76 |
PlatformUI.getWorkbench().getHelpSystem().setHelp(viewer.getControl(), |
67b625db1866
fix bugs, externalize strings, change some labels, add status line
dadubrow
parents:
1366
diff
changeset
|
77 |
"com.nokia.carbide.discovery.ui.view.DiscoveryView.viewer"); //$NON-NLS-1$ |
1360 | 78 |
makeActions(); |
79 |
contributeToActionBars(); |
|
80 |
} |
|
81 |
||
82 |
private CatalogConfiguration getConfiguration() { |
|
83 |
CatalogConfiguration configuration = new CatalogConfiguration(); |
|
84 |
configuration.setShowTagFilter(false); |
|
85 |
return configuration; |
|
86 |
} |
|
87 |
||
88 |
private Catalog getCatalog() { |
|
89 |
Catalog catalog = new Catalog(); |
|
90 |
catalog.setEnvironment(DiscoveryCore.createEnvironment()); |
|
91 |
catalog.setVerifyUpdateSiteAvailability(false); |
|
1366
c61bb9dcfe74
fix potential NPE + add bundle discovery for testing
dadubrow
parents:
1363
diff
changeset
|
92 |
|
1360 | 93 |
// look for remote descriptor |
94 |
RemoteBundleDiscoveryStrategy remoteDiscoveryStrategy = new RemoteBundleDiscoveryStrategy(); |
|
1363 | 95 |
String url = getFromServerProperties(DIRECTORY_KEY); |
1366
c61bb9dcfe74
fix potential NPE + add bundle discovery for testing
dadubrow
parents:
1363
diff
changeset
|
96 |
if (url != null) { |
c61bb9dcfe74
fix potential NPE + add bundle discovery for testing
dadubrow
parents:
1363
diff
changeset
|
97 |
remoteDiscoveryStrategy.setDirectoryUrl(url); |
c61bb9dcfe74
fix potential NPE + add bundle discovery for testing
dadubrow
parents:
1363
diff
changeset
|
98 |
catalog.getDiscoveryStrategies().add(remoteDiscoveryStrategy); |
c61bb9dcfe74
fix potential NPE + add bundle discovery for testing
dadubrow
parents:
1363
diff
changeset
|
99 |
} |
c61bb9dcfe74
fix potential NPE + add bundle discovery for testing
dadubrow
parents:
1363
diff
changeset
|
100 |
else // look for descriptors from installed bundles |
c61bb9dcfe74
fix potential NPE + add bundle discovery for testing
dadubrow
parents:
1363
diff
changeset
|
101 |
catalog.getDiscoveryStrategies().add(new BundleDiscoveryStrategy()); |
1360 | 102 |
|
103 |
return catalog; |
|
104 |
} |
|
105 |
||
1363 | 106 |
public static String getFromServerProperties(String key) { |
107 |
Location installLocation = Platform.getInstallLocation(); |
|
108 |
URL url = installLocation.getURL(); |
|
109 |
IPath path = new Path(url.getPath()); |
|
110 |
path = path.append("configuration/server.properties"); //$NON-NLS-1$ |
|
111 |
File file = path.toFile(); |
|
112 |
Properties properties = new Properties(); |
|
113 |
try { |
|
114 |
InputStream is = new FileInputStream(file); |
|
115 |
properties.load(is); |
|
116 |
is.close(); |
|
117 |
} catch (IOException e) { |
|
118 |
String message = |
|
1374
67b625db1866
fix bugs, externalize strings, change some labels, add status line
dadubrow
parents:
1366
diff
changeset
|
119 |
MessageFormat.format(Messages.DiscoveryView_MissingDirectoryURLError, key); |
1363 | 120 |
Activator.logError(message, e); |
121 |
} |
|
122 |
return (String) properties.get(key); |
|
123 |
} |
|
124 |
||
1360 | 125 |
private void contributeToActionBars() { |
126 |
IActionBars bars = getViewSite().getActionBars(); |
|
127 |
fillLocalPullDown(bars.getMenuManager()); |
|
128 |
fillLocalToolBar(bars.getToolBarManager()); |
|
129 |
} |
|
130 |
||
131 |
private void fillLocalPullDown(IMenuManager manager) { |
|
1374
67b625db1866
fix bugs, externalize strings, change some labels, add status line
dadubrow
parents:
1366
diff
changeset
|
132 |
manager.add(checkAllAction); |
67b625db1866
fix bugs, externalize strings, change some labels, add status line
dadubrow
parents:
1366
diff
changeset
|
133 |
manager.add(checkNoneAction); |
1360 | 134 |
} |
135 |
||
136 |
private void fillLocalToolBar(IToolBarManager manager) { |
|
137 |
manager.add(refreshAction); |
|
1361 | 138 |
manager.add(installAction); |
1360 | 139 |
} |
140 |
||
141 |
private void makeActions() { |
|
142 |
refreshAction = new Action() { |
|
143 |
public void run() { |
|
1374
67b625db1866
fix bugs, externalize strings, change some labels, add status line
dadubrow
parents:
1366
diff
changeset
|
144 |
viewer.setSelection(StructuredSelection.EMPTY); |
67b625db1866
fix bugs, externalize strings, change some labels, add status line
dadubrow
parents:
1366
diff
changeset
|
145 |
viewer.refresh(); |
1361 | 146 |
viewer.updateCatalog(); |
1360 | 147 |
} |
148 |
}; |
|
1374
67b625db1866
fix bugs, externalize strings, change some labels, add status line
dadubrow
parents:
1366
diff
changeset
|
149 |
refreshAction.setText(Messages.DiscoveryView_RefreshLabel); |
67b625db1866
fix bugs, externalize strings, change some labels, add status line
dadubrow
parents:
1366
diff
changeset
|
150 |
refreshAction.setImageDescriptor(Activator.getImageDescriptor("icons/refresh.gif")); //$NON-NLS-1$ |
67b625db1866
fix bugs, externalize strings, change some labels, add status line
dadubrow
parents:
1366
diff
changeset
|
151 |
checkAllAction = new BaseSelectionListenerAction(Messages.DiscoveryView_CheckAllLabel) { |
1360 | 152 |
public void run() { |
1374
67b625db1866
fix bugs, externalize strings, change some labels, add status line
dadubrow
parents:
1366
diff
changeset
|
153 |
viewer.setSelection(StructuredSelection.EMPTY); |
1360 | 154 |
viewer.setSelection(getAllItemsSelection()); |
155 |
viewer.refresh(); |
|
156 |
} |
|
157 |
||
158 |
private IStructuredSelection getAllItemsSelection() { |
|
1361 | 159 |
List<CatalogItem> catalogItems = viewer.getCatalog().getItems(); |
160 |
return new StructuredSelection(catalogItems); |
|
1360 | 161 |
} |
162 |
||
163 |
protected boolean updateSelection(IStructuredSelection selection) { |
|
164 |
return !getAllItemsSelection().equals(selection); |
|
165 |
}; |
|
166 |
}; |
|
1374
67b625db1866
fix bugs, externalize strings, change some labels, add status line
dadubrow
parents:
1366
diff
changeset
|
167 |
checkNoneAction = new BaseSelectionListenerAction(Messages.DiscoveryView_UncheckAllLabel) { |
1360 | 168 |
public void run() { |
169 |
viewer.setSelection(StructuredSelection.EMPTY); |
|
170 |
viewer.refresh(); |
|
171 |
}; |
|
172 |
||
173 |
protected boolean updateSelection(IStructuredSelection selection) { |
|
174 |
return !selection.isEmpty(); |
|
175 |
}; |
|
176 |
}; |
|
1374
67b625db1866
fix bugs, externalize strings, change some labels, add status line
dadubrow
parents:
1366
diff
changeset
|
177 |
installAction = new BaseSelectionListenerAction(Messages.DiscoveryView_InstallLabel) { |
1361 | 178 |
public void run() { |
179 |
DiscoveryUi.install(viewer.getCheckedItems(), new ProgressMonitorDialog(DiscoveryView.this.getViewSite().getShell())); |
|
180 |
}; |
|
181 |
||
182 |
protected boolean updateSelection(IStructuredSelection selection) { |
|
183 |
return !selection.isEmpty(); |
|
184 |
}; |
|
185 |
}; |
|
1374
67b625db1866
fix bugs, externalize strings, change some labels, add status line
dadubrow
parents:
1366
diff
changeset
|
186 |
installAction.setImageDescriptor(Activator.getImageDescriptor("icons/icon-discovery.png")); //$NON-NLS-1$ |
67b625db1866
fix bugs, externalize strings, change some labels, add status line
dadubrow
parents:
1366
diff
changeset
|
187 |
viewer.addSelectionChangedListener(checkAllAction); |
67b625db1866
fix bugs, externalize strings, change some labels, add status line
dadubrow
parents:
1366
diff
changeset
|
188 |
viewer.addSelectionChangedListener(checkNoneAction); |
1361 | 189 |
viewer.addSelectionChangedListener(installAction); |
1374
67b625db1866
fix bugs, externalize strings, change some labels, add status line
dadubrow
parents:
1366
diff
changeset
|
190 |
selectionListener = new ISelectionChangedListener() { |
67b625db1866
fix bugs, externalize strings, change some labels, add status line
dadubrow
parents:
1366
diff
changeset
|
191 |
@Override |
67b625db1866
fix bugs, externalize strings, change some labels, add status line
dadubrow
parents:
1366
diff
changeset
|
192 |
public void selectionChanged(SelectionChangedEvent event) { |
67b625db1866
fix bugs, externalize strings, change some labels, add status line
dadubrow
parents:
1366
diff
changeset
|
193 |
IStructuredSelection selection = (IStructuredSelection) event.getSelection(); |
67b625db1866
fix bugs, externalize strings, change some labels, add status line
dadubrow
parents:
1366
diff
changeset
|
194 |
IActionBars bars = getViewSite().getActionBars(); |
67b625db1866
fix bugs, externalize strings, change some labels, add status line
dadubrow
parents:
1366
diff
changeset
|
195 |
bars.getStatusLineManager().setMessage( |
67b625db1866
fix bugs, externalize strings, change some labels, add status line
dadubrow
parents:
1366
diff
changeset
|
196 |
selection.isEmpty() ? null : MessageFormat.format( |
67b625db1866
fix bugs, externalize strings, change some labels, add status line
dadubrow
parents:
1366
diff
changeset
|
197 |
Messages.DiscoveryView_StatusLineFmt, selection.size())); |
67b625db1866
fix bugs, externalize strings, change some labels, add status line
dadubrow
parents:
1366
diff
changeset
|
198 |
} |
67b625db1866
fix bugs, externalize strings, change some labels, add status line
dadubrow
parents:
1366
diff
changeset
|
199 |
}; |
67b625db1866
fix bugs, externalize strings, change some labels, add status line
dadubrow
parents:
1366
diff
changeset
|
200 |
viewer.addSelectionChangedListener(selectionListener); |
1360 | 201 |
} |
202 |
||
203 |
@Override |
|
204 |
public void dispose() { |
|
1374
67b625db1866
fix bugs, externalize strings, change some labels, add status line
dadubrow
parents:
1366
diff
changeset
|
205 |
viewer.removeSelectionChangedListener(checkAllAction); |
67b625db1866
fix bugs, externalize strings, change some labels, add status line
dadubrow
parents:
1366
diff
changeset
|
206 |
viewer.removeSelectionChangedListener(checkNoneAction); |
1361 | 207 |
viewer.removeSelectionChangedListener(installAction); |
1374
67b625db1866
fix bugs, externalize strings, change some labels, add status line
dadubrow
parents:
1366
diff
changeset
|
208 |
viewer.removeSelectionChangedListener(selectionListener); |
1361 | 209 |
|
1360 | 210 |
super.dispose(); |
211 |
} |
|
212 |
||
213 |
/** |
|
214 |
* Passing the focus request to the viewer's control. |
|
215 |
*/ |
|
216 |
public void setFocus() { |
|
1361 | 217 |
if (!initialized) { |
218 |
initialized = true; |
|
1360 | 219 |
Display.getDefault().asyncExec(new Runnable() { |
220 |
@Override |
|
221 |
public void run() { |
|
1374
67b625db1866
fix bugs, externalize strings, change some labels, add status line
dadubrow
parents:
1366
diff
changeset
|
222 |
if (viewer.getViewer().getContentProvider() != null) { |
67b625db1866
fix bugs, externalize strings, change some labels, add status line
dadubrow
parents:
1366
diff
changeset
|
223 |
viewer.updateCatalog(); |
67b625db1866
fix bugs, externalize strings, change some labels, add status line
dadubrow
parents:
1366
diff
changeset
|
224 |
} |
1360 | 225 |
} |
226 |
}); |
|
227 |
} |
|
228 |
} |
|
1374
67b625db1866
fix bugs, externalize strings, change some labels, add status line
dadubrow
parents:
1366
diff
changeset
|
229 |
|
1360 | 230 |
} |