comparison dwtx/ui/forms/MasterDetailsBlock.d @ 75:5d489b9f966c

Fix continue porting
author Frank Benoit <benoit@tionex.de>
date Sat, 24 May 2008 05:11:16 +0200
parents
children 04b47443bb01
comparison
equal deleted inserted replaced
74:dad2e11b8ae4 75:5d489b9f966c
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;
35 import tango.util.collection.ArraySeq;
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 {
80 ArraySeq!(Sash) sashes;
81 Listener listener;
82 public this(Composite parent, int style) {
83 sashes = new ArraySeq!(Sash);
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);
125 sashes.append(sash);
126 }
127 }
128 }
129 private void purgeSashes() {
130 foreach ( sash; sashes.dup ) {
131 if (sash.isDisposed())
132 sashes.remove(sash);
133 }
134 }
135 }
136
137 /**
138 * Creates the content of the master/details block inside the managed form.
139 * This method should be called as late as possible inside the parent part.
140 *
141 * @param managedForm
142 * the managed form to create the block in
143 */
144 public void createContent(IManagedForm managedForm) {
145 final ScrolledForm form = managedForm.getForm();
146 FormToolkit toolkit = managedForm.getToolkit();
147 GridLayout layout = new GridLayout();
148 layout.marginWidth = 0;
149 layout.marginHeight = 0;
150 form.getBody().setLayout(layout);
151 sashForm = new MDSashForm(form.getBody(), DWT.NULL);
152 sashForm.setData("form", cast(Object)managedForm); //$NON-NLS-1$
153 toolkit.adapt(sashForm, false, false);
154 sashForm.setMenu(form.getBody().getMenu());
155 sashForm.setLayoutData(new GridData(GridData.FILL_BOTH));
156 createMasterPart(managedForm, sashForm);
157 createDetailsPart(managedForm, sashForm);
158 hookResizeListener();
159 createToolBarActions(managedForm);
160 form.updateToolBar();
161 }
162
163 private void hookResizeListener() {
164 Listener listener = (cast(MDSashForm)sashForm).listener;
165 Control [] children = sashForm.getChildren();
166 for (int i=0; i<children.length; i++) {
167 if (null !is cast(Sash)children[i] ) continue;
168 children[i].addListener(DWT.Resize, listener);
169 }
170 }
171
172 /**
173 * Implement this method to create a master part in the provided parent.
174 * Typical master parts are section parts that contain tree or table viewer.
175 *
176 * @param managedForm
177 * the parent form
178 * @param parent
179 * the parent composite
180 */
181 protected abstract void createMasterPart(IManagedForm managedForm,
182 Composite parent);
183
184 /**
185 * Implement this method to statically register pages for the expected
186 * object types. This mechanism can be used when there is 1-&gt;1 mapping
187 * between object classes and details pages.
188 *
189 * @param detailsPart
190 * the details part
191 */
192 protected abstract void registerPages(DetailsPart detailsPart);
193
194 /**
195 * Implement this method to create form tool bar actions and add them to the
196 * form tool bar if desired.
197 *
198 * @param managedForm
199 * the form that owns the tool bar
200 */
201 protected abstract void createToolBarActions(IManagedForm managedForm);
202
203 private void createDetailsPart(IManagedForm mform, Composite parent) {
204 detailsPart = new DetailsPart(mform, parent, DWT.NULL);
205 mform.addPart(detailsPart);
206 registerPages(detailsPart);
207 }
208
209 private void onSashPaint(Event e) {
210 Sash sash = cast(Sash)e.widget;
211 IManagedForm form = cast(IManagedForm)sash.getParent().getData("form"); //$NON-NLS-1$
212 FormColors colors = form.getToolkit().getColors();
213 bool vertical = (sash.getStyle() & DWT.VERTICAL) !is 0;
214 GC gc = e.gc;
215 Boolean hover = cast(Boolean)sash.getData("hover"); //$NON-NLS-1$
216 gc.setBackground(colors.getColor(IFormColors.TB_BG));
217 gc.setForeground(colors.getColor(IFormColors.TB_BORDER));
218 Point size = sash.getSize();
219 if (vertical) {
220 if (hover !is null)
221 gc.fillRectangle(0, 0, size.x, size.y);
222 //else
223 //gc.drawLine(1, 0, 1, size.y-1);
224 }
225 else {
226 if (hover !is null)
227 gc.fillRectangle(0, 0, size.x, size.y);
228 //else
229 //gc.drawLine(0, 1, size.x-1, 1);
230 }
231 }
232 }