1 11 package org.eclipse.swt.custom; 12 13 import org.eclipse.swt.*; 14 import org.eclipse.swt.events.*; 15 import org.eclipse.swt.graphics.*; 16 import org.eclipse.swt.widgets.*; 17 26 public class PopupList { 27 Shell shell; 28 List list; 29 int minimumWidth; 30 35 public PopupList(Shell parent) { 36 this (parent, 0); 37 } 38 46 public PopupList(Shell parent, int style) { 47 shell = new Shell(parent, checkStyle(style)); 48 49 list = new List(shell, SWT.SINGLE | SWT.V_SCROLL); 50 51 shell.addListener(SWT.Deactivate, new Listener() { 53 public void handleEvent(Event e){ 54 shell.setVisible (false); 55 } 56 }); 57 58 shell.addControlListener(new ControlListener() { 60 public void controlMoved(ControlEvent e){} 61 public void controlResized(ControlEvent e){ 62 Rectangle shellSize = shell.getClientArea(); 63 list.setSize(shellSize.width, shellSize.height); 64 } 65 }); 66 67 list.addMouseListener(new MouseListener() { 69 public void mouseDoubleClick(MouseEvent e){} 70 public void mouseDown(MouseEvent e){} 71 public void mouseUp(MouseEvent e){ 72 shell.setVisible (false); 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 private static int checkStyle (int style) { 86 int mask = SWT.LEFT_TO_RIGHT | SWT.RIGHT_TO_LEFT; 87 return style & mask; 88 } 89 99 public Font getFont () { 100 return list.getFont(); 101 } 102 115 public String [] getItems () { 116 return list.getItems(); 117 } 118 123 public int getMinimumWidth () { 124 return minimumWidth; 125 } 126 134 public String open (Rectangle rect) { 135 136 Point listSize = list.computeSize (rect.width, SWT.DEFAULT, false); 137 Rectangle screenSize = shell.getDisplay().getBounds(); 138 139 int spaceBelow = screenSize.height - (rect.y + rect.height) - 30; 141 int spaceAbove = rect.y - 30; 142 143 int y = 0; 144 if (spaceAbove > spaceBelow && listSize.y > spaceBelow) { 145 if (listSize.y > spaceAbove){ 147 listSize.y = spaceAbove; 148 } else { 149 listSize.y += 2; 150 } 151 y = rect.y - listSize.y; 152 153 } else { 154 if (listSize.y > spaceBelow){ 156 listSize.y = spaceBelow; 157 } else { 158 listSize.y += 2; 159 } 160 y = rect.y + rect.height; 161 } 162 163 listSize.x = rect.width; 165 if (listSize.x < minimumWidth) 167 listSize.x = minimumWidth; 168 169 int x = rect.x + rect.width - listSize.x; 171 172 shell.setBounds(x, y, listSize.x, listSize.y); 173 174 shell.open(); 175 list.setFocus(); 176 177 Display display = shell.getDisplay(); 178 while (!shell.isDisposed () && shell.isVisible ()) { 179 if (!display.readAndDispatch()) display.sleep(); 180 } 181 182 String result = null; 183 if (!shell.isDisposed ()) { 184 String [] strings = list.getSelection (); 185 shell.dispose(); 186 if (strings.length != 0) result = strings [0]; 187 } 188 return result; 189 } 190 204 public void select(String string) { 205 String [] items = list.getItems(); 206 207 if (string != null){ 210 for (int i = 0; i < items.length; i++) { 211 if (items[i].startsWith(string)){ 212 int index = list.indexOf(items[i]); 213 list.select(index); 214 break; 215 } 216 } 217 } 218 } 219 232 public void setFont (Font font) { 233 list.setFont(font); 234 } 235 257 public void setItems (String [] strings) { 258 list.setItems(strings); 259 } 260 265 public void setMinimumWidth (int width) { 266 if (width < 0) 267 SWT.error(SWT.ERROR_INVALID_ARGUMENT); 268 269 minimumWidth = width; 270 } 271 } 272 | Popular Tags |