1 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 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 shell.addListener(SWT.Deactivate, new Listener() { 51 public void handleEvent(Event e){ 52 shell.setVisible (false); 53 }; 54 }); 55 56 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 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 open (Rectangle rect) { 88 89 Point listSize = getList().computeSize (rect.width, SWT.DEFAULT); 90 Rectangle screenSize = getShell().getDisplay().getBounds(); 91 92 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 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 if (listSize.y > spaceBelow){ 109 listSize.y = spaceBelow; 110 } else { 111 listSize.y += 2; 112 } 113 y = rect.y + rect.height; 114 } 115 116 listSize.x = rect.width; 118 if (listSize.x < getMinimumWidth()) 120 listSize.x = getMinimumWidth(); 121 122 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 result = null; 147 if (!shell.isDisposed ()) { 148 String [] 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 [] strings) { 156 list.setItems(strings); 157 } 158 163 public void setMinimumWidth (int width) { 164 minimumWidth = width; 165 } 166 167 170 public List getList() { 171 return list; 172 } 173 174 177 public Shell getShell() { 178 return shell; 179 } 180 181 184 public void setList(List list) { 185 this.list = list; 186 } 187 188 191 public void setShell(Shell shell) { 192 this.shell = shell; 193 } 194 195 198 public String getSelected() { 199 return selected; 200 } 201 202 205 public void setSelected(String string) { 206 selected = string; 207 } 208 209 212 public int getMinimumWidth() { 213 return minimumWidth; 214 } 215 216 } 217 | Popular Tags |