Skip to content

Commit 1ce2e77

Browse files
author
jossonsmith
committed
Add popup menu support
1 parent 02813fd commit 1ce2e77

File tree

3 files changed

+355
-0
lines changed

3 files changed

+355
-0
lines changed

tests/net.sf.j2s.test.swt/src/net/sf/j2s/test/swt/os/Popup.java

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,78 @@ public static Rectangle popupList(Rectangle bounds, Rectangle rect, int height)
7878
return new Rectangle(x, y, w, h);
7979
}
8080

81+
/**
82+
* Popup a given height box near the given rectangle box in the constrainted bounds.
83+
* The popup box is in the same width of give rectangle box.
84+
*
85+
* @param bounds
86+
* @param rect
87+
* @param width
88+
* @param height
89+
* @param preferredDirection
90+
* @return
91+
*/
92+
public static Rectangle popupMenu(Rectangle bounds, Rectangle rect, int width, int height, int preferredDirection) {
93+
if (height <= 0 || width <= 0) {
94+
return null;
95+
}
96+
int x, y, w = width, h = height;
97+
if (bounds == null) {
98+
if (rect == null) {
99+
x = y = 0;
100+
//w = 100;
101+
} else {
102+
x = rect.x;
103+
y = rect.y + height;
104+
//w = rect.width;
105+
}
106+
} else {
107+
if (rect == null) {
108+
x = bounds.x + bounds.width / 4;
109+
y = bounds.y + (bounds.height - height) / 2;
110+
//w = bounds.width / 2;
111+
} else {
112+
//x = rect.x;
113+
//w = rect.width;
114+
if (rect.y + rect.height + height > bounds.y + bounds.height) {
115+
if (rect.y - height >= bounds.y) {
116+
y = rect.y - height;
117+
} else {
118+
if (bounds.height < height) {
119+
y = bounds.y;
120+
h = bounds.height;
121+
} else {
122+
if (Math.abs(bounds.y + bounds.height - height - rect.y) > Math.abs(bounds.y + height - rect.y - rect.height)) {
123+
y = bounds.y;
124+
} else {
125+
y = bounds.y + bounds.height - height;
126+
}
127+
}
128+
}
129+
} else {
130+
y = rect.y + rect.height;
131+
}
132+
133+
if (rect.x + rect.width + width > bounds.x + bounds.width) {
134+
if (rect.x - width >= bounds.x) {
135+
x = rect.x - width;
136+
} else {
137+
if (bounds.width < width) {
138+
x = bounds.x;
139+
w = bounds.width;
140+
} else {
141+
if (Math.abs(bounds.x + bounds.width - width - rect.x) > Math.abs(bounds.x + width - rect.x - rect.width)) {
142+
x = bounds.x;
143+
} else {
144+
x = bounds.x + bounds.width - width;
145+
}
146+
}
147+
}
148+
} else {
149+
x = rect.x + rect.width;
150+
}
151+
}
152+
}
153+
return new Rectangle(x, y, w, h);
154+
}
81155
}
Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
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 PopupMenuTest extends TestCase {
25+
26+
/*
27+
* Test popup for the normal condition
28+
*/
29+
public void testMenuPopupNormal() {
30+
Rectangle rect = new Rectangle(10, 10, 0, 0);
31+
Rectangle bounds = new Rectangle(0, 0, 640, 480);
32+
int width = 200;
33+
int height = 40;
34+
Rectangle pos = Popup.popupMenu(bounds, rect, width, height, 0);
35+
assertEquals(pos, new Rectangle(10, 10, 200, 40));
36+
}
37+
38+
/*
39+
* Test popup when it's necessary to popup above the given
40+
* rectangle.
41+
*/
42+
public void testMenuPopupAboveOrBefore() {
43+
Rectangle rect = new Rectangle(10, 420, 0, 0);
44+
Rectangle bounds = new Rectangle(0, 0, 640, 480);
45+
int width = 200;
46+
int height = 60;
47+
Rectangle pos = Popup.popupMenu(bounds, rect, width, height, 0);
48+
assertEquals(pos, new Rectangle(10, 420, 200, 60));
49+
50+
rect = new Rectangle(10, 421, 0, 0);
51+
//bounds = new Rectangle(0, 0, 640, 480);
52+
height = 60;
53+
pos = Popup.popupMenu(bounds, rect, width, height, 0);
54+
assertEquals(pos, new Rectangle(10, 361, 200, 60));
55+
56+
rect = new Rectangle(440, 420, 0, 0);
57+
//bounds = new Rectangle(0, 0, 640, 480);
58+
height = 60;
59+
pos = Popup.popupMenu(bounds, rect, width, height, 0);
60+
assertEquals(pos, new Rectangle(440, 420, 200, 60));
61+
62+
rect = new Rectangle(441, 420, 0, 0);
63+
//bounds = new Rectangle(0, 0, 640, 480);
64+
height = 60;
65+
pos = Popup.popupMenu(bounds, rect, width, height, 0);
66+
assertEquals(pos, new Rectangle(241, 420, 200, 60));
67+
68+
rect = new Rectangle(441, 421, 0, 0);
69+
//bounds = new Rectangle(0, 0, 640, 480);
70+
height = 60;
71+
pos = Popup.popupMenu(bounds, rect, width, height, 0);
72+
assertEquals(pos, new Rectangle(241, 361, 200, 60));
73+
}
74+
75+
/*
76+
* Test popup when it's necessary to popup override the given
77+
* rectangle.
78+
*/
79+
public void testMenuPopupOver() {
80+
Rectangle rect = new Rectangle(10, 300, 0, 0);
81+
Rectangle bounds = new Rectangle(0, 0, 480, 640);
82+
int width = 200;
83+
int height = 340;
84+
Rectangle pos = Popup.popupMenu(bounds, rect, width, height, 0);
85+
assertEquals(pos, new Rectangle(10, 300, 200, 340));
86+
87+
rect = new Rectangle(10, 300, 0, 0);
88+
//bounds = new Rectangle(0, 0, 640, 480);
89+
height = 360;
90+
pos = Popup.popupMenu(bounds, rect, width, height, 0);
91+
assertEquals(pos, new Rectangle(10, 280, 200, 360));
92+
93+
rect = new Rectangle(10, 300, 0, 0);
94+
//bounds = new Rectangle(0, 0, 640, 480);
95+
height = 640;
96+
pos = Popup.popupMenu(bounds, rect, width, height, 0);
97+
assertEquals(pos, new Rectangle(10, 0, 200, 640));
98+
99+
rect = new Rectangle(10, 300, 0, 0);
100+
//bounds = new Rectangle(0, 0, 640, 480);
101+
height = 650;
102+
pos = Popup.popupMenu(bounds, rect, width, height, 0);
103+
assertEquals(pos, new Rectangle(10, 0, 200, 640));
104+
105+
// x
106+
rect = new Rectangle(280, 300, 0, 0);
107+
bounds = new Rectangle(0, 0, 480, 640);
108+
width = 200;
109+
height = 340;
110+
pos = Popup.popupMenu(bounds, rect, width, height, 0);
111+
assertEquals(pos, new Rectangle(280, 300, 200, 340));
112+
113+
rect = new Rectangle(120, 300, 0, 0);
114+
//bounds = new Rectangle(0, 0, 640, 480);
115+
width = 380;
116+
height = 360;
117+
pos = Popup.popupMenu(bounds, rect, width, height, 0);
118+
assertEquals(pos, new Rectangle(100, 280, 380, 360));
119+
120+
rect = new Rectangle(10, 300, 0, 0);
121+
//bounds = new Rectangle(0, 0, 640, 480);
122+
width = 480;
123+
height = 640;
124+
pos = Popup.popupMenu(bounds, rect, width, height, 0);
125+
assertEquals(pos, new Rectangle(0, 0, 480, 640));
126+
127+
rect = new Rectangle(10, 300, 0, 0);
128+
//bounds = new Rectangle(0, 0, 640, 480);
129+
width = 500;
130+
height = 650;
131+
pos = Popup.popupMenu(bounds, rect, width, height, 0);
132+
assertEquals(pos, new Rectangle(0, 0, 480, 640));
133+
}
134+
135+
public void testMenuPopupUpOrDown() {
136+
Rectangle rect = new Rectangle(10, 300, 0, 0);
137+
Rectangle bounds = new Rectangle(0, 0, 480, 640);
138+
int width = 200;
139+
int height = 0;
140+
Rectangle pos = null;
141+
142+
rect = new Rectangle(10, 300, 0, 0);
143+
//bounds = new Rectangle(0, 0, 640, 480);
144+
height = 440;
145+
pos = Popup.popupMenu(bounds, rect, width, height, 0);
146+
assertEquals(pos, new Rectangle(10, 200, 200, 440));
147+
148+
rect = new Rectangle(10, 360, 0, 0);
149+
//bounds = new Rectangle(0, 0, 640, 480);
150+
height = 440;
151+
pos = Popup.popupMenu(bounds, rect, width, height, 0);
152+
assertEquals(pos, new Rectangle(10, 0, 200, 440));
153+
154+
rect = new Rectangle(10, 320, 0, 0);
155+
//bounds = new Rectangle(0, 0, 640, 480);
156+
height = 440;
157+
pos = Popup.popupMenu(bounds, rect, width, height, 0);
158+
assertEquals(pos, new Rectangle(10, 200, 200, 440));
159+
160+
rect = new Rectangle(10, 321, 0, 0);
161+
//bounds = new Rectangle(0, 0, 640, 480);
162+
height = 440;
163+
pos = Popup.popupMenu(bounds, rect, width, height, 0);
164+
assertEquals(pos, new Rectangle(10, 0, 200, 440));
165+
166+
rect = new Rectangle(10, 319, 0, 0);
167+
//bounds = new Rectangle(0, 0, 640, 480);
168+
height = 440;
169+
pos = Popup.popupMenu(bounds, rect, width, height, 0);
170+
assertEquals(pos, new Rectangle(10, 200, 200, 440));
171+
172+
// x
173+
rect = new Rectangle(200, 300, 0, 0);
174+
//bounds = new Rectangle(0, 0, 640, 480);
175+
width = 360;
176+
height = 440;
177+
pos = Popup.popupMenu(bounds, rect, width, height, 0);
178+
assertEquals(pos, new Rectangle(120, 200, 360, 440));
179+
180+
rect = new Rectangle(280, 360, 0, 0);
181+
//bounds = new Rectangle(0, 0, 640, 480);
182+
height = 440;
183+
pos = Popup.popupMenu(bounds, rect, width, height, 0);
184+
assertEquals(pos, new Rectangle(0, 0, 360, 440));
185+
186+
rect = new Rectangle(240, 320, 0, 0);
187+
//bounds = new Rectangle(0, 0, 640, 480);
188+
height = 440;
189+
pos = Popup.popupMenu(bounds, rect, width, height, 0);
190+
assertEquals(pos, new Rectangle(120, 200, 360, 440));
191+
192+
rect = new Rectangle(241, 321, 0, 0);
193+
//bounds = new Rectangle(0, 0, 640, 480);
194+
height = 440;
195+
pos = Popup.popupMenu(bounds, rect, width, height, 0);
196+
assertEquals(pos, new Rectangle(0, 0, 360, 440));
197+
198+
rect = new Rectangle(239, 319, 0, 0);
199+
//bounds = new Rectangle(0, 0, 640, 480);
200+
height = 440;
201+
pos = Popup.popupMenu(bounds, rect, width, height, 0);
202+
assertEquals(pos, new Rectangle(120, 200, 360, 440));
203+
204+
}
205+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2000, 2004 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 net.sf.j2s.test.swt.widgets;
12+
13+
/*
14+
* Menu example snippet: create a popup menu (set in multiple controls)
15+
*
16+
* For a list of all SWT example snippets see
17+
* http://www.eclipse.org/swt/snippets/
18+
*/
19+
import org.eclipse.swt.*;
20+
import org.eclipse.swt.events.SelectionAdapter;
21+
import org.eclipse.swt.events.SelectionEvent;
22+
import org.eclipse.swt.graphics.Image;
23+
import org.eclipse.swt.widgets.*;
24+
25+
public class TestPopupMenu {
26+
27+
public static void main (String [] args) {
28+
Display display = new Display ();
29+
Shell shell = new Shell (display);
30+
Composite c1 = new Composite (shell, SWT.BORDER);
31+
c1.setSize (100, 100);
32+
Composite c2 = new Composite (shell, SWT.BORDER);
33+
c2.setBounds (100, 0, 100, 100);
34+
Menu menu = new Menu (shell, SWT.POP_UP);
35+
MenuItem item = new MenuItem (menu, SWT.PUSH);
36+
item.setText ("Popup");
37+
item = new MenuItem (menu, SWT.SEPARATOR);
38+
item = new MenuItem (menu, SWT.PUSH);
39+
item.setText ("Pas&te");
40+
item.setEnabled(false);
41+
item = new MenuItem (menu, SWT.PUSH | SWT.CHECK);
42+
item.setText ("Copy\tCtrl+C");
43+
item.setSelection(true);
44+
item.setAccelerator(SWT.CTRL + 'C');
45+
item = new MenuItem (menu, SWT.CHECK);
46+
item.setText ("&Cut\tCtrl+X");
47+
final Image imageOpen = new Image(display, TestButton.class.getResourceAsStream("openFolder.gif"));
48+
item.setImage(imageOpen);
49+
item.setSelection(true);
50+
item.setAccelerator(SWT.CTRL + 'X');
51+
item = new MenuItem (menu, SWT.RADIO);
52+
item.setText ("Copy a very long string \tCtrl+C");
53+
item.setSelection(true);
54+
item.setAccelerator(SWT.CTRL + 'T');
55+
item.addSelectionListener(new SelectionAdapter() {
56+
public void widgetSelected(SelectionEvent e) {
57+
System.out.println("Copy cut");
58+
}
59+
});
60+
item = new MenuItem (menu, SWT.RADIO);
61+
item.setText ("Remove\tCtrl+Shift+X");
62+
item.setImage(null);
63+
item.setSelection(true);
64+
item.setAccelerator(SWT.CTRL + 'R');
65+
c1.setMenu (menu);
66+
c2.setMenu (menu);
67+
shell.setMenu (menu);
68+
shell.setSize (300, 300);
69+
shell.open ();
70+
while (!shell.isDisposed ()) {
71+
if (!display.readAndDispatch ()) display.sleep ();
72+
}
73+
imageOpen.dispose();
74+
display.dispose ();
75+
}
76+
}

0 commit comments

Comments
 (0)