KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > forms > ManagedForm


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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;
12
13 import java.util.Vector JavaDoc;
14 import org.eclipse.jface.viewers.ISelection;
15 import org.eclipse.swt.widgets.Composite;
16 import org.eclipse.ui.forms.widgets.*;
17 import org.eclipse.ui.internal.forms.MessageManager;
18
19 /**
20  * Managed form wraps a form widget and adds life cycle methods for form parts.
21  * A form part is a portion of the form that participates in form life cycle
22  * events.
23  * <p>
24  * There is requirement for 1/1 mapping between widgets and form parts. A widget
25  * like Section can be a part by itself, but a number of widgets can join around
26  * one form part.
27  * <p>
28  * Note to developers: this class is left public to allow its use beyond the
29  * original intention (inside a multi-page editor's page). You should limit the
30  * use of this class to make new instances inside a form container (wizard page,
31  * dialog etc.). Clients that need access to the class should not do it
32  * directly. Instead, they should do it through IManagedForm interface as much
33  * as possible.
34  *
35  * @since 3.0
36  */

37 public class ManagedForm implements IManagedForm {
38     private Object JavaDoc input;
39
40     private ScrolledForm form;
41
42     private FormToolkit toolkit;
43
44     private Object JavaDoc container;
45
46     private boolean ownsToolkit;
47
48     private boolean initialized;
49
50     private MessageManager messageManager;
51
52     private Vector JavaDoc parts = new Vector JavaDoc();
53
54     /**
55      * Creates a managed form in the provided parent. Form toolkit and widget
56      * will be created and owned by this object.
57      *
58      * @param parent
59      * the parent widget
60      */

61     public ManagedForm(Composite parent) {
62         toolkit = new FormToolkit(parent.getDisplay());
63         ownsToolkit = true;
64         form = toolkit.createScrolledForm(parent);
65     }
66
67     /**
68      * Creates a managed form that will use the provided toolkit and
69      *
70      * @param toolkit
71      * @param form
72      */

73     public ManagedForm(FormToolkit toolkit, ScrolledForm form) {
74         this.form = form;
75         this.toolkit = toolkit;
76     }
77
78     /*
79      * (non-Javadoc)
80      *
81      * @see org.eclipse.ui.forms.IManagedForm#addPart(org.eclipse.ui.forms.IFormPart)
82      */

83     public void addPart(IFormPart part) {
84         parts.add(part);
85         part.initialize(this);
86     }
87
88     /*
89      * (non-Javadoc)
90      *
91      * @see org.eclipse.ui.forms.IManagedForm#removePart(org.eclipse.ui.forms.IFormPart)
92      */

93     public void removePart(IFormPart part) {
94         parts.remove(part);
95     }
96
97     /*
98      * (non-Javadoc)
99      *
100      * @see org.eclipse.ui.forms.IManagedForm#getParts()
101      */

102     public IFormPart[] getParts() {
103         return (IFormPart[]) parts.toArray(new IFormPart[parts.size()]);
104     }
105
106     /*
107      * (non-Javadoc)
108      *
109      * @see org.eclipse.ui.forms.IManagedForm#getToolkit()
110      */

111     public FormToolkit getToolkit() {
112         return toolkit;
113     }
114
115     /*
116      * (non-Javadoc)
117      *
118      * @see org.eclipse.ui.forms.IManagedForm#getForm()
119      */

120     public ScrolledForm getForm() {
121         return form;
122     }
123
124     /*
125      * (non-Javadoc)
126      *
127      * @see org.eclipse.ui.forms.IManagedForm#reflow(boolean)
128      */

129     public void reflow(boolean changed) {
130         form.reflow(changed);
131     }
132
133     /**
134      * A part can use this method to notify other parts that implement
135      * IPartSelectionListener about selection changes.
136      *
137      * @param part
138      * the part that broadcasts the selection
139      * @param selection
140      * the selection in the part
141      * @see IPartSelectionListener
142      */

143     public void fireSelectionChanged(IFormPart part, ISelection selection) {
144         for (int i = 0; i < parts.size(); i++) {
145             IFormPart cpart = (IFormPart) parts.get(i);
146             if (part.equals(cpart))
147                 continue;
148             if (cpart instanceof IPartSelectionListener) {
149                 ((IPartSelectionListener) cpart).selectionChanged(part,
150                         selection);
151             }
152         }
153     }
154
155     /**
156      * Initializes the form by looping through the managed parts and
157      * initializing them. Has no effect if already called once.
158      */

159     public void initialize() {
160         if (initialized)
161             return;
162         for (int i = 0; i < parts.size(); i++) {
163             IFormPart part = (IFormPart) parts.get(i);
164             part.initialize(this);
165         }
166         initialized = true;
167     }
168
169     /**
170      * Disposes all the parts in this form.
171      */

172     public void dispose() {
173         for (int i = 0; i < parts.size(); i++) {
174             IFormPart part = (IFormPart) parts.get(i);
175             part.dispose();
176         }
177         if (ownsToolkit) {
178             toolkit.dispose();
179         }
180     }
181
182     /**
183      * Refreshes the form by refreshes all the stale parts. Since 3.1, this
184      * method is performed on a UI thread when called from another thread so it
185      * is not needed to wrap the call in <code>Display.syncExec</code> or
186      * <code>asyncExec</code>.
187      */

188     public void refresh() {
189         Thread JavaDoc t = Thread.currentThread();
190         Thread JavaDoc dt = toolkit.getColors().getDisplay().getThread();
191         if (t.equals(dt))
192             doRefresh();
193         else {
194             toolkit.getColors().getDisplay().asyncExec(new Runnable JavaDoc() {
195                 public void run() {
196                     doRefresh();
197                 }
198             });
199         }
200     }
201
202     private void doRefresh() {
203         int nrefreshed = 0;
204         for (int i = 0; i < parts.size(); i++) {
205             IFormPart part = (IFormPart) parts.get(i);
206             if (part.isStale()) {
207                 part.refresh();
208                 nrefreshed++;
209             }
210         }
211         if (nrefreshed > 0)
212             form.reflow(true);
213     }
214
215     /*
216      * (non-Javadoc)
217      *
218      * @see org.eclipse.ui.forms.IManagedForm#commit(boolean)
219      */

220     public void commit(boolean onSave) {
221         for (int i = 0; i < parts.size(); i++) {
222             IFormPart part = (IFormPart) parts.get(i);
223             if (part.isDirty())
224                 part.commit(onSave);
225         }
226     }
227
228     /*
229      * (non-Javadoc)
230      *
231      * @see org.eclipse.ui.forms.IManagedForm#setInput(java.lang.Object)
232      */

233     public boolean setInput(Object JavaDoc input) {
234         boolean pageResult = false;
235
236         this.input = input;
237         for (int i = 0; i < parts.size(); i++) {
238             IFormPart part = (IFormPart) parts.get(i);
239             boolean result = part.setFormInput(input);
240             if (result)
241                 pageResult = true;
242         }
243         return pageResult;
244     }
245
246     /*
247      * (non-Javadoc)
248      *
249      * @see org.eclipse.ui.forms.IManagedForm#getInput()
250      */

251     public Object JavaDoc getInput() {
252         return input;
253     }
254
255     /**
256      * Transfers the focus to the first form part.
257      */

258     public void setFocus() {
259         if (parts.size() > 0) {
260             IFormPart part = (IFormPart) parts.get(0);
261             part.setFocus();
262         }
263     }
264
265     /*
266      * (non-Javadoc)
267      *
268      * @see org.eclipse.ui.forms.IManagedForm#isDirty()
269      */

270     public boolean isDirty() {
271         for (int i = 0; i < parts.size(); i++) {
272             IFormPart part = (IFormPart) parts.get(i);
273             if (part.isDirty())
274                 return true;
275         }
276         return false;
277     }
278
279     /*
280      * (non-Javadoc)
281      *
282      * @see org.eclipse.ui.forms.IManagedForm#isStale()
283      */

284     public boolean isStale() {
285         for (int i = 0; i < parts.size(); i++) {
286             IFormPart part = (IFormPart) parts.get(i);
287             if (part.isStale())
288                 return true;
289         }
290         return false;
291     }
292
293     /*
294      * (non-Javadoc)
295      *
296      * @see org.eclipse.ui.forms.IManagedForm#dirtyStateChanged()
297      */

298     public void dirtyStateChanged() {
299     }
300
301     /*
302      * (non-Javadoc)
303      *
304      * @see org.eclipse.ui.forms.IManagedForm#staleStateChanged()
305      */

306     public void staleStateChanged() {
307     }
308
309     /*
310      * (non-Javadoc)
311      *
312      * @see org.eclipse.ui.forms.IManagedForm#getContainer()
313      */

314     public Object JavaDoc getContainer() {
315         return container;
316     }
317
318     /*
319      * (non-Javadoc)
320      *
321      * @see org.eclipse.ui.forms.IManagedForm#setContainer(java.lang.Object)
322      */

323     public void setContainer(Object JavaDoc container) {
324         this.container = container;
325     }
326
327     /* (non-Javadoc)
328      * @see org.eclipse.ui.forms.IManagedForm#getMessageManager()
329      */

330     public IMessageManager getMessageManager() {
331         if (messageManager == null)
332             messageManager = new MessageManager(form);
333         return messageManager;
334     }
335 }
336
Popular Tags