]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms.ui/src/org/argeo/cms/viewers/JcrVersionCmsEditable.java
Add JGit to client.
[lgpl/argeo-commons.git] / org.argeo.cms.ui / src / org / argeo / cms / viewers / 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.CmsException;
12 import org.argeo.cms.ui.CmsEditable;
13 import org.argeo.cms.ui.CmsEditionEvent;
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_SET_PROPERTY)) {
30 // was Session.ACTION_ADD_NODE
31 canEdit = true;
32 if (!node.isNodeType(NodeType.MIX_VERSIONABLE)) {
33 node.addMixin(NodeType.MIX_VERSIONABLE);
34 node.getSession().save();
35 }
36 versionManager = node.getSession().getWorkspace()
37 .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",
47 "CTRL+E" });
48 display.addFilter(SWT.KeyDown, new Listener() {
49 private static final long serialVersionUID = -4378653870463187318L;
50
51 public void handleEvent(Event e) {
52 boolean ctrlPressed = (e.stateMask & SWT.CTRL) != 0;
53 if (ctrlPressed && e.keyCode == '\r')
54 stopEditing();
55 else if (ctrlPressed && e.keyCode == 'E')
56 stopEditing();
57 }
58 });
59 }
60 }
61
62 @Override
63 public Boolean canEdit() {
64 return canEdit;
65 }
66
67 public Boolean isEditing() {
68 try {
69 if (!canEdit())
70 return false;
71 return versionManager.isCheckedOut(nodePath);
72 } catch (RepositoryException e) {
73 throw new CmsException("Cannot check whether " + nodePath
74 + " is editing", e);
75 }
76 }
77
78 @Override
79 public void startEditing() {
80 try {
81 versionManager.checkout(nodePath);
82 setChanged();
83 } catch (RepositoryException e1) {
84 throw new CmsException("Cannot publish " + nodePath);
85 }
86 notifyObservers(new CmsEditionEvent(nodePath,
87 CmsEditionEvent.START_EDITING));
88 }
89
90 @Override
91 public void stopEditing() {
92 try {
93 versionManager.checkin(nodePath);
94 setChanged();
95 } catch (RepositoryException e1) {
96 throw new CmsException("Cannot publish " + nodePath, e1);
97 }
98 notifyObservers(new CmsEditionEvent(nodePath,
99 CmsEditionEvent.STOP_EDITING));
100 }
101 }