]>
git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms.ui/src/org/argeo/cms/ui/widgets/JcrComposite.java
1 package org
.argeo
.cms
.ui
.widgets
;
5 import javax
.jcr
.Property
;
6 import javax
.jcr
.RepositoryException
;
7 import javax
.jcr
.Session
;
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
;
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
46 Property property
= null;
47 if (item
instanceof Node
) {
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();
56 this.nodeId
= node
.getIdentifier();
60 setLayout(CmsUiUtils
.noSpaceGridLayout());
61 } catch (RepositoryException e
) {
62 throw new IllegalStateException("Cannot create composite from " + item
, e
);
66 public synchronized Node
getNode() {
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
);
76 private synchronized Node
getNodeInternal() throws RepositoryException
{
79 else if (session
!= null)
81 return session
.getNodeByIdentifier(nodeId
);
88 public synchronized Property
getProperty() {
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
);
101 public synchronized Boolean
itemIsNode() {
102 return property
== null;
105 /** Set/update the cache or change the node */
106 public synchronized void setNode(Node node
) {
108 throw new CmsException("Cannot set a Node on a Property");
110 if (node
== null) {// clear cache
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();
125 cache
= node
;// set/update cache
127 } catch (RepositoryException e
) {
128 throw new IllegalStateException(e
);
132 /** Set/update the cache or change the property */
133 public synchronized void setProperty(Property prop
) {
135 throw new CmsException("Cannot set a Property on a Node");
137 if (prop
== null) {// clear cache
143 if (session
== null || session
!= prop
.getSession())// check session
144 throw new IllegalArgumentException("Uncompatible session");
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();
153 cache
= node
;// set/update cache
155 } catch (RepositoryException e
) {
156 throw new IllegalStateException(e
);
160 public synchronized String
getNodeId() {
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
))
168 this.nodeId
= nodeId
;
170 cache
= session
.getNodeByIdentifier(this.nodeId
);
174 protected synchronized void itemUpdated() {
178 public Session
getSession() {