]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.client.ui.dist/src/org/argeo/slc/client/ui/dist/editors/BundleDependencyPage.java
Add cglib back
[gpl/argeo-slc.git] / org.argeo.slc.client.ui.dist / src / org / argeo / slc / client / ui / dist / editors / BundleDependencyPage.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.List;
19
20 import javax.jcr.Node;
21 import javax.jcr.NodeIterator;
22 import javax.jcr.RepositoryException;
23
24 import org.argeo.eclipse.ui.dialogs.ErrorFeedback;
25 import org.argeo.jcr.JcrUtils;
26 import org.argeo.slc.SlcException;
27 import org.argeo.slc.client.ui.dist.DistConstants;
28 import org.argeo.slc.client.ui.dist.DistImages;
29 import org.argeo.slc.jcr.SlcNames;
30 import org.argeo.slc.jcr.SlcTypes;
31 import org.eclipse.jface.viewers.ColumnLabelProvider;
32 import org.eclipse.jface.viewers.IStructuredContentProvider;
33 import org.eclipse.jface.viewers.ITreeContentProvider;
34 import org.eclipse.jface.viewers.TableViewer;
35 import org.eclipse.jface.viewers.TableViewerColumn;
36 import org.eclipse.jface.viewers.TreeViewer;
37 import org.eclipse.jface.viewers.TreeViewerColumn;
38 import org.eclipse.jface.viewers.Viewer;
39 import org.eclipse.swt.SWT;
40 import org.eclipse.swt.graphics.Image;
41 import org.eclipse.swt.layout.GridData;
42 import org.eclipse.swt.layout.GridLayout;
43 import org.eclipse.swt.widgets.Composite;
44 import org.eclipse.swt.widgets.Table;
45 import org.eclipse.swt.widgets.Tree;
46 import org.eclipse.ui.forms.IManagedForm;
47 import org.eclipse.ui.forms.editor.FormEditor;
48 import org.eclipse.ui.forms.editor.FormPage;
49 import org.eclipse.ui.forms.widgets.FormToolkit;
50 import org.eclipse.ui.forms.widgets.ScrolledForm;
51 import org.eclipse.ui.forms.widgets.Section;
52
53 /**
54 * Present main information of a given OSGI bundle
55 */
56 public class BundleDependencyPage extends FormPage implements SlcNames {
57 // private final static Log log =
58 // LogFactory.getLog(ArtifactDetailsPage.class);
59
60 // Main business Objects
61 private Node currBundle;
62
63 // This page widgets
64 private FormToolkit toolkit;
65
66 public BundleDependencyPage(FormEditor editor, String title,
67 Node currentNode) {
68 super(editor, "id", title);
69 this.currBundle = currentNode;
70 }
71
72 protected void createFormContent(IManagedForm managedForm) {
73 ScrolledForm form = managedForm.getForm();
74 toolkit = managedForm.getToolkit();
75 try {
76 if (currBundle.hasProperty(DistConstants.SLC_BUNDLE_NAME))
77 form.setText(currBundle.getProperty(
78 DistConstants.SLC_BUNDLE_NAME).getString());
79 Composite body = form.getBody();
80 GridLayout layout = new GridLayout(1, false);
81 layout.horizontalSpacing = layout.marginWidth = 0;
82 layout.verticalSpacing = layout.marginHeight = 0;
83 body.setLayout(layout);
84
85 Composite part = toolkit.createComposite(body);
86 createExportPackageSection(part);
87 GridData gd = new GridData(SWT.FILL, SWT.TOP, true, false);
88 gd.heightHint = 180;
89 part.setLayoutData(gd);
90
91 part = toolkit.createComposite(body);
92 createImportPackageSection(part);
93 gd = new GridData(SWT.FILL, SWT.FILL, true, true);
94 // gd.heightHint = 200;
95 part.setLayoutData(gd);
96
97 part = toolkit.createComposite(body);
98 createReqBundleSection(part);
99 gd = new GridData(SWT.FILL, SWT.FILL, true, true);
100 // /gd.heightHint = 200;
101 part.setLayoutData(gd);
102
103 managedForm.reflow(true);
104
105 } catch (RepositoryException e) {
106 throw new SlcException("unexpected error "
107 + "while creating bundle details page");
108 }
109 }
110
111 // Workaround to add an artificial level to the export package browser
112 private class LevelElem {
113 private String label;
114 private Object parent;
115
116 public LevelElem(String label, Object parent) {
117 this.label = label;
118 this.parent = parent;
119 }
120
121 public String toString() {
122 return label;
123 }
124
125 public Object getParent() {
126 return parent;
127 }
128 }
129
130 /** Export Package Section */
131 private void createExportPackageSection(Composite parent)
132 throws RepositoryException {
133 parent.setLayout(new GridLayout());
134
135 // Define the TableViewer
136
137 Section section = addSection(parent, "Export packages");
138 section.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
139
140 TreeViewer viewer = new TreeViewer(section, SWT.H_SCROLL | SWT.V_SCROLL
141 | SWT.BORDER);
142 final Tree tree = viewer.getTree();
143 tree.setHeaderVisible(false);
144 tree.setLinesVisible(true);
145 tree.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
146
147 TreeViewerColumn col = new TreeViewerColumn(viewer, SWT.FILL);
148 col.getColumn().setWidth(400);
149
150 col.setLabelProvider(new ColumnLabelProvider() {
151 @Override
152 public String getText(Object element) {
153 if (element instanceof Node)
154 return JcrUtils.get((Node) element, SlcNames.SLC_NAME);
155 else
156 return element.toString();
157 }
158
159 @Override
160 public Image getImage(Object element) {
161 if (element instanceof Node) {
162 try {
163 Node node = (Node) element;
164 if (node.isNodeType(SlcTypes.SLC_EXPORTED_PACKAGE))
165 return DistImages.IMG_PACKAGE;
166 } catch (RepositoryException e) {
167 throw new SlcException("Error retriving "
168 + "image for the labelProvider", e);
169 }
170 }
171 return null;
172 }
173 });
174
175 viewer.setContentProvider(new ITreeContentProvider() {
176
177 public void dispose() {
178 }
179
180 public void inputChanged(Viewer viewer, Object oldInput,
181 Object newInput) {
182 }
183
184 public Object[] getElements(Object inputElement) {
185 try {
186 List<Node> nodes = JcrUtils.nodeIteratorToList(listNodes(
187 currBundle, SlcTypes.SLC_EXPORTED_PACKAGE,
188 SlcNames.SLC_NAME));
189 return nodes.toArray();
190 } catch (RepositoryException e) {
191 throw new SlcException("Cannot list children Nodes", e);
192 }
193 }
194
195 public Object[] getChildren(Object parentElement) {
196 // Only 2 levels for the time being
197 try {
198 if (parentElement instanceof LevelElem) {
199 Node node = (Node) ((LevelElem) parentElement)
200 .getParent();
201 List<Node> nodes = JcrUtils
202 .nodeIteratorToList(listNodes(node,
203 SlcTypes.SLC_JAVA_PACKAGE,
204 SlcNames.SLC_NAME));
205 return nodes.toArray();
206 } else if (parentElement instanceof Node) {
207 Node pNode = (Node) parentElement;
208 if (pNode.isNodeType(SlcTypes.SLC_EXPORTED_PACKAGE)) {
209 if (listNodes(pNode, SlcTypes.SLC_JAVA_PACKAGE,
210 SlcNames.SLC_NAME).getSize() > 0) {
211 Object[] result = { new LevelElem("uses", pNode) };
212 return result;
213 }
214 }
215 }
216 return null;
217 } catch (RepositoryException e) {
218 throw new SlcException("Cannot list children Nodes", e);
219 }
220 }
221
222 public Object getParent(Object element) {
223 // useless
224 return null;
225 }
226
227 public boolean hasChildren(Object element) {
228 try {
229 if (element instanceof LevelElem)
230 return true;
231 else {
232 Node pNode = (Node) element;
233 if (pNode.isNodeType(SlcTypes.SLC_EXPORTED_PACKAGE)) {
234 return listNodes(pNode, SlcTypes.SLC_JAVA_PACKAGE,
235 SlcNames.SLC_NAME).getSize() > 0;
236 }
237 }
238 return false;
239 } catch (RepositoryException e) {
240 throw new SlcException("Cannot check children Nodes", e);
241 }
242 }
243 });
244
245 section.setClient(tree);
246 viewer.setInput("Initialize");
247 // work around a display problem : the tree table has only a few lines
248 // when the tree is not expended
249 // viewer.expandToLevel(2);
250 }
251
252 /** Import Package Section */
253 private void createImportPackageSection(Composite parent)
254 throws RepositoryException {
255 parent.setLayout(new GridLayout());
256
257 // Define the TableViewer
258 // toolkit.createLabel(parent, "Import packages", SWT.NONE).setFont(
259 // EclipseUiUtils.getBoldFont(parent));
260
261 Section section = addSection(parent, "Import packages");
262 section.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
263 TableViewer viewer = new TableViewer(section, SWT.H_SCROLL
264 | SWT.V_SCROLL | SWT.BORDER);
265
266 final Table table = viewer.getTable();
267 table.setHeaderVisible(true);
268 table.setLinesVisible(true);
269 table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
270
271 // Name
272 TableViewerColumn col = new TableViewerColumn(viewer, SWT.NONE);
273 col.getColumn().setWidth(350);
274 col.getColumn().setText("Name");
275 col.setLabelProvider(new ColumnLabelProvider() {
276 @Override
277 public String getText(Object element) {
278 return JcrUtils.get((Node) element, SLC_NAME);
279 }
280
281 public Image getImage(Object element) {
282 return DistImages.IMG_PACKAGE;
283 }
284
285 });
286
287 // Version
288 col = new TableViewerColumn(viewer, SWT.NONE);
289 col.getColumn().setWidth(100);
290 col.getColumn().setText("Version");
291 col.setLabelProvider(new ColumnLabelProvider() {
292 @Override
293 public String getText(Object element) {
294 return JcrUtils.get((Node) element, SLC_VERSION);
295 }
296 });
297
298 // Optional
299 col = new TableViewerColumn(viewer, SWT.NONE);
300 col.getColumn().setWidth(100);
301 col.getColumn().setText("Optional");
302 col.setLabelProvider(new ColumnLabelProvider() {
303 @Override
304 public String getText(Object element) {
305 return JcrUtils.get((Node) element, SLC_OPTIONAL);
306 }
307 });
308
309 viewer.setContentProvider(new TableContentProvider(
310 SlcTypes.SLC_IMPORTED_PACKAGE, SLC_NAME));
311 section.setClient(table);
312 viewer.setInput("Initialize");
313 }
314
315 /** Required Bundle Section */
316 private void createReqBundleSection(Composite parent)
317 throws RepositoryException {
318 parent.setLayout(new GridLayout());
319
320 // Define the TableViewer
321 Section section = addSection(parent, "Required bundles");
322 section.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
323
324 // toolkit.createLabel(parent, "Required bundles", SWT.NONE).setFont(
325 // EclipseUiUtils.getBoldFont(parent));
326 TableViewer viewer = new TableViewer(section, SWT.H_SCROLL
327 | SWT.V_SCROLL | SWT.BORDER);
328
329 final Table table = viewer.getTable();
330 table.setHeaderVisible(true);
331 table.setLinesVisible(true);
332 table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
333
334 // Name
335 TableViewerColumn col = new TableViewerColumn(viewer, SWT.NONE);
336 col.getColumn().setWidth(300);
337 col.getColumn().setText("Name");
338 col.setLabelProvider(new ColumnLabelProvider() {
339 @Override
340 public String getText(Object element) {
341 return JcrUtils.get((Node) element, SLC_SYMBOLIC_NAME);
342 }
343
344 @Override
345 public Image getImage(Object element) {
346 return DistImages.IMG_BUNDLE;
347 }
348 });
349
350 // Version
351 col = new TableViewerColumn(viewer, SWT.NONE);
352 col.getColumn().setWidth(140);
353 col.getColumn().setText("Version");
354 col.setLabelProvider(new ColumnLabelProvider() {
355 @Override
356 public String getText(Object element) {
357 return JcrUtils.get((Node) element, SLC_BUNDLE_VERSION);
358 }
359 });
360
361 // Optional
362 col = new TableViewerColumn(viewer, SWT.NONE);
363 col.getColumn().setWidth(100);
364 col.getColumn().setText("Optional");
365 col.setLabelProvider(new ColumnLabelProvider() {
366 @Override
367 public String getText(Object element) {
368 return JcrUtils.get((Node) element, SLC_OPTIONAL);
369 }
370 });
371
372 viewer.setContentProvider(new TableContentProvider(
373 SlcTypes.SLC_REQUIRED_BUNDLE, SLC_SYMBOLIC_NAME));
374 section.setClient(table);
375 viewer.setInput("Initialize");
376 }
377
378 /**
379 * Build repository request
380 *
381 * FIXME Workaround for remote repository, the path to bundleartifact (for
382 * instance
383 * .../org/argeo/slc/org.argeo.slc.client.ui.dist/1.1.12/org.argeo.slc
384 * .client.ui.dist-1.1.12/ ) is not valid for method factory.childNode(); it
385 * fails parsing the "1.1.12" part, trying to cast it as a BigDecimal
386 *
387 * */
388 private NodeIterator listNodes(Node parent, String nodeType, String orderBy)
389 throws RepositoryException {
390 // QueryManager queryManager = currBundle.getSession().getWorkspace()
391 // .getQueryManager();
392 // QueryObjectModelFactory factory = queryManager.getQOMFactory();
393 //
394 // final String nodeSelector = "nodes";
395 // Selector source = factory.selector(nodeType, nodeSelector);
396 //
397 // Constraint childOf = factory.childNode(nodeSelector,
398 // parent.getPath());
399 //
400 // Ordering order =
401 // factory.ascending(factory.propertyValue(nodeSelector,
402 // orderBy));
403 // Ordering[] orderings = { order };
404 //
405 // QueryObjectModel query = factory.createQuery(source, childOf,
406 // orderings, null);
407 //
408 // QueryResult result = query.execute();
409
410 String pattern = null;
411 if (SlcTypes.SLC_EXPORTED_PACKAGE.equals(nodeType))
412 pattern = "slc:Export-Package*";
413 else if (SlcTypes.SLC_JAVA_PACKAGE.equals(nodeType))
414 pattern = "slc:uses*";
415 else if (SlcTypes.SLC_IMPORTED_PACKAGE.equals(nodeType))
416 pattern = "slc:Import-Package*";
417 else if (SlcTypes.SLC_REQUIRED_BUNDLE.equals(nodeType))
418 pattern = "slc:Require-Bundle*";
419
420 return parent.getNodes(pattern);
421 }
422
423 private class TableContentProvider implements IStructuredContentProvider {
424 private String nodeType;
425 private String orderBy;
426
427 TableContentProvider(String nodeType, String orderBy) {
428 this.nodeType = nodeType;
429 this.orderBy = orderBy;
430 }
431
432 public void dispose() {
433 }
434
435 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
436 }
437
438 public Object[] getElements(Object arg0) {
439 try {
440 List<Node> nodes = JcrUtils.nodeIteratorToList(listNodes(
441 currBundle, nodeType, orderBy));
442 return nodes.toArray();
443 } catch (RepositoryException e) {
444 ErrorFeedback.show("Cannot list children Nodes", e);
445 return null;
446 }
447 }
448 }
449
450 /* HELPERS */
451 private Section addSection(Composite parent, String title) {
452 Section section = toolkit.createSection(parent, Section.TITLE_BAR);
453 section.setText(title);
454 section.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
455 return section;
456 }
457
458 }