1 85 86 package swingwtx.swing; 87 88 import java.util.Vector ; 89 90 import org.eclipse.swt.widgets.Button; 91 92 import swingwt.awt.event.ActionEvent; 93 import swingwt.awt.event.ActionListener; 94 import swingwt.awt.event.ItemEvent; 95 import swingwt.awt.event.ItemListener; 96 import swingwtx.swing.event.ChangeEvent; 97 import swingwtx.swing.event.ChangeListener; 98 99 110 public abstract class AbstractButton extends JComponent implements SwingConstants { 111 112 protected Button ppeer = null; 113 protected String pText = null; 114 protected ButtonModel pModel; 115 protected ButtonGroup pGroup; 116 protected char pMnemonic = ' '; 117 protected boolean pSelection = false; 118 protected int pHTextPosition = LEFT; 119 protected int pVTextPosition = TOP; 120 protected int pHAlign = LEFT; 121 protected int pVAlign = TOP; 122 123 protected Icon pIcon = null; 124 protected Action pAction = null; 125 126 protected Vector changeListeners = new Vector (); 127 protected Vector itemListeners = new Vector (); 128 129 public void setAction(Action a) { 130 if (a == null || a.equals(pAction)) return; 131 132 if (pAction != null) { removeActionListener(pAction); } 133 if (a instanceof ActionListener) { addActionListener(a); } 134 135 if (a.getValue(Action.SMALL_ICON) != null) 136 setIcon((Icon) a.getValue(Action.SMALL_ICON)); 137 if (a.getValue(Action.NAME) != null) 138 setText((String ) a.getValue(Action.NAME)); 139 if (a.getValue(Action.SHORT_DESCRIPTION) != null) 140 setToolTipText((String ) a.getValue(Action.SHORT_DESCRIPTION)); 141 if (a.getValue(Action.MNEMONIC_KEY) != null) 142 setMnemonic(((Integer ) a.getValue(Action.MNEMONIC_KEY)).intValue()); 143 144 149 setEnabled(a.isEnabled()); 150 pAction = a; 151 } 152 153 160 protected void setAction(Action a, boolean addAsListener) { 161 setAction(a); 162 removeActionListener(a); 163 } 164 165 public Action getAction() { return pAction; } 166 167 public void setIcon(Icon icon) {} 168 public void setPressedIcon(Icon icon) {} 169 public void setRolloverIcon(Icon icon) {} 170 public void setRolloverSelectedIcon(Icon icon) {} 171 public void setDisabledIcon(Icon icon) {} 172 public void setSelectedIcon(Icon icon) {} 173 174 public void setBorderPainted(boolean b) {} 175 public void setFocusPainted(boolean b) {} 176 public void setContentAreaFilled(boolean b) {} 177 178 public void addChangeListener(ChangeListener l) { 179 changeListeners.add(l); 180 } 181 182 public void removeChangeListener(ChangeListener l) { 183 changeListeners.remove(l); 184 } 185 186 187 public void addItemListener(ItemListener l) { 188 itemListeners.add(l); 189 } 190 191 public void removeItemListener(ItemListener l) { 192 itemListeners.remove(l); 193 } 194 195 196 197 protected void registerActionEvents() { 198 ppeer.addSelectionListener(new org.eclipse.swt.events.SelectionListener() { 200 public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) { 201 processActionEvent(0); 202 } 203 public void widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent e) {} 204 }); 205 } 206 207 public void setHorizontalAlignment(final int align) { pHAlign = align; SwingUtilities.invokeSync(new Runnable () { public void run() { if (SwingWTUtils.isSWTControlAvailable(ppeer)) ppeer.setAlignment(SwingWTUtils.translateSwingAlignmentConstant(align) | SwingWTUtils.translateSwingAlignmentConstant(pVAlign));}}); } 208 public void setVerticalAlignment(final int align) { pVAlign = align; SwingUtilities.invokeSync(new Runnable () { public void run() {if (SwingWTUtils.isSWTControlAvailable(ppeer))ppeer.setAlignment(SwingWTUtils.translateSwingAlignmentConstant(align) | SwingWTUtils.translateSwingAlignmentConstant(pHAlign));}});} 209 public int getHorizontalAlignment() { return pHAlign; } 210 public int getVerticalAlignment() { return pVAlign; } 211 public void setHorizontalTextPosition(int textpos) { setHorizontalAlignment(textpos); } 212 public void setVerticalTextPosition(int textpos) { setVerticalAlignment(textpos); } 213 public int getHorizontalTextPosition() { return getHorizontalAlignment(); } 214 public int getVerticalTextPosition() { return getVerticalAlignment(); } 215 216 219 public void processActionEvent(int id) { 220 221 ActionEvent ae = new ActionEvent(this, id, this.getActionCommand()); 222 for (int i = 0; i < actionListeners.size(); i++) { 223 ActionListener al = (ActionListener) actionListeners.get(i); 224 al.actionPerformed(ae); 225 } 226 227 ChangeEvent ce = new ChangeEvent(this); 229 for (int i = 0; i < changeListeners.size(); i++) { 230 ChangeListener cl = (ChangeListener) changeListeners.get(i); 231 cl.stateChanged(ce); 232 } 233 234 processItemEvent(); 236 } 237 238 241 public void processItemEvent() { 242 if (itemListeners.size() == 0) return; 243 boolean isSelected = pSelection; 244 if (SwingWTUtils.isSWTControlAvailable(ppeer)) isSelected = ppeer.getSelection(); 245 ItemEvent e = new ItemEvent(this, 0, this, (isSelected ? ItemEvent.SELECTED : ItemEvent.DESELECTED)); 246 247 for (int i = 0; i < itemListeners.size(); i++) { 248 ItemListener il = (ItemListener) itemListeners.get(i); 249 il.itemStateChanged(e); 250 } 251 if (isSelected && pGroup != null) { 253 pGroup.setSelected(this.getModel(), true); 254 } 255 } 256 257 public String getText() { return pText; } 258 public void setText(String text) { 259 pText = text; 260 if (pText == null) pText = ""; 261 showMnemonic(); 262 if (SwingWTUtils.isSWTControlAvailable(ppeer)) 263 SwingUtilities.invokeSync(new Runnable () { 264 public void run() { 265 ppeer.setText(pText); 266 } 267 }); 268 } 269 270 public int getMnemonic() { return pMnemonic; }; 271 public void setMnemonic(char mnemonic) { pMnemonic = mnemonic; showMnemonic(); } 272 public void setMnemonic(int mnemonic) { pMnemonic = (char) mnemonic; showMnemonic(); } 273 274 protected void showMnemonic() { 275 final Object me = this; 276 SwingUtilities.invokeSync(new Runnable () { 277 public void run() { 278 279 if (pText == null) return; 281 282 if (pText.indexOf("<") != -1) 284 pText = SwingWTUtils.removeHTML(pText); 285 286 if (pMnemonic == ' ') return; 288 String text = ( (!SwingWTUtils.isSWTControlAvailable(ppeer)) ? pText : ppeer.getText()); 289 290 if (me instanceof JToggleButton || me instanceof JButton) 293 if (pToolTipText.indexOf("(ALT+") == -1) { 294 pToolTipText += " (ALT+" + new String (new char[] { pMnemonic }).toUpperCase()+ ")"; 295 SwingUtilities.invokeSync(new Runnable () { 296 public void run() { 297 if (SwingWTUtils.isSWTControlAvailable(peer)) 298 peer.setToolTipText(pToolTipText); 299 } 300 }); 301 } 302 303 if (text.indexOf("&") != -1) { 305 text = SwingWTUtils.replace(text, "&", ""); 306 } 307 308 if (text.equals("")) { 310 pText = "&" + new String (new char[] {pMnemonic}).toLowerCase(); 311 if (SwingWTUtils.isSWTControlAvailable(ppeer)) ppeer.setText(text); 312 } 313 else { 314 String lower = text.toLowerCase(); 317 String mn = new String (new char[] { pMnemonic }).toLowerCase(); 318 int pos = lower.indexOf(mn); 319 320 if (pos != -1) { 321 text = text.substring(0, pos) + "&" + text.substring(pos, text.length()); 322 pText = text; 323 if (SwingWTUtils.isSWTControlAvailable(ppeer)) ppeer.setText(text); 324 } 325 } 326 } 327 }); 328 } 329 330 public abstract boolean isSelected(); 331 public abstract void setSelected(boolean b); 332 333 336 public void setToolTipText(final String text) { 337 pToolTipText = text; 338 339 if (this instanceof JToggleButton || this instanceof JButton) 340 if ( pMnemonic != ' ') 341 if (pToolTipText.indexOf("(ALT+") == -1) 342 pToolTipText += " (ALT+" + new String (new char[] { pMnemonic }).toUpperCase()+ ")"; 343 344 SwingUtilities.invokeSync(new Runnable () { 345 public void run() { 346 if (SwingWTUtils.isSWTControlAvailable(peer)) 347 peer.setToolTipText(pToolTipText); 348 } 349 }); 350 351 } 352 353 public void setModel(ButtonModel m) { pModel = m; } 354 public ButtonModel getModel() { return pModel; } 355 public void setGroup(ButtonGroup g) { pGroup = g; } 356 } | Popular Tags |