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
.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
;
25 /** Base class for viewers related to a page */
26 public abstract class AbstractPageViewer
extends ContentViewer
implements
28 private static final long serialVersionUID
= 5438688173410341485L;
30 private final static Log log
= LogFactory
.getLog(AbstractPageViewer
.class);
32 private final boolean readOnly
;
33 /** The basis for the layouts, typically a ScrolledPage. */
34 private final Composite page
;
35 private final CmsEditable cmsEditable
;
37 private MouseListener mouseListener
;
39 private EditablePart edited
;
40 private ISelection selection
= StructuredSelection
.EMPTY
;
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
);
49 this.cmsEditable
= cmsEditable
== null ? CmsEditable
.NON_EDITABLE
51 if (this.cmsEditable
instanceof Observable
)
52 ((Observable
) this.cmsEditable
).addObserver(this);
54 if (cmsEditable
.canEdit()) {
55 mouseListener
= createMouseListener();
57 page
= findPage(parent
);
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
);
65 this.cmsEditable
= cmsEditable
== null ? CmsEditable
.NON_EDITABLE
67 if (this.cmsEditable
instanceof Observable
)
68 ((Observable
) this.cmsEditable
).addObserver(this);
70 if (cmsEditable
.canEdit()) {
71 mouseListener
= createMouseListener();
73 page
= findPage(parent
);
77 * Can be called to simplify the called to isModelInitialized() and
80 protected void initModelIfNeeded(Node node
) {
82 if (!isModelInitialized(node
))
83 if (getCmsEditable().canEdit()) {
85 node
.getSession().save();
87 } catch (Exception e
) {
88 throw new CmsException("Cannot initialize model", e
);
92 /** Called if user can edit and model is not initialized */
93 protected Boolean
isModelInitialized(Node node
) throws RepositoryException
{
97 /** Called if user can edit and model is not initialized */
98 protected void initModel(Node node
) throws RepositoryException
{
101 /** Create (retrieve) the MouseListener to use. */
102 protected MouseListener
createMouseListener() {
103 return new MouseAdapter() {
104 private static final long serialVersionUID
= 1L;
108 protected Composite
findPage(Composite composite
) {
109 if (composite
instanceof ScrolledPage
) {
110 return (ScrolledPage
) composite
;
112 if (composite
.getParent() == null)
114 return findPage(composite
.getParent());
119 public void update(Observable o
, Object arg
) {
120 if (o
== cmsEditable
)
121 editingStateChanged(cmsEditable
);
124 /** To be overridden in order to provide the actual refresh */
125 protected void refresh(Control control
) throws RepositoryException
{
128 /** To be overridden.Save the edited part. */
129 protected void save(EditablePart part
) throws RepositoryException
{
132 /** Prepare the edited part */
133 protected void prepare(EditablePart part
, Object caretPosition
) {
136 /** Notified when the editing state changed. Does nothing, to be overridden */
137 protected void editingStateChanged(CmsEditable cmsEditable
) {
141 public void refresh() {
143 if (cmsEditable
.canEdit() && !readOnly
)
144 mouseListener
= createMouseListener();
146 mouseListener
= null;
147 refresh(getControl());
148 layout(getControl());
149 } catch (RepositoryException e
) {
150 throw new CmsException("Cannot refresh", e
);
155 public void setSelection(ISelection selection
, boolean reveal
) {
156 this.selection
= selection
;
159 protected void updateContent(EditablePart part
) throws RepositoryException
{
163 protected void edit(EditablePart part
, Object caretPosition
) {
168 if (edited
!= null && edited
!= part
)
173 prepare(part
, caretPosition
);
175 layout(part
.getControl());
176 } catch (RepositoryException e
) {
177 throw new CmsException("Cannot edit " + part
, e
);
181 private void stopEditing(Boolean save
) throws RepositoryException
{
182 if (edited
instanceof Widget
&& ((Widget
) edited
).isDisposed()) {
187 assert edited
!= null;
188 if (edited
== null) {
189 if (log
.isTraceEnabled())
190 log
.warn("Told to stop editing while not editing anything");
197 edited
.stopEditing();
198 updateContent(edited
);
199 layout(((EditablePart
) edited
).getControl());
203 // METHODS AVAILABLE TO EXTENDING CLASSES
204 protected void saveEdit() {
208 } catch (RepositoryException e
) {
209 throw new CmsException("Cannot stop editing", e
);
213 protected void cancelEdit() {
217 } catch (RepositoryException e
) {
218 throw new CmsException("Cannot cancel editing", e
);
222 /** Layout this controls from the related base page. */
223 public void layout(Control
... controls
) {
224 page
.layout(controls
);
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");
237 public boolean isReadOnly() {
241 protected EditablePart
getEdited() {
245 public MouseListener
getMouseListener() {
246 return mouseListener
;
249 public CmsEditable
getCmsEditable() {
254 public ISelection
getSelection() {