]> git.argeo.org Git - lgpl/argeo-commons.git/blob - jcr/org.argeo.cms.ui/src/org/argeo/cms/ui/widgets/JcrComposite.java
Adapt to new third parties
[lgpl/argeo-commons.git] / jcr / org.argeo.cms.ui / src / org / argeo / cms / ui / widgets / JcrComposite.java
1 package org.argeo.cms.ui.widgets;
2
3 import javax.jcr.Item;
4 import javax.jcr.ItemNotFoundException;
5 import javax.jcr.Node;
6 import javax.jcr.Property;
7 import javax.jcr.RepositoryException;
8 import javax.jcr.Session;
9
10 import org.argeo.cms.swt.CmsSwtUtils;
11 import org.argeo.jcr.JcrException;
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 UnsupportedOperationException("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(CmsSwtUtils.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 IllegalStateException("Item is not a Node");
70 return getNodeInternal();
71 } catch (RepositoryException e) {
72 throw new JcrException("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 String getPropertyName() {
89 try {
90 return getProperty().getName();
91 } catch (RepositoryException e) {
92 throw new JcrException("Cannot get property name", e);
93 }
94 }
95
96 public synchronized Node getPropertyNode() {
97 try {
98 return getProperty().getNode();
99 } catch (RepositoryException e) {
100 throw new JcrException("Cannot get property name", e);
101 }
102 }
103
104 public synchronized Property getProperty() {
105 try {
106 if (itemIsNode())
107 throw new IllegalStateException("Item is not a Property");
108 Node node = getNodeInternal();
109 if (!node.hasProperty(property))
110 throw new IllegalStateException("Property " + property + " is not set on " + node);
111 return node.getProperty(property);
112 } catch (RepositoryException e) {
113 throw new JcrException("Cannot get property " + property + " from node " + nodeId, e);
114 }
115 }
116
117 public synchronized boolean itemIsNode() {
118 return property == null;
119 }
120
121 public synchronized boolean itemExists() {
122 if (session == null)
123 return false;
124 try {
125 Node n = session.getNodeByIdentifier(nodeId);
126 if (!itemIsNode())
127 return n.hasProperty(property);
128 else
129 return true;
130 } catch (ItemNotFoundException e) {
131 return false;
132 } catch (RepositoryException e) {
133 throw new JcrException("Cannot check whether node exists", e);
134 }
135 }
136
137 /** Set/update the cache or change the node */
138 public synchronized void setNode(Node node) {
139 if (!itemIsNode())
140 throw new IllegalArgumentException("Cannot set a Node on a Property");
141
142 if (node == null) {// clear cache
143 this.cache = null;
144 return;
145 }
146
147 try {
148 // if (session != null || session != node.getSession())// check session
149 // throw new IllegalArgumentException("Uncompatible session");
150 // if (session == null)
151 session = node.getSession();
152 if (nodeId == null || !nodeId.equals(node.getIdentifier())) {
153 nodeId = node.getIdentifier();
154 cache = node;
155 itemUpdated();
156 } else {
157 cache = node;// set/update cache
158 }
159 } catch (RepositoryException e) {
160 throw new IllegalStateException(e);
161 }
162 }
163
164 /** Set/update the cache or change the property */
165 public synchronized void setProperty(Property prop) {
166 if (itemIsNode())
167 throw new IllegalArgumentException("Cannot set a Property on a Node");
168
169 if (prop == null) {// clear cache
170 this.cache = null;
171 return;
172 }
173
174 try {
175 if (session == null || session != prop.getSession())// check session
176 throw new IllegalArgumentException("Uncompatible session");
177
178 Node node = prop.getNode();
179 if (nodeId == null || !nodeId.equals(node.getIdentifier()) || !property.equals(prop.getName())) {
180 nodeId = node.getIdentifier();
181 property = prop.getName();
182 cache = node;
183 itemUpdated();
184 } else {
185 cache = node;// set/update cache
186 }
187 } catch (RepositoryException e) {
188 throw new IllegalStateException(e);
189 }
190 }
191
192 public synchronized String getNodeId() {
193 return nodeId;
194 }
195
196 /** Change the node, does nothing if same. */
197 public synchronized void setNodeId(String nodeId) throws RepositoryException {
198 if (this.nodeId != null && this.nodeId.equals(nodeId))
199 return;
200 this.nodeId = nodeId;
201 if (cache != null)
202 cache = session.getNodeByIdentifier(this.nodeId);
203 itemUpdated();
204 }
205
206 protected synchronized void itemUpdated() {
207 layout();
208 }
209
210 public Session getSession() {
211 return session;
212 }
213 }