]> git.argeo.org Git - lgpl/argeo-commons.git/blob - JcrVersionCmsEditable.java
bcd42851fb6a0eb1529eeff4111d03790d6ed7d0
[lgpl/argeo-commons.git] / JcrVersionCmsEditable.java
1 package org.argeo.cms.viewers;
2
3 import java.util.Observable;
4
5 import javax.jcr.Node;
6 import javax.jcr.RepositoryException;
7 import javax.jcr.Session;
8 import javax.jcr.nodetype.NodeType;
9 import javax.jcr.version.VersionManager;
10
11 import org.argeo.cms.CmsEditable;
12 import org.argeo.cms.CmsEditionEvent;
13 import org.argeo.cms.CmsException;
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 Observable implements CmsEditable {
22 private final String nodePath;// cache
23 private final VersionManager versionManager;
24 private final Boolean canEdit;
25
26 public JcrVersionCmsEditable(Node node) throws RepositoryException {
27 this.nodePath = node.getPath();
28 if (node.getSession().hasPermission(node.getPath(),
29 Session.ACTION_ADD_NODE)) {
30 canEdit = true;
31 if (!node.isNodeType(NodeType.MIX_VERSIONABLE)) {
32 node.addMixin(NodeType.MIX_VERSIONABLE);
33 node.getSession().save();
34 }
35 versionManager = node.getSession().getWorkspace()
36 .getVersionManager();
37 } else {
38 canEdit = false;
39 versionManager = null;
40 }
41
42 // bind keys
43 if (canEdit) {
44 Display display = Display.getCurrent();
45 display.setData(RWT.ACTIVE_KEYS, new String[] { "CTRL+RETURN",
46 "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 CmsException("Cannot check whether " + nodePath
73 + " is editing", e);
74 }
75 }
76
77 @Override
78 public void startEditing() {
79 try {
80 versionManager.checkout(nodePath);
81 setChanged();
82 } catch (RepositoryException e1) {
83 throw new CmsException("Cannot publish " + nodePath);
84 }
85 notifyObservers(new CmsEditionEvent(nodePath,
86 CmsEditionEvent.START_EDITING));
87 }
88
89 @Override
90 public void stopEditing() {
91 try {
92 versionManager.checkin(nodePath);
93 setChanged();
94 } catch (RepositoryException e1) {
95 throw new CmsException("Cannot publish " + nodePath, e1);
96 }
97 notifyObservers(new CmsEditionEvent(nodePath,
98 CmsEditionEvent.STOP_EDITING));
99 }
100 }