]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.eclipse.swt.win32.win32.x86_64/src/org/eclipse/swt/custom/ControlEditor.java
Remove invalid SHA-256-Digests
[simantics/platform.git] / bundles / org.eclipse.swt.win32.win32.x86_64 / src / org / eclipse / swt / custom / ControlEditor.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2016 IBM Corporation and others.
3  *
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/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     IBM Corporation - initial API and implementation
13  *******************************************************************************/
14 package org.eclipse.swt.custom;
15
16
17 import org.eclipse.swt.*;
18 import org.eclipse.swt.graphics.*;
19 import org.eclipse.swt.widgets.*;
20
21 /**
22 *
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
27 * above a control.
28 *
29 * <p> Here is an example of using a ControlEditor:</p>
30 *
31 * <pre><code>
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
39 * // of the canvas.
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);
45 *               dialog.open();
46 *               RGB rgb = dialog.getRGB();
47 *               if (rgb != null) {
48 *                       if (color != null) color.dispose();
49 *                       color = new Color(null, rgb);
50 *                       canvas.setBackground(color);
51 *               }
52 *
53 *       }
54 * });
55 *
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);
64 * </code></pre>
65 *
66 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
67 */
68 public class ControlEditor {
69
70         /**
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.
73         */
74         public int horizontalAlignment = SWT.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 boolean 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 SWT.TOP, SWT.BOTTOM and SWT.CENTER.  The default value is SWT.CENTER.
93         */
94         public int verticalAlignment = SWT.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 boolean 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 boolean hadFocus;
113         private Listener controlListener;
114         private Listener scrollbarListener;
115
116         private final static int [] EVENTS = {SWT.KeyDown, SWT.KeyUp, SWT.MouseDown, SWT.MouseUp, SWT.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 ControlEditor (Composite parent) {
124         this.parent = parent;
125
126         controlListener = e -> layout ();
127         for (int i=0; i<EVENTS.length; i++) {
128                 parent.addListener (EVENTS [i], controlListener);
129         }
130
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);
136 }
137 Rectangle computeBounds () {
138         Rectangle clientArea = parent.getClientArea();
139         Rectangle editorRect = new Rectangle(clientArea.x, clientArea.y, minimumWidth, minimumHeight);
140
141         if (grabHorizontal)
142                 editorRect.width = Math.max(clientArea.width, minimumWidth);
143
144         if (grabVertical)
145                 editorRect.height = Math.max(clientArea.height, minimumHeight);
146
147         switch (horizontalAlignment) {
148                 case SWT.RIGHT:
149                         editorRect.x += clientArea.width - editorRect.width;
150                         break;
151                 case SWT.LEFT:
152                         // do nothing - clientArea.x is the right answer
153                         break;
154                 default:
155                         // default is CENTER
156                         editorRect.x += (clientArea.width - editorRect.width)/2;
157         }
158
159         switch (verticalAlignment) {
160                 case SWT.BOTTOM:
161                         editorRect.y += clientArea.height - editorRect.height;
162                         break;
163                 case SWT.TOP:
164                         // do nothing - clientArea.y is the right answer
165                         break;
166                 default :
167                         // default is CENTER
168                         editorRect.y += (clientArea.height - editorRect.height)/2;
169         }
170
171
172         return editorRect;
173
174 }
175 /**
176  * Removes all associations between the Editor and the underlying composite.  The
177  * composite and the editor Control are <b>not</b> disposed.
178  */
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);
183                 }
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);
188         }
189
190         parent = null;
191         editor = null;
192         hadFocus = false;
193         controlListener = null;
194         scrollbarListener = null;
195 }
196 /**
197 * Returns the Control that is displayed above the composite being edited.
198 *
199 * @return the Control that is displayed above the composite being edited
200 */
201 public Control getEditor () {
202         return editor;
203 }
204 /**
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.
208  *
209  * @since 2.1
210  */
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 ());
219         if (hadFocus) {
220                 if (editor == null || editor.isDisposed()) return;
221                 editor.setFocus ();
222         }
223 }
224 void scroll (Event e) {
225         if (editor == null || editor.isDisposed()) return;
226         layout();
227 }
228 /**
229 * Specify the Control that is to be displayed.
230 *
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.
233 *
234 * @param editor the Control that is displayed above the composite being edited
235 */
236 public void setEditor (Control editor) {
237
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
241                 this.editor = null;
242                 return;
243         }
244
245         this.editor = editor;
246         layout();
247         if (this.editor == null || this.editor.isDisposed()) return;
248         editor.setVisible(true);
249 }
250 }