Mercurial > projects > dwt-addons
annotate dwtx/ui/forms/DetailsPart.d @ 104:04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
These new wrappers now use the tango.util.containers instead of the tango.util.collections.
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Thu, 07 Aug 2008 15:01:33 +0200 |
parents | 5d489b9f966c |
children |
rev | line source |
---|---|
75 | 1 /******************************************************************************* |
2 * Copyright (c) 2000, 2006 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.DetailsPart; | |
14 | |
15 import dwtx.ui.forms.IFormPart; | |
16 import dwtx.ui.forms.IPartSelectionListener; | |
17 import dwtx.ui.forms.IManagedForm; | |
18 import dwtx.ui.forms.IDetailsPageProvider; | |
19 import dwtx.ui.forms.IDetailsPage; | |
20 | |
21 import dwt.DWT; | |
22 import dwt.custom.BusyIndicator; | |
23 import dwt.widgets.Composite; | |
24 import dwt.widgets.Control; | |
25 import dwtx.jface.viewers.ISelection; | |
26 import dwtx.jface.viewers.IStructuredSelection; | |
27 import dwtx.ui.forms.widgets.ScrolledPageBook; | |
28 | |
29 import dwt.dwthelper.utils; | |
30 import dwt.dwthelper.Runnable; | |
104
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
75
diff
changeset
|
31 import dwtx.dwtxhelper.Collection; |
75 | 32 |
33 /** | |
34 * This managed form part handles the 'details' portion of the | |
35 * 'master/details' block. It has a page book that manages pages | |
36 * of details registered for the current selection. | |
37 * <p>By default, details part accepts any number of pages. | |
38 * If dynamic page provider is registered, this number may | |
39 * be excessive. To avoid running out of steam (by creating | |
40 * a large number of pages with widgets on each), maximum | |
41 * number of pages can be set to some reasonable value (e.g. 10). | |
42 * When this number is reached, old pages (those created first) | |
43 * will be removed and disposed as new ones are added. If | |
44 * the disposed pages are needed again after that, they | |
45 * will be created again. | |
46 * | |
47 * @since 3.0 | |
48 */ | |
49 public final class DetailsPart : IFormPart, IPartSelectionListener { | |
50 private IManagedForm managedForm; | |
51 private ScrolledPageBook pageBook; | |
52 private IFormPart masterPart; | |
53 private IStructuredSelection currentSelection; | |
104
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
75
diff
changeset
|
54 private Hashtable pages; |
75 | 55 private IDetailsPageProvider pageProvider; |
56 private int pageLimit=Integer.MAX_VALUE; | |
57 | |
58 private static class PageBag { | |
59 private static int counter; | |
60 private int ticket; | |
61 private IDetailsPage page; | |
62 private bool fixed; | |
63 | |
64 public this(IDetailsPage page, bool fixed) { | |
65 this.page= page; | |
66 this.fixed = fixed; | |
67 this.ticket = ++counter; | |
68 } | |
69 public int getTicket() { | |
70 return ticket; | |
71 } | |
72 public IDetailsPage getPage() { | |
73 return page; | |
74 } | |
75 public void dispose() { | |
76 page.dispose(); | |
77 page=null; | |
78 } | |
79 public bool isDisposed() { | |
80 return page is null; | |
81 } | |
82 public bool isFixed() { | |
83 return fixed; | |
84 } | |
85 public static int getCurrentTicket() { | |
86 return counter; | |
87 } | |
88 } | |
89 /** | |
90 * Creates a details part by wrapping the provided page book. | |
91 * @param mform the parent form | |
92 * @param pageBook the page book to wrap | |
93 */ | |
94 public this(IManagedForm mform, ScrolledPageBook pageBook) { | |
95 this.pageBook = pageBook; | |
104
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
75
diff
changeset
|
96 pages = new Hashtable(); |
75 | 97 initialize(mform); |
98 } | |
99 /** | |
100 * Creates a new details part in the provided form by creating | |
101 * the page book. | |
102 * @param mform the parent form | |
103 * @param parent the composite to create the page book in | |
104 * @param style the style for the page book | |
105 */ | |
106 public this(IManagedForm mform, Composite parent, int style) { | |
107 this(mform, mform.getToolkit().createPageBook(parent, style|DWT.V_SCROLL|DWT.H_SCROLL)); | |
108 } | |
109 /** | |
110 * Registers the details page to be used for all the objects of | |
111 * the provided object class. | |
112 * @param objectClass an object of type 'java.lang.Class' to be used | |
113 * as a key for the provided page | |
114 * @param page the page to show for objects of the provided object class | |
115 */ | |
116 public void registerPage(Object objectClass, IDetailsPage page) { | |
117 registerPage(objectClass, page, true); | |
118 } | |
119 | |
120 private void registerPage(Object objectClass, IDetailsPage page, bool fixed) { | |
104
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
75
diff
changeset
|
121 pages.put(objectClass, new PageBag(page, fixed)); |
75 | 122 page.initialize(managedForm); |
123 } | |
124 /** | |
125 * Sets the dynamic page provider. The dynamic provider can return | |
126 * different pages for objects of the same class based on their state. | |
127 * @param provider the provider to use | |
128 */ | |
129 public void setPageProvider(IDetailsPageProvider provider) { | |
130 this.pageProvider = provider; | |
131 } | |
132 /** | |
133 * Commits the part by committing the current page. | |
134 * @param onSave <code>true</code> if commit is requested as a result | |
135 * of the 'save' action, <code>false</code> otherwise. | |
136 */ | |
137 public void commit(bool onSave) { | |
138 IDetailsPage page = getCurrentPage(); | |
139 if (page !is null) | |
140 page.commit(onSave); | |
141 } | |
142 /** | |
143 * Returns the current page visible in the part. | |
144 * @return the current page | |
145 */ | |
146 public IDetailsPage getCurrentPage() { | |
147 Control control = pageBook.getCurrentPage(); | |
148 if (control !is null) { | |
149 Object data = control.getData(); | |
150 if (null !is cast(IDetailsPage)data ) | |
151 return cast(IDetailsPage) data; | |
152 } | |
153 return null; | |
154 } | |
155 /* | |
156 * (non-Javadoc) | |
157 * | |
158 * @see dwtx.ui.forms.IFormPart#dispose() | |
159 */ | |
160 public void dispose() { | |
104
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
75
diff
changeset
|
161 for (Enumeration enm = pages.elements(); enm.hasMoreElements();) { |
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
75
diff
changeset
|
162 PageBag pageBag = cast(PageBag) enm.nextElement(); |
75 | 163 pageBag.dispose(); |
164 } | |
165 } | |
166 /* | |
167 * (non-Javadoc) | |
168 * | |
169 * @see dwtx.ui.forms.IFormPart#initialize(dwtx.ui.forms.IManagedForm) | |
170 */ | |
171 public void initialize(IManagedForm form) { | |
172 this.managedForm = form; | |
173 } | |
174 /** | |
175 * Tests if the currently visible page is dirty. | |
176 * @return <code>true</code> if the page is dirty, <code>false</code> otherwise. | |
177 */ | |
178 public bool isDirty() { | |
179 IDetailsPage page = getCurrentPage(); | |
180 if (page !is null) | |
181 return page.isDirty(); | |
182 return false; | |
183 } | |
184 /** | |
185 * Tests if the currently visible page is stale and needs refreshing. | |
186 * @return <code>true</code> if the page is stale, <code>false</code> otherwise. | |
187 */ | |
188 public bool isStale() { | |
189 IDetailsPage page = getCurrentPage(); | |
190 if (page !is null) | |
191 return page.isStale(); | |
192 return false; | |
193 } | |
194 | |
195 /** | |
196 * Refreshes the current page. | |
197 */ | |
198 public void refresh() { | |
199 IDetailsPage page = getCurrentPage(); | |
200 if (page !is null) | |
201 page.refresh(); | |
202 } | |
203 /** | |
204 * Sets the focus to the currently visible page. | |
205 */ | |
206 public void setFocus() { | |
207 IDetailsPage page = getCurrentPage(); | |
208 if (page !is null) | |
209 page.setFocus(); | |
210 } | |
211 /* | |
212 * (non-Javadoc) | |
213 * | |
214 * @see dwtx.ui.forms.IFormPart#setFormInput(java.lang.Object) | |
215 */ | |
216 public bool setFormInput(Object input) { | |
217 return false; | |
218 } | |
219 /* | |
220 * (non-Javadoc) | |
221 * | |
222 * @see dwtx.ui.forms.IPartSelectionListener#selectionChanged(dwtx.ui.forms.IFormPart, | |
223 * dwtx.jface.viewers.ISelection) | |
224 */ | |
225 public void selectionChanged(IFormPart part, ISelection selection) { | |
226 this.masterPart = part; | |
227 if (currentSelection !is null) { | |
228 } | |
229 if (null !is cast(IStructuredSelection)selection ) | |
230 currentSelection = cast(IStructuredSelection) selection; | |
231 else | |
232 currentSelection = null; | |
233 update(); | |
234 } | |
235 private void update() { | |
236 Object key = null; | |
237 if (currentSelection !is null) { | |
104
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
75
diff
changeset
|
238 for (Iterator iter = currentSelection.iterator(); iter.hasNext();) { |
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
75
diff
changeset
|
239 Object obj = iter.next(); |
75 | 240 if (key is null) |
241 key = getKey(obj); | |
242 else if (getKey(obj).opEquals(key) is false) { | |
243 key = null; | |
244 break; | |
245 } | |
246 } | |
247 } | |
248 showPage(key); | |
249 } | |
250 private Object getKey(Object object) { | |
251 if (pageProvider !is null) { | |
252 Object key = pageProvider.getPageKey(object); | |
253 if (key !is null) | |
254 return key; | |
255 } | |
256 return object.classinfo; | |
257 } | |
258 private void showPage( Object key) { | |
259 checkLimit(); | |
260 final IDetailsPage oldPage = getCurrentPage(); | |
261 if (key !is null) { | |
262 PageBag pageBag = cast(PageBag)pages.get(key); | |
263 IDetailsPage page = pageBag !is null?pageBag.getPage():null; | |
264 if (page is null) { | |
265 // try to get the page dynamically from the provider | |
266 if (pageProvider !is null) { | |
267 page = pageProvider.getPage(key); | |
268 if (page !is null) { | |
269 registerPage(key, page, false); | |
270 } | |
271 } | |
272 } | |
273 if (page !is null) { | |
274 BusyIndicator.showWhile(pageBook.getDisplay(), dgRunnable( (IDetailsPage fpage,Object key,IDetailsPage oldPage) { | |
275 if (!pageBook.hasPage(key)) { | |
276 Composite parent = pageBook.createPage(key); | |
277 fpage.createContents(parent); | |
278 parent.setData(cast(Object)fpage); | |
279 } | |
280 //commit the current page | |
281 if (oldPage !is null && oldPage.isDirty()) | |
282 oldPage.commit(false); | |
283 //refresh the new page | |
284 if (fpage.isStale()) | |
285 fpage.refresh(); | |
286 fpage.selectionChanged(masterPart, currentSelection); | |
287 pageBook.showPage(key); | |
288 }, page, key, oldPage)); | |
289 return; | |
290 } | |
291 } | |
292 // If we are switching from an old page to nothing, | |
293 // don't loose data | |
294 if (oldPage !is null && oldPage.isDirty()) | |
295 oldPage.commit(false); | |
296 pageBook.showEmptyPage(); | |
297 } | |
298 private void checkLimit() { | |
299 if (pages.size() <= getPageLimit()) return; | |
300 // overflow | |
301 int currentTicket = PageBag.getCurrentTicket(); | |
302 int cutoffTicket = currentTicket - getPageLimit(); | |
104
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
75
diff
changeset
|
303 for (Enumeration enm=pages.keys(); enm.hasMoreElements();) { |
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
75
diff
changeset
|
304 Object key = enm.nextElement(); |
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
75
diff
changeset
|
305 PageBag pageBag = cast(PageBag)pages.get(key); |
75 | 306 if (pageBag.getTicket()<=cutoffTicket) { |
307 // candidate - see if it is active and not fixed | |
308 if (!pageBag.isFixed() && !(cast(Object)pageBag.getPage()).opEquals(cast(Object)getCurrentPage())) { | |
309 // drop it | |
310 pageBag.dispose(); | |
311 pages.remove(key); | |
312 pageBook.removePage(key, false); | |
313 } | |
314 } | |
315 } | |
316 } | |
317 /** | |
318 * Returns the maximum number of pages that should be | |
319 * maintained in this part. When an attempt is made to | |
320 * add more pages, old pages are removed and disposed | |
321 * based on the order of creation (the oldest pages | |
322 * are removed). The exception is made for the | |
323 * page that should otherwise be disposed but is | |
324 * currently active. | |
325 * @return maximum number of pages for this part | |
326 */ | |
327 public int getPageLimit() { | |
328 return pageLimit; | |
329 } | |
330 /** | |
331 * Sets the page limit for this part. | |
332 * @see #getPageLimit() | |
333 * @param pageLimit the maximum number of pages that | |
334 * should be maintained in this part. | |
335 */ | |
336 public void setPageLimit(int pageLimit) { | |
337 this.pageLimit = pageLimit; | |
338 checkLimit(); | |
339 } | |
340 } |