1 package org
.argeo
.cms
.ui
.viewers
;
3 import java
.security
.AccessControlContext
;
4 import java
.security
.AccessController
;
5 import java
.security
.PrivilegedAction
;
6 import java
.util
.Observable
;
7 import java
.util
.Observer
;
10 import javax
.jcr
.RepositoryException
;
11 import javax
.jcr
.Session
;
12 import javax
.security
.auth
.Subject
;
14 import org
.apache
.commons
.logging
.Log
;
15 import org
.apache
.commons
.logging
.LogFactory
;
16 import org
.argeo
.cms
.ui
.CmsEditable
;
17 import org
.argeo
.cms
.ui
.widgets
.ScrolledPage
;
18 import org
.argeo
.jcr
.JcrException
;
19 import org
.eclipse
.jface
.viewers
.ContentViewer
;
20 import org
.eclipse
.jface
.viewers
.ISelection
;
21 import org
.eclipse
.jface
.viewers
.StructuredSelection
;
22 import org
.eclipse
.swt
.SWT
;
23 import org
.eclipse
.swt
.events
.FocusEvent
;
24 import org
.eclipse
.swt
.events
.FocusListener
;
25 import org
.eclipse
.swt
.events
.MouseAdapter
;
26 import org
.eclipse
.swt
.events
.MouseListener
;
27 import org
.eclipse
.swt
.widgets
.Composite
;
28 import org
.eclipse
.swt
.widgets
.Control
;
29 import org
.eclipse
.swt
.widgets
.Widget
;
30 import org
.xml
.sax
.SAXParseException
;
32 /** Base class for viewers related to a page */
33 public abstract class AbstractPageViewer
extends ContentViewer
implements Observer
{
34 private static final long serialVersionUID
= 5438688173410341485L;
36 private final static Log log
= LogFactory
.getLog(AbstractPageViewer
.class);
38 private final boolean readOnly
;
39 /** The basis for the layouts, typically a ScrolledPage. */
40 private final Composite page
;
41 private final CmsEditable cmsEditable
;
43 private MouseListener mouseListener
;
44 private FocusListener focusListener
;
46 private EditablePart edited
;
47 private ISelection selection
= StructuredSelection
.EMPTY
;
49 private AccessControlContext accessControlContext
;
51 protected AbstractPageViewer(Section parent
, int style
, CmsEditable cmsEditable
) {
52 // read only at UI level
53 readOnly
= SWT
.READ_ONLY
== (style
& SWT
.READ_ONLY
);
55 this.cmsEditable
= cmsEditable
== null ? CmsEditable
.NON_EDITABLE
: cmsEditable
;
56 if (this.cmsEditable
instanceof Observable
)
57 ((Observable
) this.cmsEditable
).addObserver(this);
59 if (cmsEditable
.canEdit()) {
60 mouseListener
= createMouseListener();
61 focusListener
= createFocusListener();
63 page
= findPage(parent
);
64 accessControlContext
= AccessController
.getContext();
68 * Can be called to simplify the called to isModelInitialized() and initModel()
70 protected void initModelIfNeeded(Node node
) {
72 if (!isModelInitialized(node
))
73 if (getCmsEditable().canEdit()) {
75 node
.getSession().save();
77 } catch (RepositoryException e
) {
78 throw new JcrException("Cannot initialize model", e
);
82 /** Called if user can edit and model is not initialized */
83 protected Boolean
isModelInitialized(Node node
) throws RepositoryException
{
87 /** Called if user can edit and model is not initialized */
88 protected void initModel(Node node
) throws RepositoryException
{
91 /** Create (retrieve) the MouseListener to use. */
92 protected MouseListener
createMouseListener() {
93 return new MouseAdapter() {
94 private static final long serialVersionUID
= 1L;
98 /** Create (retrieve) the FocusListener to use. */
99 protected FocusListener
createFocusListener() {
100 return new FocusListener() {
101 private static final long serialVersionUID
= 1L;
104 public void focusLost(FocusEvent event
) {
108 public void focusGained(FocusEvent event
) {
113 protected Composite
findPage(Composite composite
) {
114 if (composite
instanceof ScrolledPage
) {
115 return (ScrolledPage
) composite
;
117 if (composite
.getParent() == null)
119 return findPage(composite
.getParent());
123 public void layoutPage() {
125 page
.layout(true, true);
128 protected void showControl(Control control
) {
129 if (page
!= null && (page
instanceof ScrolledPage
))
130 ((ScrolledPage
) page
).showControl(control
);
134 public void update(Observable o
, Object arg
) {
135 if (o
== cmsEditable
)
136 editingStateChanged(cmsEditable
);
139 /** To be overridden in order to provide the actual refresh */
140 protected void refresh(Control control
) throws RepositoryException
{
143 /** To be overridden.Save the edited part. */
144 protected void save(EditablePart part
) throws RepositoryException
{
147 /** Prepare the edited part */
148 protected void prepare(EditablePart part
, Object caretPosition
) {
151 /** Notified when the editing state changed. Does nothing, to be overridden */
152 protected void editingStateChanged(CmsEditable cmsEditable
) {
156 public void refresh() {
157 // TODO check actual context in order to notice a discrepancy
158 Subject viewerSubject
= getViewerSubject();
159 Subject
.doAs(viewerSubject
, (PrivilegedAction
<Void
>) () -> {
161 if (cmsEditable
.canEdit() && !readOnly
)
162 mouseListener
= createMouseListener();
164 mouseListener
= null;
165 refresh(getControl());
166 // layout(getControl());
168 } catch (RepositoryException e
) {
169 throw new JcrException("Cannot refresh", e
);
176 public void setSelection(ISelection selection
, boolean reveal
) {
177 this.selection
= selection
;
180 protected void updateContent(EditablePart part
) throws RepositoryException
{
184 protected void edit(EditablePart part
, Object caretPosition
) {
189 if (edited
!= null && edited
!= part
) {
190 EditablePart previouslyEdited
= edited
;
193 } catch (Exception e
) {
194 notifyEditionException(e
);
195 edit(previouslyEdited
, caretPosition
);
203 prepare(part
, caretPosition
);
204 edited
.getControl().addFocusListener(new FocusListener() {
205 private static final long serialVersionUID
= 6883521812717097017L;
208 public void focusLost(FocusEvent event
) {
213 public void focusGained(FocusEvent event
) {
217 layout(part
.getControl());
218 showControl(part
.getControl());
219 } catch (RepositoryException e
) {
220 throw new JcrException("Cannot edit " + part
, e
);
224 protected void stopEditing(Boolean save
) {
225 if (edited
instanceof Widget
&& ((Widget
) edited
).isDisposed()) {
230 assert edited
!= null;
231 if (edited
== null) {
232 if (log
.isTraceEnabled())
233 log
.warn("Told to stop editing while not editing anything");
241 edited
.stopEditing();
242 EditablePart editablePart
= edited
;
243 Control control
= ((EditablePart
) edited
).getControl();
245 // TODO make edited state management more robust
246 updateContent(editablePart
);
248 } catch (RepositoryException e
) {
249 throw new JcrException("Cannot stop editing", e
);
255 // METHODS AVAILABLE TO EXTENDING CLASSES
256 protected void saveEdit() {
261 protected void cancelEdit() {
266 /** Layout this controls from the related base page. */
267 public void layout(Control
... controls
) {
268 page
.layout(controls
);
272 * Find the first {@link EditablePart} in the parents hierarchy of this control
274 protected EditablePart
findDataParent(Control parent
) {
275 if (parent
instanceof EditablePart
) {
276 return (EditablePart
) parent
;
278 if (parent
.getParent() != null)
279 return findDataParent(parent
.getParent());
281 throw new IllegalStateException("No data parent found");
285 /** Check whether the edited part is in a proper state */
286 protected void checkEdited() {
287 if (edited
== null || (edited
instanceof Widget
) && ((Widget
) edited
).isDisposed())
288 throw new IllegalStateException("Edited should not be null or disposed at this stage");
291 /** Persist all changes. */
292 protected void persistChanges(Session session
) throws RepositoryException
{
294 session
.refresh(false);
295 // TODO notify that changes have been persisted
298 /** Convenience method using a Node in order to save the underlying session. */
299 protected void persistChanges(Node anyNode
) throws RepositoryException
{
300 persistChanges(anyNode
.getSession());
303 /** Notify edition exception */
304 protected void notifyEditionException(Throwable e
) {
305 Throwable eToLog
= e
;
306 if (e
instanceof IllegalArgumentException
)
307 if (e
.getCause() instanceof SAXParseException
)
308 eToLog
= e
.getCause();
309 log
.error(eToLog
.getMessage(), eToLog
);
310 // if (log.isTraceEnabled())
311 // log.trace("Full stack of " + eToLog.getMessage(), e);
312 // TODO Light error notification popup
315 protected Subject
getViewerSubject() {
317 if (accessControlContext
!= null) {
318 res
= Subject
.getSubject(accessControlContext
);
321 throw new IllegalStateException("No subject associated with this viewer");
326 public boolean isReadOnly() {
330 protected EditablePart
getEdited() {
334 public MouseListener
getMouseListener() {
335 return mouseListener
;
338 public FocusListener
getFocusListener() {
339 return focusListener
;
342 public CmsEditable
getCmsEditable() {
347 public ISelection
getSelection() {