1 /*******************************************************************************
2 * Copyright (c) 2000, 2016 IBM Corporation and others.
4 * This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License 2.0
6 * which accompanies this distribution, and is available at
7 * https://www.eclipse.org/legal/epl-2.0/
9 * SPDX-License-Identifier: EPL-2.0
12 * IBM Corporation - initial API and implementation
13 *******************************************************************************/
14 package org.eclipse.swt.custom;
17 import org.eclipse.swt.*;
18 import org.eclipse.swt.graphics.*;
19 import org.eclipse.swt.widgets.*;
23 * A ControlEditor is a manager for a Control that appears above a composite and tracks with the
24 * moving and resizing of that composite. It can be used to display one control above
25 * another control. This could be used when editing a control that does not have editing
26 * capabilities by using a text editor or for launching a dialog by placing a button
29 * <p> Here is an example of using a ControlEditor:</p>
32 * Canvas canvas = new Canvas(shell, SWT.BORDER);
33 * canvas.setBounds(10, 10, 300, 300);
34 * Color color = new Color(null, 255, 0, 0);
35 * canvas.setBackground(color);
36 * ControlEditor editor = new ControlEditor (canvas);
37 * // The editor will be a button in the bottom right corner of the canvas.
38 * // When selected, it will launch a Color dialog that will change the background
40 * Button button = new Button(canvas, SWT.PUSH);
41 * button.setText("Select Color...");
42 * button.addSelectionListener (new SelectionAdapter() {
43 * public void widgetSelected(SelectionEvent e) {
44 * ColorDialog dialog = new ColorDialog(shell);
46 * RGB rgb = dialog.getRGB();
48 * if (color != null) color.dispose();
49 * color = new Color(null, rgb);
50 * canvas.setBackground(color);
56 * editor.horizontalAlignment = SWT.RIGHT;
57 * editor.verticalAlignment = SWT.BOTTOM;
58 * editor.grabHorizontal = false;
59 * editor.grabVertical = false;
60 * Point size = button.computeSize(SWT.DEFAULT, SWT.DEFAULT);
61 * editor.minimumWidth = size.x;
62 * editor.minimumHeight = size.y;
63 * editor.setEditor (button);
66 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
68 public class ControlEditor {
71 * Specifies how the editor should be aligned relative to the control. Allowed values
72 * are SWT.LEFT, SWT.RIGHT and SWT.CENTER. The default value is SWT.CENTER.
74 public int horizontalAlignment = SWT.CENTER;
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.
81 public boolean grabHorizontal = false;
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.
88 public int minimumWidth = 0;
91 * Specifies how the editor should be aligned relative to the control. Allowed values
92 * are SWT.TOP, SWT.BOTTOM and SWT.CENTER. The default value is SWT.CENTER.
94 public int verticalAlignment = SWT.CENTER;
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.
101 public boolean grabVertical = false;
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.
108 public int minimumHeight = 0;
112 private boolean hadFocus;
113 private Listener controlListener;
114 private Listener scrollbarListener;
116 private final static int [] EVENTS = {SWT.KeyDown, SWT.KeyUp, SWT.MouseDown, SWT.MouseUp, SWT.Resize};
118 * Creates a ControlEditor for the specified Composite.
120 * @param parent the Composite above which this editor will be displayed
123 public ControlEditor (Composite parent) {
124 this.parent = parent;
126 controlListener = e -> layout ();
127 for (int i=0; i<EVENTS.length; i++) {
128 parent.addListener (EVENTS [i], controlListener);
131 scrollbarListener = e -> scroll (e);
132 ScrollBar hBar = parent.getHorizontalBar ();
133 if (hBar != null) hBar.addListener (SWT.Selection, scrollbarListener);
134 ScrollBar vBar = parent.getVerticalBar ();
135 if (vBar != null) vBar.addListener (SWT.Selection, scrollbarListener);
137 Rectangle computeBounds () {
138 Rectangle clientArea = parent.getClientArea();
139 Rectangle editorRect = new Rectangle(clientArea.x, clientArea.y, minimumWidth, minimumHeight);
142 editorRect.width = Math.max(clientArea.width, minimumWidth);
145 editorRect.height = Math.max(clientArea.height, minimumHeight);
147 switch (horizontalAlignment) {
149 editorRect.x += clientArea.width - editorRect.width;
152 // do nothing - clientArea.x is the right answer
156 editorRect.x += (clientArea.width - editorRect.width)/2;
159 switch (verticalAlignment) {
161 editorRect.y += clientArea.height - editorRect.height;
164 // do nothing - clientArea.y is the right answer
168 editorRect.y += (clientArea.height - editorRect.height)/2;
176 * Removes all associations between the Editor and the underlying composite. The
177 * composite and the editor Control are <b>not</b> disposed.
179 public void dispose () {
180 if (parent != null && !parent.isDisposed()) {
181 for (int i=0; i<EVENTS.length; i++) {
182 parent.removeListener (EVENTS [i], controlListener);
184 ScrollBar hBar = parent.getHorizontalBar ();
185 if (hBar != null) hBar.removeListener (SWT.Selection, scrollbarListener);
186 ScrollBar vBar = parent.getVerticalBar ();
187 if (vBar != null) vBar.removeListener (SWT.Selection, scrollbarListener);
193 controlListener = null;
194 scrollbarListener = null;
197 * Returns the Control that is displayed above the composite being edited.
199 * @return the Control that is displayed above the composite being edited
201 public Control getEditor () {
205 * Lays out the control within the underlying composite. This
206 * method should be called after changing one or more fields to
207 * force the Editor to resize.
211 public void layout () {
212 if (editor == null || editor.isDisposed()) return;
213 if (editor.getVisible ()) {
214 hadFocus = editor.isFocusControl();
215 } // this doesn't work because
216 // resizing the column takes the focus away
217 // before we get here
218 editor.setBounds (computeBounds ());
220 if (editor == null || editor.isDisposed()) return;
224 void scroll (Event e) {
225 if (editor == null || editor.isDisposed()) return;
229 * Specify the Control that is to be displayed.
231 * <p>Note: The Control provided as the editor <b>must</b> be created with its parent
232 * being the Composite specified in the ControlEditor constructor.
234 * @param editor the Control that is displayed above the composite being edited
236 public void setEditor (Control editor) {
238 if (editor == null) {
239 // this is the case where the caller is setting the editor to be blank
240 // set all the values accordingly
245 this.editor = editor;
247 if (this.editor == null || this.editor.isDisposed()) return;
248 editor.setVisible(true);