]>
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 final 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 throws RepositoryException
{
33 this(parent
, style
, item
, false);
36 public JcrComposite(Composite parent
, int style
, Item item
,
37 boolean cacheImmediately
) throws RepositoryException
{
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(
53 "Multiple properties not supported yet.");
54 this.property
= property
.getName();
55 node
= property
.getParent();
57 this.nodeId
= node
.getIdentifier();
61 setLayout(CmsUiUtils
.noSpaceGridLayout());
64 public synchronized Node
getNode() {
67 throw new CmsException("Item is not a Node");
68 return getNodeInternal();
69 } catch (RepositoryException e
) {
70 throw new CmsException("Cannot get node " + nodeId
, e
);
74 private synchronized Node
getNodeInternal() throws RepositoryException
{
77 else if (session
!= null)
79 return session
.getNodeByIdentifier(nodeId
);
86 public synchronized Property
getProperty() {
89 throw new CmsException("Item is not a Property");
90 Node node
= getNodeInternal();
91 if (!node
.hasProperty(property
))
92 throw new CmsException("Property " + property
93 + " is not set on " + node
);
94 return node
.getProperty(property
);
95 } catch (RepositoryException e
) {
96 throw new CmsException("Cannot get property " + property
97 + " 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
) throws RepositoryException
{
108 throw new CmsException("Cannot set a Node on a Property");
110 if (node
== null) {// clear cache
115 if (session
== null || session
!= node
.getSession())// check session
116 throw new CmsException("Uncompatible session");
118 if (nodeId
== null || !nodeId
.equals(node
.getIdentifier())) {
119 nodeId
= node
.getIdentifier();
123 cache
= node
;// set/update cache
127 /** Set/update the cache or change the property */
128 public synchronized void setProperty(Property prop
)
129 throws RepositoryException
{
131 throw new CmsException("Cannot set a Property on a Node");
133 if (prop
== null) {// clear cache
138 if (session
== null || session
!= prop
.getSession())// check session
139 throw new CmsException("Uncompatible session");
141 Node node
= prop
.getNode();
142 if (nodeId
== null || !nodeId
.equals(node
.getIdentifier())
143 || !property
.equals(prop
.getName())) {
144 nodeId
= node
.getIdentifier();
145 property
= prop
.getName();
149 cache
= node
;// set/update cache
153 public synchronized String
getNodeId() {
157 /** Change the node, does nothing if same. */
158 public synchronized void setNodeId(String nodeId
)
159 throws RepositoryException
{
160 if (this.nodeId
!= null && this.nodeId
.equals(nodeId
))
162 this.nodeId
= nodeId
;
164 cache
= session
.getNodeByIdentifier(this.nodeId
);
168 protected synchronized void itemUpdated() {
172 public Session
getSession() {