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