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