001: /* ====================================================================
002: * Copyright (c) 1998 - 2003 David F. Glasser. All rights
003: * reserved.
004: *
005: * This file is part of the QueryForm Database Tool.
006: *
007: * The QueryForm Database Tool is free software; you can redistribute it
008: * and/or modify it under the terms of the GNU General Public License as
009: * published by the Free Software Foundation; either version 2 of the
010: * License, or (at your option) any later version.
011: *
012: * The QueryForm Database Tool is distributed in the hope that it will
013: * be useful, but WITHOUT ANY WARRANTY; without even the implied
014: * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
015: * See the GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with the QueryForm Database Tool; if not, write to:
019: *
020: * The Free Software Foundation, Inc.,
021: * 59 Temple Place, Suite 330
022: * Boston, MA 02111-1307 USA
023: *
024: * or visit http://www.gnu.org.
025: *
026: * ====================================================================
027: *
028: * This product includes software developed by the
029: * Apache Software Foundation (http://www.apache.org/).
030: *
031: * ====================================================================
032: */
033: package org.glasser.qform;
034:
035: import javax.swing.*;
036: import org.glasser.util.*;
037: import org.glasser.sql.*;
038: import org.glasser.swing.*;
039: import java.awt.event.*;
040: import java.awt.*;
041: import javax.swing.border.*;
042: import javax.swing.event.*;
043: import java.text.SimpleDateFormat;
044:
045: public abstract class ExportPanel extends JPanel implements
046: ActionListener {
047:
048: private String[] columnNames = null;
049:
050: private Formatter[] formatters = null;
051:
052: private JCheckBox[] checkboxes = null;
053:
054: private JTextField[] nameFields = null;
055:
056: private JComboBox[] formatterSelectors = null;
057:
058: private Column[] cols = null;
059:
060: protected abstract Object[] getFormatterChoices(int dataType);
061:
062: public ExportPanel(TableInfo ti) {
063:
064: cols = ti.getColumns();
065: checkboxes = new JCheckBox[cols.length];
066: nameFields = new JTextField[cols.length];
067: formatterSelectors = new JComboBox[cols.length];
068:
069: setLayout(new GridBagLayout());
070: GridBagConstraints gc0 = new GridBagConstraints();
071:
072: gc0.gridx = 0;
073: gc0.fill = gc0.BOTH;
074: gc0.anchor = gc0.CENTER;
075: gc0.weightx = .5;
076:
077: GridBagConstraints gc1 = (GridBagConstraints) gc0.clone();
078: GridBagConstraints gc2 = (GridBagConstraints) gc0.clone();
079:
080: gc1.gridx = 1;
081: gc2.gridx = 2;
082: gc0.weightx = 0;
083:
084: for (int j = 0; j < cols.length; j++) {
085: gc0.gridy = j;
086: gc1.gridy = j;
087: gc2.gridy = j;
088:
089: IndexedCheckBox cb = new IndexedCheckBox(j);
090: checkboxes[j] = cb;
091: cb.setSelected(true);
092:
093: add(cb, gc0);
094:
095: // the column's data type will determine
096: // the choices that appear in the combo box.
097: int type = cols[j].getDataType();
098:
099: if (DBUtil.isBinaryType(type)) {
100: cb.setSelected(false);
101: }
102:
103: // It's necessary that the ActionListener be added to the
104: // checkbox after the possible call to setSelected() above.
105: cb.addActionListener(this );
106:
107: JTextField field = new JTextField();
108: field.setText(cols[j].getColumnName());
109: field.setEnabled(cb.isSelected());
110: field.setEditable(cb.isSelected());
111: nameFields[j] = field;
112: add(field, gc1);
113:
114: // populate the combo box with the appropriate choices for
115: // this data type and export type.
116: JComboBox cmb = new JComboBox(getFormatterChoices(type));
117:
118: cmb.setSelectedIndex(1);
119:
120: // this ActionListener will make the combo box editable whenever
121: // the user selects the first (blank line) item, and non-editable
122: // when one of the Formatter objects are selected from the list.
123: cmb.addActionListener(new ActionListener() {
124:
125: // this gets called when the user changes the selection on
126: // the combo box.
127: public void actionPerformed(ActionEvent e) {
128:
129: JComboBox combo = (JComboBox) e.getSource();
130: int selectedIndex = combo.getSelectedIndex();
131: Object o = combo.getSelectedItem();
132:
133: System.out.println("--ActionEvent:" + selectedIndex
134: + "/" + o);
135:
136: if (selectedIndex < 0) {
137: return;
138: }
139: if (selectedIndex == 0) {
140: combo.setEditable(true);
141: combo.requestFocus();
142: } else {
143: combo.setEditable(false);
144:
145: // this works around an apparent swing bug where the selected
146: // index seems to get changed by the call to setEditable(false).
147: if (o != null)
148: combo.setSelectedItem(o);
149: }
150: }
151: });
152: cmb.setEnabled(cb.isSelected());
153: formatterSelectors[j] = cmb;
154: add(cmb, gc2);
155: }
156:
157: }
158:
159: /**
160: * This gets called whenever a JCheckBox is toggled.
161: */
162: public void actionPerformed(ActionEvent e) {
163:
164: // get the checkbox that fired this event, and read its index
165: IndexedCheckBox chk = (IndexedCheckBox) e.getSource();
166: int index = chk.index;
167:
168: // enable/disable the text field and the combo box to match
169: // the selected state of the checkbox.
170: nameFields[index].setEnabled(chk.isSelected());
171: nameFields[index].setEditable(chk.isSelected());
172: JComboBox cmb = formatterSelectors[index];
173: cmb.setEnabled(chk.isSelected());
174: // the editable state of the combo box is not affected by
175: // this event.
176: }
177:
178: public void update() {
179:
180: columnNames = new String[checkboxes.length];
181: this .formatters = new Formatter[checkboxes.length];
182: for (int j = 0; j < checkboxes.length; j++) {
183: if (checkboxes[j].isSelected()) {
184: columnNames[j] = nameFields[j].getText();
185: Object o = formatterSelectors[j].getSelectedItem();
186: if (o == null) {
187: formatters[j] = new LiteralFormatter("");
188: } else if (o instanceof Formatter) {
189: formatters[j] = (Formatter) o;
190: } else {
191: formatters[j] = new LiteralFormatter(o.toString());
192: }
193: }
194: }
195: }
196:
197: public String[] getColumnNames() {
198: return columnNames;
199: }
200:
201: public Formatter[] getFormatters() {
202: return formatters;
203: }
204:
205: /**
206: * This is a JCheckBox that knows what its index is in
207: * the array of checkboxes.
208: */
209: public static class IndexedCheckBox extends JCheckBox {
210:
211: public int index = 0;
212:
213: public IndexedCheckBox(int index) {
214: super ();
215: this .index = index;
216: }
217: }
218:
219: protected static class LiteralFormatter implements Formatter {
220:
221: String literal = null;
222:
223: public LiteralFormatter(String literal) {
224: this .literal = literal;
225: }
226:
227: public String getFormattedString(Object o) {
228: return literal;
229: }
230: }
231:
232: }
|