1 32 33 package it.businesslogic.ireport.gui.sheet; 34 import com.lowagie.text.Font; 35 import java.util.*; 36 import javax.swing.*; 37 import it.businesslogic.ireport.gui.event.*; 38 import java.awt.Color ; 39 43 public class SheetProperty implements java.awt.event.ActionListener { 44 45 public static final int STRING = 0; 46 public static final int NUMBER = 1; 47 public static final int INTEGER = 6; 48 public static final int COMBOBOX = 2; 49 public static final int COLOR = 3; 50 public static final int BOOLEAN = 4; 51 public static final int COMBOBOX_NK = 5; public static final int PASSWORD = 7; 53 public static final int CATEGORY_LABEL = 99; 54 55 protected String name = ""; 56 protected String keyName = ""; 57 protected String group = ""; 58 protected int type; 59 private java.util.Vector tags = null; 60 protected Object defaultValue = null; 61 protected Object value = null; 62 63 private boolean readOnly = false; 64 65 private boolean setting = false; 66 67 protected JComponent component = null; 68 69 private JComponent labelComponent = null; 70 71 private boolean showResetButton = true; 72 73 private java.awt.Color labelColor = java.awt.Color.BLACK; 74 75 private Vector currentSelection = new Vector(); 76 77 78 79 82 private javax.swing.event.EventListenerList listenerList = null; 83 84 public SheetProperty(String keyName, String name, int type) { 85 this(keyName,name,type,null); 86 } 87 88 public SheetProperty(String keyName, String name, int type, Object defaultValue) { 89 this.setName( name ); 90 this.setKeyName( keyName ); 91 this.setDefaultValue( defaultValue ); 92 setType(type); 93 tags = new Vector(); 94 } 95 96 public SheetProperty(String keyName, String name,String groupname, int type, Object defaultValue) { 97 this.setName( name ); 98 this.setKeyName( keyName ); 99 this.setDefaultValue( defaultValue ); 100 setType(type); 101 tags = new Vector(); 102 } 103 104 public String getName() { 105 return name; 106 } 107 108 public void setName(String name) { 109 this.name = name; 110 } 111 112 public int getType() { 113 return type; 114 } 115 116 public void setType(int type) { 117 this.type = type; 118 } 119 120 public java.util.Vector getTags() { 121 return tags; 122 } 123 124 public void setTags(java.util.Vector tags) { 125 this.tags = tags; 126 } 127 128 public void setTags(Tag[] tags_array) { 129 130 this.tags = new Vector(); 131 132 for (int i=0; i<tags_array.length; ++i) 133 { 134 tags.add(tags_array[i]); 135 } 136 } 137 138 public JComponent getEditor() 139 { 140 141 if (component != null) return component; 142 143 if (this.getType() == NUMBER) 144 { 145 component = new it.businesslogic.ireport.gui.JNumberField(); 146 component.setBorder(null); 147 148 ((it.businesslogic.ireport.gui.JNumberField)component).addActionListener( this ); 149 150 } 151 else if (this.getType() == INTEGER) 152 { 153 component = new it.businesslogic.ireport.gui.JNumberField(); 154 component.setBorder(null); 155 try { 156 ((it.businesslogic.ireport.gui.JNumberField)component).setInteger(true); 157 } catch (Exception ex){} 158 159 ((it.businesslogic.ireport.gui.JNumberField)component).addActionListener( this ); 160 161 } 162 else if (this.getType() == COMBOBOX || this.getType() == COMBOBOX_NK) 163 { 164 component = new JComboBox(this.getTags()); 165 component.setBorder(null); 166 167 169 for (int i=0; i< component.getComponentCount(); ++i) 170 { 171 try { 172 Object obj = component.getComponent(i); 173 if (obj != null && obj.getClass().getMethod("setBorder", new Class []{javax.swing.border.Border .class}) != null) 175 { 176 java.lang.reflect.Method mtd = obj.getClass().getMethod("setBorder", new Class []{javax.swing.border.Border .class}); 177 mtd.invoke(obj, new Object []{null}); 178 } 179 } catch (Exception ex) { } 180 } 181 182 ((JComboBox)component).addActionListener( this ); 183 } 184 else if (this.getType() == BOOLEAN) 185 { 186 component = new JCheckBox(""); 187 ((JCheckBox)component).addActionListener( this ); 188 } 189 else if (this.getType() == COLOR) 190 { 191 component = new ColorSelectorPanel(); 192 ((ColorSelectorPanel)component).addActionListener( this ); 193 } 194 else if (this.getType() == CATEGORY_LABEL) 195 { 196 component = new JPanel(); 197 component.setBackground( java.awt.Color.LIGHT_GRAY); 198 } 199 else if (this.getType() == PASSWORD) 200 { 201 component = new JPasswordField(); 202 component.setBorder(null); 203 ((JTextField)component).getDocument().addDocumentListener( new javax.swing.event.DocumentListener () { 204 public void changedUpdate(javax.swing.event.DocumentEvent evt) { 205 actionPerformed(null); 206 } 207 public void insertUpdate(javax.swing.event.DocumentEvent evt) { 208 actionPerformed(null); 209 } 210 public void removeUpdate(javax.swing.event.DocumentEvent evt) { 211 actionPerformed(null); 212 } 213 }); 214 } 215 else 216 { 217 component = new JTextField(); 219 component.setBorder(null); 220 ((JTextField)component).getDocument().addDocumentListener( new javax.swing.event.DocumentListener () { 221 public void changedUpdate(javax.swing.event.DocumentEvent evt) { 222 actionPerformed(null); 223 } 224 public void insertUpdate(javax.swing.event.DocumentEvent evt) { 225 actionPerformed(null); 226 } 227 public void removeUpdate(javax.swing.event.DocumentEvent evt) { 228 actionPerformed(null); 229 } 230 }); 231 } 232 233 234 return component; 235 236 } 237 238 public Object getEditorValue(JComponent component) 239 { 240 if (this.getType() == NUMBER) 241 { 242 return new Double (((it.businesslogic.ireport.gui.JNumberField)component).getValue()); 243 } 244 else if (this.getType() == INTEGER) 245 { 246 return new Integer ( (int)((it.businesslogic.ireport.gui.JNumberField)component).getValue()); 247 } 248 else if (this.getType() == COMBOBOX) 249 { 250 Object obj = ((JComboBox)component).getSelectedItem(); 251 if (obj != null) 252 { 253 if (obj instanceof Tag) 254 { 255 return ((Tag)obj).getValue(); 256 } 257 else 258 { 259 return obj; 260 } 261 } 262 return null; 263 } 264 else if (this.getType() == COMBOBOX_NK) 265 { 266 Object obj = ((JComboBox)component).getSelectedItem(); 267 if (obj != null) 268 { 269 return new Integer (((Tag)obj).getValue()+""); 270 } 271 return null; 272 } 273 else if (this.getType() == BOOLEAN) 274 { 275 return new Boolean (((JCheckBox)component).isSelected()); 276 } 277 else if (this.getType() == BOOLEAN) 278 { 279 return new String ( ((JPasswordField)component).getPassword() ); 280 } 281 else if (this.getType() == COLOR) 282 { 283 return ((ColorSelectorPanel)component).getValue(); 284 } 285 286 return ((JTextField)component).getText(); 287 } 288 289 public synchronized void setEditorValue(JComponent component, Object obj) 290 { 291 292 String str = ""+obj; 293 294 component.setForeground(Color.BLACK ); 295 296 try { 297 298 setSetting(true); 299 if (this.getType() == NUMBER) 300 { 301 if (obj == null) str ="0"; 302 ((it.businesslogic.ireport.gui.JNumberField)component).setValue(Double.parseDouble(str) ); 303 304 if (obj == null) ((it.businesslogic.ireport.gui.JNumberField)component).setForeground( Color.LIGHT_GRAY); 305 306 } 307 else if (this.getType() == INTEGER) 308 { 309 if (obj == null) str ="0"; 310 ((it.businesslogic.ireport.gui.JNumberField)component).setValue(Integer.parseInt(str) ); 311 if (obj == null) ((it.businesslogic.ireport.gui.JNumberField)component).setForeground( Color.LIGHT_GRAY); 312 } 313 else if (this.getType() == COMBOBOX) 314 { 315 if (obj == null) { 316 setSetting(false); 317 if (getTags().size() > 0 && ((Tag)getTags().elementAt(0)).getValue() == null) 318 { 319 ((JComboBox)component).setSelectedIndex(0); 320 } 321 return; 322 } 323 boolean found = false; 324 for (int i=0; i<getTags().size(); ++i) 325 { 326 327 Tag t = (Tag)getTags().elementAt(i); 328 if (t.getValue() != null && t.getValue().equals(str)) 329 { 330 found = true; 331 ((JComboBox)component).setSelectedIndex(i); 332 break; 333 } 334 } 335 if (!found && ((JComboBox)component).isEditable()) 336 { 337 ((JComboBox)component).setSelectedItem(obj); 338 } 339 } 340 else if (this.getType() == COMBOBOX_NK) 341 { 342 if (obj == null) { setSetting(false); 343 if (getTags().size() > 0) 344 { 345 ((JComboBox)component).setSelectedIndex(0); 346 } 347 return; 348 } 349 for (int i=0; i<getTags().size(); ++i) 350 { 351 Tag t = (Tag)getTags().elementAt(i); 352 if ((t.getValue()+"").equals(str)) 353 { 354 ((JComboBox)component).setSelectedIndex(i); 355 break; 356 } 357 } 358 } 359 else if (this.getType() == BOOLEAN) 360 { 361 if (obj == null) { ((JCheckBox)component).setSelected(false); setSetting(false); return;} 362 ((JCheckBox)component).setSelected(str.equals("true")); 363 } 364 else if (this.getType() == COLOR) 365 { 366 ((ColorSelectorPanel)component).setValue(obj); 367 } 368 else if (this.getType() == PASSWORD) 369 { 370 if (obj == null) { ((JPasswordField)component).setText(""); setSetting(false); return;} 371 ((JPasswordField)component).setText(str); 372 } 373 else 374 { 375 if (obj == null) { ((JTextField)component).setText(""); setSetting(false); return;} 376 ((JTextField)component).setText(str); 377 } 378 } catch (Exception ex) { 379 ex.printStackTrace(); 380 } 381 finally { 382 setSetting(false); 383 } 384 385 } 386 387 public String getKeyName() { 388 return keyName; 389 } 390 391 public void setKeyName(String keyName) { 392 this.keyName = keyName; 393 } 394 395 public Object getDefaultValue() { 396 return defaultValue; 397 } 398 399 public void setDefaultValue(Object defaultValue) { 400 this.defaultValue = defaultValue; 401 } 402 403 public String getGroup() { 404 return group; 405 } 406 407 public void setGroup(String group) { 408 this.group = group; 409 } 410 411 public Object getValue() { 412 return value; 413 } 414 415 public void setValue(Object value) { 416 417 this.value = value; 418 this.setEditorValue(getEditor(), (value == null) ? getDefaultValue() : value); 419 updateLabel(); 420 421 } 422 423 public void updateLabel() 424 { 425 426 if (this.getType() != CATEGORY_LABEL) 427 { 428 try { 429 if (getLabelComponent() != null) 430 { 431 432 java.awt.Font f = getLabelComponent().getFont(); 433 java.awt.Font f2 = new java.awt.Font ( f.getName(), (value == null || this.isReadOnly() || !isShowResetButton()) ? 0 : java.awt.Font.BOLD, f.getSize()); 434 getLabelComponent().setFont(f2); 435 436 getLabelComponent().setForeground( getLabelColor()); 437 438 if (this.isReadOnly()) 439 { 440 getLabelComponent().setEnabled( false ); 441 } 442 else 443 { 444 getLabelComponent().setEnabled( true ); 445 } 446 447 448 getLabelComponent().updateUI(); 449 } 450 } catch (Exception ex) 451 { 452 453 } 454 } 455 } 456 457 public void actionPerformed(java.awt.event.ActionEvent event) 458 { 459 if (isSetting()) return; 460 getEditor().setForeground( Color.BLACK); 461 Object new_value = getEditorValue( this.getEditor() ); 462 if (new_value != null && new_value.equals(value)) return; 463 464 Object oldValue = value; 465 value = new_value; 466 467 if(oldValue == null || new_value == null) 468 { 469 updateLabel(); 470 } 471 472 fireSheetPropertyValueChangedListenerSheetPropertyValueChanged( 473 new SheetPropertyValueChangedEvent(getKeyName(),oldValue,new_value, this)); 474 } 475 476 480 public synchronized void addSheetPropertyValueChangedListener(it.businesslogic.ireport.gui.event.SheetPropertyValueChangedListener listener) { 481 482 if (listenerList == null ) { 483 listenerList = new javax.swing.event.EventListenerList (); 484 } 485 listenerList.add (it.businesslogic.ireport.gui.event.SheetPropertyValueChangedListener.class, listener); 486 } 487 488 492 public synchronized void removeSheetPropertyValueChangedListener(it.businesslogic.ireport.gui.event.SheetPropertyValueChangedListener listener) { 493 494 listenerList.remove (it.businesslogic.ireport.gui.event.SheetPropertyValueChangedListener.class, listener); 495 } 496 497 502 public void fireSheetPropertyValueChangedListenerSheetPropertyValueChanged(it.businesslogic.ireport.gui.event.SheetPropertyValueChangedEvent event) { 503 504 if (listenerList == null) return; 505 Object [] listeners = listenerList.getListenerList (); 506 for (int i = listeners.length-2; i>=0; i-=2) { 507 if (listeners[i]==it.businesslogic.ireport.gui.event.SheetPropertyValueChangedListener.class) { 508 ((it.businesslogic.ireport.gui.event.SheetPropertyValueChangedListener)listeners[i+1]).sheetPropertyValueChanged (event); 509 } 510 } 511 } 512 513 public boolean isReadOnly() { 514 return readOnly; 515 } 516 517 public void setReadOnly(boolean readOnly) { 518 this.readOnly = readOnly; 519 getEditor().setEnabled(!readOnly); 520 } 521 522 public JComponent getLabelComponent() { 523 return labelComponent; 524 } 525 526 public void setLabelComponent(JComponent labelComponent) { 527 this.labelComponent = labelComponent; 528 } 529 530 public boolean isSetting() { 531 return setting; 532 } 533 534 public void setSetting(boolean setting) { 535 this.setting = setting; 536 } 537 538 public boolean isShowResetButton() { 539 return showResetButton; 540 } 541 542 public void setShowResetButton(boolean showResetButton) { 543 this.showResetButton = showResetButton; 544 } 545 546 public java.awt.Color getLabelColor() { 547 return labelColor; 548 } 549 550 public void setLabelColor(java.awt.Color labelColor) { 551 this.labelColor = labelColor; 552 } 553 554 public Vector getCurrentSelection() { 555 return currentSelection; 556 } 557 558 public void setCurrentSelection(Vector currentSelection) { 559 this.currentSelection = currentSelection; 560 } 561 562 } 563 | Popular Tags |