View Javadoc
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.cms.e4.jcr;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  
21  import javax.jcr.Node;
22  import javax.jcr.Property;
23  import javax.jcr.PropertyIterator;
24  import javax.jcr.RepositoryException;
25  
26  import org.argeo.cms.ui.jcr.PropertyLabelProvider;
27  import org.argeo.eclipse.ui.EclipseUiException;
28  import org.eclipse.jface.layout.TreeColumnLayout;
29  import org.eclipse.jface.viewers.ColumnWeightData;
30  import org.eclipse.jface.viewers.IBaseLabelProvider;
31  import org.eclipse.jface.viewers.ITreeContentProvider;
32  import org.eclipse.jface.viewers.TreeViewer;
33  import org.eclipse.jface.viewers.Viewer;
34  import org.eclipse.swt.SWT;
35  import org.eclipse.swt.custom.ScrolledComposite;
36  import org.eclipse.swt.layout.FillLayout;
37  import org.eclipse.swt.widgets.Composite;
38  import org.eclipse.swt.widgets.Tree;
39  import org.eclipse.swt.widgets.TreeColumn;
40  
41  /**
42   * Generic editor property page. Lists all properties of current node as a
43   * complex tree. TODO: enable editing
44   */
45  public class GenericPropertyPage {
46  
47  	// Main business Objects
48  	private Node currentNode;
49  
50  	public GenericPropertyPage(Node currentNode) {
51  		this.currentNode = currentNode;
52  	}
53  
54  	protected void createFormContent(Composite parent) {
55  		Composite innerBox = new Composite(parent, SWT.NONE);
56  		// Composite innerBox = new Composite(body, SWT.NO_FOCUS);
57  		FillLayout layout = new FillLayout();
58  		layout.marginHeight = 5;
59  		layout.marginWidth = 5;
60  		innerBox.setLayout(layout);
61  		createComplexTree(innerBox);
62  		// TODO TreeColumnLayout triggers a scroll issue with the form:
63  		// The inside body is always to big and a scroll bar is shown
64  		// Composite tableCmp = new Composite(body, SWT.NO_FOCUS);
65  		// createComplexTree(tableCmp);
66  	}
67  
68  	private TreeViewer createComplexTree(Composite parent) {
69  		int style = SWT.BORDER | SWT.MULTI | SWT.FULL_SELECTION;
70  		Tree tree = new Tree(parent, style);
71  		TreeColumnLayout tableColumnLayout = new TreeColumnLayout();
72  
73  		createColumn(tree, tableColumnLayout, "Property", SWT.LEFT, 200, 30);
74  		createColumn(tree, tableColumnLayout, "Value(s)", SWT.LEFT, 300, 60);
75  		createColumn(tree, tableColumnLayout, "Type", SWT.LEFT, 75, 10);
76  		createColumn(tree, tableColumnLayout, "Attributes", SWT.LEFT, 75, 0);
77  		// Do not apply the treeColumnLayout it does not work yet
78  		// parent.setLayout(tableColumnLayout);
79  
80  		tree.setLinesVisible(true);
81  		tree.setHeaderVisible(true);
82  
83  		TreeViewer treeViewer = new TreeViewer(tree);
84  		treeViewer.setContentProvider(new TreeContentProvider());
85  		treeViewer.setLabelProvider((IBaseLabelProvider) new PropertyLabelProvider());
86  		treeViewer.setInput(currentNode);
87  		treeViewer.expandAll();
88  		return treeViewer;
89  	}
90  
91  	private static TreeColumn createColumn(Tree parent, TreeColumnLayout tableColumnLayout, String name, int style,
92  			int width, int weight) {
93  		TreeColumn column = new TreeColumn(parent, style);
94  		column.setText(name);
95  		column.setWidth(width);
96  		column.setMoveable(true);
97  		column.setResizable(true);
98  		tableColumnLayout.setColumnData(column, new ColumnWeightData(weight, width, true));
99  		return column;
100 	}
101 
102 	private class TreeContentProvider implements ITreeContentProvider {
103 		private static final long serialVersionUID = -6162736530019406214L;
104 
105 		public Object[] getElements(Object parent) {
106 			Object[] props = null;
107 			try {
108 
109 				if (parent instanceof Node) {
110 					Node node = (Node) parent;
111 					PropertyIterator pi;
112 					pi = node.getProperties();
113 					List<Property> propList = new ArrayList<Property>();
114 					while (pi.hasNext()) {
115 						propList.add(pi.nextProperty());
116 					}
117 					props = propList.toArray();
118 				}
119 			} catch (RepositoryException e) {
120 				throw new EclipseUiException("Unexpected exception while listing node properties", e);
121 			}
122 			return props;
123 		}
124 
125 		public Object getParent(Object child) {
126 			return null;
127 		}
128 
129 		public Object[] getChildren(Object parent) {
130 			if (parent instanceof Property) {
131 				Property prop = (Property) parent;
132 				try {
133 					if (prop.isMultiple())
134 						return prop.getValues();
135 				} catch (RepositoryException e) {
136 					throw new EclipseUiException("Cannot get multi-prop values on " + prop, e);
137 				}
138 			}
139 			return null;
140 		}
141 
142 		public boolean hasChildren(Object parent) {
143 			try {
144 				return (parent instanceof Property && ((Property) parent).isMultiple());
145 			} catch (RepositoryException e) {
146 				throw new EclipseUiException("Cannot check if property is multiple for " + parent, e);
147 			}
148 		}
149 
150 		public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
151 		}
152 
153 		public void dispose() {
154 		}
155 	}
156 }