1 14 package org.wings; 15 16 17 import org.wings.style.CSSSelector; 18 import org.wings.style.CSSStyleSheet; 19 import org.wings.style.CSSAttributeSet; 20 import org.wings.style.CSSProperty; 21 22 import javax.swing.event.ChangeEvent ; 23 import javax.swing.event.ChangeListener ; 24 import javax.swing.event.EventListenerList ; 25 import java.awt.*; 26 import java.awt.event.ItemEvent ; 27 import java.awt.event.ItemListener ; 28 29 37 public abstract class SAbstractIconTextCompound 38 extends SComponent 39 implements ItemSelectable { 40 public static final int ICON_COUNT = 7; 41 public static final int DISABLED_ICON = 0; 42 public static final int DISABLED_SELECTED_ICON = 1; 43 public static final int ENABLED_ICON = 2; 44 public static final int SELECTED_ICON = 3; 45 public static final int ROLLOVER_ICON = 4; 46 public static final int ROLLOVER_SELECTED_ICON = 5; 47 public static final int PRESSED_ICON = 6; 48 49 public static final CSSSelector SELECTOR_SELECTION = new CSSSelector.Pseudo("SELECTION"); 50 51 private SButtonModel model; 52 53 56 protected ChangeListener changeListener = null; 57 58 protected transient ChangeEvent changeEvent = null; 59 60 63 private String text; 64 65 68 private SIcon icon; 69 70 private SIcon disabledIcon; 71 72 private SIcon selectedIcon; 73 74 private SIcon pressedIcon; 75 76 private SIcon disabledSelectedIcon; 77 78 private SIcon rolloverIcon; 79 80 private SIcon rolloverSelectedIcon; 81 82 private int verticalTextPosition = SConstants.CENTER; 83 84 private int horizontalTextPosition = SConstants.RIGHT; 85 86 private int iconTextGap = 0; 87 88 private boolean delayEvents = false; 89 90 95 public SAbstractIconTextCompound(String text) { 96 setText(text); 97 model = new SDefaultButtonModel(); 98 } 99 100 103 public SAbstractIconTextCompound() { 104 this(null); 105 } 106 107 public SButtonModel getModel() { 108 return model; 109 } 110 111 public void setModel(SButtonModel model) { 112 if (model == null) 113 throw new IllegalArgumentException ("null not allowed"); 114 this.model = model; 115 reloadIfChange(this.model, model); 116 } 117 118 121 public Object [] getSelectedObjects() { 122 return model.isSelected() ? new Object []{this} : null; 123 } 124 125 130 public void addItemListener(ItemListener il) { 131 addEventListener(ItemListener .class, il); 132 } 133 134 140 public void removeItemListener(ItemListener il) { 141 removeEventListener(ItemListener .class, il); 142 } 143 144 149 public void addChangeListener(ChangeListener l) { 150 addEventListener(ChangeListener .class, l); 151 } 152 153 158 public void removeChangeListener(ChangeListener l) { 159 removeEventListener(ChangeListener .class, l); 160 } 161 162 170 protected void fireStateChanged() { 171 Object [] listeners = getListenerList(); 173 for (int i = listeners.length - 2; i >= 0; i -= 2) { 176 if (listeners[i] == ChangeListener .class) { 177 if (changeEvent == null) 179 changeEvent = new ChangeEvent (this); 180 ((ChangeListener ) listeners[i + 1]).stateChanged(changeEvent); 181 } 182 } 183 } 184 185 public void setHorizontalTextPosition(int textPosition) { 186 horizontalTextPosition = textPosition; 187 reloadIfChange(this.horizontalTextPosition, textPosition); 188 189 } 190 191 public int getHorizontalTextPosition() { 192 return horizontalTextPosition; 193 } 194 195 public void setVerticalTextPosition(int textPosition) { 196 verticalTextPosition = textPosition; 197 reloadIfChange(this.verticalTextPosition, textPosition); 198 } 199 200 public int getVerticalTextPosition() { 201 return verticalTextPosition; 202 } 203 204 public void setIconTextGap(int gap) { 205 iconTextGap = gap; 206 reloadIfChange(this.iconTextGap, gap); 207 } 208 209 public int getIconTextGap() { 210 return iconTextGap; 211 } 212 213 public void setIcon(SIcon i) { 214 reloadIfChange(icon, i); 215 icon = i; 216 } 217 218 public SIcon getIcon() { 219 return icon; 220 } 221 222 public void setPressedIcon(SIcon i) { 223 reloadIfChange(pressedIcon, i); 224 pressedIcon = i; 225 } 226 227 public SIcon getPressedIcon() { 228 return pressedIcon; 229 } 230 231 public void setRolloverIcon(SIcon i) { 232 reloadIfChange(rolloverIcon, i); 233 rolloverIcon = i; 234 } 235 236 public SIcon getRolloverIcon() { 237 return rolloverIcon; 238 } 239 240 public void setRolloverSelectedIcon(SIcon i) { 241 reloadIfChange(rolloverSelectedIcon, i); 242 rolloverSelectedIcon = i; 243 } 244 245 public SIcon getRolloverSelectedIcon() { 246 return rolloverSelectedIcon; 247 } 248 249 public void setSelectedIcon(SIcon i) { 250 reloadIfChange(selectedIcon, i); 251 selectedIcon = i; 252 } 253 254 public SIcon getSelectedIcon() { 255 return selectedIcon; 256 } 257 258 public void setDisabledSelectedIcon(SIcon i) { 259 reloadIfChange(disabledSelectedIcon, i); 260 disabledSelectedIcon = i; 261 } 262 263 public SIcon getDisabledSelectedIcon() { 264 return disabledSelectedIcon; 265 } 266 267 public void setDisabledIcon(SIcon i) { 268 reloadIfChange(disabledIcon, i); 269 disabledIcon = i; 270 } 271 272 public SIcon getDisabledIcon() { 273 279 return disabledIcon; 280 } 281 282 287 public Color getSelectionBackground() { 288 return dynamicStyles == null || dynamicStyles.get(SELECTOR_SELECTION) == null ? null : CSSStyleSheet.getBackground((CSSAttributeSet) dynamicStyles.get(SELECTOR_SELECTION)); 289 } 290 291 296 public void setSelectionBackground(Color color) { 297 setAttribute(SELECTOR_SELECTION, CSSProperty.BACKGROUND_COLOR, CSSStyleSheet.getAttribute(color)); 298 } 299 300 305 public Color getSelectionForeground() { 306 return dynamicStyles == null || dynamicStyles.get(SELECTOR_SELECTION) == null ? null : CSSStyleSheet.getForeground((CSSAttributeSet) dynamicStyles.get(SELECTOR_SELECTION)); 307 } 308 309 314 public void setSelectionForeground(Color color) { 315 setAttribute(SELECTOR_SELECTION, CSSProperty.COLOR, CSSStyleSheet.getAttribute(color)); 316 } 317 318 323 public void setSelectionFont(SFont font) { 324 setAttributes(SELECTOR_SELECTION, CSSStyleSheet.getAttributes(font)); 325 } 326 327 332 public SFont getSelectionFont() { 333 return dynamicStyles == null || dynamicStyles.get(SELECTOR_SELECTION) == null ? null : CSSStyleSheet.getFont((CSSAttributeSet) dynamicStyles.get(SELECTOR_SELECTION)); 334 } 335 336 339 public void setText(String t) { 340 reloadIfChange(text, t); 341 text = t; 342 } 343 344 public String getText() { 345 return text; 346 } 347 348 public boolean isSelected() { 349 return model.isSelected(); 350 } 351 352 357 public void setSelected(boolean selected) { 358 if (model.isSelected() != selected) { 359 model.setSelected(selected); 360 fireStateChanged(); 361 reload(); 362 } 363 } 364 365 368 public void setIcons(SIcon[] icons) { 369 setIcon(icons[ENABLED_ICON]); 370 setDisabledIcon(icons[DISABLED_ICON]); 371 setDisabledSelectedIcon(icons[DISABLED_SELECTED_ICON]); 372 setRolloverIcon(icons[ROLLOVER_ICON]); 373 setRolloverSelectedIcon(icons[ROLLOVER_SELECTED_ICON]); 374 setPressedIcon(icons[PRESSED_ICON]); 375 setSelectedIcon(icons[SELECTED_ICON]); 376 } 377 378 379 private ItemEvent delayedItemEvent; 380 381 protected final void delayEvents(boolean b) { 382 delayEvents = b; 383 } 384 385 protected final boolean shouldDelayEvents() { 386 return delayEvents; 387 } 388 389 396 protected void fireItemStateChanged(ItemEvent ie) { 397 if (ie == null) 398 return; 399 400 if (delayEvents) { 401 delayedItemEvent = ie; 402 return; 403 } 405 Object [] listeners = getListenerList(); 407 for (int i = listeners.length - 2; i >= 0; i -= 2) { 410 if (listeners[i] == ItemListener .class) { 411 ((ItemListener ) listeners[i + 1]).itemStateChanged(ie); 412 } 413 } 414 } 415 416 public void fireIntermediateEvents() { 417 if (delayEvents && delayedItemEvent != null) { 418 delayEvents = false; 419 fireItemStateChanged(delayedItemEvent); 420 delayEvents = true; 421 } 423 } 424 425 public void fireFinalEvents() { 426 super.fireFinalEvents(); 427 delayEvents = false; 428 } 429 } 430 | Popular Tags |