]> git.argeo.org Git - lgpl/argeo-commons.git/blob - jcr/org.argeo.cms.ui/src/org/argeo/cms/ui/eclipse/forms/editor/FormPage.java
Adapt to new third parties
[lgpl/argeo-commons.git] / jcr / org.argeo.cms.ui / src / org / argeo / cms / ui / eclipse / forms / editor / FormPage.java
1 package org.argeo.cms.ui.eclipse.forms.editor;
2 import org.argeo.cms.ui.eclipse.forms.IManagedForm;
3 import org.argeo.cms.ui.eclipse.forms.ManagedForm;
4 import org.eclipse.swt.custom.BusyIndicator;
5 import org.eclipse.swt.custom.ScrolledComposite;
6 import org.eclipse.swt.graphics.Image;
7 import org.eclipse.swt.widgets.Composite;
8 import org.eclipse.swt.widgets.Control;
9 /**
10 * A base class that all pages that should be added to FormEditor must subclass.
11 * Form page has an instance of PageForm that extends managed form. Subclasses
12 * should override method 'createFormContent(ManagedForm)' to fill the form with
13 * content. Note that page itself can be loaded lazily (on first open).
14 * Consequently, the call to create the form content can come after the editor
15 * has been opened for a while (in fact, it is possible to open and close the
16 * editor and never create the form because no attempt has been made to show the
17 * page).
18 *
19 * @since 1.0
20 */
21 public class FormPage implements IFormPage {
22 private FormEditor editor;
23 private PageForm mform;
24 private int index;
25 private String id;
26
27 private String partName;
28
29
30
31 public void setPartName(String partName) {
32 this.partName = partName;
33 }
34 private static class PageForm extends ManagedForm {
35 public PageForm(FormPage page, ScrolledComposite form) {
36 super(page.getEditor().getToolkit(), form);
37 setContainer(page);
38 }
39
40 public FormPage getPage() {
41 return (FormPage)getContainer();
42 }
43 public void dirtyStateChanged() {
44 getPage().getEditor().editorDirtyStateChanged();
45 }
46 public void staleStateChanged() {
47 if (getPage().isActive())
48 refresh();
49 }
50 }
51 /**
52 * A constructor that creates the page and initializes it with the editor.
53 *
54 * @param editor
55 * the parent editor
56 * @param id
57 * the unique identifier
58 * @param title
59 * the page title
60 */
61 public FormPage(FormEditor editor, String id, String title) {
62 this(id, title);
63 initialize(editor);
64 }
65 /**
66 * The constructor. The parent editor need to be passed in the
67 * <code>initialize</code> method if this constructor is used.
68 *
69 * @param id
70 * a unique page identifier
71 * @param title
72 * a user-friendly page title
73 */
74 public FormPage(String id, String title) {
75 this.id = id;
76 setPartName(title);
77 }
78 /**
79 * Initializes the form page.
80 *
81 * @see IEditorPart#init
82 */
83 // public void init(IEditorSite site, IEditorInput input) {
84 // setSite(site);
85 // setInput(input);
86 // }
87 /**
88 * Primes the form page with the parent editor instance.
89 *
90 * @param editor
91 * the parent editor
92 */
93 public void initialize(FormEditor editor) {
94 this.editor = editor;
95 }
96 /**
97 * Returns the parent editor.
98 *
99 * @return parent editor instance
100 */
101 public FormEditor getEditor() {
102 return editor;
103 }
104 /**
105 * Returns the managed form owned by this page.
106 *
107 * @return the managed form
108 */
109 public IManagedForm getManagedForm() {
110 return mform;
111 }
112 /**
113 * Implements the required method by refreshing the form when set active.
114 * Subclasses must call super when overriding this method.
115 */
116 public void setActive(boolean active) {
117 if (active) {
118 // We are switching to this page - refresh it
119 // if needed.
120 if (mform != null)
121 mform.refresh();
122 }
123 }
124 /**
125 * Tests if the page is active by asking the parent editor if this page is
126 * the currently active page.
127 *
128 * @return <code>true</code> if the page is currently active,
129 * <code>false</code> otherwise.
130 */
131 public boolean isActive() {
132 return this.equals(editor.getActivePageInstance());
133 }
134 /**
135 * Creates the part control by creating the managed form using the parent
136 * editor's toolkit. Subclasses should override
137 * <code>createFormContent(IManagedForm)</code> to populate the form with
138 * content.
139 *
140 * @param parent
141 * the page parent composite
142 */
143 public void createPartControl(Composite parent) {
144 ScrolledComposite form = editor.getToolkit().createScrolledForm(parent);
145 mform = new PageForm(this, form);
146 BusyIndicator.showWhile(parent.getDisplay(), new Runnable() {
147 public void run() {
148 createFormContent(mform);
149 }
150 });
151 }
152 /**
153 * Subclasses should override this method to create content in the form
154 * hosted in this page.
155 *
156 * @param managedForm
157 * the form hosted in this page.
158 */
159 protected void createFormContent(IManagedForm managedForm) {
160 }
161 /**
162 * Returns the form page control.
163 *
164 * @return managed form's control
165 */
166 public Control getPartControl() {
167 return mform != null ? mform.getForm() : null;
168 }
169 /**
170 * Disposes the managed form.
171 */
172 public void dispose() {
173 if (mform != null)
174 mform.dispose();
175 }
176 /**
177 * Returns the unique identifier that can be used to reference this page.
178 *
179 * @return the unique page identifier
180 */
181 public String getId() {
182 return id;
183 }
184 /**
185 * Returns <code>null</code>- form page has no title image. Subclasses
186 * may override.
187 *
188 * @return <code>null</code>
189 */
190 public Image getTitleImage() {
191 return null;
192 }
193 /**
194 * Sets the focus by delegating to the managed form.
195 */
196 public void setFocus() {
197 if (mform != null)
198 mform.setFocus();
199 }
200 /**
201 * @see org.eclipse.ui.ISaveablePart#doSave(org.eclipse.core.runtime.IProgressMonitor)
202 */
203 // public void doSave(IProgressMonitor monitor) {
204 // if (mform != null)
205 // mform.commit(true);
206 // }
207 /**
208 * @see org.eclipse.ui.ISaveablePart#doSaveAs()
209 */
210 public void doSaveAs() {
211 }
212 /**
213 * @see org.eclipse.ui.ISaveablePart#isSaveAsAllowed()
214 */
215 public boolean isSaveAsAllowed() {
216 return false;
217 }
218 /**
219 * Implemented by testing if the managed form is dirty.
220 *
221 * @return <code>true</code> if the managed form is dirty,
222 * <code>false</code> otherwise.
223 *
224 * @see org.eclipse.ui.ISaveablePart#isDirty()
225 */
226 public boolean isDirty() {
227 return mform != null ? mform.isDirty() : false;
228 }
229 /**
230 * Preserves the page index.
231 *
232 * @param index
233 * the assigned page index
234 */
235 public void setIndex(int index) {
236 this.index = index;
237 }
238 /**
239 * Returns the saved page index.
240 *
241 * @return the page index
242 */
243 public int getIndex() {
244 return index;
245 }
246 /**
247 * Form pages are not editors.
248 *
249 * @return <code>false</code>
250 */
251 public boolean isEditor() {
252 return false;
253 }
254 /**
255 * Attempts to select and reveal the given object by passing the request to
256 * the managed form.
257 *
258 * @param object
259 * the object to select and reveal in the page if possible.
260 * @return <code>true</code> if the page has been successfully selected
261 * and revealed by one of the managed form parts, <code>false</code>
262 * otherwise.
263 */
264 public boolean selectReveal(Object object) {
265 if (mform != null)
266 return mform.setInput(object);
267 return false;
268 }
269 /**
270 * By default, editor will be allowed to flip the page.
271 * @return <code>true</code>
272 */
273 public boolean canLeaveThePage() {
274 return true;
275 }
276 }