KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > swt > custom > ControlEditor


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 package org.eclipse.swt.custom;
12
13
14 import org.eclipse.swt.*;
15 import org.eclipse.swt.graphics.*;
16 import org.eclipse.swt.widgets.*;
17
18 /**
19 *
20 * A ControlEditor is a manager for a Control that appears above a composite and tracks with the
21 * moving and resizing of that composite. It can be used to display one control above
22 * another control. This could be used when editing a control that does not have editing
23 * capabilities by using a text editor or for launching a dialog by placing a button
24 * above a control.
25 *
26 * <p> Here is an example of using a ControlEditor:
27 *
28 * <code><pre>
29 * Canvas canvas = new Canvas(shell, SWT.BORDER);
30 * canvas.setBounds(10, 10, 300, 300);
31 * Color color = new Color(null, 255, 0, 0);
32 * canvas.setBackground(color);
33 * ControlEditor editor = new ControlEditor (canvas);
34 * // The editor will be a button in the bottom right corner of the canvas.
35 * // When selected, it will launch a Color dialog that will change the background
36 * // of the canvas.
37 * Button button = new Button(canvas, SWT.PUSH);
38 * button.setText("Select Color...");
39 * button.addSelectionListener (new SelectionAdapter() {
40 * public void widgetSelected(SelectionEvent e) {
41 * ColorDialog dialog = new ColorDialog(shell);
42 * dialog.open();
43 * RGB rgb = dialog.getRGB();
44 * if (rgb != null) {
45 * if (color != null) color.dispose();
46 * color = new Color(null, rgb);
47 * canvas.setBackground(color);
48 * }
49 *
50 * }
51 * });
52 *
53 * editor.horizontalAlignment = SWT.RIGHT;
54 * editor.verticalAlignment = SWT.BOTTOM;
55 * editor.grabHorizontal = false;
56 * editor.grabVertical = false;
57 * Point size = button.computeSize(SWT.DEFAULT, SWT.DEFAULT);
58 * editor.minimumWidth = size.x;
59 * editor.minimumHeight = size.y;
60 * editor.setEditor (button);
61 * </pre></code>
62 */

