1 package org
.argeo
.cms
.viewers
;
3 import java
.util
.Observable
;
4 import java
.util
.Observer
;
7 import javax
.jcr
.RepositoryException
;
8 import javax
.jcr
.Session
;
10 import org
.apache
.commons
.logging
.Log
;
11 import org
.apache
.commons
.logging
.LogFactory
;
12 import org
.argeo
.cms
.CmsEditable
;
13 import org
.argeo
.cms
.CmsException
;
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 import org
.xml
.sax
.SAXParseException
;
26 /** Base class for viewers related to a page */
27 public abstract class AbstractPageViewer
extends ContentViewer
implements
29 private static final long serialVersionUID
= 5438688173410341485L;
31 private final static Log log
= LogFactory
.getLog(AbstractPageViewer
.class);
33 private final boolean readOnly
;
34 /** The basis for the layouts, typically a ScrolledPage. */
35 private final Composite page
;
36 private final CmsEditable cmsEditable
;
38 private MouseListener mouseListener
;
40 private EditablePart edited
;
41 private ISelection selection
= StructuredSelection
.EMPTY
;
43 protected AbstractPageViewer(Section parent
, int style
,
44 CmsEditable cmsEditable
) {
45 // read only at UI level
46 readOnly
= SWT
.READ_ONLY
== (style
& SWT
.READ_ONLY
);
48 this.cmsEditable
= cmsEditable
== null ? CmsEditable
.NON_EDITABLE
50 if (this.cmsEditable
instanceof Observable
)
51 ((Observable
) this.cmsEditable
).addObserver(this);
53 if (cmsEditable
.canEdit()) {
54 mouseListener
= createMouseListener();
56 page
= findPage(parent
);
60 * Can be called to simplify the called to isModelInitialized() and
63 protected void initModelIfNeeded(Node node
) {
65 if (!isModelInitialized(node
))
66 if (getCmsEditable().canEdit()) {
68 node
.getSession().save();
70 } catch (Exception e
) {
71 throw new CmsException("Cannot initialize model", e
);
75 /** Called if user can edit and model is not initialized */
76 protected Boolean
isModelInitialized(Node node
) throws RepositoryException
{
80 /** Called if user can edit and model is not initialized */
81 protected void initModel(Node node
) throws RepositoryException
{
84 /** Create (retrieve) the MouseListener to use. */
85 protected MouseListener
createMouseListener() {
86 return new MouseAdapter() {
87 private static final long serialVersionUID
= 1L;
91 protected Composite
findPage(Composite composite
) {
92 if (composite
instanceof ScrolledPage
) {
93 return (ScrolledPage
) composite
;
95 if (composite
.getParent() == null)
97 return findPage(composite
.getParent());
102 public void update(Observable o
, Object arg
) {
103 if (o
== cmsEditable
)
104 editingStateChanged(cmsEditable
);
107 /** To be overridden in order to provide the actual refresh */
108 protected void refresh(Control control
) throws RepositoryException
{
111 /** To be overridden.Save the edited part. */
112 protected void save(EditablePart part
) throws RepositoryException
{
115 /** Prepare the edited part */
116 protected void prepare(EditablePart part
, Object caretPosition
) {
119 /** Notified when the editing state changed. Does nothing, to be overridden */
120 protected void editingStateChanged(CmsEditable cmsEditable
) {
124 public void refresh() {
126 if (cmsEditable
.canEdit() && !readOnly
)
127 mouseListener
= createMouseListener();
129 mouseListener
= null;
130 refresh(getControl());
131 layout(getControl());
132 } catch (RepositoryException e
) {
133 throw new CmsException("Cannot refresh", e
);
138 public void setSelection(ISelection selection
, boolean reveal
) {
139 this.selection
= selection
;
142 protected void updateContent(EditablePart part
) throws RepositoryException
{
146 protected void edit(EditablePart part
, Object caretPosition
) {
151 if (edited
!= null && edited
!= part
) {
152 EditablePart previouslyEdited
= edited
;
155 } catch (Exception e
) {
156 notifyEditionException(e
);
157 edit(previouslyEdited
, caretPosition
);
164 prepare(part
, caretPosition
);
166 layout(part
.getControl());
167 } catch (RepositoryException e
) {
168 throw new CmsException("Cannot edit " + part
, e
);
172 private void stopEditing(Boolean save
) throws RepositoryException
{
173 if (edited
instanceof Widget
&& ((Widget
) edited
).isDisposed()) {
178 assert edited
!= null;
179 if (edited
== null) {
180 if (log
.isTraceEnabled())
181 log
.warn("Told to stop editing while not editing anything");
188 edited
.stopEditing();
189 updateContent(edited
);
190 layout(((EditablePart
) edited
).getControl());
194 // METHODS AVAILABLE TO EXTENDING CLASSES
195 protected void saveEdit() {
199 } catch (RepositoryException e
) {
200 throw new CmsException("Cannot stop editing", e
);
204 protected void cancelEdit() {
208 } catch (RepositoryException e
) {
209 throw new CmsException("Cannot cancel editing", e
);
213 /** Layout this controls from the related base page. */
214 public void layout(Control
... controls
) {
215 page
.layout(controls
);
219 * Find the first {@link EditablePart} in the parents hierarchy of this
222 protected EditablePart
findDataParent(Control parent
) {
223 if (parent
instanceof EditablePart
) {
224 return (EditablePart
) parent
;
226 if (parent
.getParent() != null)
227 return findDataParent(parent
.getParent());
229 throw new CmsException("No data parent found");
233 /** Check whether the edited part is in a proper state */
234 protected void checkEdited() {
235 if (edited
== null || (edited
instanceof Widget
)
236 && ((Widget
) edited
).isDisposed())
237 throw new CmsException(
238 "Edited should not be null or disposed at this stage");
241 /** Persist all changes. */
242 protected void persistChanges(Session session
) throws RepositoryException
{
244 session
.refresh(false);
245 // TODO notify that changes have been persisted
248 /** Convenience method using a Node in order to save the underlying session. */
249 protected void persistChanges(Node anyNode
) throws RepositoryException
{
250 persistChanges(anyNode
.getSession());
253 /** Notify edition exception */
254 protected void notifyEditionException(Throwable e
) {
255 Throwable eToLog
= e
;
256 if (e
instanceof IllegalArgumentException
)
257 if (e
.getCause() instanceof SAXParseException
)
258 eToLog
= e
.getCause();
259 log
.error(eToLog
.getMessage());
260 if (log
.isTraceEnabled())
261 log
.trace("Full stack of " + eToLog
.getMessage(), e
);
262 // TODO Light error notification popup
266 public boolean isReadOnly() {
270 protected EditablePart
getEdited() {
274 public MouseListener
getMouseListener() {
275 return mouseListener
;
278 public CmsEditable
getCmsEditable() {
283 public ISelection
getSelection() {