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