001: package net.sourceforge.squirrel_sql.fw.datasetviewer.cellcomponent;
002:
003: import java.awt.GridBagConstraints;
004: import java.awt.GridBagLayout;
005: import java.awt.Insets;
006: import java.text.NumberFormat;
007:
008: import javax.swing.BorderFactory;
009: import javax.swing.ButtonGroup;
010: import javax.swing.JRadioButton;
011:
012: import net.sourceforge.squirrel_sql.fw.gui.OkJPanel;
013: import net.sourceforge.squirrel_sql.fw.util.StringManager;
014: import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
015:
016: public class FloatingPointBase extends BaseDataTypeComponent {
017: private static final StringManager s_stringMgr = StringManagerFactory
018: .getStringManager(FloatingPointBase.class);
019:
020: // flag for whether we have already loaded the properties or not
021: private static boolean propertiesAlreadyLoaded = false;
022:
023: // flag for whether to use the default Java format (true)
024: // or the Locale-dependent format (false)
025: protected static boolean useJavaDefaultFormat = false;
026:
027: /**
028: * Generate a JPanel containing controls that allow the user
029: * to adjust the properties for this DataType.
030: * All properties are static accross all instances of this DataType.
031: * However, the class may choose to apply the information differentially,
032: * such as keeping a list (also entered by the user) of table/column names
033: * for which certain properties should be used.
034: * <P>
035: * This is called ONLY if there is at least one property entered into the DTProperties
036: * for this class.
037: * <P>
038: * Since this method is called by reflection on the Method object derived from this class,
039: * it does not need to be included in the Interface.
040: * It would be nice to include this in the Interface for consistancy, documentation, etc,
041: * but the Interface does not seem to like static methods.
042: */
043: public static OkJPanel getControlPanel() {
044:
045: /*
046: * If you add this method to one of the standard DataTypes in the
047: * fw/datasetviewer/cellcomponent directory, you must also add the name
048: * of that DataType class to the list in CellComponentFactory, method
049: * getControlPanels, variable named initialClassNameList.
050: * If the class is being registered with the factory using registerDataType,
051: * then you should not include the class name in the list (it will be found
052: * automatically), but if the DataType is part of the case statement in the
053: * factory method getDataTypeObject, then it does need to be explicitly listed
054: * in the getControlPanels method also.
055: */
056:
057: // if this panel is called before any instances of the class have been
058: // created, we need to load the properties from the DTProperties.
059: loadProperties();
060:
061: return new FloatingPointOkJPanel();
062: }
063:
064: public FloatingPointBase() {
065: loadProperties();
066: }
067:
068: private static void loadProperties() {
069:
070: //set the property values
071: // Note: this may have already been done by another instance of
072: // this DataType created to handle a different column.
073: if (propertiesAlreadyLoaded == false) {
074: // get parameters previously set by user, or set default values
075: useJavaDefaultFormat = false; // set to use the Java default
076: String useJavaDefaultFormatString = DTProperties.get(
077: DataTypeBigDecimal.class.getName(),
078: "useJavaDefaultFormat");
079:
080: if (useJavaDefaultFormatString != null
081: && useJavaDefaultFormatString.equals("true")) {
082: useJavaDefaultFormat = true;
083: }
084: }
085: }
086:
087: /**
088: * Inner class that extends OkJPanel so that we can call the ok()
089: * method to save the data when the user is happy with it.
090: */
091: private static class FloatingPointOkJPanel extends OkJPanel {
092: private static final long serialVersionUID = 3745853322636427759L;
093:
094: JRadioButton optUseDefaultFormat;
095: JRadioButton optUseLocaleDependendFormat;
096:
097: public FloatingPointOkJPanel() {
098: NumberFormat numberFormat = NumberFormat.getInstance();
099: numberFormat.setMaximumFractionDigits(5);
100:
101: optUseDefaultFormat = new JRadioButton(s_stringMgr
102: .getString("floatingPointBase.useDefaultFormat",
103: new Double(3.14159).toString()));
104: optUseLocaleDependendFormat = new JRadioButton(
105: s_stringMgr
106: .getString(
107: "floatingPointBase.uselocaleDependendFormat",
108: numberFormat.format(new Double(
109: 3.14159))));
110:
111: setBorder(BorderFactory.createTitledBorder(s_stringMgr
112: .getString("floatingPointBase.typeBigDecimal")));
113:
114: setLayout(new GridBagLayout());
115:
116: GridBagConstraints gbc;
117: gbc = new GridBagConstraints(0, 0, 1, 1, 0, 0,
118: GridBagConstraints.NORTHWEST,
119: GridBagConstraints.HORIZONTAL, new Insets(4, 4, 4,
120: 4), 0, 0);
121: add(optUseLocaleDependendFormat, gbc);
122: gbc = new GridBagConstraints(0, 1, 1, 1, 0, 0,
123: GridBagConstraints.NORTHWEST,
124: GridBagConstraints.HORIZONTAL, new Insets(0, 4, 4,
125: 4), 0, 0);
126: add(optUseDefaultFormat, gbc);
127:
128: ButtonGroup bg = new ButtonGroup();
129: bg.add(optUseDefaultFormat);
130: bg.add(optUseLocaleDependendFormat);
131:
132: optUseLocaleDependendFormat
133: .setSelected(!useJavaDefaultFormat);
134: optUseDefaultFormat.setSelected(useJavaDefaultFormat);
135: }
136:
137: /**
138: * User has clicked OK in the surrounding JPanel,
139: * so save the current state of all variables
140: */
141: public void ok() {
142: // get the values from the controls and set them in the static properties
143: useJavaDefaultFormat = optUseDefaultFormat.isSelected();
144: DTProperties.put(DataTypeBigDecimal.class.getName(),
145: "useJavaDefaultFormat", Boolean.valueOf(
146: useJavaDefaultFormat).toString());
147: }
148: } // end of inner class
149:
150: }
|