View Javadoc
1   package org.argeo.cms.ui.util;
2   
3   import java.text.DateFormat;
4   import java.text.SimpleDateFormat;
5   
6   import javax.jcr.Node;
7   import javax.jcr.NodeIterator;
8   import javax.jcr.Property;
9   import javax.jcr.PropertyIterator;
10  import javax.jcr.PropertyType;
11  import javax.jcr.RepositoryException;
12  import javax.jcr.Value;
13  
14  import org.argeo.cms.CmsException;
15  import org.argeo.cms.ui.CmsUiProvider;
16  import org.argeo.jcr.JcrUtils;
17  import org.eclipse.rap.rwt.RWT;
18  import org.eclipse.swt.SWT;
19  import org.eclipse.swt.layout.GridLayout;
20  import org.eclipse.swt.widgets.Composite;
21  import org.eclipse.swt.widgets.Control;
22  import org.eclipse.swt.widgets.Label;
23  
24  public class SimpleDynamicPages implements CmsUiProvider {
25  
26  	@Override
27  	public Control createUi(Composite parent, Node context)
28  			throws RepositoryException {
29  		if (context == null)
30  			throw new CmsException("Context cannot be null");
31  		parent.setLayout(new GridLayout(2, false));
32  
33  		// parent
34  		if (!context.getPath().equals("/")) {
35  			new CmsLink("..", context.getParent().getPath()).createUi(parent,
36  					context);
37  			new Label(parent, SWT.NONE).setText(context.getParent()
38  					.getPrimaryNodeType().getName());
39  		}
40  
41  		// context
42  		Label contextL = new Label(parent, SWT.NONE);
43  		contextL.setData(RWT.MARKUP_ENABLED, true);
44  		contextL.setText("<b>" + context.getName() + "</b>");
45  		new Label(parent, SWT.NONE).setText(context.getPrimaryNodeType()
46  				.getName());
47  
48  		// children
49  		// Label childrenL = new Label(parent, SWT.NONE);
50  		// childrenL.setData(RWT.MARKUP_ENABLED, true);
51  		// childrenL.setText("<i>Children:</i>");
52  		// childrenL.setLayoutData(new GridData(SWT.LEAD, SWT.CENTER, false,
53  		// false, 2, 1));
54  
55  		for (NodeIterator nIt = context.getNodes(); nIt.hasNext();) {
56  			Node child = nIt.nextNode();
57  			new CmsLink(child.getName(), child.getPath()).createUi(parent,
58  					context);
59  
60  			new Label(parent, SWT.NONE).setText(child.getPrimaryNodeType()
61  					.getName());
62  		}
63  
64  		// properties
65  		// Label propsL = new Label(parent, SWT.NONE);
66  		// propsL.setData(RWT.MARKUP_ENABLED, true);
67  		// propsL.setText("<i>Properties:</i>");
68  		// propsL.setLayoutData(new GridData(SWT.LEAD, SWT.CENTER, false, false,
69  		// 2, 1));
70  		for (PropertyIterator pIt = context.getProperties(); pIt.hasNext();) {
71  			Property property = pIt.nextProperty();
72  
73  			Label label = new Label(parent, SWT.NONE);
74  			label.setText(property.getName());
75  			label.setToolTipText(JcrUtils
76  					.getPropertyDefinitionAsString(property));
77  
78  			new Label(parent, SWT.NONE).setText(getPropAsString(property));
79  		}
80  
81  		return null;
82  	}
83  
84  	private String getPropAsString(Property property)
85  			throws RepositoryException {
86  		String result = "";
87  		DateFormat timeFormatter = new SimpleDateFormat("");
88  		if (property.isMultiple()) {
89  			result = getMultiAsString(property, ", ");
90  		} else {
91  			Value value = property.getValue();
92  			if (value.getType() == PropertyType.BINARY)
93  				result = "<binary>";
94  			else if (value.getType() == PropertyType.DATE)
95  				result = timeFormatter.format(value.getDate().getTime());
96  			else
97  				result = value.getString();
98  		}
99  		return result;
100 	}
101 
102 	private String getMultiAsString(Property property, String separator)
103 			throws RepositoryException {
104 		if (separator == null)
105 			separator = "; ";
106 		Value[] values = property.getValues();
107 		StringBuilder builder = new StringBuilder();
108 		for (Value val : values) {
109 			String currStr = val.getString();
110 			if (!"".equals(currStr.trim()))
111 				builder.append(currStr).append(separator);
112 		}
113 		if (builder.lastIndexOf(separator) >= 0)
114 			return builder.substring(0, builder.length() - separator.length());
115 		else
116 			return builder.toString();
117 	}
118 }