]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms/src/org/argeo/cms/users/UserViewerOld.java
6b522f5d9ed07d4a000e551e6da719fb474d598d
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / users / UserViewerOld.java
1 package org.argeo.cms.users;
2
3 import java.util.Observable;
4 import java.util.Observer;
5
6 import javax.jcr.Node;
7 import javax.jcr.RepositoryException;
8
9 import org.apache.commons.logging.Log;
10 import org.apache.commons.logging.LogFactory;
11 import org.argeo.ArgeoException;
12 import org.argeo.cms.CmsEditable;
13 import org.argeo.cms.CmsException;
14 import org.argeo.cms.util.CmsUtils;
15 import org.argeo.cms.viewers.EditablePart;
16 import org.argeo.cms.viewers.JcrVersionCmsEditable;
17 import org.eclipse.jface.viewers.ContentViewer;
18 import org.eclipse.jface.viewers.ISelection;
19 import org.eclipse.jface.viewers.StructuredSelection;
20 import org.eclipse.swt.events.MouseAdapter;
21 import org.eclipse.swt.events.MouseListener;
22 import org.eclipse.swt.widgets.Composite;
23 import org.eclipse.swt.widgets.Control;
24 import org.eclipse.swt.widgets.Widget;
25
26 /** Manage edition of the users */
27 public class UserViewerOld extends ContentViewer implements Observer {
28 private static final long serialVersionUID = -5055220955004863645L;
29
30 private final static Log log = LogFactory.getLog(UserViewerOld.class);
31
32 // private UserPart userPart = new UserPart();
33
34 private CmsEditable cmsEditable;
35 private Node currentDisplayed;
36
37 /** The basis for the layouts, typically the body of a ScrolledPage. */
38 private final Composite page;
39
40 private MouseListener mouseListener;
41
42 private EditablePart edited;
43 private ISelection selection = StructuredSelection.EMPTY;
44
45 protected UserViewerOld(Composite composite) {
46 page = composite;
47 }
48
49 void setDisplayedUser(Node node) {
50 try {
51
52 if (!hasChanged(node))
53 return;
54
55 CmsUtils.clear(page);
56 // userPart.createUi(page, node);
57
58 page.layout();
59 page.getParent().layout();
60
61 // update
62 cmsEditable = new JcrVersionCmsEditable(node);
63
64 if (this.cmsEditable instanceof Observable)
65 ((Observable) this.cmsEditable).addObserver(this);
66 // // if (cmsEditable.canEdit()) {
67 // // mouseListener = createMouseListener();
68 // // refresh();
69 // // }
70 } catch (RepositoryException re) {
71 throw new ArgeoException("Unable to display " + node, re);
72 }
73 }
74
75 private boolean hasChanged(Node node) throws RepositoryException {
76 if (currentDisplayed == null && node == null)
77 return false;
78 else if (currentDisplayed == null || node == null)
79 return true;
80 else
81 return node.getIdentifier()
82 .equals(currentDisplayed.getIdentifier());
83 }
84
85 /** Create (retrieve) the MouseListener to use. */
86 protected MouseListener createMouseListener() {
87 return new MouseAdapter() {
88 private static final long serialVersionUID = 1L;
89 };
90 }
91
92 @Override
93 public void update(Observable o, Object arg) {
94 if (o == cmsEditable)
95 editingStateChanged(cmsEditable);
96 }
97
98 /** To be overridden in order to provide the actual refresh */
99 protected void refresh(Control control) throws RepositoryException {
100 }
101
102 /** To be overridden.Save the edited part. */
103 protected void save(EditablePart part) throws RepositoryException {
104 }
105
106 /** Prepare the edited part */
107 protected void prepare(EditablePart part, Object caretPosition) {
108 }
109
110 /** Notified when the editing state changed. Does nothing, to be overridden */
111 protected void editingStateChanged(CmsEditable cmsEditable) {
112 }
113
114 @Override
115 public Control getControl() {
116 return null;
117 }
118
119 @Override
120 public void refresh() {
121 try {
122 // if (cmsEditable.canEdit() && !readOnly)
123 // mouseListener = createMouseListener();
124 // else
125 // mouseListener = null;
126 refresh(getControl());
127 layout(getControl());
128 } catch (RepositoryException e) {
129 throw new CmsException("Cannot refresh", e);
130 }
131 }
132
133 @Override
134 public void setSelection(ISelection selection, boolean reveal) {
135 this.selection = selection;
136 }
137
138 protected void updateContent(EditablePart part) throws RepositoryException {
139 }
140
141 // LOW LEVEL EDITION
142 protected void edit(EditablePart part, Object caretPosition) {
143 try {
144 if (edited == part)
145 return;
146
147 if (edited != null && edited != part)
148 stopEditing(true);
149
150 part.startEditing();
151 updateContent(part);
152 prepare(part, caretPosition);
153 edited = part;
154 layout(part.getControl());
155 } catch (RepositoryException e) {
156 throw new CmsException("Cannot edit " + part, e);
157 }
158 }
159
160 private void stopEditing(Boolean save) throws RepositoryException {
161 if (edited instanceof Widget && ((Widget) edited).isDisposed()) {
162 edited = null;
163 return;
164 }
165
166 assert edited != null;
167 if (edited == null) {
168 if (log.isTraceEnabled())
169 log.warn("Told to stop editing while not editing anything");
170 return;
171 }
172
173 if (save)
174 save(edited);
175
176 edited.stopEditing();
177 updateContent(edited);
178 layout(((EditablePart) edited).getControl());
179 edited = null;
180 }
181
182 // METHODS AVAILABLE TO EXTENDING CLASSES
183 protected void saveEdit() {
184 try {
185 if (edited != null)
186 stopEditing(true);
187 } catch (RepositoryException e) {
188 throw new CmsException("Cannot stop editing", e);
189 }
190 }
191
192 protected void cancelEdit() {
193 try {
194 if (edited != null)
195 stopEditing(false);
196 } catch (RepositoryException e) {
197
198 throw new CmsException("Cannot cancel editing", e);
199 }
200 }
201
202 /** Layout this controls from the related base page. */
203 public void layout(Control... controls) {
204 page.layout(controls);
205 }
206
207 // UTILITIES
208 /** Check whether the edited part is in a proper state */
209 protected void checkEdited() {
210 if (edited == null || (edited instanceof Widget)
211 && ((Widget) edited).isDisposed())
212 throw new CmsException(
213 "Edited should not be null or disposed at this stage");
214 }
215
216 // GETTERS / SETTERS
217 protected EditablePart getEdited() {
218 return edited;
219 }
220
221 public MouseListener getMouseListener() {
222 return mouseListener;
223 }
224
225 public CmsEditable getCmsEditable() {
226 return cmsEditable;
227 }
228
229 @Override
230 public ISelection getSelection() {
231 return selection;
232 }
233 }