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.Formatter;
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.util.HashMap;
044:
045: public class ExportDialog extends JDialog implements ActionListener {
046:
047: static class IndexedCheckBox extends JCheckBox {
048:
049: public int index = 0;
050:
051: public IndexedCheckBox(int index) {
052: super ();
053: this .index = index;
054: }
055: }
056:
057: JPanel topPanel = new JPanel();
058: JPanel scrollHolder = new JPanel();
059: JPanel buttonPanel = new JPanel();
060:
061: JTextField txtTableName = new JTextField();
062: JTextField txtTerminalChar = new JTextField();
063:
064: JButton btnOk = new JButton("OK");
065: JButton btnCancel = new JButton("Cancel");
066:
067: Object[][] formFields = {
068: { txtTableName, "Table Name",
069: "Name of the table that will be used in each insert statement." },
070: {
071: txtTerminalChar,
072: "Terminal Character(s)",
073: "Optional character(s) that will be appended to the end of each insert statement. (Usually \";\")" } };
074:
075: Object[][] buttonConfig = {
076: { btnOk, "O", "OK", "Export resultset." },
077: { btnCancel, "C", "CANCEL", "Cancel the export operation." } };
078:
079: public ExportDialog(Frame parent) {
080: super (parent);
081:
082: JPanel cp = new JPanel();
083: cp.setLayout(new BorderLayout());
084:
085: GUIHelper.buildFormPanel(topPanel, formFields, -1, 5, null,
086: Color.black, false, -1);
087:
088: cp.add(topPanel, BorderLayout.NORTH);
089:
090: cp.add(scrollHolder, BorderLayout.CENTER);
091:
092: buttonPanel.setLayout(new FlowLayout());
093: Border defaultBorder = txtTerminalChar.getBorder();
094: txtTerminalChar.setBorder(new CompoundBorder(defaultBorder,
095: new EmptyBorder(0, 15, 0, 0)));
096: txtTerminalChar.setText(";");
097: txtTerminalChar.setFont(new Font("Dialog", Font.BOLD, 13));
098: topPanel.setBorder(new EmptyBorder(10, 10, 0, 10));
099: scrollHolder.setLayout(new BorderLayout());
100: scrollHolder.setBorder(new EmptyBorder(10, 10, 10, 10));
101:
102: for (int j = 0; j < buttonConfig.length; j++) {
103: JButton button = (JButton) buttonConfig[j][0];
104: button.setMnemonic(((String) buttonConfig[j][1]).charAt(0));
105: button.setActionCommand((String) buttonConfig[j][2]);
106: button.setToolTipText((String) buttonConfig[j][3]);
107: button.addActionListener(this );
108: buttonPanel.add(button);
109: }
110:
111: // buttonPanel.setBackground(Color.pink);
112: // topPanel.setBackground(Color.pink);
113: // cp.setBackground(Color.blue);
114: cp.add(buttonPanel, BorderLayout.SOUTH);
115:
116: setContentPane(cp);
117:
118: }
119:
120: private String[] columnNames = null;
121:
122: private Formatter[] formatters = null;
123:
124: public String[] getColumnNames() {
125: return columnNames;
126: }
127:
128: public Formatter[] getFormatters() {
129: return formatters;
130: }
131:
132: public String getTableName() {
133: if (columnNames == null)
134: return null;
135: return txtTableName.getText();
136: }
137:
138: public String getTerminal() {
139: if (columnNames == null)
140: return null;
141: return txtTerminalChar.getText();
142: }
143:
144: public void actionPerformed(ActionEvent e) {
145: String command = e.getActionCommand();
146: if (command.equals("OK")) {
147: exportPanel.update();
148: columnNames = exportPanel.getColumnNames();
149: formatters = exportPanel.getFormatters();
150: defaultTerminals[currentExportType] = this .txtTerminalChar
151: .getText();
152: setVisible(false);
153: } else if (command.equals("CANCEL")) {
154:
155: columnNames = null;
156: formatters = null;
157: setVisible(false);
158: }
159: }
160:
161: ExportPanel exportPanel = null;
162:
163: HashMap cachedPanels = new HashMap();
164:
165: int currentExportType = EXPORT_INSERT_STATEMENTS;
166:
167: public void openDialog(TableInfo ti, int exportType) {
168:
169: if (exportType == EXPORT_CSV) {
170: topPanel.setVisible(false);
171: } else {
172: topPanel.setVisible(true);
173: }
174:
175: columnNames = null;
176: formatters = null;
177: currentExportType = exportType;
178:
179: txtTableName.setText(ti.getQualifiedTableName());
180: exportPanel = getExportPanel(ti, exportType);
181:
182: scrollHolder.removeAll();
183:
184: JScrollPane sp = new JScrollPane(exportPanel);
185: scrollHolder.add(sp, BorderLayout.CENTER);
186:
187: this .txtTerminalChar.setText(defaultTerminals[exportType]);
188:
189: pack();
190:
191: // don't let the dialog be more than 80% of the screen height
192: Dimension size = this .getSize();
193: Dimension screenSize = Toolkit.getDefaultToolkit()
194: .getScreenSize();
195:
196: size = (Dimension) size.clone();
197: size.height = Math.min((int) (screenSize.height * .8),
198: size.height);
199: setSize(size);
200: setModal(true);
201: setVisible(true);
202: }
203:
204: public final static int EXPORT_INSERT_STATEMENTS = 0;
205:
206: public final static int EXPORT_CSV = 1;
207:
208: HashMap[] panelCaches = { new HashMap(), new HashMap() };
209:
210: String[] defaultTerminals = { ";", "" };
211:
212: private ExportPanel getExportPanel(TableInfo ti, int exportType) {
213:
214: ExportPanel panel = (ExportPanel) panelCaches[exportType]
215: .get(ti);
216: if (panel != null)
217: return panel;
218: switch (exportType) {
219: case EXPORT_INSERT_STATEMENTS:
220: panel = new InsertExportPanel(ti);
221: break;
222: case EXPORT_CSV:
223: panel = new CsvExportPanel(ti);
224: break;
225: default: // should never be reached because a NullPointer would already have been thrown
226: throw new IllegalArgumentException("Invalid export type: "
227: + exportType);
228: }
229:
230: // explicitly set the sizes
231: Dimension d = panel.getPreferredSize();
232: panel.setMinimumSize((Dimension) d.clone());
233: panel.setPreferredSize((Dimension) d.clone());
234:
235: panelCaches[exportType].put(ti, panel);
236:
237: return panel;
238:
239: }
240:
241: }
|