]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms.ui/src/org/argeo/cms/ui/eclipse/forms/ManagedForm.java
19e8211e6220e4beab1666c7d38682dd10fb7d38
[lgpl/argeo-commons.git] / org.argeo.cms.ui / src / org / argeo / cms / ui / eclipse / forms / ManagedForm.java
1 package org.argeo.cms.ui.eclipse.forms;
2
3 import java.util.Vector;
4 import org.eclipse.jface.viewers.ISelection;
5 import org.eclipse.swt.custom.ScrolledComposite;
6 import org.eclipse.swt.widgets.Composite;
7 //import org.eclipse.ui.forms.widgets.FormToolkit;
8 //import org.eclipse.ui.forms.widgets.ScrolledForm;
9
10 /**
11 * Managed form wraps a form widget and adds life cycle methods for form parts.
12 * A form part is a portion of the form that participates in form life cycle
13 * events.
14 * <p>
15 * There is requirement for 1/1 mapping between widgets and form parts. A widget
16 * like Section can be a part by itself, but a number of widgets can join around
17 * one form part.
18 * <p>
19 * Note to developers: this class is left public to allow its use beyond the
20 * original intention (inside a multi-page editor's page). You should limit the
21 * use of this class to make new instances inside a form container (wizard page,
22 * dialog etc.). Clients that need access to the class should not do it
23 * directly. Instead, they should do it through IManagedForm interface as much
24 * as possible.
25 *
26 * @since 1.0
27 */
28 public class ManagedForm implements IManagedForm {
29 private Object input;
30
31 private ScrolledComposite form;
32
33 private FormToolkit toolkit;
34
35 private Object container;
36
37 private boolean ownsToolkit;
38
39 private boolean initialized;
40
41 private Vector parts = new Vector();
42
43 /**
44 * Creates a managed form in the provided parent. Form toolkit and widget
45 * will be created and owned by this object.
46 *
47 * @param parent
48 * the parent widget
49 */
50 public ManagedForm(Composite parent) {
51 toolkit = new FormToolkit(parent.getDisplay());
52 ownsToolkit = true;
53 form = toolkit.createScrolledForm(parent);
54 }
55
56 /**
57 * Creates a managed form that will use the provided toolkit and
58 *
59 * @param toolkit
60 * @param form
61 */
62 public ManagedForm(FormToolkit toolkit, ScrolledComposite form) {
63 this.form = form;
64 this.toolkit = toolkit;
65 }
66
67 /*
68 * (non-Javadoc)
69 *
70 * @see org.eclipse.ui.forms.IManagedForm#addPart(org.eclipse.ui.forms.IFormPart)
71 */
72 public void addPart(IFormPart part) {
73 parts.add(part);
74 part.initialize(this);
75 }
76
77 /*
78 * (non-Javadoc)
79 *
80 * @see org.eclipse.ui.forms.IManagedForm#removePart(org.eclipse.ui.forms.IFormPart)
81 */
82 public void removePart(IFormPart part) {
83 parts.remove(part);
84 }
85
86 /*
87 * (non-Javadoc)
88 *
89 * @see org.eclipse.ui.forms.IManagedForm#getParts()
90 */
91 public IFormPart[] getParts() {
92 return (IFormPart[]) parts.toArray(new IFormPart[parts.size()]);
93 }
94
95 /*
96 * (non-Javadoc)
97 *
98 * @see org.eclipse.ui.forms.IManagedForm#getToolkit()
99 */
100 public FormToolkit getToolkit() {
101 return toolkit;
102 }
103
104 /*
105 * (non-Javadoc)
106 *
107 * @see org.eclipse.ui.forms.IManagedForm#getForm()
108 */
109 public ScrolledComposite getForm() {
110 return form;
111 }
112
113 /*
114 * (non-Javadoc)
115 *
116 * @see org.eclipse.ui.forms.IManagedForm#reflow(boolean)
117 */
118 public void reflow(boolean changed) {
119 // form.reflow(changed);
120 }
121
122 /**
123 * A part can use this method to notify other parts that implement
124 * IPartSelectionListener about selection changes.
125 *
126 * @param part
127 * the part that broadcasts the selection
128 * @param selection
129 * the selection in the part
130 * @see IPartSelectionListener
131 */
132 public void fireSelectionChanged(IFormPart part, ISelection selection) {
133 for (int i = 0; i < parts.size(); i++) {
134 IFormPart cpart = (IFormPart) parts.get(i);
135 if (part.equals(cpart))
136 continue;
137 // if (cpart instanceof IPartSelectionListener) {
138 // ((IPartSelectionListener) cpart).selectionChanged(part,
139 // selection);
140 // }
141 }
142 }
143
144 /**
145 * Initializes the form by looping through the managed parts and
146 * initializing them. Has no effect if already called once.
147 */
148 public void initialize() {
149 if (initialized)
150 return;
151 for (int i = 0; i < parts.size(); i++) {
152 IFormPart part = (IFormPart) parts.get(i);
153 part.initialize(this);
154 }
155 initialized = true;
156 }
157
158 /**
159 * Disposes all the parts in this form.
160 */
161 public void dispose() {
162 for (int i = 0; i < parts.size(); i++) {
163 IFormPart part = (IFormPart) parts.get(i);
164 part.dispose();
165 }
166 if (ownsToolkit) {
167 toolkit.dispose();
168 }
169 }
170
171 /**
172 * Refreshes the form by refreshes all the stale parts. Since 3.1, this
173 * method is performed on a UI thread when called from another thread so it
174 * is not needed to wrap the call in <code>Display.syncExec</code> or
175 * <code>asyncExec</code>.
176 */
177 public void refresh() {
178 Thread t = Thread.currentThread();
179 Thread dt = toolkit.getColors().getDisplay().getThread();
180 if (t.equals(dt))
181 doRefresh();
182 else {
183 toolkit.getColors().getDisplay().asyncExec(new Runnable() {
184 public void run() {
185 doRefresh();
186 }
187 });
188 }
189 }
190
191 private void doRefresh() {
192 int nrefreshed = 0;
193 for (int i = 0; i < parts.size(); i++) {
194 IFormPart part = (IFormPart) parts.get(i);
195 if (part.isStale()) {
196 part.refresh();
197 nrefreshed++;
198 }
199 }
200 // if (nrefreshed > 0)
201 // form.reflow(true);
202 }
203
204 /*
205 * (non-Javadoc)
206 *
207 * @see org.eclipse.ui.forms.IManagedForm#commit(boolean)
208 */
209 public void commit(boolean onSave) {
210 for (int i = 0; i < parts.size(); i++) {
211 IFormPart part = (IFormPart) parts.get(i);
212 if (part.isDirty())
213 part.commit(onSave);
214 }
215 }
216
217 /*
218 * (non-Javadoc)
219 *
220 * @see org.eclipse.ui.forms.IManagedForm#setInput(java.lang.Object)
221 */
222 public boolean setInput(Object input) {
223 boolean pageResult = false;
224
225 this.input = input;
226 for (int i = 0; i < parts.size(); i++) {
227 IFormPart part = (IFormPart) parts.get(i);
228 boolean result = part.setFormInput(input);
229 if (result)
230 pageResult = true;
231 }
232 return pageResult;
233 }
234
235 /*
236 * (non-Javadoc)
237 *
238 * @see org.eclipse.ui.forms.IManagedForm#getInput()
239 */
240 public Object getInput() {
241 return input;
242 }
243
244 /**
245 * Transfers the focus to the first form part.
246 */
247 public void setFocus() {
248 if (parts.size() > 0) {
249 IFormPart part = (IFormPart) parts.get(0);
250 part.setFocus();
251 }
252 }
253
254 /*
255 * (non-Javadoc)
256 *
257 * @see org.eclipse.ui.forms.IManagedForm#isDirty()
258 */
259 public boolean isDirty() {
260 for (int i = 0; i < parts.size(); i++) {
261 IFormPart part = (IFormPart) parts.get(i);
262 if (part.isDirty())
263 return true;
264 }
265 return false;
266 }
267
268 /*
269 * (non-Javadoc)
270 *
271 * @see org.eclipse.ui.forms.IManagedForm#isStale()
272 */
273 public boolean isStale() {
274 for (int i = 0; i < parts.size(); i++) {
275 IFormPart part = (IFormPart) parts.get(i);
276 if (part.isStale())
277 return true;
278 }
279 return false;
280 }
281
282 /*
283 * (non-Javadoc)
284 *
285 * @see org.eclipse.ui.forms.IManagedForm#dirtyStateChanged()
286 */
287 public void dirtyStateChanged() {
288 }
289
290 /*
291 * (non-Javadoc)
292 *
293 * @see org.eclipse.ui.forms.IManagedForm#staleStateChanged()
294 */
295 public void staleStateChanged() {
296 }
297
298 /*
299 * (non-Javadoc)
300 *
301 * @see org.eclipse.ui.forms.IManagedForm#getContainer()
302 */
303 public Object getContainer() {
304 return container;
305 }
306
307 /*
308 * (non-Javadoc)
309 *
310 * @see org.eclipse.ui.forms.IManagedForm#setContainer(java.lang.Object)
311 */
312 public void setContainer(Object container) {
313 this.container = container;
314 }
315
316 /* (non-Javadoc)
317 * @see org.eclipse.ui.forms.IManagedForm#getMessageManager()
318 */
319 // public IMessageManager getMessageManager() {
320 // return form.getMessageManager();
321 // }
322 }