Mercurial > projects > dwt-addons
annotate dwtx/ui/forms/MasterDetailsBlock.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 | c3583c6ec027 |
rev | line source |
---|---|
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.MasterDetailsBlock; | |
14 | |
15 import dwtx.ui.forms.DetailsPart; | |
16 import dwtx.ui.forms.IManagedForm; | |
17 import dwtx.ui.forms.FormColors; | |
18 import dwtx.ui.forms.IFormColors; | |
19 | |
20 import dwt.DWT; | |
21 import dwt.custom.SashForm; | |
22 import dwt.graphics.GC; | |
23 import dwt.graphics.Point; | |
24 import dwt.layout.GridData; | |
25 import dwt.layout.GridLayout; | |
26 import dwt.widgets.Composite; | |
27 import dwt.widgets.Control; | |
28 import dwt.widgets.Event; | |
29 import dwt.widgets.Listener; | |
30 import dwt.widgets.Sash; | |
31 import dwtx.ui.forms.widgets.FormToolkit; | |
32 import dwtx.ui.forms.widgets.ScrolledForm; | |
33 | |
34 import dwt.dwthelper.utils; | |
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
|
35 import dwtx.dwtxhelper.Collection; |
75 | 36 |
37 /** | |
38 * This class implements the 'master/details' UI pattern suitable for inclusion | |
39 * in a form. The block consists of two parts: 'master' and 'details' in a sash | |
40 * form that allows users to change the relative ratio on the page. The master | |
41 * part needs to be created by the users of this class. The details part is | |
42 * created by the block. | |
43 * <p> | |
44 * The master part is responsible for adding itself as a form part and firing | |
45 * selection events. The details part catches the selection events and tries to | |
46 * load a page registered to handle the selected object(s). The page shows the | |
47 * details of the selected object(s) and allows users to edit them. | |
48 * <p> | |
49 * Details pages can be registered statically using 'registerPage' or | |
50 * dynamically through the use of 'IDetailsPageProvider' in case where different | |
51 * pages need to be shown for objects of the same type depending on their state. | |
52 * <p> | |
53 * Subclasses are required to implement abstract methods of this class. Master | |
54 * part must be created and at least one details page should be registered in | |
55 * order to show details of the objects selected in the master part. Tool bar | |
56 * actions can be optionally added to the tool bar manager. | |
57 * | |
58 * @see DetailsPart | |
59 * @see IDetailsPage | |
60 * @see IDetailsPageProvider | |
61 * @since 3.0 | |
62 */ | |
63 public abstract class MasterDetailsBlock { | |
64 /** | |
65 * Details part created by the block. No attempt should be made to access | |
66 * this field inside <code>createMasterPart</code> because it has not been | |
67 * created yet and will be <code>null</code>. | |
68 */ | |
69 protected DetailsPart detailsPart; | |
70 | |
71 /** | |
72 * The form that is the parent of both master and details part. The form | |
73 * allows users to change the ratio between the two parts. | |
74 */ | |
75 protected SashForm sashForm; | |
76 | |
77 static const int DRAGGER_SIZE = 40; | |
78 | |
79 class MDSashForm : SashForm { | |
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
|
80 ArrayList sashes; |
75 | 81 Listener listener; |
82 public this(Composite parent, int style) { | |
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
|
83 sashes = new ArrayList(); |
75 | 84 listener = dgListener ( (Event e){ |
85 switch (e.type) { | |
86 case DWT.MouseEnter: | |
87 e.widget.setData("hover", Boolean.TRUE); //$NON-NLS-1$ | |
88 (cast(Control)e.widget).redraw(); | |
89 break; | |
90 case DWT.MouseExit: | |
91 e.widget.setData("hover", null); //$NON-NLS-1$ | |
92 (cast(Control)e.widget).redraw(); | |
93 break; | |
94 case DWT.Paint: | |
95 onSashPaint(e); | |
96 break; | |
97 case DWT.Resize: | |
98 hookSashListeners(); | |
99 break; | |
100 } | |
101 }); | |
102 super(parent, style); | |
103 } | |
104 | |
105 public void layout(bool changed) { | |
106 super.layout(changed); | |
107 hookSashListeners(); | |
108 } | |
109 | |
110 public void layout(Control [] children) { | |
111 super.layout(children); | |
112 hookSashListeners(); | |
113 } | |
114 | |
115 private void hookSashListeners() { | |
116 purgeSashes(); | |
117 Control [] children = getChildren(); | |
118 for (int i=0; i<children.length; i++) { | |
119 if ( auto sash = cast(Sash)children[i] ) { | |
120 if (sashes.contains(sash)) | |
121 continue; | |
122 sash.addListener(DWT.Paint, listener); | |
123 sash.addListener(DWT.MouseEnter, listener); | |
124 sash.addListener(DWT.MouseExit, listener); | |
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
|
125 sashes.add(sash); |
75 | 126 } |
127 } | |
128 } | |
129 private void purgeSashes() { | |
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
|
130 for (Iterator iter=sashes.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
|
131 Sash sash = cast(Sash)iter.next(); |
75 | 132 if (sash.isDisposed()) |
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
|
133 iter.remove(); |
75 | 134 } |
135 } | |
136 } | |
137 | |
138 /** | |
139 * Creates the content of the master/details block inside the managed form. | |
140 * This method should be called as late as possible inside the parent part. | |
141 * | |
142 * @param managedForm | |
143 * the managed form to create the block in | |
144 */ | |
145 public void createContent(IManagedForm managedForm) { | |
146 final ScrolledForm form = managedForm.getForm(); | |
147 FormToolkit toolkit = managedForm.getToolkit(); | |
148 GridLayout layout = new GridLayout(); | |
149 layout.marginWidth = 0; | |
150 layout.marginHeight = 0; | |
151 form.getBody().setLayout(layout); | |
152 sashForm = new MDSashForm(form.getBody(), DWT.NULL); | |
153 sashForm.setData("form", cast(Object)managedForm); //$NON-NLS-1$ | |
154 toolkit.adapt(sashForm, false, false); | |
155 sashForm.setMenu(form.getBody().getMenu()); | |
156 sashForm.setLayoutData(new GridData(GridData.FILL_BOTH)); | |
157 createMasterPart(managedForm, sashForm); | |
158 createDetailsPart(managedForm, sashForm); | |
159 hookResizeListener(); | |
160 createToolBarActions(managedForm); | |
161 form.updateToolBar(); | |
162 } | |
163 | |
164 private void hookResizeListener() { | |
165 Listener listener = (cast(MDSashForm)sashForm).listener; | |
166 Control [] children = sashForm.getChildren(); | |
167 for (int i=0; i<children.length; i++) { | |
168 if (null !is cast(Sash)children[i] ) continue; | |
169 children[i].addListener(DWT.Resize, listener); | |
170 } | |
171 } | |
172 | |
173 /** | |
174 * Implement this method to create a master part in the provided parent. | |
175 * Typical master parts are section parts that contain tree or table viewer. | |
176 * | |
177 * @param managedForm | |
178 * the parent form | |
179 * @param parent | |
180 * the parent composite | |
181 */ | |
182 protected abstract void createMasterPart(IManagedForm managedForm, | |
183 Composite parent); | |
184 | |
185 /** | |
186 * Implement this method to statically register pages for the expected | |
187 * object types. This mechanism can be used when there is 1->1 mapping | |
188 * between object classes and details pages. | |
189 * | |
190 * @param detailsPart | |
191 * the details part | |
192 */ | |
193 protected abstract void registerPages(DetailsPart detailsPart); | |
194 | |
195 /** | |
196 * Implement this method to create form tool bar actions and add them to the | |
197 * form tool bar if desired. | |
198 * | |
199 * @param managedForm | |
200 * the form that owns the tool bar | |
201 */ | |
202 protected abstract void createToolBarActions(IManagedForm managedForm); | |
203 | |
204 private void createDetailsPart(IManagedForm mform, Composite parent) { | |
205 detailsPart = new DetailsPart(mform, parent, DWT.NULL); | |
206 mform.addPart(detailsPart); | |
207 registerPages(detailsPart); | |
208 } | |
209 | |
210 private void onSashPaint(Event e) { | |
211 Sash sash = cast(Sash)e.widget; | |
212 IManagedForm form = cast(IManagedForm)sash.getParent().getData("form"); //$NON-NLS-1$ | |
213 FormColors colors = form.getToolkit().getColors(); | |
214 bool vertical = (sash.getStyle() & DWT.VERTICAL) !is 0; | |
215 GC gc = e.gc; | |
216 Boolean hover = cast(Boolean)sash.getData("hover"); //$NON-NLS-1$ | |
217 gc.setBackground(colors.getColor(IFormColors.TB_BG)); | |
218 gc.setForeground(colors.getColor(IFormColors.TB_BORDER)); | |
219 Point size = sash.getSize(); | |
220 if (vertical) { | |
221 if (hover !is null) | |
222 gc.fillRectangle(0, 0, size.x, size.y); | |
223 //else | |
224 //gc.drawLine(1, 0, 1, size.y-1); | |
225 } | |
226 else { | |
227 if (hover !is null) | |
228 gc.fillRectangle(0, 0, size.x, size.y); | |
229 //else | |
230 //gc.drawLine(0, 1, size.x-1, 1); | |
231 } | |
232 } | |
233 } |