]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms.ui/src/org/argeo/cms/ui/eclipse/forms/ManagedForm.java
Major refactoring of Argeo CMS UI
[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 /**
58 * Creates a managed form that will use the provided toolkit and
59 *
60 * @param toolkit
61 * @param form
62 */
63 public ManagedForm(FormToolkit toolkit, ScrolledComposite form) {
64 this.form = form;
65 this.toolkit = toolkit;
66 }
67
68 /*
69 * (non-Javadoc)
70 *
71 * @see org.eclipse.ui.forms.IManagedForm#addPart(org.eclipse.ui.forms.IFormPart)
72 */
73 public void addPart(IFormPart part) {
74 parts.add(part);
75 part.initialize(this);
76 }
77
78 /*
79 * (non-Javadoc)
80 *
81 * @see org.eclipse.ui.forms.IManagedForm#removePart(org.eclipse.ui.forms.IFormPart)
82 */
83 public void removePart(IFormPart part) {
84 parts.remove(part);
85 }
86
87 /*
88 * (non-Javadoc)
89 *
90 * @see org.eclipse.ui.forms.IManagedForm#getParts()
91 */
92 public IFormPart[] getParts() {
93 return (IFormPart[]) parts.toArray(new IFormPart[parts.size()]);
94 }
95
96 /*
97 * (non-Javadoc)
98 *
99 * @see org.eclipse.ui.forms.IManagedForm#getToolkit()
100 */
101 public FormToolkit getToolkit() {
102 return toolkit;
103 }
104
105 /*
106 * (non-Javadoc)
107 *
108 * @see org.eclipse.ui.forms.IManagedForm#getForm()
109 */
110 public ScrolledComposite getForm() {
111 return form;
112 }
113
114 /*
115 * (non-Javadoc)
116 *
117 * @see org.eclipse.ui.forms.IManagedForm#reflow(boolean)
118 */
119 public void reflow(boolean changed) {
120 // form.reflow(changed);
121 }
122
123 /**
124 * A part can use this method to notify other parts that implement
125 * IPartSelectionListener about selection changes.
126 *
127 * @param part
128 * the part that broadcasts the selection
129 * @param selection
130 * the selection in the part
131 * @see IPartSelectionListener
132 */
133 public void fireSelectionChanged(IFormPart part, ISelection selection) {
134 for (int i = 0; i < parts.size(); i++) {
135 IFormPart cpart = (IFormPart) parts.get(i);
136 if (part.equals(cpart))
137 continue;
138 // if (cpart instanceof IPartSelectionListener) {
139 // ((IPartSelectionListener) cpart).selectionChanged(part,
140 // selection);
141 // }
142 }
143 }
144
145 /**
146 * Initializes the form by looping through the managed parts and
147 * initializing them. Has no effect if already called once.
148 */
149 public void initialize() {
150 if (initialized)
151 return;
152 for (int i = 0; i < parts.size(); i++) {
153 IFormPart part = (IFormPart) parts.get(i);
154 part.initialize(this);
155 }
156 initialized = true;
157 }
158
159 /**
160 * Disposes all the parts in this form.
161 */
162 public void dispose() {
163 for (int i = 0; i < parts.size(); i++) {
164 IFormPart part = (IFormPart) parts.get(i);
165 part.dispose();
166 }
167 if (ownsToolkit) {
168 toolkit.dispose();
169 }
170 }
171
172 /**
173 * Refreshes the form by refreshes all the stale parts. Since 3.1, this
174 * method is performed on a UI thread when called from another thread so it
175 * is not needed to wrap the call in <code>Display.syncExec</code> or
176 * <code>asyncExec</code>.
177 */
178 public void refresh() {
179 Thread t = Thread.currentThread();
180 Thread dt = toolkit.getColors().getDisplay().getThread();
181 if (t.equals(dt))
182 doRefresh();
183 else {
184 toolkit.getColors().getDisplay().asyncExec(new Runnable() {
185 public void run() {
186 doRefresh();
187 }
188 });
189 }
190 }
191
192 private void doRefresh() {
193 int nrefreshed = 0;
194 for (int i = 0; i < parts.size(); i++) {
195 IFormPart part = (IFormPart) parts.get(i);
196 if (part.isStale()) {
197 part.refresh();
198 nrefreshed++;
199 }
200 }
201 // if (nrefreshed > 0)
202 // form.reflow(true);
203 }
204
205 /*
206 * (non-Javadoc)
207 *
208 * @see org.eclipse.ui.forms.IManagedForm#commit(boolean)
209 */
210 public void commit(boolean onSave) {
211 for (int i = 0; i < parts.size(); i++) {
212 IFormPart part = (IFormPart) parts.get(i);
213 if (part.isDirty())
214 part.commit(onSave);
215 }
216 }
217
218 /*
219 * (non-Javadoc)
220 *
221 * @see org.eclipse.ui.forms.IManagedForm#setInput(java.lang.Object)
222 */
223 public boolean setInput(Object input) {
224 boolean pageResult = false;
225
226 this.input = input;
227 for (int i = 0; i < parts.size(); i++) {
228 IFormPart part = (IFormPart) parts.get(i);
229 boolean result = part.setFormInput(input);
230 if (result)
231 pageResult = true;
232 }
233 return pageResult;
234 }
235
236 /*
237 * (non-Javadoc)
238 *
239 * @see org.eclipse.ui.forms.IManagedForm#getInput()
240 */
241 public Object getInput() {
242 return input;
243 }
244
245 /**
246 * Transfers the focus to the first form part.
247 */
248 public void setFocus() {
249 if (parts.size() > 0) {
250 IFormPart part = (IFormPart) parts.get(0);
251 part.setFocus();
252 }
253 }
254
255 /*
256 * (non-Javadoc)
257 *
258 * @see org.eclipse.ui.forms.IManagedForm#isDirty()
259 */
260 public boolean isDirty() {
261 for (int i = 0; i < parts.size(); i++) {
262 IFormPart part = (IFormPart) parts.get(i);
263 if (part.isDirty())
264 return true;
265 }
266 return false;
267 }
268
269 /*
270 * (non-Javadoc)
271 *
272 * @see org.eclipse.ui.forms.IManagedForm#isStale()
273 */
274 public boolean isStale() {
275 for (int i = 0; i < parts.size(); i++) {
276 IFormPart part = (IFormPart) parts.get(i);
277 if (part.isStale())
278 return true;
279 }
280 return false;
281 }
282
283 /*
284 * (non-Javadoc)
285 *
286 * @see org.eclipse.ui.forms.IManagedForm#dirtyStateChanged()
287 */
288 public void dirtyStateChanged() {
289 }
290
291 /*
292 * (non-Javadoc)
293 *
294 * @see org.eclipse.ui.forms.IManagedForm#staleStateChanged()
295 */
296 public void staleStateChanged() {
297 }
298
299 /*
300 * (non-Javadoc)
301 *
302 * @see org.eclipse.ui.forms.IManagedForm#getContainer()
303 */
304 public Object getContainer() {
305 return container;
306 }
307
308 /*
309 * (non-Javadoc)
310 *
311 * @see org.eclipse.ui.forms.IManagedForm#setContainer(java.lang.Object)
312 */
313 public void setContainer(Object container) {
314 this.container = container;
315 }
316
317 /* (non-Javadoc)
318 * @see org.eclipse.ui.forms.IManagedForm#getMessageManager()
319 */
320 // public IMessageManager getMessageManager() {
321 // return form.getMessageManager();
322 // }
323 }