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