KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > forms > editor > FormPage


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.forms.editor;
12 import org.eclipse.core.runtime.IProgressMonitor;
13 import org.eclipse.swt.custom.BusyIndicator;
14 import org.eclipse.swt.graphics.Image;
15 import org.eclipse.swt.widgets.*;
16 import org.eclipse.ui.*;
17 import org.eclipse.ui.forms.*;
18 import org.eclipse.ui.forms.widgets.ScrolledForm;
19 import org.eclipse.ui.part.EditorPart;
20 /**
21  * A base class that all pages that should be added to FormEditor must subclass.
22  * Form page has an instance of PageForm that extends managed form. Subclasses
23  * should override method 'createFormContent(ManagedForm)' to fill the form with
24  * content. Note that page itself can be loaded lazily (on first open).
25  * Consequently, the call to create the form content can come after the editor
26  * has been opened for a while (in fact, it is possible to open and close the
27  * editor and never create the form because no attempt has been made to show the
28  * page).
29  *
30  * @since 3.0
31  */

32 public class FormPage extends EditorPart implements IFormPage {
33     private FormEditor editor;
34     private PageForm mform;
35     private int index;
36     private String JavaDoc id;
37     
38     private static class PageForm extends ManagedForm {
39         public PageForm(FormPage page, ScrolledForm form) {
40             super(page.getEditor().getToolkit(), form);
41             setContainer(page);
42         }
43         
44         public FormPage getPage() {
45             return (FormPage)getContainer();
46         }
47         public void dirtyStateChanged() {
48             getPage().getEditor().editorDirtyStateChanged();
49         }
50         public void staleStateChanged() {
51             if (getPage().isActive())
52                 refresh();
53         }
54     }
55     /**
56      * A constructor that creates the page and initializes it with the editor.
57      *
58      * @param editor
59      * the parent editor
60      * @param id
61      * the unique identifier
62      * @param title
63      * the page title
64      */

65     public FormPage(FormEditor editor, String JavaDoc id, String JavaDoc title) {
66         this(id, title);
67         initialize(editor);
68     }
69     /**
70      * The constructor. The parent editor need to be passed in the
71      * <code>initialize</code> method if this constructor is used.
72      *
73      * @param id
74      * a unique page identifier
75      * @param title
76      * a user-friendly page title
77      */

78     public FormPage(String JavaDoc id, String JavaDoc title) {
79         this.id = id;
80         setPartName(title);
81     }
82     /**
83      * Initializes the form page.
84      *
85      * @see IEditorPart#init
86      */

87     public void init(IEditorSite site, IEditorInput input) {
88         setSite(site);
89         setInput(input);
90     }
91     /**
92      * Primes the form page with the parent editor instance.
93      *
94      * @param editor
95      * the parent editor
96      */

97     public void initialize(FormEditor editor) {
98         this.editor = editor;
99     }
100     /**
101      * Returns the parent editor.
102      *
103      * @return parent editor instance
104      */

105     public FormEditor getEditor() {
106         return editor;
107     }
108     /**
109      * Returns the managed form owned by this page.
110      *
111      * @return the managed form
112      */

113     public IManagedForm getManagedForm() {
114         return mform;
115     }
116     /**
117      * Implements the required method by refreshing the form when set active.
118      * Subclasses must call super when overriding this method.
119      */

120     public void setActive(boolean active) {
121         if (active) {
122             // We are switching to this page - refresh it
123
// if needed.
124
mform.refresh();
125         }
126     }
127     /**
128      * Tests if the page is active by asking the parent editor if this page is
129      * the currently active page.
130      *
131      * @return <code>true</code> if the page is currently active,
132      * <code>false</code> otherwise.
133      */

134     public boolean isActive() {
135         return this.equals(editor.getActivePageInstance());
136     }
137     /**
138      * Creates the part control by creating the managed form using the parent
139      * editor's toolkit. Subclasses should override
140      * <code>createFormContent(IManagedForm)</code> to populate the form with
141      * content.
142      *
143      * @param parent
144      * the page parent composite
145      */

146     public void createPartControl(Composite parent) {
147         ScrolledForm form = editor.getToolkit().createScrolledForm(parent);
148         mform = new PageForm(this, form);
149         BusyIndicator.showWhile(parent.getDisplay(), new Runnable JavaDoc() {
150             public void run() {
151                 createFormContent(mform);
152             }
153         });
154     }
155     /**
156      * Subclasses should override this method to create content in the form
157      * hosted in this page.
158      *
159      * @param managedForm
160      * the form hosted in this page.
161      */

162     protected void createFormContent(IManagedForm managedForm) {
163     }
164     /**
165      * Returns the form page control.
166      *
167      * @return managed form's control
168      */

169     public Control getPartControl() {
170         return mform != null ? mform.getForm() : null;
171     }
172     /**
173      * Disposes the managed form.
174      */

175     public void dispose() {
176         if (mform != null)
177             mform.dispose();
178     }
179     /**
180      * Returns the unique identifier that can be used to reference this page.
181      *
182      * @return the unique page identifier
183      */

184     public String JavaDoc getId() {
185         return id;
186     }
187     /**
188      * Returns <code>null</code>- form page has no title image. Subclasses
189      * may override.
190      *
191      * @return <code>null</code>
192      */

193     public Image getTitleImage() {
194         return null;
195     }
196     /**
197      * Sets the focus by delegating to the managed form.
198      */

199     public void setFocus() {
200         if (mform != null)
201             mform.setFocus();
202     }
203     /**
204      * @see org.eclipse.ui.ISaveablePart#doSave(org.eclipse.core.runtime.IProgressMonitor)
205      */

206     public void doSave(IProgressMonitor monitor) {
207         if (mform != null)
208             mform.commit(true);
209     }
210     /**
211      * @see org.eclipse.ui.ISaveablePart#doSaveAs()
212      */

213     public void doSaveAs() {
214     }
215     /**
216      * @see org.eclipse.ui.ISaveablePart#isSaveAsAllowed()
217      */

218     public boolean isSaveAsAllowed() {
219         return false;
220     }
221     /**
222      * Implemented by testing if the managed form is dirty.
223      *
224      * @return <code>true</code> if the managed form is dirty,
225      * <code>false</code> otherwise.
226      *
227      * @see org.eclipse.ui.ISaveablePart#isDirty()
228      */

229     public boolean isDirty() {
230         return mform != null ? mform.isDirty() : false;
231     }
232     /**
233      * Preserves the page index.
234      *
235      * @param index
236      * the assigned page index
237      */

238     public void setIndex(int index) {
239         this.index = index;
240     }
241     /**
242      * Returns the saved page index.
243      *
244      * @return the page index
245      */

246     public int getIndex() {
247         return index;
248     }
249     /**
250      * Form pages are not editors.
251      *
252      * @return <code>false</code>
253      */

254     public boolean isEditor() {
255         return false;
256     }
257     /**
258      * Attempts to select and reveal the given object by passing the request to
259      * the managed form.
260      *
261      * @param object
262      * the object to select and reveal in the page if possible.
263      * @return <code>true</code> if the page has been successfully selected
264      * and revealed by one of the managed form parts, <code>false</code>
265      * otherwise.
266      */

267     public boolean selectReveal(Object JavaDoc object) {
268         if (mform != null)
269             return mform.setInput(object);
270         return false;
271     }
272     /**
273      * By default, editor will be allowed to flip the page.
274      * @return <code>true</code>
275      */

276     public boolean canLeaveThePage() {
277         return true;
278     }
279 }
280
Popular Tags