1 14 package org.wings; 15 16 import org.apache.commons.logging.Log; 17 import org.apache.commons.logging.LogFactory; 18 import org.wings.plaf.ButtonCG; 19 20 import javax.swing.*; 21 import java.awt.event.ActionEvent ; 22 import java.awt.event.ActionListener ; 23 import java.beans.PropertyChangeEvent ; 24 import java.beans.PropertyChangeListener ; 25 26 33 public abstract class SAbstractButton 34 extends SAbstractIconTextCompound 35 implements LowLevelEventListener { 36 private final transient static Log log = LogFactory.getLog(SAbstractButton.class); 37 38 public static final String SUBMIT_BUTTON = "submit"; 39 public static final String RESET_BUTTON = "reset"; 40 public static final String IMAGE_BUTTON = "image"; 41 public static final String CHECKBOX = "checkbox"; 42 public static final String RADIOBUTTON = "radio"; 43 44 private String type = SUBMIT_BUTTON; 45 46 private SButtonGroup buttonGroup; 47 48 protected String actionCommand; 49 50 private String eventTarget; 51 52 private Action action; 53 54 private PropertyChangeListener actionPropertyChangeListener; 55 56 private String mnemonic; 57 58 59 private boolean epochCheckEnabled = true; 60 61 66 public SAbstractButton(String text) { 67 super(text); 68 } 69 70 public SAbstractButton(Action action) { 71 setAction(action); 72 } 73 74 81 public SAbstractButton(String text, String type) { 82 this(text); 83 setType(type); 84 } 85 86 89 public SAbstractButton() { 90 } 91 92 97 public void setActionCommand(String ac) { 98 actionCommand = ac; 99 } 100 101 106 public final String getActionCommand() { 107 return actionCommand; 108 } 109 110 111 116 public final SButtonGroup getGroup() { 117 return buttonGroup; 118 } 119 120 protected void setParentFrame(SFrame f) { 121 if (buttonGroup != null && getDispatcher() != null) { 122 getDispatcher().removeLowLevelEventListener(this, buttonGroup.getComponentId()); 123 } 125 super.setParentFrame(f); 126 127 if (buttonGroup != null && getDispatcher() != null) { 128 getDispatcher().addLowLevelEventListener(this, buttonGroup.getComponentId()); 129 } } 131 132 137 protected void setGroup(SButtonGroup g) { 138 if (isDifferent(buttonGroup, g)) { 139 if (buttonGroup != null && getDispatcher() != null) { 140 getDispatcher().removeLowLevelEventListener(this, buttonGroup.getComponentId()); 141 } buttonGroup = g; 143 if (buttonGroup != null && getDispatcher() != null) { 144 getDispatcher().removeLowLevelEventListener(this, buttonGroup.getComponentId()); 145 } reload(); 147 } 148 } 149 150 155 public void addActionListener(ActionListener listener) { 156 addEventListener(ActionListener .class, listener); 157 } 158 159 162 public void removeActionListener(ActionListener listener) { 163 removeEventListener(ActionListener .class, listener); 164 } 165 166 173 public ActionListener [] getActionListeners() { 174 return (ActionListener []) (getListeners(ActionListener .class)); 175 } 176 177 182 protected void fireActionPerformed(ActionEvent event) { 183 Object [] listeners = getListenerList(); 185 ActionEvent e = null; 186 for (int i = listeners.length - 2; i >= 0; i -= 2) { 189 if (listeners[i] == ActionListener .class) { 190 if (e == null) { 191 String actionCommand = event.getActionCommand(); 192 if (actionCommand == null) { 193 actionCommand = getActionCommand(); 194 } 195 e = new ActionEvent (SAbstractButton.this, 196 ActionEvent.ACTION_PERFORMED, 197 actionCommand, 198 event.getWhen(), 199 event.getModifiers()); 200 } 201 ((ActionListener ) listeners[i + 1]).actionPerformed(e); 202 } 203 } 204 } 205 206 207 216 public void setType(String t) { 217 if (isDifferent(type, t)) { 218 type = t; 219 reload(); 220 } 221 } 222 223 228 public final String getType() { 229 return type; 230 } 231 232 235 public void doClick() { 236 setSelected(!isSelected()); 237 238 fireActionPerformed(new ActionEvent (this, ActionEvent.ACTION_PERFORMED, getActionCommand())); 239 } 240 241 246 public void setSelected(boolean b) { 247 if (isSelected() != b) { 248 if (buttonGroup != null) { 249 buttonGroup.setSelected(this, b); 250 } 251 super.setSelected(b); 252 } 253 } 254 255 public void processLowLevelEvent(String action, String [] values) { 256 processKeyEvents(values); 257 258 boolean requestSelection = isSelected(); 259 260 int eventCount = 0; 261 262 if (buttonGroup != null) { 263 for (int i = 0; i < values.length; i++) { 266 267 String value = values[i]; 271 272 if (value.length() < 3) { continue; } 274 275 278 if (!value.startsWith(super.getLowLevelEventId())) { continue; } 280 281 switch (value.charAt(value.length() - 1)) { 284 case '1': 285 requestSelection = true; 286 ++eventCount; 287 break; 288 case '0': 289 requestSelection = false; 290 ++eventCount; 291 break; 292 } 293 } 294 } else { 295 for (int i = 0; i < values.length; i++) { 296 requestSelection = parseSelectionToggle(values[0]); 297 ++eventCount; 298 } 299 } 300 301 314 if (eventCount == 2) { 315 requestSelection = true; 316 } 317 318 if (isSelected() != requestSelection) { 319 delayEvents(true); 320 if (buttonGroup != null) { 321 buttonGroup.setDelayEvents(true); 322 setSelected(requestSelection); 323 buttonGroup.setDelayEvents(false); 324 } else { 325 setSelected(requestSelection); 326 } 327 328 SForm.addArmedComponent(this); 329 } 330 } 331 332 public void fireIntermediateEvents() { 333 super.fireIntermediateEvents(); 334 if (buttonGroup != null) { 335 buttonGroup.fireDelayedIntermediateEvents(); 336 } 337 } 338 339 public void fireFinalEvents() { 340 requestFocus(); 341 super.fireFinalEvents(); 342 fireActionPerformed(new ActionEvent (this, ActionEvent.ACTION_PERFORMED, getActionCommand())); 343 if (buttonGroup != null) 344 buttonGroup.fireDelayedFinalEvents(); 345 } 346 347 public final String getEventTarget() { 348 return eventTarget; 349 } 350 351 352 public void setEventTarget(String target) { 353 if (isDifferent(eventTarget, target)) { 354 eventTarget = target; 355 reload(); 356 } 357 } 358 359 protected boolean parseSelectionToggle(String toggleParameter) { 360 if ("1".equals(toggleParameter)) 361 return true; 362 else if ("0".equals(toggleParameter)) 363 return false; 364 365 return isSelected(); 367 } 368 369 public String getToggleSelectionParameter() { 370 return isSelected() ? getDeselectionParameter() : getSelectionParameter(); 371 } 372 373 public String getSelectionParameter() { 374 return "1"; 375 } 376 377 public String getDeselectionParameter() { 378 return "0"; 379 } 380 381 382 388 389 public void setAction(Action a) { 390 Action oldValue = getAction(); 391 if (action == null || !action.equals(a)) { 392 action = a; 393 if (oldValue != null) { 394 removeActionListener(oldValue); 395 oldValue.removePropertyChangeListener(actionPropertyChangeListener); 396 actionPropertyChangeListener = null; 397 } 398 configurePropertiesFromAction(action); 399 if (action != null) { 400 if (!isListener(ActionListener .class, action)) { 402 addActionListener(action); 403 } 404 actionPropertyChangeListener = createActionPropertyChangeListener(action); 406 action.addPropertyChangeListener(actionPropertyChangeListener); 407 } 408 reload(); 409 } 410 } 411 412 private boolean isListener(Class c, ActionListener a) { 413 boolean isListener = false; 414 Object [] listeners = getListenerList(); 415 for (int i = listeners.length - 2; i >= 0; i -= 2) { 416 if (listeners[i] == c && listeners[i + 1] == a) { 417 isListener = true; 418 } 419 } 420 return isListener; 421 } 422 423 430 public Action getAction() { 431 return action; 432 } 433 434 public void setCG(ButtonCG cg) { 435 super.setCG(cg); 436 } 437 438 protected void configurePropertiesFromAction(Action a) { 439 setText((a != null ? (String ) a.getValue(Action.NAME) : null)); 444 setIcon((a != null ? (SIcon) a.getValue(Action.SMALL_ICON) : null)); 445 setEnabled((a != null ? a.isEnabled() : true)); 446 setToolTipText((a != null ? (String ) a.getValue(Action.SHORT_DESCRIPTION) : null)); 447 } 448 449 protected PropertyChangeListener createActionPropertyChangeListener(Action a) { 450 return new ButtonActionPropertyChangeListener(this, a); 451 } 452 453 private static class ButtonActionPropertyChangeListener 454 extends AbstractActionPropertyChangeListener { 455 ButtonActionPropertyChangeListener(SAbstractButton b, Action a) { 456 super(b, a); 457 } 458 459 public void propertyChange(PropertyChangeEvent e) { 460 String propertyName = e.getPropertyName(); 461 SAbstractButton button = (SAbstractButton) getTarget(); 462 if (button == null) { 463 Action action = (Action) e.getSource(); 464 action.removePropertyChangeListener(this); 465 } else { 466 if (e.getPropertyName().equals(Action.NAME)) { 467 String text = (String ) e.getNewValue(); 468 button.setText(text); 469 } else if (e.getPropertyName().equals(Action.SHORT_DESCRIPTION)) { 470 String text = (String ) e.getNewValue(); 471 button.setToolTipText(text); 472 } else if (propertyName.equals("enabled")) { 473 Boolean enabled = (Boolean ) e.getNewValue(); 474 button.setEnabled(enabled.booleanValue()); 475 } else if (e.getPropertyName().equals(Action.SMALL_ICON)) { 476 SIcon icon = (SIcon) e.getNewValue(); 477 button.setIcon(icon); 478 } 479 484 } 485 } 486 } 487 488 public void setMnemonic(String mnemonic) { 489 reloadIfChange(this.mnemonic, mnemonic); 490 this.mnemonic = mnemonic; 491 } 492 493 public String getMnemonic() { 494 return mnemonic; 495 } 496 497 498 public boolean isEpochCheckEnabled() { 499 return epochCheckEnabled; 500 } 501 502 503 public void setEpochCheckEnabled(boolean epochCheckEnabled) { 504 this.epochCheckEnabled = epochCheckEnabled; 505 } 506 } 507 | Popular Tags |