63 public class ControlEditor {
64
65     /**
66     * Specifies how the editor should be aligned relative to the control. Allowed values
67     * are SWT.LEFT, SWT.RIGHT and SWT.CENTER. The default value is SWT.CENTER.
68     */

69     public int horizontalAlignment = SWT.CENTER;
70     
71     /**
72     * Specifies whether the editor should be sized to use the entire width of the control.
73     * True means resize the editor to the same width as the cell. False means do not adjust
74     * the width of the editor. The default value is false.
75     */

76     public boolean grabHorizontal = false;
77     
78     /**
79     * Specifies the minimum width the editor can have. This is used in association with
80     * a true value of grabHorizontal. If the cell becomes smaller than the minimumWidth, the
81     * editor will not made smaller than the minimum width value. The default value is 0.
82     */

83     public int minimumWidth = 0;
84     
85     /**
86     * Specifies how the editor should be aligned relative to the control. Allowed values
87     * are SWT.TOP, SWT.BOTTOM and SWT.CENTER. The default value is SWT.CENTER.
88     */

89     public int verticalAlignment = SWT.CENTER;
90     
91     /**
92     * Specifies whether the editor should be sized to use the entire height of the control.
93     * True means resize the editor to the same height as the underlying control. False means do not adjust
94     * the height of the editor. The default value is false.
95     */

96     public boolean grabVertical = false;
97     
98     /**
99     * Specifies the minimum height the editor can have. This is used in association with
100     * a true value of grabVertical. If the control becomes smaller than the minimumHeight, the
101     * editor will not made smaller than the minimum height value. The default value is 0.
102     */

103     public int minimumHeight = 0;
104
105     Composite parent;
106     Control editor;
107     private boolean hadFocus;
108     private Listener controlListener;
109     private Listener scrollbarListener;
110     
111     private final static int [] EVENTS = {SWT.KeyDown, SWT.KeyUp, SWT.MouseDown, SWT.MouseUp, SWT.Resize};
112 /**
113 * Creates a ControlEditor for the specified Composite.
114 *
115 * @param parent the Composite above which this editor will be displayed
116 *
117 */

118 public ControlEditor (Composite parent) {
119     this.parent = parent;
120
121     controlListener = new Listener() {
122         public void handleEvent(Event e) {
123             layout ();
124         }
125     };
126     for (int i=0; i<EVENTS.length; i++) {
127         parent.addListener (EVENTS [i], controlListener);
128     }
129     
130     scrollbarListener = new Listener() {
131         public void handleEvent(Event e) {
132             scroll (e);
133         }
134     };
135     ScrollBar hBar = parent.getHorizontalBar ();
136     if (hBar != null) hBar.addListener (SWT.Selection, scrollbarListener);
137     ScrollBar vBar = parent.getVerticalBar ();
138     if (vBar != null) vBar.addListener (SWT.Selection, scrollbarListener);
139 }
140 Rectangle computeBounds () {
141     Rectangle clientArea = parent.getClientArea();
142     Rectangle editorRect = new Rectangle(clientArea.x, clientArea.y, minimumWidth, minimumHeight);
143     
144     if (grabHorizontal)
145         editorRect.width = Math.max(clientArea.width, minimumWidth);
146     
147     if (grabVertical)
148         editorRect.height = Math.max(clientArea.height, minimumHeight);
149
150     switch (horizontalAlignment) {
151         case SWT.RIGHT:
152             editorRect.x += clientArea.width - editorRect.width;
153             break;
154         case SWT.LEFT:
155             // do nothing - clientArea.x is the right answer
156
break;
157         default:
158             // default is CENTER
159
editorRect.x += (clientArea.width - editorRect.width)/2;
160     }
161     
162     switch (verticalAlignment) {
163         case SWT.BOTTOM:
164             editorRect.y += clientArea.height - editorRect.height;
165             break;
166         case SWT.TOP:
167             // do nothing - clientArea.y is the right answer
168
break;
169         default :
170             // default is CENTER
171
editorRect.y += (clientArea.height - editorRect.height)/2;
172     }
173
174     
175     return editorRect;
176
177 }
178 /**
179  * Removes all associations between the Editor and the underlying composite. The
180  * composite and the editor Control are <b>not</b> disposed.
181  */

182 public void dispose () {
183     if (parent != null && !parent.isDisposed()) {
184         for (int i=0; i<EVENTS.length; i++) {
185             parent.removeListener (EVENTS [i], controlListener);
186         }
187         ScrollBar hBar = parent.getHorizontalBar ();
188         if (hBar != null) hBar.removeListener (SWT.Selection, scrollbarListener);
189         ScrollBar vBar = parent.getVerticalBar ();
190         if (vBar != null) vBar.removeListener (SWT.Selection, scrollbarListener);
191     }
192     
193     parent = null;
194     editor = null;
195     hadFocus = false;
196     controlListener = null;
197     scrollbarListener = null;
198 }
199 /**
200 * Returns the Control that is displayed above the composite being edited.
201 *
202 * @return the Control that is displayed above the composite being edited
203 */

204 public Control getEditor () {
205     return editor;
206 }
207 /**
208  * Lays out the control within the underlying composite. This
209  * method should be called after changing one or more fields to
210  * force the Editor to resize.
211  *
212  * @since 2.1
213  */

214 public void layout () {
215     if (editor == null || editor.isDisposed()) return;
216     if (editor.getVisible ()) {
217         hadFocus = editor.isFocusControl();
218     } // this doesn't work because
219
// resizing the column takes the focus away
220
// before we get here
221
editor.setBounds (computeBounds ());
222     if (hadFocus) {
223         if (editor == null || editor.isDisposed()) return;
224         editor.setFocus ();
225     }
226 }
227 void scroll (Event e) {
228     if (editor == null || editor.isDisposed()) return;
229     layout();
230 }
231 /**
232 * Specify the Control that is to be displayed.
233 *
234 * <p>Note: The Control provided as the editor <b>must</b> be created with its parent
235 * being the Composite specified in the ControlEditor constructor.
236 *
237 * @param editor the Control that is displayed above the composite being edited
238 */

239 public void setEditor (Control editor) {
240     
241     if (editor == null) {
242         // this is the case where the caller is setting the editor to be blank
243
// set all the values accordingly
244
this.editor = null;
245         return;
246     }
247     
248     this.editor = editor;
249     layout();
250     if (this.editor == null || this.editor.isDisposed()) return;
251     editor.setVisible(true);
252 }
253 }
254
Popular Tags