]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms/src/org/argeo/cms/viewers/AbstractPageViewer.java
Use char array for password
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / viewers / AbstractPageViewer.java
1 package org.argeo.cms.viewers;
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.cms.CmsEditable;
12 import org.argeo.cms.CmsException;
13 import org.argeo.cms.widgets.JcrComposite;
14 import org.argeo.cms.widgets.ScrolledPage;
15 import org.eclipse.jface.viewers.ContentViewer;
16 import org.eclipse.jface.viewers.ISelection;
17 import org.eclipse.jface.viewers.StructuredSelection;
18 import org.eclipse.swt.SWT;
19 import org.eclipse.swt.events.MouseAdapter;
20 import org.eclipse.swt.events.MouseListener;
21 import org.eclipse.swt.widgets.Composite;
22 import org.eclipse.swt.widgets.Control;
23 import org.eclipse.swt.widgets.Widget;
24
25 /** Base class for viewers related to a page */
26 public abstract class AbstractPageViewer extends ContentViewer implements
27 Observer {
28 private static final long serialVersionUID = 5438688173410341485L;
29
30 private final static Log log = LogFactory.getLog(AbstractPageViewer.class);
31
32 private final boolean readOnly;
33 /** The basis for the layouts, typically a ScrolledPage. */
34 private final Composite page;
35 private final CmsEditable cmsEditable;
36
37 private MouseListener mouseListener;
38
39 private EditablePart edited;
40 private ISelection selection = StructuredSelection.EMPTY;
41
42 // FIXME Added by BSinou to manage non-section Composite.
43 // Is it the correct method?
44 protected AbstractPageViewer(Composite parent, int style,
45 CmsEditable cmsEditable) {
46 // read only at UI level
47 readOnly = SWT.READ_ONLY == (style & SWT.READ_ONLY);
48
49 this.cmsEditable = cmsEditable == null ? CmsEditable.NON_EDITABLE
50 : cmsEditable;
51 if (this.cmsEditable instanceof Observable)
52 ((Observable) this.cmsEditable).addObserver(this);
53
54 if (cmsEditable.canEdit()) {
55 mouseListener = createMouseListener();
56 }
57 page = findPage(parent);
58 }
59
60 protected AbstractPageViewer(Section parent, int style,
61 CmsEditable cmsEditable) {
62 // read only at UI level
63 readOnly = SWT.READ_ONLY == (style & SWT.READ_ONLY);
64
65 this.cmsEditable = cmsEditable == null ? CmsEditable.NON_EDITABLE
66 : cmsEditable;
67 if (this.cmsEditable instanceof Observable)
68 ((Observable) this.cmsEditable).addObserver(this);
69
70 if (cmsEditable.canEdit()) {
71 mouseListener = createMouseListener();
72 }
73 page = findPage(parent);
74 }
75
76 /**
77 * Can be called to simplify the called to isModelInitialized() and
78 * initModel()
79 */
80 protected void initModelIfNeeded(Node node) {
81 try {
82 if (!isModelInitialized(node))
83 if (getCmsEditable().canEdit()) {
84 initModel(node);
85 node.getSession().save();
86 }
87 } catch (Exception e) {
88 throw new CmsException("Cannot initialize model", e);
89 }
90 }
91
92 /** Called if user can edit and model is not initialized */
93 protected Boolean isModelInitialized(Node node) throws RepositoryException {
94 return true;
95 }
96
97 /** Called if user can edit and model is not initialized */
98 protected void initModel(Node node) throws RepositoryException {
99 }
100
101 /** Create (retrieve) the MouseListener to use. */
102 protected MouseListener createMouseListener() {
103 return new MouseAdapter() {
104 private static final long serialVersionUID = 1L;
105 };
106 }
107
108 protected Composite findPage(Composite composite) {
109 if (composite instanceof ScrolledPage) {
110 return (ScrolledPage) composite;
111 } else {
112 if (composite.getParent() == null)
113 return composite;
114 return findPage(composite.getParent());
115 }
116 }
117
118 @Override
119 public void update(Observable o, Object arg) {
120 if (o == cmsEditable)
121 editingStateChanged(cmsEditable);
122 }
123
124 /** To be overridden in order to provide the actual refresh */
125 protected void refresh(Control control) throws RepositoryException {
126 }
127
128 /** To be overridden.Save the edited part. */
129 protected void save(EditablePart part) throws RepositoryException {
130 }
131
132 /** Prepare the edited part */
133 protected void prepare(EditablePart part, Object caretPosition) {
134 }
135
136 /** Notified when the editing state changed. Does nothing, to be overridden */
137 protected void editingStateChanged(CmsEditable cmsEditable) {
138 }
139
140 @Override
141 public void refresh() {
142 try {
143 if (cmsEditable.canEdit() && !readOnly)
144 mouseListener = createMouseListener();
145 else
146 mouseListener = null;
147 refresh(getControl());
148 layout(getControl());
149 } catch (RepositoryException e) {
150 throw new CmsException("Cannot refresh", e);
151 }
152 }
153
154 @Override
155 public void setSelection(ISelection selection, boolean reveal) {
156 this.selection = selection;
157 }
158
159 protected void updateContent(EditablePart part) throws RepositoryException {
160 }
161
162 // LOW LEVEL EDITION
163 protected void edit(EditablePart part, Object caretPosition) {
164 try {
165 if (edited == part)
166 return;
167
168 if (edited != null && edited != part)
169 stopEditing(true);
170
171 part.startEditing();
172 updateContent(part);
173 prepare(part, caretPosition);
174 edited = part;
175 layout(part.getControl());
176 } catch (RepositoryException e) {
177 throw new CmsException("Cannot edit " + part, e);
178 }
179 }
180
181 private void stopEditing(Boolean save) throws RepositoryException {
182 if (edited instanceof Widget && ((Widget) edited).isDisposed()) {
183 edited = null;
184 return;
185 }
186
187 assert edited != null;
188 if (edited == null) {
189 if (log.isTraceEnabled())
190 log.warn("Told to stop editing while not editing anything");
191 return;
192 }
193
194 if (save)
195 save(edited);
196
197 edited.stopEditing();
198 updateContent(edited);
199 layout(((EditablePart) edited).getControl());
200 edited = null;
201 }
202
203 // METHODS AVAILABLE TO EXTENDING CLASSES
204 protected void saveEdit() {
205 try {
206 if (edited != null)
207 stopEditing(true);
208 } catch (RepositoryException e) {
209 throw new CmsException("Cannot stop editing", e);
210 }
211 }
212
213 protected void cancelEdit() {
214 try {
215 if (edited != null)
216 stopEditing(false);
217 } catch (RepositoryException e) {
218 throw new CmsException("Cannot cancel editing", e);
219 }
220 }
221
222 /** Layout this controls from the related base page. */
223 public void layout(Control... controls) {
224 page.layout(controls);
225 }
226
227 // UTILITIES
228 /** Check whether the edited part is in a proper state */
229 protected void checkEdited() {
230 if (edited == null || (edited instanceof Widget)
231 && ((Widget) edited).isDisposed())
232 throw new CmsException(
233 "Edited should not be null or disposed at this stage");
234 }
235
236 // GETTERS / SETTERS
237 public boolean isReadOnly() {
238 return readOnly;
239 }
240
241 protected EditablePart getEdited() {
242 return edited;
243 }
244
245 public MouseListener getMouseListener() {
246 return mouseListener;
247 }
248
249 public CmsEditable getCmsEditable() {
250 return cmsEditable;
251 }
252
253 @Override
254 public ISelection getSelection() {
255 return selection;
256 }
257
258 }