1 package com.nokia.carbide.discovery.ui.view; |
1 package com.nokia.carbide.discovery.ui.view; |
2 |
2 |
3 |
3 |
4 import java.io.File; |
|
5 import java.net.MalformedURLException; |
|
6 import java.util.ArrayList; |
|
7 import java.util.List; |
4 import java.util.List; |
8 |
5 |
9 import org.eclipse.equinox.internal.p2.discovery.Catalog; |
6 import org.eclipse.equinox.internal.p2.discovery.Catalog; |
10 import org.eclipse.equinox.internal.p2.discovery.DiscoveryCore; |
7 import org.eclipse.equinox.internal.p2.discovery.DiscoveryCore; |
11 import org.eclipse.equinox.internal.p2.discovery.compatibility.BundleDiscoveryStrategy; |
|
12 import org.eclipse.equinox.internal.p2.discovery.compatibility.RemoteBundleDiscoveryStrategy; |
8 import org.eclipse.equinox.internal.p2.discovery.compatibility.RemoteBundleDiscoveryStrategy; |
13 import org.eclipse.equinox.internal.p2.discovery.model.CatalogItem; |
9 import org.eclipse.equinox.internal.p2.discovery.model.CatalogItem; |
14 import org.eclipse.equinox.internal.p2.ui.discovery.DiscoveryUi; |
10 import org.eclipse.equinox.internal.p2.ui.discovery.DiscoveryUi; |
15 import org.eclipse.equinox.internal.p2.ui.discovery.wizards.CatalogConfiguration; |
11 import org.eclipse.equinox.internal.p2.ui.discovery.wizards.CatalogConfiguration; |
16 import org.eclipse.equinox.internal.p2.ui.discovery.wizards.CatalogViewer; |
12 import org.eclipse.equinox.internal.p2.ui.discovery.wizards.CatalogViewer; |
17 import org.eclipse.jface.action.Action; |
13 import org.eclipse.jface.action.Action; |
18 import org.eclipse.jface.action.IMenuManager; |
14 import org.eclipse.jface.action.IMenuManager; |
19 import org.eclipse.jface.action.IToolBarManager; |
15 import org.eclipse.jface.action.IToolBarManager; |
|
16 import org.eclipse.jface.dialogs.ProgressMonitorDialog; |
20 import org.eclipse.jface.layout.GridDataFactory; |
17 import org.eclipse.jface.layout.GridDataFactory; |
21 import org.eclipse.jface.layout.GridLayoutFactory; |
18 import org.eclipse.jface.layout.GridLayoutFactory; |
22 import org.eclipse.jface.resource.ImageDescriptor; |
|
23 import org.eclipse.jface.viewers.ISelectionChangedListener; |
|
24 import org.eclipse.jface.viewers.IStructuredContentProvider; |
|
25 import org.eclipse.jface.viewers.IStructuredSelection; |
19 import org.eclipse.jface.viewers.IStructuredSelection; |
26 import org.eclipse.jface.viewers.SelectionChangedEvent; |
|
27 import org.eclipse.jface.viewers.StructuredSelection; |
20 import org.eclipse.jface.viewers.StructuredSelection; |
28 import org.eclipse.swt.SWT; |
21 import org.eclipse.swt.SWT; |
29 import org.eclipse.swt.events.SelectionAdapter; |
|
30 import org.eclipse.swt.events.SelectionEvent; |
|
31 import org.eclipse.swt.widgets.Button; |
|
32 import org.eclipse.swt.widgets.Composite; |
22 import org.eclipse.swt.widgets.Composite; |
33 import org.eclipse.swt.widgets.Display; |
23 import org.eclipse.swt.widgets.Display; |
34 import org.eclipse.ui.IActionBars; |
24 import org.eclipse.ui.IActionBars; |
35 import org.eclipse.ui.PlatformUI; |
25 import org.eclipse.ui.PlatformUI; |
36 import org.eclipse.ui.actions.BaseSelectionListenerAction; |
26 import org.eclipse.ui.actions.BaseSelectionListenerAction; |
57 * presented in the same way everywhere. |
47 * presented in the same way everywhere. |
58 * <p> |
48 * <p> |
59 */ |
49 */ |
60 |
50 |
61 @SuppressWarnings("restriction") |
51 @SuppressWarnings("restriction") |
62 public class DiscoveryView extends ViewPart implements ISelectionChangedListener { |
52 public class DiscoveryView extends ViewPart { |
63 |
53 |
64 /** |
54 /** |
65 * The ID of the view as specified by the extension. |
55 * The ID of the view as specified by the extension. |
66 */ |
56 */ |
67 public static final String ID = "testdiscovery.views.TestView"; |
57 public static final String ID = "testdiscovery.views.TestView"; |
68 |
58 |
69 private CatalogViewer viewer; |
59 private CatalogViewer viewer; |
70 private Action refreshAction; |
60 private Action refreshAction; |
71 private BaseSelectionListenerAction selectAllAction; |
61 private BaseSelectionListenerAction selectAllAction; |
72 private BaseSelectionListenerAction selectNoneAction; |
62 private BaseSelectionListenerAction selectNoneAction; |
|
63 private BaseSelectionListenerAction installAction; |
73 |
64 |
74 private boolean updated; |
65 private boolean initialized; |
75 |
|
76 private Button installButton; |
|
77 |
66 |
78 public DiscoveryView() { |
67 public DiscoveryView() { |
79 } |
68 } |
80 |
69 |
81 /** |
70 /** |
87 GridLayoutFactory.fillDefaults().applyTo(c); |
76 GridLayoutFactory.fillDefaults().applyTo(c); |
88 viewer = new CatalogViewer(getCatalog(), getSite(), getSite().getWorkbenchWindow(), getConfiguration()); |
77 viewer = new CatalogViewer(getCatalog(), getSite(), getSite().getWorkbenchWindow(), getConfiguration()); |
89 viewer.createControl(c); |
78 viewer.createControl(c); |
90 GridDataFactory.fillDefaults().grab(true, true).applyTo(viewer.getControl()); |
79 GridDataFactory.fillDefaults().grab(true, true).applyTo(viewer.getControl()); |
91 |
80 |
92 installButton = new Button(c, SWT.PUSH); |
|
93 installButton.setText("Install Checked Items..."); |
|
94 ImageDescriptor imgDesc = Activator.getImageDescriptor("icons/icon-discovery.png"); |
|
95 installButton.setImage(imgDesc.createImage()); |
|
96 installButton.addSelectionListener(new SelectionAdapter() { |
|
97 @Override |
|
98 public void widgetSelected(SelectionEvent e) { |
|
99 DiscoveryUi.install(viewer.getCheckedItems(), getSite().getWorkbenchWindow()); |
|
100 } |
|
101 }); |
|
102 viewer.addSelectionChangedListener(this); |
|
103 |
|
104 // Create the help context id for the viewer's control |
81 // Create the help context id for the viewer's control |
105 PlatformUI.getWorkbench().getHelpSystem().setHelp(viewer.getControl(), "TestDiscovery.viewer"); |
82 PlatformUI.getWorkbench().getHelpSystem().setHelp(viewer.getControl(), "TestDiscovery.viewer"); |
106 makeActions(); |
83 makeActions(); |
107 contributeToActionBars(); |
84 contributeToActionBars(); |
108 } |
|
109 |
|
110 @Override |
|
111 public void selectionChanged(SelectionChangedEvent event) { |
|
112 installButton.setEnabled(!event.getSelection().isEmpty()); |
|
113 } |
85 } |
114 |
86 |
115 private CatalogConfiguration getConfiguration() { |
87 private CatalogConfiguration getConfiguration() { |
116 CatalogConfiguration configuration = new CatalogConfiguration(); |
88 CatalogConfiguration configuration = new CatalogConfiguration(); |
117 configuration.setShowTagFilter(false); |
89 configuration.setShowTagFilter(false); |
122 Catalog catalog = new Catalog(); |
94 Catalog catalog = new Catalog(); |
123 catalog.setEnvironment(DiscoveryCore.createEnvironment()); |
95 catalog.setEnvironment(DiscoveryCore.createEnvironment()); |
124 catalog.setVerifyUpdateSiteAvailability(false); |
96 catalog.setVerifyUpdateSiteAvailability(false); |
125 |
97 |
126 // look for descriptors from installed bundles |
98 // look for descriptors from installed bundles |
127 catalog.getDiscoveryStrategies().add(new BundleDiscoveryStrategy()); |
99 // catalog.getDiscoveryStrategies().add(new BundleDiscoveryStrategy()); |
128 |
100 |
129 // look for remote descriptor |
101 // look for remote descriptor |
130 RemoteBundleDiscoveryStrategy remoteDiscoveryStrategy = new RemoteBundleDiscoveryStrategy(); |
102 RemoteBundleDiscoveryStrategy remoteDiscoveryStrategy = new RemoteBundleDiscoveryStrategy(); |
131 File f = new File("D:\\Users\\Discovery\\discovery.xml"); |
103 remoteDiscoveryStrategy.setDirectoryUrl("http://daaus001.americas.nokia.com/carbide/public/updates/3.0"); |
132 try { |
104 catalog.getDiscoveryStrategies().add(remoteDiscoveryStrategy); |
133 remoteDiscoveryStrategy.setDirectoryUrl(f.toURI().toURL().toExternalForm()); |
|
134 } catch (MalformedURLException e) { |
|
135 throw new IllegalArgumentException(e); |
|
136 } |
|
137 // catalog.getDiscoveryStrategies().add(remoteDiscoveryStrategy); |
|
138 |
105 |
139 return catalog; |
106 return catalog; |
140 } |
107 } |
141 |
108 |
142 private void contributeToActionBars() { |
109 private void contributeToActionBars() { |
150 manager.add(selectNoneAction); |
117 manager.add(selectNoneAction); |
151 } |
118 } |
152 |
119 |
153 private void fillLocalToolBar(IToolBarManager manager) { |
120 private void fillLocalToolBar(IToolBarManager manager) { |
154 manager.add(refreshAction); |
121 manager.add(refreshAction); |
|
122 manager.add(installAction); |
155 } |
123 } |
156 |
124 |
157 private void makeActions() { |
125 private void makeActions() { |
158 refreshAction = new Action() { |
126 refreshAction = new Action() { |
159 public void run() { |
127 public void run() { |
160 viewer.refresh(); |
128 viewer.updateCatalog(); |
161 } |
129 } |
162 }; |
130 }; |
163 refreshAction.setText("Refresh"); |
131 refreshAction.setText("Refresh"); |
164 refreshAction.setImageDescriptor(Activator.getImageDescriptor("icons/refresh.gif")); |
132 refreshAction.setImageDescriptor(Activator.getImageDescriptor("icons/refresh.gif")); |
165 selectAllAction = new BaseSelectionListenerAction("Select All Items") { |
133 selectAllAction = new BaseSelectionListenerAction("Select All Items") { |
167 viewer.setSelection(getAllItemsSelection()); |
135 viewer.setSelection(getAllItemsSelection()); |
168 viewer.refresh(); |
136 viewer.refresh(); |
169 } |
137 } |
170 |
138 |
171 private IStructuredSelection getAllItemsSelection() { |
139 private IStructuredSelection getAllItemsSelection() { |
172 Object[] elements = getAllCatalogItems(); |
140 List<CatalogItem> catalogItems = viewer.getCatalog().getItems(); |
173 IStructuredSelection allItemsSelection = new StructuredSelection(elements); |
141 return new StructuredSelection(catalogItems); |
174 return allItemsSelection; |
|
175 } |
142 } |
176 |
143 |
177 private Object[] getAllCatalogItems() { |
|
178 List<Object> catalogItems = new ArrayList<Object>(); |
|
179 IStructuredContentProvider provider = |
|
180 (IStructuredContentProvider) viewer.getViewer().getContentProvider(); |
|
181 Object[] elements = provider.getElements(viewer.getViewer().getInput()); |
|
182 for (Object item : elements) { |
|
183 if (item instanceof CatalogItem) |
|
184 catalogItems.add(item); |
|
185 } |
|
186 return catalogItems.toArray(); |
|
187 }; |
|
188 |
|
189 protected boolean updateSelection(IStructuredSelection selection) { |
144 protected boolean updateSelection(IStructuredSelection selection) { |
190 return !getAllItemsSelection().equals(selection); |
145 return !getAllItemsSelection().equals(selection); |
191 }; |
146 }; |
192 }; |
147 }; |
193 selectNoneAction = new BaseSelectionListenerAction("Deselect All Items") { |
148 selectNoneAction = new BaseSelectionListenerAction("Deselect All Items") { |
198 |
153 |
199 protected boolean updateSelection(IStructuredSelection selection) { |
154 protected boolean updateSelection(IStructuredSelection selection) { |
200 return !selection.isEmpty(); |
155 return !selection.isEmpty(); |
201 }; |
156 }; |
202 }; |
157 }; |
|
158 installAction = new BaseSelectionListenerAction("Install Checked Items...") { |
|
159 public void run() { |
|
160 DiscoveryUi.install(viewer.getCheckedItems(), new ProgressMonitorDialog(DiscoveryView.this.getViewSite().getShell())); |
|
161 }; |
|
162 |
|
163 protected boolean updateSelection(IStructuredSelection selection) { |
|
164 return !selection.isEmpty(); |
|
165 }; |
|
166 }; |
|
167 installAction.setImageDescriptor(Activator.getImageDescriptor("icons/icon-discovery.png")); |
203 viewer.addSelectionChangedListener(selectAllAction); |
168 viewer.addSelectionChangedListener(selectAllAction); |
204 viewer.addSelectionChangedListener(selectNoneAction); |
169 viewer.addSelectionChangedListener(selectNoneAction); |
|
170 viewer.addSelectionChangedListener(installAction); |
205 } |
171 } |
206 |
172 |
207 @Override |
173 @Override |
208 public void dispose() { |
174 public void dispose() { |
209 viewer.removeSelectionChangedListener(selectAllAction); |
175 viewer.removeSelectionChangedListener(selectAllAction); |
210 viewer.removeSelectionChangedListener(selectNoneAction); |
176 viewer.removeSelectionChangedListener(selectNoneAction); |
211 viewer.removeSelectionChangedListener(this); |
177 viewer.removeSelectionChangedListener(installAction); |
|
178 |
212 super.dispose(); |
179 super.dispose(); |
213 } |
180 } |
214 |
181 |
215 /** |
182 /** |
216 * Passing the focus request to the viewer's control. |
183 * Passing the focus request to the viewer's control. |
217 */ |
184 */ |
218 public void setFocus() { |
185 public void setFocus() { |
219 updateCatalog(); |
186 if (!initialized) { |
220 } |
187 initialized = true; |
221 |
|
222 private void updateCatalog() { |
|
223 if (!updated) { |
|
224 updated = true; |
|
225 Display.getDefault().asyncExec(new Runnable() { |
188 Display.getDefault().asyncExec(new Runnable() { |
226 @Override |
189 @Override |
227 public void run() { |
190 public void run() { |
228 viewer.updateCatalog(); |
191 viewer.updateCatalog(); |
|
192 viewer.setSelection(StructuredSelection.EMPTY); |
229 } |
193 } |
230 }); |
194 }); |
231 } |
195 } |
232 } |
196 } |
233 |
|
234 } |
197 } |