X Tutup
Skip to content

Commit b17e8d5

Browse files
author
jossonsmith
committed
Improve Combo popup box: Adjust the popup according to the screen constrainted bounds.
1 parent 54131f1 commit b17e8d5

File tree

3 files changed

+204
-0
lines changed

3 files changed

+204
-0
lines changed

tests/net.sf.j2s.test.swt/src/net/sf/j2s/test/swt/ajunit/ComboTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ public void testSetSpinnerSize() {
4141
final Combo spn2 = new Combo(shell, SWT.BORDER);
4242
spn2.setLayoutData(new GridData(150, 50));
4343
final Combo spn3 = new Combo(shell, SWT.NONE);
44+
spn3.setItems(new String[] {"Helo", "Wrld", "Wrld", "Woooooooooorld", "Wrld", "Wrld", "Wrld", "Woooooooooorld", "Wrld", "Wrld", "Wrld", "Woooooooooorld", "Wrld", "Wrld", "Wrld", "Woooooooooorld", "Wrld", "Wrld", "Wrld", "Woooooooooorld", "Wrld"});
45+
spn3.setVisibleItemCount(spn3.getItemCount());
4446
spn3.setLayoutData(new GridData(120, 30));
4547
final Combo spn0 = new Combo(shell, SWT.NONE);
4648
spn0.setLayoutData(new GridData(120, 24));
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*******************************************************************************
2+
* Java2Script Pacemaker (http://j2s.sourceforge.net)
3+
*
4+
* Copyright (c) 2006 ognize.com and others.
5+
* All rights reserved. This program and the accompanying materials
6+
* are made available under the terms of the Eclipse Public License v1.0
7+
* which accompanies this distribution, and is available at
8+
* http://www.eclipse.org/legal/epl-v10.html
9+
*
10+
* Contributors:
11+
* ognize.com - initial API and implementation
12+
*******************************************************************************/
13+
14+
package net.sf.j2s.test.swt.os;
15+
16+
import org.eclipse.swt.graphics.Rectangle;
17+
18+
/**
19+
* To be copied as class org.eclipse.swt.internal.browser.Popup
20+
*
21+
* @author josson smith
22+
*
23+
* 2006-9-12
24+
*/
25+
public class Popup {
26+
27+
/**
28+
* Popup a given height box near the given rectangle box in the constrainted bounds.
29+
* The popup box is in the same width of give rectangle box.
30+
*
31+
* @param bounds
32+
* @param rect
33+
* @param height
34+
* @return
35+
*/
36+
public static Rectangle popupList(Rectangle bounds, Rectangle rect, int height) {
37+
if (height <= 0) {
38+
return null;
39+
}
40+
int x, y, w, h = height;
41+
if (bounds == null) {
42+
if (rect == null) {
43+
x = y = 0;
44+
w = 100;
45+
} else {
46+
x = rect.x;
47+
y = rect.y + height;
48+
w = rect.width;
49+
}
50+
} else {
51+
if (rect == null) {
52+
x = bounds.x + bounds.width / 4;
53+
y = bounds.y + (bounds.height - height) / 2;
54+
w = bounds.width / 2;
55+
} else {
56+
x = rect.x;
57+
w = rect.width;
58+
if (rect.y + rect.height + height > bounds.y + bounds.height) {
59+
if (rect.y - height >= bounds.y) {
60+
y = rect.y - height;
61+
} else {
62+
if (bounds.height < height) {
63+
y = bounds.y;
64+
h = bounds.height;
65+
} else {
66+
if (Math.abs(bounds.y + bounds.height - height - rect.y) > Math.abs(bounds.y + height - rect.y - rect.height)) {
67+
y = bounds.y;
68+
} else {
69+
y = bounds.y + bounds.height - height;
70+
}
71+
}
72+
}
73+
} else {
74+
y = rect.y + rect.height;
75+
}
76+
}
77+
}
78+
return new Rectangle(x, y, w, h);
79+
}
80+
81+
}
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/*******************************************************************************
2+
* Java2Script Pacemaker (http://j2s.sourceforge.net)
3+
*
4+
* Copyright (c) 2006 ognize.com and others.
5+
* All rights reserved. This program and the accompanying materials
6+
* are made available under the terms of the Eclipse Public License v1.0
7+
* which accompanies this distribution, and is available at
8+
* http://www.eclipse.org/legal/epl-v10.html
9+
*
10+
* Contributors:
11+
* ognize.com - initial API and implementation
12+
*******************************************************************************/
13+
14+
package net.sf.j2s.test.swt.os;
15+
16+
import org.eclipse.swt.graphics.Rectangle;
17+
import junit.framework.TestCase;
18+
19+
/**
20+
* @author josson smith
21+
*
22+
* 2006-9-12
23+
*/
24+
public class PopupTest extends TestCase {
25+
26+
/*
27+
* Test popup for the normal condition
28+
*/
29+
public void testComboPopupNormal() {
30+
Rectangle rect = new Rectangle(10, 10, 200, 20);
31+
Rectangle bounds = new Rectangle(0, 0, 640, 480);
32+
int height = 40;
33+
Rectangle pos = Popup.popupList(bounds, rect, height);
34+
assertEquals(pos, new Rectangle(10, 30, 200, 40));
35+
}
36+
37+
/*
38+
* Test popup when it's necessary to popup above the given
39+
* rectangle.
40+
*/
41+
public void testComboPopupAbove() {
42+
Rectangle rect = new Rectangle(10, 420, 200, 20);
43+
Rectangle bounds = new Rectangle(0, 0, 640, 480);
44+
int height = 40;
45+
Rectangle pos = Popup.popupList(bounds, rect, height);
46+
assertEquals(pos, new Rectangle(10, 440, 200, 40));
47+
48+
rect = new Rectangle(10, 421, 200, 20);
49+
//bounds = new Rectangle(0, 0, 640, 480);
50+
height = 40;
51+
pos = Popup.popupList(bounds, rect, height);
52+
assertEquals(pos, new Rectangle(10, 381, 200, 40));
53+
}
54+
55+
/*
56+
* Test popup when it's necessary to popup override the given
57+
* rectangle.
58+
*/
59+
public void testComboPopupOver() {
60+
Rectangle rect = new Rectangle(10, 300, 200, 20);
61+
Rectangle bounds = new Rectangle(0, 0, 480, 640);
62+
int height = 320;
63+
Rectangle pos = Popup.popupList(bounds, rect, height);
64+
assertEquals(pos, new Rectangle(10, 320, 200, 320));
65+
66+
rect = new Rectangle(10, 300, 200, 20);
67+
//bounds = new Rectangle(0, 0, 640, 480);
68+
height = 340;
69+
pos = Popup.popupList(bounds, rect, height);
70+
assertEquals(pos, new Rectangle(10, 300, 200, 340));
71+
72+
rect = new Rectangle(10, 300, 200, 20);
73+
//bounds = new Rectangle(0, 0, 640, 480);
74+
height = 640;
75+
pos = Popup.popupList(bounds, rect, height);
76+
assertEquals(pos, new Rectangle(10, 0, 200, 640));
77+
78+
rect = new Rectangle(10, 300, 200, 20);
79+
//bounds = new Rectangle(0, 0, 640, 480);
80+
height = 650;
81+
pos = Popup.popupList(bounds, rect, height);
82+
assertEquals(pos, new Rectangle(10, 0, 200, 640));
83+
}
84+
85+
public void testComboPopupUpOrDown() {
86+
Rectangle rect = new Rectangle(10, 300, 200, 20);
87+
Rectangle bounds = new Rectangle(0, 0, 480, 640);
88+
int height = 0;
89+
Rectangle pos = null;
90+
91+
rect = new Rectangle(10, 300, 200, 20);
92+
//bounds = new Rectangle(0, 0, 640, 480);
93+
height = 440;
94+
pos = Popup.popupList(bounds, rect, height);
95+
assertEquals(pos, new Rectangle(10, 200, 200, 440));
96+
97+
rect = new Rectangle(10, 360, 200, 20);
98+
//bounds = new Rectangle(0, 0, 640, 480);
99+
height = 440;
100+
pos = Popup.popupList(bounds, rect, height);
101+
assertEquals(pos, new Rectangle(10, 0, 200, 440));
102+
103+
rect = new Rectangle(10, 310, 200, 20);
104+
//bounds = new Rectangle(0, 0, 640, 480);
105+
height = 440;
106+
pos = Popup.popupList(bounds, rect, height);
107+
assertEquals(pos, new Rectangle(10, 200, 200, 440));
108+
109+
rect = new Rectangle(10, 311, 200, 20);
110+
//bounds = new Rectangle(0, 0, 640, 480);
111+
height = 440;
112+
pos = Popup.popupList(bounds, rect, height);
113+
assertEquals(pos, new Rectangle(10, 0, 200, 440));
114+
115+
rect = new Rectangle(10, 309, 200, 20);
116+
//bounds = new Rectangle(0, 0, 640, 480);
117+
height = 440;
118+
pos = Popup.popupList(bounds, rect, height);
119+
assertEquals(pos, new Rectangle(10, 200, 200, 440));
120+
}
121+
}

0 commit comments

Comments
 (0)
X Tutup