108
|
1 /*******************************************************************************
|
101
|
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
|
108
|
10 * Port to the D programming language:
|
|
11 * Frank Benoit <benoit@tionex.de>
|
101
|
12 *******************************************************************************/
|
|
13 module dwt.custom.ControlEditor;
|
|
14
|
|
15
|
|
16
|
|
17 import dwt.DWT;
|
|
18 import dwt.graphics.Rectangle;
|
|
19 import dwt.widgets.Composite;
|
|
20 import dwt.widgets.Control;
|
|
21 import dwt.widgets.Event;
|
|
22 import dwt.widgets.Listener;
|
|
23 import dwt.widgets.ScrollBar;
|
|
24
|
|
25 /**
|
|
26 *
|
|
27 * A ControlEditor is a manager for a Control that appears above a composite and tracks with the
|
|
28 * moving and resizing of that composite. It can be used to display one control above
|
|
29 * another control. This could be used when editing a control that does not have editing
|
|
30 * capabilities by using a text editor or for launching a dialog by placing a button
|
|
31 * above a control.
|
|
32 *
|
|
33 * <p> Here is an example of using a ControlEditor:
|
|
34 *
|
|
35 * <code><pre>
|
|
36 * Canvas canvas = new Canvas(shell, DWT.BORDER);
|
|
37 * canvas.setBounds(10, 10, 300, 300);
|
|
38 * Color color = new Color(null, 255, 0, 0);
|
|
39 * canvas.setBackground(color);
|
|
40 * ControlEditor editor = new ControlEditor (canvas);
|
|
41 * // The editor will be a button in the bottom right corner of the canvas.
|
|
42 * // When selected, it will launch a Color dialog that will change the background
|
|
43 * // of the canvas.
|
|
44 * Button button = new Button(canvas, DWT.PUSH);
|
|
45 * button.setText("Select Color...");
|
|
46 * button.addSelectionListener (new SelectionAdapter() {
|
|
47 * public void widgetSelected(SelectionEvent e) {
|
|
48 * ColorDialog dialog = new ColorDialog(shell);
|
|
49 * dialog.open();
|
|
50 * RGB rgb = dialog.getRGB();
|
|
51 * if (rgb !is null) {
|
|
52 * if (color !is null) color.dispose();
|
|
53 * color = new Color(null, rgb);
|
|
54 * canvas.setBackground(color);
|
|
55 * }
|
|
56 *
|
|
57 * }
|
|
58 * });
|
|
59 *
|
|
60 * editor.horizontalAlignment = DWT.RIGHT;
|
|
61 * editor.verticalAlignment = DWT.BOTTOM;
|
|
62 * editor.grabHorizontal = false;
|
|
63 * editor.grabVertical = false;
|
|
64 * Point size = button.computeSize(DWT.DEFAULT, DWT.DEFAULT);
|
|
65 * editor.minimumWidth = size.x;
|
|
66 * editor.minimumHeight = size.y;
|
|
67 * editor.setEditor (button);
|
|
68 * </pre></code>
|
|
69 */
|
|
70 public class ControlEditor {
|
|
71
|
|
72 /**
|
|
73 * Specifies how the editor should be aligned relative to the control. Allowed values
|
|
74 * are DWT.LEFT, DWT.RIGHT and DWT.CENTER. The default value is DWT.CENTER.
|
|
75 */
|
|
76 public int horizontalAlignment = DWT.CENTER;
|
|
77
|
|
78 /**
|
|
79 * Specifies whether the editor should be sized to use the entire width of the control.
|
|
80 * True means resize the editor to the same width as the cell. False means do not adjust
|
|
81 * the width of the editor. The default value is false.
|
|
82 */
|
|
83 public bool grabHorizontal = false;
|
|
84
|
|
85 /**
|
|
86 * Specifies the minimum width the editor can have. This is used in association with
|
|
87 * a true value of grabHorizontal. If the cell becomes smaller than the minimumWidth, the
|
|
88 * editor will not made smaller than the minimum width value. The default value is 0.
|
|
89 */
|
|
90 public int minimumWidth = 0;
|
|
91
|
|
92 /**
|
|
93 * Specifies how the editor should be aligned relative to the control. Allowed values
|
|
94 * are DWT.TOP, DWT.BOTTOM and DWT.CENTER. The default value is DWT.CENTER.
|
|
95 */
|
|
96 public int verticalAlignment = DWT.CENTER;
|
|
97
|
|
98 /**
|
|
99 * Specifies whether the editor should be sized to use the entire height of the control.
|
|
100 * True means resize the editor to the same height as the underlying control. False means do not adjust
|
|
101 * the height of the editor. The default value is false.
|
|
102 */
|
|
103 public bool grabVertical = false;
|
|
104
|
|
105 /**
|
|
106 * Specifies the minimum height the editor can have. This is used in association with
|
|
107 * a true value of grabVertical. If the control becomes smaller than the minimumHeight, the
|
|
108 * editor will not made smaller than the minimum height value. The default value is 0.
|
|
109 */
|
|
110 public int minimumHeight = 0;
|
|
111
|
|
112 Composite parent;
|
|
113 Control editor;
|
|
114 private bool hadFocus;
|
|
115 private Listener controlListener;
|
|
116 private Listener scrollbarListener;
|
|
117
|
|
118 private final static int [] EVENTS = [DWT.KeyDown, DWT.KeyUp, DWT.MouseDown, DWT.MouseUp, DWT.Resize];
|
|
119 /**
|
|
120 * Creates a ControlEditor for the specified Composite.
|
|
121 *
|
|
122 * @param parent the Composite above which this editor will be displayed
|
|
123 *
|
|
124 */
|
|
125 public this (Composite parent) {
|
|
126 this.parent = parent;
|
|
127
|
|
128 controlListener = new class() Listener {
|
|
129 public void handleEvent(Event e) {
|
|
130 layout ();
|
|
131 }
|
|
132 };
|
|
133 for (int i=0; i<EVENTS.length; i++) {
|
|
134 parent.addListener (EVENTS [i], controlListener);
|
|
135 }
|
|
136
|
|
137 scrollbarListener = new class() Listener {
|
|
138 public void handleEvent(Event e) {
|
|
139 scroll (e);
|
|
140 }
|
|
141 };
|
|
142 ScrollBar hBar = parent.getHorizontalBar ();
|
|
143 if (hBar !is null) hBar.addListener (DWT.Selection, scrollbarListener);
|
|
144 ScrollBar vBar = parent.getVerticalBar ();
|
|
145 if (vBar !is null) vBar.addListener (DWT.Selection, scrollbarListener);
|
|
146 }
|
|
147 Rectangle computeBounds () {
|
|
148 Rectangle clientArea = parent.getClientArea();
|
|
149 Rectangle editorRect = new Rectangle(clientArea.x, clientArea.y, minimumWidth, minimumHeight);
|
|
150
|
|
151 if (grabHorizontal)
|
|
152 editorRect.width = Math.max(clientArea.width, minimumWidth);
|
|
153
|
|
154 if (grabVertical)
|
|
155 editorRect.height = Math.max(clientArea.height, minimumHeight);
|
|
156
|
|
157 switch (horizontalAlignment) {
|
|
158 case DWT.RIGHT:
|
|
159 editorRect.x += clientArea.width - editorRect.width;
|
|
160 break;
|
|
161 case DWT.LEFT:
|
|
162 // do nothing - clientArea.x is the right answer
|
|
163 break;
|
|
164 default:
|
|
165 // default is CENTER
|
|
166 editorRect.x += (clientArea.width - editorRect.width)/2;
|
|
167 }
|
|
168
|
|
169 switch (verticalAlignment) {
|
|
170 case DWT.BOTTOM:
|
|
171 editorRect.y += clientArea.height - editorRect.height;
|
|
172 break;
|
|
173 case DWT.TOP:
|
|
174 // do nothing - clientArea.y is the right answer
|
|
175 break;
|
|
176 default :
|
|
177 // default is CENTER
|
|
178 editorRect.y += (clientArea.height - editorRect.height)/2;
|
|
179 }
|
|
180
|
|
181
|
|
182 return editorRect;
|
|
183
|
|
184 }
|
|
185 /**
|
|
186 * Removes all associations between the Editor and the underlying composite. The
|
|
187 * composite and the editor Control are <b>not</b> disposed.
|
|
188 */
|
|
189 public void dispose () {
|
|
190 if (parent !is null && !parent.isDisposed()) {
|
|
191 for (int i=0; i<EVENTS.length; i++) {
|
|
192 parent.removeListener (EVENTS [i], controlListener);
|
|
193 }
|
|
194 ScrollBar hBar = parent.getHorizontalBar ();
|
|
195 if (hBar !is null) hBar.removeListener (DWT.Selection, scrollbarListener);
|
|
196 ScrollBar vBar = parent.getVerticalBar ();
|
|
197 if (vBar !is null) vBar.removeListener (DWT.Selection, scrollbarListener);
|
|
198 }
|
|
199
|
|
200 parent = null;
|
|
201 editor = null;
|
|
202 hadFocus = false;
|
|
203 controlListener = null;
|
|
204 scrollbarListener = null;
|
|
205 }
|
|
206 /**
|
|
207 * Returns the Control that is displayed above the composite being edited.
|
|
208 *
|
|
209 * @return the Control that is displayed above the composite being edited
|
|
210 */
|
|
211 public Control getEditor () {
|
|
212 return editor;
|
|
213 }
|
|
214 /**
|
|
215 * Lays out the control within the underlying composite. This
|
|
216 * method should be called after changing one or more fields to
|
|
217 * force the Editor to resize.
|
|
218 *
|
|
219 * @since 2.1
|
|
220 */
|
|
221 public void layout () {
|
|
222 if (editor is null || editor.isDisposed()) return;
|
|
223 if (editor.getVisible ()) {
|
|
224 hadFocus = editor.isFocusControl();
|
|
225 } // this doesn't work because
|
|
226 // resizing the column takes the focus away
|
|
227 // before we get here
|
|
228 editor.setBounds (computeBounds ());
|
|
229 if (hadFocus) {
|
|
230 if (editor is null || editor.isDisposed()) return;
|
|
231 editor.setFocus ();
|
|
232 }
|
|
233 }
|
|
234 void scroll (Event e) {
|
|
235 if (editor is null || editor.isDisposed()) return;
|
|
236 layout();
|
|
237 }
|
|
238 /**
|
|
239 * Specify the Control that is to be displayed.
|
|
240 *
|
|
241 * <p>Note: The Control provided as the editor <b>must</b> be created with its parent
|
|
242 * being the Composite specified in the ControlEditor constructor.
|
|
243 *
|
|
244 * @param editor the Control that is displayed above the composite being edited
|
|
245 */
|
|
246 public void setEditor (Control editor) {
|
|
247
|
|
248 if (editor is null) {
|
|
249 // this is the case where the caller is setting the editor to be blank
|
|
250 // set all the values accordingly
|
|
251 this.editor = null;
|
|
252 return;
|
|
253 }
|
|
254
|
|
255 this.editor = editor;
|
|
256 layout();
|
|
257 if (this.editor is null || this.editor.isDisposed()) return;
|
|
258 editor.setVisible(true);
|
|
259 }
|
|
260 }
|