]> git.argeo.org Git - lgpl/argeo-commons.git/blob - eclipse/plugins/org.argeo.eclipse.ui.jcr/src/main/java/org/argeo/eclipse/ui/jcr/views/GenericJcrBrowser.java
Expose Jackrabbit repositories via HTTP
[lgpl/argeo-commons.git] / eclipse / plugins / org.argeo.eclipse.ui.jcr / src / main / java / org / argeo / eclipse / ui / jcr / views / GenericJcrBrowser.java
1 package org.argeo.eclipse.ui.jcr.views;
2
3 import java.util.ArrayList;
4 import java.util.Arrays;
5 import java.util.Comparator;
6 import java.util.List;
7 import java.util.Set;
8 import java.util.TreeSet;
9
10 import javax.jcr.Item;
11 import javax.jcr.Node;
12 import javax.jcr.NodeIterator;
13 import javax.jcr.Property;
14 import javax.jcr.PropertyIterator;
15 import javax.jcr.PropertyType;
16 import javax.jcr.RepositoryException;
17 import javax.jcr.Session;
18 import javax.jcr.nodetype.NodeType;
19
20 import org.argeo.ArgeoException;
21 import org.eclipse.jface.viewers.ColumnLabelProvider;
22 import org.eclipse.jface.viewers.ISelectionChangedListener;
23 import org.eclipse.jface.viewers.IStructuredContentProvider;
24 import org.eclipse.jface.viewers.IStructuredSelection;
25 import org.eclipse.jface.viewers.ITreeContentProvider;
26 import org.eclipse.jface.viewers.LabelProvider;
27 import org.eclipse.jface.viewers.SelectionChangedEvent;
28 import org.eclipse.jface.viewers.TableViewer;
29 import org.eclipse.jface.viewers.TableViewerColumn;
30 import org.eclipse.jface.viewers.TreeViewer;
31 import org.eclipse.jface.viewers.Viewer;
32 import org.eclipse.swt.SWT;
33 import org.eclipse.swt.custom.SashForm;
34 import org.eclipse.swt.layout.FillLayout;
35 import org.eclipse.swt.layout.GridData;
36 import org.eclipse.swt.layout.GridLayout;
37 import org.eclipse.swt.widgets.Composite;
38 import org.eclipse.ui.part.ViewPart;
39
40 public class GenericJcrBrowser extends ViewPart {
41 // private final static Log log =
42 // LogFactory.getLog(GenericJcrBrowser.class);
43
44 private TreeViewer nodesViewer;
45 private TableViewer propertiesViewer;
46
47 private Session jcrSession;
48
49 private Comparator<Item> itemComparator = new Comparator<Item>() {
50 public int compare(Item o1, Item o2) {
51 try {
52 return o1.getName().compareTo(o2.getName());
53 } catch (RepositoryException e) {
54 throw new ArgeoException("Cannot compare " + o1 + " and " + o2,
55 e);
56 }
57 }
58 };
59
60 @Override
61 public void createPartControl(Composite parent) {
62 parent.setLayout(new FillLayout());
63
64 SashForm sashForm = new SashForm(parent, SWT.VERTICAL);
65 sashForm.setSashWidth(4);
66 sashForm.setLayout(new FillLayout());
67
68 Composite top = new Composite(sashForm, SWT.NONE);
69 GridLayout gl = new GridLayout(1, false);
70 top.setLayout(gl);
71
72 nodesViewer = new TreeViewer(top, SWT.MULTI | SWT.H_SCROLL
73 | SWT.V_SCROLL);
74 nodesViewer.getTree().setLayoutData(
75 new GridData(SWT.FILL, SWT.FILL, true, true));
76 nodesViewer.setContentProvider(new NodeContentProvider());
77 nodesViewer.setLabelProvider(new NodeLabelProvider());
78 nodesViewer
79 .addSelectionChangedListener(new ISelectionChangedListener() {
80 public void selectionChanged(SelectionChangedEvent event) {
81 if (!event.getSelection().isEmpty()) {
82 IStructuredSelection sel = (IStructuredSelection) event
83 .getSelection();
84 propertiesViewer.setInput(sel.getFirstElement());
85 } else {
86 propertiesViewer.setInput(getViewSite());
87 }
88 }
89 });
90 try {
91 nodesViewer.setInput(jcrSession.getRootNode());
92 } catch (RepositoryException e) {
93 throw new ArgeoException("Cannot initialize view", e);
94 }
95
96 Composite bottom = new Composite(sashForm, SWT.NONE);
97 bottom.setLayout(new GridLayout(1, false));
98
99 propertiesViewer = new TableViewer(bottom);
100 propertiesViewer.getTable().setLayoutData(
101 new GridData(SWT.FILL, SWT.FILL, true, true));
102 propertiesViewer.getTable().setHeaderVisible(true);
103 propertiesViewer.setContentProvider(new PropertiesContentProvider());
104 TableViewerColumn col = new TableViewerColumn(propertiesViewer,
105 SWT.NONE);
106 col.getColumn().setText("Name");
107 col.getColumn().setWidth(200);
108 col.setLabelProvider(new ColumnLabelProvider() {
109 public String getText(Object element) {
110 try {
111 return ((Property) element).getName();
112 } catch (RepositoryException e) {
113 throw new ArgeoException(
114 "Unexpected exception in label provider", e);
115 }
116 }
117 });
118 col = new TableViewerColumn(propertiesViewer, SWT.NONE);
119 col.getColumn().setText("Value");
120 col.getColumn().setWidth(400);
121 col.setLabelProvider(new ColumnLabelProvider() {
122 public String getText(Object element) {
123 try {
124 Property property = (Property) element;
125 if (property.getType() == PropertyType.BINARY)
126 return "<binary>";
127 else if (property.isMultiple())
128 return Arrays.asList(property.getValues()).toString();
129 else
130 return property.getValue().getString();
131 } catch (RepositoryException e) {
132 throw new ArgeoException(
133 "Unexpected exception in label provider", e);
134 }
135 }
136 });
137 col = new TableViewerColumn(propertiesViewer, SWT.NONE);
138 col.getColumn().setText("Type");
139 col.getColumn().setWidth(200);
140 col.setLabelProvider(new ColumnLabelProvider() {
141 public String getText(Object element) {
142 try {
143 return PropertyType.nameFromValue(((Property) element)
144 .getType());
145 } catch (RepositoryException e) {
146 throw new ArgeoException(
147 "Unexpected exception in label provider", e);
148 }
149 }
150 });
151 propertiesViewer.setInput(getViewSite());
152
153 sashForm.setWeights(getWeights());
154
155 }
156
157 @Override
158 public void setFocus() {
159 nodesViewer.getTree().setFocus();
160 }
161
162 /**
163 * To be overidden to adapt size of form and result frames.
164 *
165 * @return
166 */
167 protected int[] getWeights() {
168 return new int[] { 70, 30 };
169 }
170
171 public void setJcrSession(Session jcrSession) {
172 this.jcrSession = jcrSession;
173 }
174
175 /*
176 * NODES
177 */
178 private class NodeContentProvider implements ITreeContentProvider {
179
180 public Object[] getElements(Object inputElement) {
181 return getChildren(inputElement);
182 }
183
184 public Object[] getChildren(Object parentElement) {
185 try {
186 if (parentElement instanceof Node) {
187 List<Node> children = new ArrayList<Node>();
188 NodeIterator nit = ((Node) parentElement).getNodes();
189 while (nit.hasNext()) {
190 Node node = nit.nextNode();
191 children.add(node);
192 }
193 Node[] arr = children.toArray(new Node[children.size()]);
194 Arrays.sort(arr, itemComparator);
195 return arr;
196 } else {
197 return null;
198 }
199 } catch (RepositoryException e) {
200 throw new ArgeoException("Cannot retrieve children for "
201 + parentElement, e);
202 }
203 }
204
205 public Object getParent(Object element) {
206 try {
207 if (element instanceof Node) {
208 return ((Node) element).getParent();
209 }
210 return null;
211 } catch (RepositoryException e) {
212 throw new ArgeoException(
213 "Cannot retrieve parent of " + element, e);
214 }
215 }
216
217 public boolean hasChildren(Object element) {
218 try {
219 if (element instanceof Node) {
220 return ((Node) element).hasNodes();
221 }
222 return false;
223 } catch (RepositoryException e) {
224 throw new ArgeoException("Cannot check children of " + element,
225 e);
226 }
227 }
228
229 public void dispose() {
230 }
231
232 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
233 }
234
235 }
236
237 class NodeLabelProvider extends LabelProvider {
238
239 public String getText(Object element) {
240 try {
241 if (element instanceof Node) {
242 Node node = (Node) element;
243 String label = node.getName();
244 // try {
245 // Item primaryItem = node.getPrimaryItem();
246 // label = primaryItem instanceof Property ? ((Property)
247 // primaryItem)
248 // .getValue().getString()
249 // + " ("
250 // + node.getName()
251 // + ")" : node.getName();
252 // } catch (RepositoryException e) {
253 // label = node.getName();
254 // }
255 StringBuffer mixins = new StringBuffer("");
256 for (NodeType type : node.getMixinNodeTypes())
257 mixins.append(' ').append(type.getName());
258
259 return label + " [" + node.getPrimaryNodeType().getName()
260 + mixins + "]";
261 }
262 return element.toString();
263 } catch (RepositoryException e) {
264 throw new ArgeoException("Cannot get text for of " + element, e);
265 }
266 }
267
268 }
269
270 /*
271 * PROPERTIES
272 */
273 private class PropertiesContentProvider implements
274 IStructuredContentProvider {
275
276 public void dispose() {
277 }
278
279 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
280 }
281
282 public Object[] getElements(Object inputElement) {
283 try {
284 if (inputElement instanceof Node) {
285 Set<Property> props = new TreeSet<Property>(itemComparator);
286 PropertyIterator pit = ((Node) inputElement)
287 .getProperties();
288 while (pit.hasNext())
289 props.add(pit.nextProperty());
290 return props.toArray();
291 }
292 return new Object[] {};
293 } catch (RepositoryException e) {
294 throw new ArgeoException("Cannot get element for "
295 + inputElement, e);
296 }
297 }
298
299 }
300 }