Skip to content

Package: PopupWindow

PopupWindow

nameinstructionbranchcomplexitylinemethod
PopupWindow(Control, int)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
PopupWindow(Control, int, int)
M: 18 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
PopupWindow(Control, int, int, boolean)
M: 18 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
close()
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
configurePopupWindow(Control, int, boolean)
M: 70 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 16 C: 0
0%
M: 1 C: 0
0%
getContent()
M: 20 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
open()
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2016 EclipseSource Muenchen GmbH and others.
3: *
4: * All rights reserved. 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: * Alexandra Buzila- initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emfforms.spi.swt.core.util;
15:
16: import org.eclipse.core.runtime.Assert;
17: import org.eclipse.swt.SWT;
18: import org.eclipse.swt.graphics.Point;
19: import org.eclipse.swt.graphics.Rectangle;
20: import org.eclipse.swt.layout.FillLayout;
21: import org.eclipse.swt.layout.GridLayout;
22: import org.eclipse.swt.widgets.Composite;
23: import org.eclipse.swt.widgets.Control;
24: import org.eclipse.swt.widgets.Shell;
25:
26: /**
27: * Control used for displaying a popup shell.
28: *
29: * @author Alexandra Buzila
30: * @since 1.10
31: *
32: */
33: public class PopupWindow {
34:         private Composite content;
35:         private final Shell shell;
36:
37:         /**
38:          * Creates a new resizable popup window, with size and location relative to the reference control.
39:          *
40:          * @param control the reference control
41:          * @param maxHeight the maxHeight of the window
42:          */
43:         public PopupWindow(final Control control, int maxHeight) {
44:                 this(control, maxHeight, SWT.RESIZE);
45:         }
46:
47:         /**
48:          * Creates a new resizable popup window, with size and location relative to the reference control.
49:          *
50:          * @param control the reference control
51:          * @param maxHeight the maximum height of the window
52:          * @param style the style of the window
53:          */
54:         public PopupWindow(final Control control, int maxHeight, int style) {
55:                 Assert.isNotNull(control);
56:                 shell = new Shell(control.getShell(), style);
57:                 configurePopupWindow(control, maxHeight, false);
58:         }
59:
60:         /**
61:          * Creates a new resizable popup window, with size and location relative to the reference control.
62:          *
63:          * @param control the reference control
64:          * @param maxHeight the maximum height of the window
65:          * @param style the style of the window
66:          * @param stretchUp If the window does not fit on screen vertically, it will stretch up to reach the maxHeight
67:          */
68:         public PopupWindow(final Control control, int maxHeight, int style, boolean stretchUp) {
69:                 Assert.isNotNull(control);
70:                 shell = new Shell(control.getShell(), style);
71:                 configurePopupWindow(control, maxHeight, stretchUp);
72:         }
73:
74:         /**
75:          * Configures the site, layout and location of the popup.
76:          *
77:          * @param control the reference control
78:          * @param maxHeight the maximum height of the window
79:          * @param stretchUp If the window does not fit on screen vertically, it will stretch up to reach the maxHeight
80:          */
81:         protected void configurePopupWindow(final Control control, int maxHeight, boolean stretchUp) {
82:                 final Point location = control.toDisplay(0, 0);
83:                 final Shell parentShell = control.getShell();
84:                 final Rectangle clientArea = control.getShell().getClientArea();
85:                 final Point bottomRight = parentShell.toDisplay(clientArea.width, clientArea.height);
86:                 final int distanceToScreenBottom = bottomRight.y - location.y;
87:                 int verticalMoveUp = 0;
88:                 int shellHeight = maxHeight;
89:•                if (distanceToScreenBottom < maxHeight) {
90:•                        if (!stretchUp) {
91:                                 shellHeight = Math.min(maxHeight, distanceToScreenBottom);
92:                         } else {
93:                                 verticalMoveUp = maxHeight - distanceToScreenBottom;
94:                         }
95:
96:                 }
97:                 shell.setSize(control.getSize().x, shellHeight);
98:                 shell.setLayout(new FillLayout());
99:                 shell.setLocation(location.x - 4, location.y - 4 - verticalMoveUp);// compensate for shell's margins
100:         }
101:
102:         /**
103:          * Returns the content {@link Composite} of the popup, which clients may use.
104:          *
105:          * @return the content {@link Composite}.
106:          */
107:         public Composite getContent() {
108:•                if (content == null) {
109:                         content = new Composite(shell, SWT.NONE);
110:                         content.setLayout(new GridLayout());
111:                 }
112:                 return content;
113:         }
114:
115:         /**
116:          * Opens the popup window.
117:          *
118:          * @see Shell#open()
119:          */
120:         public void open() {
121:                 shell.open();
122:         }
123:
124:         /**
125:          * Closes the popup window.
126:          *
127:          * @see Shell#close()
128:          */
129:         public void close() {
130:                 shell.close();
131:         }
132: }