1 package org
.argeo
.cms
.ui
.viewers
;
3 import javax
.jcr
.ItemNotFoundException
;
5 import javax
.jcr
.RepositoryException
;
6 import javax
.jcr
.Session
;
7 import javax
.jcr
.nodetype
.NodeType
;
8 import javax
.jcr
.version
.VersionManager
;
10 import org
.argeo
.api
.cms
.CmsLog
;
11 import org
.argeo
.api
.cms
.ux
.CmsEditionEvent
;
12 import org
.argeo
.cms
.ux
.AbstractCmsEditable
;
13 import org
.argeo
.jcr
.JcrException
;
14 import org
.eclipse
.rap
.rwt
.RWT
;
15 import org
.eclipse
.swt
.SWT
;
16 import org
.eclipse
.swt
.widgets
.Display
;
17 import org
.eclipse
.swt
.widgets
.Event
;
18 import org
.eclipse
.swt
.widgets
.Listener
;
20 /** Provides the CmsEditable semantic based on JCR versioning. */
21 public class JcrVersionCmsEditable
extends AbstractCmsEditable
{
22 private final static CmsLog log
= CmsLog
.getLog(JcrVersionCmsEditable
.class);
24 private final String nodePath
;// cache
25 private final VersionManager versionManager
;
26 private final Boolean canEdit
;
28 public JcrVersionCmsEditable(Node node
) throws RepositoryException
{
29 this.nodePath
= node
.getPath();
30 if (node
.getSession().hasPermission(node
.getPath(), Session
.ACTION_SET_PROPERTY
)) {
31 // was Session.ACTION_ADD_NODE
33 if (!node
.isNodeType(NodeType
.MIX_VERSIONABLE
)) {
34 node
.addMixin(NodeType
.MIX_VERSIONABLE
);
35 node
.getSession().save();
37 versionManager
= node
.getSession().getWorkspace().getVersionManager();
40 versionManager
= null;
45 Display display
= Display
.getCurrent();
46 display
.setData(RWT
.ACTIVE_KEYS
, new String
[] { "CTRL+RETURN", "CTRL+E" });
47 display
.addFilter(SWT
.KeyDown
, new Listener() {
48 private static final long serialVersionUID
= -4378653870463187318L;
50 public void handleEvent(Event e
) {
51 boolean ctrlPressed
= (e
.stateMask
& SWT
.CTRL
) != 0;
52 if (ctrlPressed
&& e
.keyCode
== '\r')
54 else if (ctrlPressed
&& e
.keyCode
== 'E')
62 public Boolean
canEdit() {
66 public Boolean
isEditing() {
70 return versionManager
.isCheckedOut(nodePath
);
71 } catch (RepositoryException e
) {
72 throw new JcrException("Cannot check whether " + nodePath
+ " is editing", e
);
77 public void startEditing() {
79 versionManager
.checkout(nodePath
);
81 } catch (RepositoryException e
) {
82 throw new JcrException("Cannot publish " + nodePath
, e
);
84 notifyListeners(new CmsEditionEvent(nodePath
, CmsEditionEvent
.START_EDITING
, this));
88 public void stopEditing() {
90 versionManager
.checkin(nodePath
);
92 } catch (ItemNotFoundException e
) {
93 // we ignore that the version was not found
94 // since it is most likely because the user doesn't have the rights
95 // on the whole workspace
96 if (log
.isTraceEnabled())
97 log
.trace("Cannot retrieve version after check in", e
);
98 } catch (RepositoryException e
) {
99 throw new JcrException("Cannot publish " + nodePath
, e
);
101 notifyListeners(new CmsEditionEvent(nodePath
, CmsEditionEvent
.STOP_EDITING
, this));