1 package org
.argeo
.cms
.swt
.acr
;
3 import java
.security
.PrivilegedAction
;
5 import javax
.security
.auth
.Subject
;
7 import org
.argeo
.api
.cms
.CmsLog
;
8 import org
.argeo
.api
.cms
.ux
.CmsEditable
;
9 import org
.argeo
.cms
.CurrentUser
;
10 import org
.argeo
.cms
.swt
.SwtEditablePart
;
11 import org
.argeo
.cms
.swt
.widgets
.ScrolledPage
;
12 import org
.eclipse
.swt
.SWT
;
13 import org
.eclipse
.swt
.events
.FocusEvent
;
14 import org
.eclipse
.swt
.events
.FocusListener
;
15 import org
.eclipse
.swt
.events
.MouseAdapter
;
16 import org
.eclipse
.swt
.events
.MouseListener
;
17 import org
.eclipse
.swt
.widgets
.Composite
;
18 import org
.eclipse
.swt
.widgets
.Control
;
19 import org
.eclipse
.swt
.widgets
.Widget
;
20 import org
.xml
.sax
.SAXParseException
;
22 /** Base class for viewers related to a page */
23 public abstract class AbstractPageViewer
{
25 private final static CmsLog log
= CmsLog
.getLog(AbstractPageViewer
.class);
27 private final boolean readOnly
;
28 /** The basis for the layouts, typically a ScrolledPage. */
29 private final Composite page
;
30 private final CmsEditable cmsEditable
;
32 private MouseListener mouseListener
;
33 private FocusListener focusListener
;
35 private SwtEditablePart edited
;
36 // private ISelection selection = StructuredSelection.EMPTY;
38 private Subject viewerSubject
;
40 protected AbstractPageViewer(Composite parent
, int style
, CmsEditable cmsEditable
) {
41 // read only at UI level
42 readOnly
= SWT
.READ_ONLY
== (style
& SWT
.READ_ONLY
);
44 this.cmsEditable
= cmsEditable
== null ? CmsEditable
.NON_EDITABLE
: cmsEditable
;
45 // if (this.cmsEditable instanceof Observable)
46 // ((Observable) this.cmsEditable).addObserver(this);
48 if (cmsEditable
.canEdit()) {
49 mouseListener
= createMouseListener();
50 focusListener
= createFocusListener();
52 page
= findPage(parent
);
53 // accessControlContext = AccessController.getContext();
54 viewerSubject
= CurrentUser
.getCmsSession().getSubject();
57 public abstract Control
getControl();
60 // * Can be called to simplify the called to isModelInitialized() and initModel()
62 // protected void initModelIfNeeded(Node node) {
64 // if (!isModelInitialized(node))
65 // if (getCmsEditable().canEdit()) {
67 // node.getSession().save();
69 // } catch (RepositoryException e) {
70 // throw new JcrException("Cannot initialize model", e);
74 // /** Called if user can edit and model is not initialized */
75 // protected Boolean isModelInitialized(Node node) throws RepositoryException {
79 // /** Called if user can edit and model is not initialized */
80 // protected void initModel(Node node) throws RepositoryException {
83 /** Create (retrieve) the MouseListener to use. */
84 protected MouseListener
createMouseListener() {
85 return new MouseAdapter() {
86 private static final long serialVersionUID
= 1L;
90 /** Create (retrieve) the FocusListener to use. */
91 protected FocusListener
createFocusListener() {
92 return new FocusListener() {
93 private static final long serialVersionUID
= 1L;
96 public void focusLost(FocusEvent event
) {
100 public void focusGained(FocusEvent event
) {
105 protected Composite
findPage(Composite composite
) {
106 if (composite
instanceof ScrolledPage
) {
107 return (ScrolledPage
) composite
;
109 if (composite
.getParent() == null)
111 return findPage(composite
.getParent());
115 public void layoutPage() {
117 page
.layout(true, true);
120 protected void showControl(Control control
) {
121 if (page
!= null && (page
instanceof ScrolledPage
))
122 ((ScrolledPage
) page
).showControl(control
);
126 // public void update(Observable o, Object arg) {
127 // if (o == cmsEditable)
128 // editingStateChanged(cmsEditable);
131 /** To be overridden in order to provide the actual refresh */
132 protected void refresh(Control control
) {
135 /** To be overridden.Save the edited part. */
136 protected void save(SwtEditablePart part
) {
139 /** Prepare the edited part */
140 protected void prepare(SwtEditablePart part
, Object caretPosition
) {
143 /** Notified when the editing state changed. Does nothing, to be overridden */
144 protected void editingStateChanged(CmsEditable cmsEditable
) {
147 public void refresh() {
148 // TODO check actual context in order to notice a discrepancy
149 Subject viewerSubject
= getViewerSubject();
150 Subject
.doAs(viewerSubject
, (PrivilegedAction
<Void
>) () -> {
151 if (cmsEditable
.canEdit() && !readOnly
)
152 mouseListener
= createMouseListener();
154 mouseListener
= null;
155 refresh(getControl());
156 // layout(getControl());
157 if (!getControl().isDisposed())
164 // public void setSelection(ISelection selection, boolean reveal) {
165 // this.selection = selection;
168 protected void updateContent(SwtEditablePart part
) {
172 protected void edit(SwtEditablePart part
, Object caretPosition
) {
176 if (edited
!= null && edited
!= part
) {
177 SwtEditablePart previouslyEdited
= edited
;
180 } catch (Exception e
) {
181 notifyEditionException(e
);
182 edit(previouslyEdited
, caretPosition
);
190 prepare(part
, caretPosition
);
191 edited
.getControl().addFocusListener(new FocusListener() {
192 private static final long serialVersionUID
= 6883521812717097017L;
195 public void focusLost(FocusEvent event
) {
200 public void focusGained(FocusEvent event
) {
204 layout(part
.getControl());
205 showControl(part
.getControl());
208 protected void stopEditing(Boolean save
) {
209 if (edited
instanceof Widget
&& ((Widget
) edited
).isDisposed()) {
214 assert edited
!= null;
215 if (edited
== null) {
216 if (log
.isTraceEnabled())
217 log
.warn("Told to stop editing while not editing anything");
225 edited
.stopEditing();
226 SwtEditablePart editablePart
= edited
;
227 Control control
= ((SwtEditablePart
) edited
).getControl();
229 // TODO make edited state management more robust
230 updateContent(editablePart
);
237 // METHODS AVAILABLE TO EXTENDING CLASSES
238 protected void saveEdit() {
243 protected void cancelEdit() {
248 /** Layout this controls from the related base page. */
249 public void layout(Control
... controls
) {
250 page
.layout(controls
);
254 * Find the first {@link SwtEditablePart} in the parents hierarchy of this
257 protected SwtEditablePart
findDataParent(Control parent
) {
258 if (parent
instanceof SwtEditablePart
) {
259 return (SwtEditablePart
) parent
;
261 if (parent
.getParent() != null)
262 return findDataParent(parent
.getParent());
264 throw new IllegalStateException("No data parent found");
268 /** Check whether the edited part is in a proper state */
269 protected void checkEdited() {
270 if (edited
== null || (edited
instanceof Widget
) && ((Widget
) edited
).isDisposed())
271 throw new IllegalStateException("Edited should not be null or disposed at this stage");
274 // /** Persist all changes. */
275 // protected void persistChanges(ContentSession session) {
277 //// session.refresh(false);
278 // // TODO notify that changes have been persisted
281 // /** Convenience method using a Node in order to save the underlying session. */
282 // protected void persistChanges(Content anyNode) {
283 // persistChanges(((ProvidedContent) anyNode).getSession());
286 /** Notify edition exception */
287 protected void notifyEditionException(Throwable e
) {
288 Throwable eToLog
= e
;
289 if (e
instanceof IllegalArgumentException
)
290 if (e
.getCause() instanceof SAXParseException
)
291 eToLog
= e
.getCause();
292 log
.error(eToLog
.getMessage(), eToLog
);
293 // if (log.isTraceEnabled())
294 // log.trace("Full stack of " + eToLog.getMessage(), e);
295 // TODO Light error notification popup
298 protected Subject
getViewerSubject() {
299 return viewerSubject
;
300 // Subject res = null;
301 // if (accessControlContext != null) {
302 // res = Subject.getSubject(accessControlContext);
305 // throw new IllegalStateException("No subject associated with this viewer");
310 public boolean isReadOnly() {
314 protected SwtEditablePart
getEdited() {
318 public MouseListener
getMouseListener() {
319 return mouseListener
;
322 public FocusListener
getFocusListener() {
323 return focusListener
;
326 public CmsEditable
getCmsEditable() {
331 // public ISelection getSelection() {