]>
git.argeo.org Git - lgpl/argeo-commons.git/blob - jcr/org.argeo.cms.ui/src/org/argeo/cms/ui/widgets/JcrComposite.java
1 package org
.argeo
.cms
.ui
.widgets
;
4 import javax
.jcr
.ItemNotFoundException
;
6 import javax
.jcr
.Property
;
7 import javax
.jcr
.RepositoryException
;
8 import javax
.jcr
.Session
;
10 import org
.argeo
.cms
.swt
.CmsSwtUtils
;
11 import org
.argeo
.jcr
.JcrException
;
12 import org
.eclipse
.swt
.widgets
.Composite
;
14 /** A composite which can (optionally) manage a JCR Item. */
15 public class JcrComposite
extends Composite
{
16 private static final long serialVersionUID
= -1447009015451153367L;
18 private Session session
;
20 private String nodeId
;
21 private String property
= null;
24 /** Regular composite constructor. No layout is set. */
25 public JcrComposite(Composite parent
, int style
) {
31 public JcrComposite(Composite parent
, int style
, Item item
) {
32 this(parent
, style
, item
, false);
35 public JcrComposite(Composite parent
, int style
, Item item
, boolean cacheImmediately
) {
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;
46 Property property
= null;
47 if (item
instanceof Node
) {
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();
56 this.nodeId
= node
.getIdentifier();
60 setLayout(CmsSwtUtils
.noSpaceGridLayout());
61 } catch (RepositoryException e
) {
62 throw new IllegalStateException("Cannot create composite from " + item
, e
);
66 public synchronized Node
getNode() {
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
);
76 private synchronized Node
getNodeInternal() throws RepositoryException
{
79 else if (session
!= null)
81 return session
.getNodeByIdentifier(nodeId
);
88 public synchronized String
getPropertyName() {
90 return getProperty().getName();
91 } catch (RepositoryException e
) {
92 throw new JcrException("Cannot get property name", e
);
96 public synchronized Node
getPropertyNode() {
98 return getProperty().getNode();
99 } catch (RepositoryException e
) {
100 throw new JcrException("Cannot get property name", e
);
104 public synchronized Property
getProperty() {
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
);
117 public synchronized boolean itemIsNode() {
118 return property
== null;
121 public synchronized boolean itemExists() {
125 Node n
= session
.getNodeByIdentifier(nodeId
);
127 return n
.hasProperty(property
);
130 } catch (ItemNotFoundException e
) {
132 } catch (RepositoryException e
) {
133 throw new JcrException("Cannot check whether node exists", e
);
137 /** Set/update the cache or change the node */
138 public synchronized void setNode(Node node
) {
140 throw new IllegalArgumentException("Cannot set a Node on a Property");
142 if (node
== null) {// clear cache
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();
157 cache
= node
;// set/update cache
159 } catch (RepositoryException e
) {
160 throw new IllegalStateException(e
);
164 /** Set/update the cache or change the property */
165 public synchronized void setProperty(Property prop
) {
167 throw new IllegalArgumentException("Cannot set a Property on a Node");
169 if (prop
== null) {// clear cache
175 if (session
== null || session
!= prop
.getSession())// check session
176 throw new IllegalArgumentException("Uncompatible session");
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();
185 cache
= node
;// set/update cache
187 } catch (RepositoryException e
) {
188 throw new IllegalStateException(e
);
192 public synchronized String
getNodeId() {
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
))
200 this.nodeId
= nodeId
;
202 cache
= session
.getNodeByIdentifier(this.nodeId
);
206 protected synchronized void itemUpdated() {
210 // public Session getSession() {