001: /*
002: * Copyright (C) 2005 - 2008 JasperSoft Corporation. All rights reserved.
003: * http://www.jaspersoft.com.
004: *
005: * Unless you have purchased a commercial license agreement from JasperSoft,
006: * the following license terms apply:
007: *
008: * This program is free software; you can redistribute it and/or modify
009: * it under the terms of the GNU General Public License version 2 as published by
010: * the Free Software Foundation.
011: *
012: * This program is distributed WITHOUT ANY WARRANTY; and without the
013: * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
014: * See the GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
018: * or write to:
019: *
020: * Free Software Foundation, Inc.,
021: * 59 Temple Place - Suite 330,
022: * Boston, MA USA 02111-1307
023: *
024: *
025: *
026: *
027: * NumberComboBoxSheetProperty.java
028: *
029: * Created on 16 febbraio 2005, 19.13
030: *
031: */
032:
033: package it.businesslogic.ireport.gui.sheet;
034:
035: import it.businesslogic.ireport.gui.JNumberComboBox;
036: import it.businesslogic.ireport.gui.event.SheetPropertyValueChangedEvent;
037: import it.businesslogic.ireport.gui.event.ValueChangedEvent;
038: import it.businesslogic.ireport.gui.event.ValueChangedListener;
039: import java.util.*;
040: import javax.swing.*;
041: import it.businesslogic.ireport.util.*;
042:
043: public class NumberComboBoxSheetProperty extends SheetProperty {
044:
045: JNumberComboBox jNumberComboBox = null;
046:
047: public NumberComboBoxSheetProperty(String key, String name) {
048: super (key, name, SheetProperty.COMBOBOX);
049: }
050:
051: public Object getEditorValue(JComponent component) {
052: return "" + (int) jNumberComboBox.getValue();
053: }
054:
055: public void setValue(Object value) {
056: //if (this.value == value || (this.value+"").equals(value+"")) return;
057:
058: this .value = value;
059: this .setEditorValue(getEditor(),
060: (value == null) ? getDefaultValue() : value);
061: updateLabel();
062: }
063:
064: public void setEditorValue(JComponent component, Object str) {
065: if (str == null || str.equals("")) {
066: str = getDefaultValue();
067: }
068: if (str == null)
069: str = "";
070:
071: boolean setting = isSetting();
072: this .setSetting(true);
073: try {
074: ((JComboBox) component).setSelectedItem(str);
075: } catch (Exception ex) {
076: ex.printStackTrace();
077: }
078: this .setSetting(setting);
079: }
080:
081: public void addEntry(String name, int value) {
082: ((JNumberComboBox) getEditor()).addEntry(name, (double) value);
083: }
084:
085: public JComponent getEditor() {
086: if (jNumberComboBox != null)
087: return jNumberComboBox;
088:
089: jNumberComboBox = new JNumberComboBox();
090: jNumberComboBox.setEditable(true);
091:
092: /*
093: for (int i=0; i<this.getTags().size(); ++i)
094: {
095: Tag t = (Tag)getTags().elementAt(i);
096: jNumberComboBox.addEntry( t.getName(), Double.parseDouble( t.getValue()+""));
097: }
098: */
099:
100: jNumberComboBox.setBorder(null);
101:
102: /*
103: ((JComboBox)jNumberComboBox).setEditor(new SimpleComboBoxEditor() );
104: for (int i=0; i< jNumberComboBox.getComponentCount(); ++i)
105: {
106: try {
107: Object obj = jNumberComboBox.getComponent(i);
108: //System.out.println( obj );
109: if (obj != null && obj.getClass().getMethod("setBorder", new Class[]{javax.swing.border.Border.class}) != null)
110: {
111: java.lang.reflect.Method mtd = obj.getClass().getMethod("setBorder", new Class[]{javax.swing.border.Border.class});
112: mtd.invoke(obj, new Object[]{null});
113: }
114: } catch (Exception ex) { }
115: }
116:
117: */
118: jNumberComboBox
119: .addItemListener(new java.awt.event.ItemListener() {
120: public void itemStateChanged(
121: java.awt.event.ItemEvent evt) {
122:
123: if (evt.getStateChange() == evt.SELECTED) {
124: actionPerformed(null);
125: }
126: }
127: });
128: jNumberComboBox.addActionListener(this );
129:
130: return jNumberComboBox;
131: }
132:
133: public void actionPerformed(java.awt.event.ActionEvent event) {
134: if (isSetting())
135: return;
136:
137: //System.out.println("Value changed!" + evt.getOldValue() + " --> " + evt.getNewValue());
138:
139: Object new_value = getEditorValue(this .getEditor());
140: if (new_value != null && new_value.equals(value))
141: return;
142: if (new_value != null) {
143: try {
144: Double.parseDouble(new_value + "");
145: } catch (Exception ex) {
146: return;
147: }
148: }
149:
150: Object oldValue = value;
151: value = new_value;
152: //System.out.println(this.getName() + ": " + getValue());
153: if (oldValue == null || value == null) {
154: updateLabel();
155: }
156:
157: fireSheetPropertyValueChangedListenerSheetPropertyValueChanged(new SheetPropertyValueChangedEvent(
158: getKeyName(), oldValue, value, this ));
159: }
160:
161: public void updateValues(Vector values, boolean addNullEntry) {
162:
163: Misc.updateComboBox((JComboBox) getEditor(), values,
164: addNullEntry);
165:
166: }
167:
168: }
|