]> git.argeo.org Git - gpl/argeo-slc.git/blob - plugins/org.argeo.slc.client.ui.dist/src/main/java/org/argeo/slc/client/ui/dist/editors/DistributionOverviewPage.java
Rename icons for repos
[gpl/argeo-slc.git] / plugins / org.argeo.slc.client.ui.dist / src / main / java / org / argeo / slc / client / ui / dist / editors / DistributionOverviewPage.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.argeo.slc.client.ui.dist.editors;
17
18 import java.util.ArrayList;
19 import java.util.List;
20
21 import javax.jcr.Node;
22 import javax.jcr.NodeIterator;
23 import javax.jcr.PropertyType;
24 import javax.jcr.RepositoryException;
25 import javax.jcr.Session;
26 import javax.jcr.query.QueryManager;
27 import javax.jcr.query.QueryResult;
28 import javax.jcr.query.qom.Ordering;
29 import javax.jcr.query.qom.QueryObjectModel;
30 import javax.jcr.query.qom.QueryObjectModelFactory;
31 import javax.jcr.query.qom.Selector;
32
33 import org.argeo.eclipse.ui.ErrorFeedback;
34 import org.argeo.jcr.JcrUtils;
35 import org.argeo.slc.client.ui.dist.DistPlugin;
36 import org.argeo.slc.client.ui.dist.commands.DeleteArtifacts;
37 import org.argeo.slc.client.ui.dist.utils.CommandHelpers;
38 import org.argeo.slc.client.ui.dist.utils.NodeViewerComparator;
39 import org.argeo.slc.jcr.SlcNames;
40 import org.argeo.slc.jcr.SlcTypes;
41 import org.eclipse.jface.action.IMenuListener;
42 import org.eclipse.jface.action.IMenuManager;
43 import org.eclipse.jface.action.MenuManager;
44 import org.eclipse.jface.viewers.ColumnLabelProvider;
45 import org.eclipse.jface.viewers.IStructuredContentProvider;
46 import org.eclipse.jface.viewers.TableViewer;
47 import org.eclipse.jface.viewers.TableViewerColumn;
48 import org.eclipse.jface.viewers.Viewer;
49 import org.eclipse.swt.SWT;
50 import org.eclipse.swt.events.SelectionAdapter;
51 import org.eclipse.swt.events.SelectionEvent;
52 import org.eclipse.swt.layout.GridData;
53 import org.eclipse.swt.layout.GridLayout;
54 import org.eclipse.swt.widgets.Menu;
55 import org.eclipse.swt.widgets.Table;
56 import org.eclipse.ui.IWorkbenchWindow;
57 import org.eclipse.ui.forms.IManagedForm;
58 import org.eclipse.ui.forms.editor.FormEditor;
59 import org.eclipse.ui.forms.editor.FormPage;
60 import org.eclipse.ui.forms.widgets.ScrolledForm;
61 import org.osgi.framework.Constants;
62
63 /** Table giving an overview of an OSGi distribution */
64 public class DistributionOverviewPage extends FormPage implements SlcNames {
65 final static String PAGE_ID = "distributionOverviewPage";
66
67 private TableViewer viewer;
68 private Session session;
69
70 private NodeViewerComparator comparator;
71
72 public DistributionOverviewPage(FormEditor formEditor, String title,
73 Session session) {
74 super(formEditor, PAGE_ID, title);
75 this.session = session;
76 }
77
78 @Override
79 protected void createFormContent(IManagedForm managedForm) {
80 ScrolledForm form = managedForm.getForm();
81 GridLayout layout = new GridLayout(1, false);
82 form.getBody().setLayout(layout);
83
84 // helpers to enable sorting by column
85 List<String> propertiesList = new ArrayList<String>();
86 List<Integer> propertyTypesList = new ArrayList<Integer>();
87
88 // Define the TableViewer
89 viewer = new TableViewer(form.getBody(), SWT.MULTI | SWT.H_SCROLL
90 | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.BORDER);
91
92 TableViewerColumn col = new TableViewerColumn(viewer, SWT.V_SCROLL);
93 col.getColumn().setWidth(300);
94 col.getColumn().setText("Symbolic name");
95 col.setLabelProvider(new ColumnLabelProvider() {
96 @Override
97 public String getText(Object element) {
98 return JcrUtils.get((Node) element, SLC_SYMBOLIC_NAME);
99 }
100 });
101 col.getColumn().addSelectionListener(getSelectionAdapter(0));
102 propertiesList.add(SLC_SYMBOLIC_NAME);
103 propertyTypesList.add(PropertyType.STRING);
104
105 col = new TableViewerColumn(viewer, SWT.NONE);
106 col.getColumn().setWidth(100);
107 col.getColumn().setText("Version");
108 col.setLabelProvider(new ColumnLabelProvider() {
109 @Override
110 public String getText(Object element) {
111 return JcrUtils.get((Node) element, SLC_BUNDLE_VERSION);
112 }
113 });
114 col.getColumn().addSelectionListener(getSelectionAdapter(1));
115 propertiesList.add(SLC_BUNDLE_VERSION);
116 propertyTypesList.add(PropertyType.STRING);
117
118 col = new TableViewerColumn(viewer, SWT.NONE);
119 col.getColumn().setWidth(150);
120 col.getColumn().setText("Group ID");
121 col.setLabelProvider(new ColumnLabelProvider() {
122 @Override
123 public String getText(Object element) {
124 return JcrUtils.get((Node) element, SLC_GROUP_ID);
125 }
126 });
127 col.getColumn().addSelectionListener(getSelectionAdapter(2));
128 propertiesList.add(SLC_GROUP_ID);
129 propertyTypesList.add(PropertyType.STRING);
130
131 col = new TableViewerColumn(viewer, SWT.NONE);
132 col.getColumn().setWidth(300);
133 col.getColumn().setText("Name");
134 col.setLabelProvider(new ColumnLabelProvider() {
135 @Override
136 public String getText(Object element) {
137 return JcrUtils.get((Node) element, SLC_
138 + Constants.BUNDLE_NAME);
139 }
140 });
141 col.getColumn().addSelectionListener(getSelectionAdapter(3));
142 propertiesList.add(SLC_ + Constants.BUNDLE_NAME);
143 propertyTypesList.add(PropertyType.STRING);
144
145 final Table table = viewer.getTable();
146 table.setHeaderVisible(true);
147 table.setLinesVisible(true);
148 table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
149
150 viewer.setContentProvider(new DistributionsContentProvider());
151 getSite().setSelectionProvider(viewer);
152
153 viewer.setInput(session);
154 comparator = new NodeViewerComparator(1,
155 NodeViewerComparator.DESCENDING, propertiesList,
156 propertyTypesList);
157 viewer.setComparator(comparator);
158
159 MenuManager menuManager = new MenuManager();
160 Menu menu = menuManager.createContextMenu(viewer.getTable());
161 menuManager.addMenuListener(new IMenuListener() {
162 public void menuAboutToShow(IMenuManager manager) {
163 contextMenuAboutToShow(manager);
164 }
165 });
166 viewer.getTable().setMenu(menu);
167 getSite().registerContextMenu(menuManager, viewer);
168
169 }
170
171 @Override
172 public void setFocus() {
173 viewer.getTable().setFocus();
174 }
175
176 /** force refresh of the artifact list */
177 public void refresh() {
178 viewer.refresh();
179 }
180
181 /** Programatically configure the context menu */
182 protected void contextMenuAboutToShow(IMenuManager menuManager) {
183 IWorkbenchWindow window = DistPlugin.getDefault().getWorkbench()
184 .getActiveWorkbenchWindow();
185
186 // Build conditions depending on element type (repo or workspace)
187
188 // Delete selected artifacts
189 CommandHelpers.refreshCommand(menuManager, window, DeleteArtifacts.ID,
190 DeleteArtifacts.DEFAULT_LABEL,
191 DeleteArtifacts.DEFAULT_ICON_PATH, true);
192
193 }
194
195 static NodeIterator listBundleArtifacts(Session session)
196 throws RepositoryException {
197 QueryManager queryManager = session.getWorkspace().getQueryManager();
198 QueryObjectModelFactory factory = queryManager.getQOMFactory();
199
200 final String bundleArtifactsSelector = "bundleArtifacts";
201 Selector source = factory.selector(SlcTypes.SLC_BUNDLE_ARTIFACT,
202 bundleArtifactsSelector);
203
204 Ordering order = factory.ascending(factory.propertyValue(
205 bundleArtifactsSelector, SlcNames.SLC_SYMBOLIC_NAME));
206 Ordering[] orderings = { order };
207
208 QueryObjectModel query = factory.createQuery(source, null, orderings,
209 null);
210
211 QueryResult result = query.execute();
212 return result.getNodes();
213 }
214
215 private SelectionAdapter getSelectionAdapter(final int index) {
216 SelectionAdapter selectionAdapter = new SelectionAdapter() {
217 @Override
218 public void widgetSelected(SelectionEvent e) {
219 Table table = viewer.getTable();
220 comparator.setColumn(index);
221 int dir = table.getSortDirection();
222 if (table.getSortColumn() == table.getColumn(index)) {
223 dir = dir == SWT.UP ? SWT.DOWN : SWT.UP;
224 } else {
225 dir = SWT.DOWN;
226 }
227 table.setSortDirection(dir);
228 table.setSortColumn(table.getColumn(index));
229 viewer.refresh();
230 }
231 };
232 return selectionAdapter;
233 }
234
235 private static class DistributionsContentProvider implements
236 IStructuredContentProvider {
237 private Session session;
238
239 public void dispose() {
240 }
241
242 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
243 session = (Session) newInput;
244 }
245
246 public Object[] getElements(Object arg0) {
247 try {
248 List<Node> nodes = JcrUtils
249 .nodeIteratorToList(listBundleArtifacts(session));
250 return nodes.toArray();
251 } catch (RepositoryException e) {
252 ErrorFeedback.show("Cannot list bundles", e);
253 return null;
254 }
255 }
256 }
257 //
258 // private class BoundedLayout extends Layout {
259 // protected Layout delegateLayout;
260 //
261 // protected Method computeSizeMethod;
262 // protected Method layoutMethod;
263 //
264 // protected boolean widthBound;
265 //
266 // public BoundedLayout(Layout delegateLayout, boolean widthBound) {
267 // setDelegateLayout(delegateLayout);
268 // this.widthBound = widthBound;
269 // }
270 //
271 // public Layout getDelegateLayout() {
272 // return delegateLayout;
273 // }
274 //
275 // public void setDelegateLayout(Layout delegateLayout) {
276 // this.delegateLayout = delegateLayout;
277 //
278 // try {
279 // computeSizeMethod = delegateLayout.getClass()
280 // .getDeclaredMethod("computeSize", Composite.class,
281 // int.class, int.class, boolean.class);
282 // computeSizeMethod.setAccessible(true);
283 //
284 // layoutMethod = delegateLayout.getClass().getDeclaredMethod(
285 // "layout", Composite.class, boolean.class);
286 // layoutMethod.setAccessible(true);
287 // } catch (Exception e) {
288 // throw new RuntimeException(e);
289 // }
290 // }
291 //
292 // @Override
293 // protected Point computeSize(Composite composite, int wHint, int hHint,
294 // boolean flushCache) {
295 // // get comp size to make sure we don't let any children exceed it
296 // Point compSize = composite.getSize();
297 //
298 // try {
299 // Point layoutComputedSize = (Point) computeSizeMethod.invoke(
300 // delegateLayout, composite, wHint, hHint, flushCache);
301 //
302 // if (widthBound) {
303 // layoutComputedSize.x = Math.min(compSize.x,
304 // layoutComputedSize.x);
305 // } else {
306 // layoutComputedSize.y = Math.min(compSize.y,
307 // layoutComputedSize.y);
308 // }
309 //
310 // return layoutComputedSize;
311 // } catch (Exception e) {
312 // throw new RuntimeException(e);
313 // }
314 // }
315 //
316 // @Override
317 // protected void layout(Composite composite, boolean flushCache) {
318 // try {
319 // layoutMethod.invoke(delegateLayout, composite, flushCache);
320 // } catch (Exception e) {
321 // throw new RuntimeException(e);
322 // }
323 // }
324 // }
325 }