]> git.argeo.org Git - lgpl/argeo-commons.git/blob - jcr/org.argeo.cms.ui/src/org/argeo/cms/ui/viewers/JcrVersionCmsEditable.java
298fbdea9b6d3dd2b58fc6e01d50a5cb954c58b8
[lgpl/argeo-commons.git] / jcr / org.argeo.cms.ui / src / org / argeo / cms / ui / viewers / JcrVersionCmsEditable.java
1 package org.argeo.cms.ui.viewers;
2
3 import javax.jcr.Node;
4 import javax.jcr.RepositoryException;
5 import javax.jcr.Session;
6 import javax.jcr.nodetype.NodeType;
7 import javax.jcr.version.VersionManager;
8
9 import org.argeo.api.cms.ux.CmsEditionEvent;
10 import org.argeo.cms.ux.AbstractCmsEditable;
11 import org.argeo.jcr.JcrException;
12 import org.eclipse.rap.rwt.RWT;
13 import org.eclipse.swt.SWT;
14 import org.eclipse.swt.widgets.Display;
15 import org.eclipse.swt.widgets.Event;
16 import org.eclipse.swt.widgets.Listener;
17
18 /** Provides the CmsEditable semantic based on JCR versioning. */
19 public class JcrVersionCmsEditable extends AbstractCmsEditable {
20 private final String nodePath;// cache
21 private final VersionManager versionManager;
22 private final Boolean canEdit;
23
24 public JcrVersionCmsEditable(Node node) throws RepositoryException {
25 this.nodePath = node.getPath();
26 if (node.getSession().hasPermission(node.getPath(), Session.ACTION_SET_PROPERTY)) {
27 // was Session.ACTION_ADD_NODE
28 canEdit = true;
29 if (!node.isNodeType(NodeType.MIX_VERSIONABLE)) {
30 node.addMixin(NodeType.MIX_VERSIONABLE);
31 node.getSession().save();
32 }
33 versionManager = node.getSession().getWorkspace().getVersionManager();
34 } else {
35 canEdit = false;
36 versionManager = null;
37 }
38
39 // bind keys
40 if (canEdit) {
41 Display display = Display.getCurrent();
42 display.setData(RWT.ACTIVE_KEYS, new String[] { "CTRL+RETURN", "CTRL+E" });
43 display.addFilter(SWT.KeyDown, new Listener() {
44 private static final long serialVersionUID = -4378653870463187318L;
45
46 public void handleEvent(Event e) {
47 boolean ctrlPressed = (e.stateMask & SWT.CTRL) != 0;
48 if (ctrlPressed && e.keyCode == '\r')
49 stopEditing();
50 else if (ctrlPressed && e.keyCode == 'E')
51 stopEditing();
52 }
53 });
54 }
55 }
56
57 @Override
58 public Boolean canEdit() {
59 return canEdit;
60 }
61
62 public Boolean isEditing() {
63 try {
64 if (!canEdit())
65 return false;
66 return versionManager.isCheckedOut(nodePath);
67 } catch (RepositoryException e) {
68 throw new JcrException("Cannot check whether " + nodePath + " is editing", e);
69 }
70 }
71
72 @Override
73 public void startEditing() {
74 try {
75 versionManager.checkout(nodePath);
76 // setChanged();
77 } catch (RepositoryException e1) {
78 throw new JcrException("Cannot publish " + nodePath, e1);
79 }
80 notifyListeners(new CmsEditionEvent(nodePath, CmsEditionEvent.START_EDITING, this));
81 }
82
83 @Override
84 public void stopEditing() {
85 try {
86 versionManager.checkin(nodePath);
87 // setChanged();
88 } catch (RepositoryException e1) {
89 throw new JcrException("Cannot publish " + nodePath, e1);
90 }
91 notifyListeners(new CmsEditionEvent(nodePath, CmsEditionEvent.STOP_EDITING, this));
92 }
93
94 }