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 protected AbstractPageViewer(Section parent
, int style
,
42 CmsEditable cmsEditable
) {
43 // read only at UI level
44 readOnly
= SWT
.READ_ONLY
== (style
& SWT
.READ_ONLY
);
46 this.cmsEditable
= cmsEditable
== null ? CmsEditable
.NON_EDITABLE
48 if (this.cmsEditable
instanceof Observable
)
49 ((Observable
) this.cmsEditable
).addObserver(this);
51 if (cmsEditable
.canEdit()) {
52 mouseListener
= createMouseListener();
54 page
= findPage(parent
);
58 * Can be called to simplify the called to isModelInitialized() and
61 protected void initModelIfNeeded(Node node
) {
63 if (!isModelInitialized(node
))
64 if (getCmsEditable().canEdit()) {
66 node
.getSession().save();
68 } catch (Exception e
) {
69 throw new CmsException("Cannot initialize model", e
);
73 /** Called if user can edit and model is not initialized */
74 protected Boolean
isModelInitialized(Node node
) throws RepositoryException
{
78 /** Called if user can edit and model is not initialized */
79 protected void initModel(Node node
) throws RepositoryException
{
82 /** Create (retrieve) the MouseListener to use. */
83 protected MouseListener
createMouseListener() {
84 return new MouseAdapter() {
85 private static final long serialVersionUID
= 1L;
89 protected Composite
findPage(Composite composite
) {
90 if (composite
instanceof ScrolledPage
) {
91 return (ScrolledPage
) composite
;
93 if (composite
.getParent() == null)
95 return findPage(composite
.getParent());
100 public void update(Observable o
, Object arg
) {
101 if (o
== cmsEditable
)
102 editingStateChanged(cmsEditable
);
105 /** To be overridden in order to provide the actual refresh */
106 protected void refresh(Control control
) throws RepositoryException
{
109 /** To be overridden.Save the edited part. */
110 protected void save(EditablePart part
) throws RepositoryException
{
113 /** Prepare the edited part */
114 protected void prepare(EditablePart part
, Object caretPosition
) {
117 /** Notified when the editing state changed. Does nothing, to be overridden */
118 protected void editingStateChanged(CmsEditable cmsEditable
) {
122 public void refresh() {
124 if (cmsEditable
.canEdit() && !readOnly
)
125 mouseListener
= createMouseListener();
127 mouseListener
= null;
128 refresh(getControl());
129 layout(getControl());
130 } catch (RepositoryException e
) {
131 throw new CmsException("Cannot refresh", e
);
136 public void setSelection(ISelection selection
, boolean reveal
) {
137 this.selection
= selection
;
140 protected void updateContent(EditablePart part
) throws RepositoryException
{
144 protected void edit(EditablePart part
, Object caretPosition
) {
149 if (edited
!= null && edited
!= part
)
154 prepare(part
, caretPosition
);
156 layout(part
.getControl());
157 } catch (RepositoryException e
) {
158 throw new CmsException("Cannot edit " + part
, e
);
162 private void stopEditing(Boolean save
) throws RepositoryException
{
163 if (edited
instanceof Widget
&& ((Widget
) edited
).isDisposed()) {
168 assert edited
!= null;
169 if (edited
== null) {
170 if (log
.isTraceEnabled())
171 log
.warn("Told to stop editing while not editing anything");
178 edited
.stopEditing();
179 updateContent(edited
);
180 layout(((EditablePart
) edited
).getControl());
184 // METHODS AVAILABLE TO EXTENDING CLASSES
185 protected void saveEdit() {
189 } catch (RepositoryException e
) {
190 throw new CmsException("Cannot stop editing", e
);
194 protected void cancelEdit() {
198 } catch (RepositoryException e
) {
199 throw new CmsException("Cannot cancel editing", e
);
203 /** Layout this controls from the related base page. */
204 public void layout(Control
... controls
) {
205 page
.layout(controls
);
209 /** Check whether the edited part is in a proper state */
210 protected void checkEdited() {
211 if (edited
== null || (edited
instanceof Widget
)
212 && ((Widget
) edited
).isDisposed())
213 throw new CmsException(
214 "Edited should not be null or disposed at this stage");
218 public boolean isReadOnly() {
222 protected EditablePart
getEdited() {
226 public MouseListener
getMouseListener() {
227 return mouseListener
;
230 public CmsEditable
getCmsEditable() {
235 public ISelection
getSelection() {