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