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