]> git.argeo.org Git - gpl/argeo-jcr.git/blob - swt/org.argeo.cms.jcr.ui/src/org/argeo/cms/ui/viewers/JcrVersionCmsEditable.java
91224490a7aa4d8c54fffd03f3986ab0183bca90
[gpl/argeo-jcr.git] / swt / org.argeo.cms.jcr.ui / src / org / argeo / cms / ui / viewers / JcrVersionCmsEditable.java
1 package org.argeo.cms.ui.viewers;
2
3 import javax.jcr.ItemNotFoundException;
4 import javax.jcr.Node;
5 import javax.jcr.RepositoryException;
6 import javax.jcr.Session;
7 import javax.jcr.nodetype.NodeType;
8 import javax.jcr.version.VersionManager;
9
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;
19
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);
23
24 private final String nodePath;// cache
25 private final VersionManager versionManager;
26 private final Boolean canEdit;
27
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
32 canEdit = true;
33 // if (!node.isNodeType(NodeType.MIX_SIMPLE_VERSIONABLE)) {
34 // node.addMixin(NodeType.MIX_SIMPLE_VERSIONABLE);
35 // node.getSession().save();
36 // }
37 versionManager = node.getSession().getWorkspace().getVersionManager();
38 } else {
39 canEdit = false;
40 versionManager = null;
41 }
42
43 // bind keys
44 if (canEdit) {
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;
49
50 public void handleEvent(Event e) {
51 boolean ctrlPressed = (e.stateMask & SWT.CTRL) != 0;
52 if (ctrlPressed && e.keyCode == '\r')
53 stopEditing();
54 else if (ctrlPressed && e.keyCode == 'E')
55 stopEditing();
56 }
57 });
58 }
59 }
60
61 @Override
62 public Boolean canEdit() {
63 return canEdit;
64 }
65
66 public Boolean isEditing() {
67 try {
68 if (!canEdit())
69 return false;
70 return versionManager.isCheckedOut(nodePath);
71 } catch (RepositoryException e) {
72 throw new JcrException("Cannot check whether " + nodePath + " is editing", e);
73 }
74 }
75
76 @Override
77 public void startEditing() {
78 try {
79 versionManager.checkout(nodePath);
80 // setChanged();
81 } catch (RepositoryException e) {
82 throw new JcrException("Cannot publish " + nodePath, e);
83 }
84 notifyListeners(new CmsEditionEvent(nodePath, CmsEditionEvent.START_EDITING, this));
85 }
86
87 @Override
88 public void stopEditing() {
89 try {
90 versionManager.checkin(nodePath);
91 // setChanged();
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);
100 }
101 notifyListeners(new CmsEditionEvent(nodePath, CmsEditionEvent.STOP_EDITING, this));
102 }
103
104 }