Mercurial > projects > dwt-addons
annotate dwtx/ui/forms/MasterDetailsBlock.d @ 192:c3583c6ec027
Added missing default cases for switch statements
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Mon, 03 Nov 2008 22:52:26 +0100 |
parents | 04b47443bb01 |
children |
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; | |
192
c3583c6ec027
Added missing default cases for switch statements
Frank Benoit <benoit@tionex.de>
parents:
104
diff
changeset
|
100 default: |
75 | 101 } |
102 }); | |
103 super(parent, style); | |
104 } | |
105 | |
106 public void layout(bool changed) { | |
107 super.layout(changed); | |
108 hookSashListeners(); | |
109 } | |
110 | |
111 public void layout(Control [] children) { | |
112 super.layout(children); | |
113 hookSashListeners(); | |
114 } | |
115 | |
116 private void hookSashListeners() { | |
117 purgeSashes(); | |
118 Control [] children = getChildren(); | |
119 for (int i=0; i<children.length; i++) { | |
120 if ( auto sash = cast(Sash)children[i] ) { | |
121 if (sashes.contains(sash)) | |
122 continue; | |
123 sash.addListener(DWT.Paint, listener); | |
124 sash.addListener(DWT.MouseEnter, listener); | |
125 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
|
126 sashes.add(sash); |
75 | 127 } |
128 } | |
129 } | |
130 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
|
131 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
|
132 Sash sash = cast(Sash)iter.next(); |
75 | 133 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
|
134 iter.remove(); |
75 | 135 } |
136 } | |
137 } | |
138 | |
139 /** | |
140 * Creates the content of the master/details block inside the managed form. | |
141 * This method should be called as late as possible inside the parent part. | |
142 * | |
143 * @param managedForm | |
144 * the managed form to create the block in | |
145 */ | |
146 public void createContent(IManagedForm managedForm) { | |
147 final ScrolledForm form = managedForm.getForm(); | |
148 FormToolkit toolkit = managedForm.getToolkit(); | |
149 GridLayout layout = new GridLayout(); | |
150 layout.marginWidth = 0; | |
151 layout.marginHeight = 0; | |
152 form.getBody().setLayout(layout); | |
153 sashForm = new MDSashForm(form.getBody(), DWT.NULL); | |
154 sashForm.setData("form", cast(Object)managedForm); //$NON-NLS-1$ | |
155 toolkit.adapt(sashForm, false, false); | |
156 sashForm.setMenu(form.getBody().getMenu()); | |
157 sashForm.setLayoutData(new GridData(GridData.FILL_BOTH)); | |
158 createMasterPart(managedForm, sashForm); | |
159 createDetailsPart(managedForm, sashForm); | |
160 hookResizeListener(); | |
161 createToolBarActions(managedForm); | |
162 form.updateToolBar(); | |
163 } | |
164 | |
165 private void hookResizeListener() { | |
166 Listener listener = (cast(MDSashForm)sashForm).listener; | |
167 Control [] children = sashForm.getChildren(); | |
168 for (int i=0; i<children.length; i++) { | |
169 if (null !is cast(Sash)children[i] ) continue; | |
170 children[i].addListener(DWT.Resize, listener); | |
171 } | |
172 } | |
173 | |
174 /** | |
175 * Implement this method to create a master part in the provided parent. | |
176 * Typical master parts are section parts that contain tree or table viewer. | |
177 * | |
178 * @param managedForm | |
179 * the parent form | |
180 * @param parent | |
181 * the parent composite | |
182 */ | |
183 protected abstract void createMasterPart(IManagedForm managedForm, | |
184 Composite parent); | |
185 | |
186 /** | |
187 * Implement this method to statically register pages for the expected | |
188 * object types. This mechanism can be used when there is 1->1 mapping | |
189 * between object classes and details pages. | |
190 * | |
191 * @param detailsPart | |
192 * the details part | |
193 */ | |
194 protected abstract void registerPages(DetailsPart detailsPart); | |
195 | |
196 /** | |
197 * Implement this method to create form tool bar actions and add them to the | |
198 * form tool bar if desired. | |
199 * | |
200 * @param managedForm | |
201 * the form that owns the tool bar | |
202 */ | |
203 protected abstract void createToolBarActions(IManagedForm managedForm); | |
204 | |
205 private void createDetailsPart(IManagedForm mform, Composite parent) { | |
206 detailsPart = new DetailsPart(mform, parent, DWT.NULL); | |
207 mform.addPart(detailsPart); | |
208 registerPages(detailsPart); | |
209 } | |
210 | |
211 private void onSashPaint(Event e) { | |
212 Sash sash = cast(Sash)e.widget; | |
213 IManagedForm form = cast(IManagedForm)sash.getParent().getData("form"); //$NON-NLS-1$ | |
214 FormColors colors = form.getToolkit().getColors(); | |
215 bool vertical = (sash.getStyle() & DWT.VERTICAL) !is 0; | |
216 GC gc = e.gc; | |
217 Boolean hover = cast(Boolean)sash.getData("hover"); //$NON-NLS-1$ | |
218 gc.setBackground(colors.getColor(IFormColors.TB_BG)); | |
219 gc.setForeground(colors.getColor(IFormColors.TB_BORDER)); | |
220 Point size = sash.getSize(); | |
221 if (vertical) { | |
222 if (hover !is null) | |
223 gc.fillRectangle(0, 0, size.x, size.y); | |
224 //else | |
225 //gc.drawLine(1, 0, 1, size.y-1); | |
226 } | |
227 else { | |
228 if (hover !is null) | |
229 gc.fillRectangle(0, 0, size.x, size.y); | |
230 //else | |
231 //gc.drawLine(0, 1, size.x-1, 1); | |
232 } | |
233 } | |
234 } |