]> git.argeo.org Git - lgpl/argeo-commons.git/blob - ui/widgets/JcrComposite.java
Prepare next development cycle
[lgpl/argeo-commons.git] / ui / widgets / JcrComposite.java
1 package org.argeo.cms.ui.widgets;
2
3 import javax.jcr.Item;
4 import javax.jcr.Node;
5 import javax.jcr.Property;
6 import javax.jcr.RepositoryException;
7 import javax.jcr.Session;
8
9 import org.argeo.cms.CmsException;
10 import org.argeo.cms.ui.util.CmsUiUtils;
11 import org.eclipse.swt.SWT;
12 import org.eclipse.swt.widgets.Composite;
13
14 /** A composite which can (optionally) manage a JCR Item. */
15 public class JcrComposite extends Composite {
16 private static final long serialVersionUID = -1447009015451153367L;
17
18 private Session session;
19
20 private String nodeId;
21 private String property = null;
22 private Node cache;
23
24 /** Regular composite constructor. No layout is set. */
25 public JcrComposite(Composite parent, int style) {
26 super(parent, style);
27 session = null;
28 nodeId = null;
29 }
30
31 public JcrComposite(Composite parent, int style, Item item) {
32 this(parent, style, item, false);
33 }
34
35 public JcrComposite(Composite parent, int style, Item item, boolean cacheImmediately) {
36 super(parent, style);
37 if (item != null)
38 try {
39 this.session = item.getSession();
40 // if (!cacheImmediately && (SWT.READ_ONLY == (style & SWT.READ_ONLY))) {
41 // // (useless?) optimization: we only save a pointer to the session,
42 // // not even a reference to the item
43 // this.nodeId = null;
44 // } else {
45 Node node;
46 Property property = null;
47 if (item instanceof Node) {
48 node = (Node) item;
49 } else {// Property
50 property = (Property) item;
51 if (property.isMultiple())// TODO manage property index
52 throw new CmsException("Multiple properties not supported yet.");
53 this.property = property.getName();
54 node = property.getParent();
55 }
56 this.nodeId = node.getIdentifier();
57 if (cacheImmediately)
58 this.cache = node;
59 // }
60 setLayout(CmsUiUtils.noSpaceGridLayout());
61 } catch (RepositoryException e) {
62 throw new IllegalStateException("Cannot create composite from " + item, e);
63 }
64 }
65
66 public synchronized Node getNode() {
67 try {
68 if (!itemIsNode())
69 throw new CmsException("Item is not a Node");
70 return getNodeInternal();
71 } catch (RepositoryException e) {
72 throw new CmsException("Cannot get node " + nodeId, e);
73 }
74 }
75
76 private synchronized Node getNodeInternal() throws RepositoryException {
77 if (cache != null)
78 return cache;
79 else if (session != null)
80 if (nodeId != null)
81 return session.getNodeByIdentifier(nodeId);
82 else
83 return null;
84 else
85 return null;
86 }
87
88 public synchronized Property getProperty() {
89 try {
90 if (itemIsNode())
91 throw new CmsException("Item is not a Property");
92 Node node = getNodeInternal();
93 if (!node.hasProperty(property))
94 throw new CmsException("Property " + property + " is not set on " + node);
95 return node.getProperty(property);
96 } catch (RepositoryException e) {
97 throw new CmsException("Cannot get property " + property + " from node " + nodeId, e);
98 }
99 }
100
101 public synchronized Boolean itemIsNode() {
102 return property == null;
103 }
104
105 /** Set/update the cache or change the node */
106 public synchronized void setNode(Node node) {
107 if (!itemIsNode())
108 throw new CmsException("Cannot set a Node on a Property");
109
110 if (node == null) {// clear cache
111 this.cache = null;
112 return;
113 }
114
115 try {
116 // if (session != null || session != node.getSession())// check session
117 // throw new IllegalArgumentException("Uncompatible session");
118 // if (session == null)
119 session = node.getSession();
120 if (nodeId == null || !nodeId.equals(node.getIdentifier())) {
121 nodeId = node.getIdentifier();
122 cache = node;
123 itemUpdated();
124 } else {
125 cache = node;// set/update cache
126 }
127 } catch (RepositoryException e) {
128 throw new IllegalStateException(e);
129 }
130 }
131
132 /** Set/update the cache or change the property */
133 public synchronized void setProperty(Property prop) {
134 if (itemIsNode())
135 throw new CmsException("Cannot set a Property on a Node");
136
137 if (prop == null) {// clear cache
138 this.cache = null;
139 return;
140 }
141
142 try {
143 if (session == null || session != prop.getSession())// check session
144 throw new IllegalArgumentException("Uncompatible session");
145
146 Node node = prop.getNode();
147 if (nodeId == null || !nodeId.equals(node.getIdentifier()) || !property.equals(prop.getName())) {
148 nodeId = node.getIdentifier();
149 property = prop.getName();
150 cache = node;
151 itemUpdated();
152 } else {
153 cache = node;// set/update cache
154 }
155 } catch (RepositoryException e) {
156 throw new IllegalStateException(e);
157 }
158 }
159
160 public synchronized String getNodeId() {
161 return nodeId;
162 }
163
164 /** Change the node, does nothing if same. */
165 public synchronized void setNodeId(String nodeId) throws RepositoryException {
166 if (this.nodeId != null && this.nodeId.equals(nodeId))
167 return;
168 this.nodeId = nodeId;
169 if (cache != null)
170 cache = session.getNodeByIdentifier(this.nodeId);
171 itemUpdated();
172 }
173
174 protected synchronized void itemUpdated() {
175 layout();
176 }
177
178 public Session getSession() {
179 return session;
180 }
181 }