1 package org
.argeo
.cms
.viewers
;
3 import java
.util
.Observable
;
4 import java
.util
.Observer
;
7 import javax
.jcr
.RepositoryException
;
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
.ScrolledPage
;
14 import org
.eclipse
.jface
.viewers
.ContentViewer
;
15 import org
.eclipse
.jface
.viewers
.ISelection
;
16 import org
.eclipse
.jface
.viewers
.StructuredSelection
;
17 import org
.eclipse
.swt
.SWT
;
18 import org
.eclipse
.swt
.events
.MouseAdapter
;
19 import org
.eclipse
.swt
.events
.MouseListener
;
20 import org
.eclipse
.swt
.widgets
.Composite
;
21 import org
.eclipse
.swt
.widgets
.Control
;
22 import org
.eclipse
.swt
.widgets
.Widget
;
24 /** Base class for viewers related to a page */
25 public abstract class AbstractPageViewer
extends ContentViewer
implements
27 private static final long serialVersionUID
= 5438688173410341485L;
29 private final static Log log
= LogFactory
.getLog(AbstractPageViewer
.class);
31 private final boolean readOnly
;
32 /** The basis for the layouts, typically a ScrolledPage. */
33 private final Composite page
;
34 private final CmsEditable cmsEditable
;
36 private MouseListener mouseListener
;
38 private EditablePart edited
;
39 private ISelection selection
= StructuredSelection
.EMPTY
;
41 // FIXME Added by BSinou to manage non-section Composite.
42 // Is it the correct method?
43 protected AbstractPageViewer(Composite 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
);
59 protected AbstractPageViewer(Section parent
, int style
,
60 CmsEditable cmsEditable
) {
61 // read only at UI level
62 readOnly
= SWT
.READ_ONLY
== (style
& SWT
.READ_ONLY
);
64 this.cmsEditable
= cmsEditable
== null ? CmsEditable
.NON_EDITABLE
66 if (this.cmsEditable
instanceof Observable
)
67 ((Observable
) this.cmsEditable
).addObserver(this);
69 if (cmsEditable
.canEdit()) {
70 mouseListener
= createMouseListener();
72 page
= findPage(parent
);
76 * Can be called to simplify the called to isModelInitialized() and
79 protected void initModelIfNeeded(Node node
) {
81 if (!isModelInitialized(node
))
82 if (getCmsEditable().canEdit()) {
84 node
.getSession().save();
86 } catch (Exception e
) {
87 throw new CmsException("Cannot initialize model", e
);
91 /** Called if user can edit and model is not initialized */
92 protected Boolean
isModelInitialized(Node node
) throws RepositoryException
{
96 /** Called if user can edit and model is not initialized */
97 protected void initModel(Node node
) throws RepositoryException
{
100 /** Create (retrieve) the MouseListener to use. */
101 protected MouseListener
createMouseListener() {
102 return new MouseAdapter() {
103 private static final long serialVersionUID
= 1L;
107 protected Composite
findPage(Composite composite
) {
108 if (composite
instanceof ScrolledPage
) {
109 return (ScrolledPage
) composite
;
111 if (composite
.getParent() == null)
113 return findPage(composite
.getParent());
118 public void update(Observable o
, Object arg
) {
119 if (o
== cmsEditable
)
120 editingStateChanged(cmsEditable
);
123 /** To be overridden in order to provide the actual refresh */
124 protected void refresh(Control control
) throws RepositoryException
{
127 /** To be overridden.Save the edited part. */
128 protected void save(EditablePart part
) throws RepositoryException
{
131 /** Prepare the edited part */
132 protected void prepare(EditablePart part
, Object caretPosition
) {
135 /** Notified when the editing state changed. Does nothing, to be overridden */
136 protected void editingStateChanged(CmsEditable cmsEditable
) {
140 public void refresh() {
142 if (cmsEditable
.canEdit() && !readOnly
)
143 mouseListener
= createMouseListener();
145 mouseListener
= null;
146 refresh(getControl());
147 layout(getControl());
148 } catch (RepositoryException e
) {
149 throw new CmsException("Cannot refresh", e
);
154 public void setSelection(ISelection selection
, boolean reveal
) {
155 this.selection
= selection
;
158 protected void updateContent(EditablePart part
) throws RepositoryException
{
162 protected void edit(EditablePart part
, Object caretPosition
) {
167 if (edited
!= null && edited
!= part
)
172 prepare(part
, caretPosition
);
174 layout(part
.getControl());
175 } catch (RepositoryException e
) {
176 throw new CmsException("Cannot edit " + part
, e
);
180 private void stopEditing(Boolean save
) throws RepositoryException
{
181 if (edited
instanceof Widget
&& ((Widget
) edited
).isDisposed()) {
186 assert edited
!= null;
187 if (edited
== null) {
188 if (log
.isTraceEnabled())
189 log
.warn("Told to stop editing while not editing anything");
196 edited
.stopEditing();
197 updateContent(edited
);
198 layout(((EditablePart
) edited
).getControl());
202 // METHODS AVAILABLE TO EXTENDING CLASSES
203 protected void saveEdit() {
207 } catch (RepositoryException e
) {
208 throw new CmsException("Cannot stop editing", e
);
212 protected void cancelEdit() {
216 } catch (RepositoryException e
) {
217 throw new CmsException("Cannot cancel editing", e
);
221 /** Layout this controls from the related base page. */
222 public void layout(Control
... controls
) {
223 page
.layout(controls
);
227 /** Check whether the edited part is in a proper state */
228 protected void checkEdited() {
229 if (edited
== null || (edited
instanceof Widget
)
230 && ((Widget
) edited
).isDisposed())
231 throw new CmsException(
232 "Edited should not be null or disposed at this stage");
236 public boolean isReadOnly() {
240 protected EditablePart
getEdited() {
244 public MouseListener
getMouseListener() {
245 return mouseListener
;
248 public CmsEditable
getCmsEditable() {
253 public ISelection
getSelection() {