KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ca > mcgill > sable > soot > ui > PopupListSelector


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 2003 Jennifer Lhotak
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */

19
20
21 package ca.mcgill.sable.soot.ui;
22
23 import org.eclipse.swt.SWT;
24 import org.eclipse.swt.events.*;
25 import org.eclipse.swt.graphics.Color;
26 import org.eclipse.swt.graphics.Point;
27 import org.eclipse.swt.graphics.RGB;
28 import org.eclipse.swt.graphics.Rectangle;
29 import org.eclipse.swt.widgets.*;
30
31 import ca.mcgill.sable.soot.SootPlugin;
32
33
34 public class PopupListSelector {
35     private Shell shell;
36     private List list;
37     private String JavaDoc selected;
38     private int minimumWidth;
39     
40
41     public PopupListSelector(Shell parent){
42     
43         shell = new Shell(parent, 0);
44     
45         list = new List(shell, SWT.SINGLE | SWT.V_SCROLL);
46         list.setBackground(SootPlugin.getDefault().getColorManager().getColor(new RGB(255, 255, 255)));
47         list.setFont(SootPlugin.getDefault().getSootFont());
48         
49         // close dialog if user selects outside of the shell
50
shell.addListener(SWT.Deactivate, new Listener() {
51             public void handleEvent(Event e){
52                 shell.setVisible (false);
53             };
54         });
55         
56         // resize shell when list resizes
57
shell.addControlListener(new ControlListener() {
58             public void controlMoved(ControlEvent e){}
59             public void controlResized(ControlEvent e){
60                 Rectangle shellSize = shell.getClientArea();
61                 list.setSize(shellSize.width, shellSize.height);
62             }
63         });
64         
65         // return list selection on Mouse Up or Carriage Return
66
list.addMouseListener(new MouseListener() {
67             public void mouseDoubleClick(MouseEvent e){};
68             public void mouseDown(MouseEvent e){};
69             public void mouseUp(MouseEvent e){
70                 setSelected(list.getSelection()[0]);
71                 shell.setVisible (false);
72                 };
73             });
74             
75         list.addKeyListener(new KeyListener() {
76             public void keyReleased(KeyEvent e){};
77             public void keyPressed(KeyEvent e){
78                 if (e.character == '\r'){
79                     shell.setVisible (false);
80                 }
81             };
82         });
83         
84         
85     }
86     
87     public String JavaDoc open (Rectangle rect) {
88
89         Point listSize = getList().computeSize (rect.width, SWT.DEFAULT);
90         Rectangle screenSize = getShell().getDisplay().getBounds();
91
92         // Position the dialog so that it does not run off the screen and the largest number of items are visible
93
int spaceBelow = screenSize.height - (rect.y + rect.height) - 30;
94         int spaceAbove = rect.y - 30;
95
96         int y = 0;
97         if (spaceAbove > spaceBelow && listSize.y > spaceBelow) {
98             // place popup list above table cell
99
if (listSize.y > spaceAbove){
100                 listSize.y = spaceAbove;
101             } else {
102                 listSize.y += 2;
103             }
104             y = rect.y - listSize.y;
105         
106         } else {
107             // place popup list below table cell
108
if (listSize.y > spaceBelow){
109                 listSize.y = spaceBelow;
110             } else {
111                 listSize.y += 2;
112             }
113             y = rect.y + rect.height;
114         }
115     
116         // Make dialog as wide as the cell
117
listSize.x = rect.width;
118         // dialog width should not be les than minimumwidth
119
if (listSize.x < getMinimumWidth())
120             listSize.x = getMinimumWidth();
121     
122         // Align right side of dialog with right side of cell
123
int x = rect.x + rect.width - listSize.x;
124         y = 0;
125         if (spaceAbove <= spaceBelow){
126             y = spaceAbove + rect.y;
127         }
128         else {
129             y = rect.y - spaceBelow;
130         }
131         
132         y = (rect.y * 16) + 85;
133     
134     
135         getShell().setBounds(rect.x, y, listSize.x, listSize.y);
136     
137         shell.open();
138         list.setFocus();
139         
140
141         Display display = shell.getDisplay();
142         while (!shell.isDisposed () && shell.isVisible ()) {
143             if (!display.readAndDispatch()) display.sleep();
144         }
145     
146         String JavaDoc result = null;
147         if (!shell.isDisposed ()) {
148             String JavaDoc [] strings = list.getSelection ();
149             shell.dispose();
150             if (strings.length != 0) result = strings [0];
151         }
152         return result;
153     }
154     
155     public void setItems (String JavaDoc[] strings) {
156         list.setItems(strings);
157     }
158     /**
159     * Sets the minimum width of the list.
160     *
161     * @param width the minimum width of the list
162     */

163     public void setMinimumWidth (int width) {
164         minimumWidth = width;
165     }
166     
167     /**
168      * @return
169      */

170     public List getList() {
171         return list;
172     }
173
174     /**
175      * @return
176      */

177     public Shell getShell() {
178         return shell;
179     }
180
181     /**
182      * @param list
183      */

184     public void setList(List list) {
185         this.list = list;
186     }
187
188     /**
189      * @param shell
190      */

191     public void setShell(Shell shell) {
192         this.shell = shell;
193     }
194
195     /**
196      * @return
197      */

198     public String JavaDoc getSelected() {
199         return selected;
200     }
201
202     /**
203      * @param string
204      */

205     public void setSelected(String JavaDoc string) {
206         selected = string;
207     }
208
209     /**
210      * @return
211      */

212     public int getMinimumWidth() {
213         return minimumWidth;
214     }
215
216 }
217
Popular Tags