001: /*
002: * SQLeonardo :: java database frontend
003: * Copyright (C) 2004 nickyb@users.sourceforge.net
004: *
005: * This program is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU General Public License
007: * as published by the Free Software Foundation; either version 2
008: * of the License, or (at your option) any later version.
009: *
010: * This program is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013: * GNU General Public License for more details.
014: *
015: * You should have received a copy of the GNU General Public License
016: * along with this program; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
018: *
019: */
020:
021: package nickyb.sqleonardo.environment.ctrl.content;
022:
023: import java.awt.FlowLayout;
024: import java.awt.GridLayout;
025: import java.awt.event.ItemEvent;
026: import java.awt.event.ItemListener;
027: import java.io.FileNotFoundException;
028: import java.io.FileOutputStream;
029: import java.io.PrintStream;
030: import java.sql.Types;
031:
032: import javax.swing.ButtonGroup;
033: import javax.swing.JCheckBox;
034: import javax.swing.JLabel;
035: import javax.swing.JPanel;
036: import javax.swing.JRadioButton;
037: import javax.swing.JTextField;
038: import javax.swing.border.TitledBorder;
039:
040: import nickyb.sqleonardo.common.gui.BorderLayoutPanel;
041: import nickyb.sqleonardo.common.util.Text;
042: import nickyb.sqleonardo.environment.Application;
043:
044: public class MaskExport extends AbstractMaskPerform {
045: private AbstractChoice eChoice;
046:
047: public void setEnabled(boolean b) {
048: super .setEnabled(b);
049: for (int i = 0; i < eChoice.getComponentCount(); i++)
050: eChoice.getComponent(i).setEnabled(b);
051: }
052:
053: void setType(short type, String tname, String fname) {
054: if (eChoice != null)
055: remove(eChoice);
056:
057: progress.setValue(0);
058: progress.setMaximum(0);
059:
060: if (type == WEB) {
061: if (!fname.endsWith(".htm") && !fname.endsWith(".html"))
062: fname = fname + ".html";
063: setComponentCenter(eChoice = new WebChoice());
064: } else if (type == SQL) {
065: if (!fname.endsWith(".sql"))
066: fname = fname + ".sql";
067: setComponentCenter(eChoice = new SqlChoice(tname));
068: } else if (type == TXT) {
069: if (!fname.endsWith(".txt"))
070: fname = fname + ".txt";
071: setComponentCenter(eChoice = new TxtChoice());
072: }
073:
074: lblFile.setText("file: " + fname);
075: }
076:
077: // -----------------------------------------------------------------------------------------
078: // ?????????????????????????????????????????????????????????????????????????????????????????
079: // -----------------------------------------------------------------------------------------
080: void init() {
081: super .init();
082:
083: progress.setValue(0);
084: progress.setMaximum(eChoice.getLastRow()
085: - eChoice.getFirstRow() + 1);
086:
087: eChoice.open();
088: }
089:
090: void next() {
091: eChoice.handle(view.getValues(progress.getValue()
092: + eChoice.getFirstRow() - 1));
093: progress.setValue(progress.getValue() + 1);
094: }
095:
096: boolean finished() {
097: if (progress.getValue() == progress.getMaximum()) {
098: eChoice.close();
099:
100: btnStop.setEnabled(false);
101: lblMsg.setText("ready!");
102:
103: return true;
104: }
105:
106: return false;
107: }
108:
109: // -----------------------------------------------------------------------------------------
110: // -----------------------------------------------------------------------------------------
111: private abstract class AbstractChoice extends BorderLayoutPanel {
112: private PrintStream stream;
113:
114: JRadioButton rbAll;
115: JRadioButton rbBlock;
116: JRadioButton rbUser;
117:
118: JTextField txtInterval;
119:
120: AbstractChoice() {
121: setBorder(new TitledBorder("options"));
122: initComponents();
123: }
124:
125: void initComponents() {
126: JPanel pnl = new JPanel(new FlowLayout(FlowLayout.LEFT));
127: setComponentSouth(pnl);
128:
129: pnl.add(new JLabel("records:"));
130: pnl.add(rbAll = new JRadioButton("all", true));
131: pnl.add(rbBlock = new JRadioButton("current block"));
132: pnl.add(rbUser = new JRadioButton("define:"));
133: pnl.add(txtInterval = new JTextField("1..", 8));
134: txtInterval.setEditable(false);
135: txtInterval.setEnabled(false);
136:
137: ButtonGroup bg = new ButtonGroup();
138: ItemListener il = new ItemListener() {
139: public void itemStateChanged(ItemEvent e) {
140: txtInterval.setEditable(rbUser.isSelected());
141: txtInterval.setEnabled(rbUser.isSelected());
142:
143: if (rbAll.isSelected()) {
144: txtInterval.setText("1..");
145: } else if (rbBlock.isSelected()) {
146: int last = view.getBlock()
147: * ContentModel.MAX_BLOCK_RECORDS;
148: int first = last
149: - ContentModel.MAX_BLOCK_RECORDS;
150: if (first == 0)
151: first = 1;
152: if (last > view.getFlatRowCount())
153: last = view.getFlatRowCount() - 1;
154:
155: txtInterval.setText(first + ".." + last);
156: }
157: }
158: };
159:
160: bg.add(rbAll);
161: bg.add(rbBlock);
162: bg.add(rbUser);
163:
164: rbAll.addItemListener(il);
165: rbBlock.addItemListener(il);
166: rbUser.addItemListener(il);
167: }
168:
169: int getFirstRow() {
170: String interval = eChoice.txtInterval.getText();
171: int pos = interval.indexOf("..");
172:
173: return Integer.valueOf(interval.substring(0, pos))
174: .intValue();
175: }
176:
177: int getLastRow() {
178: String interval = eChoice.txtInterval.getText();
179: int pos = interval.indexOf("..") + 2;
180:
181: return pos < interval.length() ? Integer.valueOf(
182: interval.substring(pos)).intValue() : view
183: .getFlatRowCount();
184: }
185:
186: void open() {
187: try {
188: stream = new PrintStream(new FileOutputStream(
189: MaskExport.this .lblFile.getText().substring(6)));
190: } catch (FileNotFoundException e) {
191: Application.println(e, true);
192: }
193: }
194:
195: abstract void handle(Object[] vals);
196:
197: void close() {
198: stream.close();
199: }
200:
201: void print(String s) {
202: stream.print(s);
203: }
204:
205: void println(String s) {
206: stream.println(s);
207: }
208: }
209:
210: private class WebChoice extends AbstractChoice {
211: JCheckBox cbxHeader;
212:
213: void initComponents() {
214: JPanel pnl = new JPanel(new FlowLayout(FlowLayout.LEFT));
215: pnl.add(cbxHeader = new JCheckBox("with header"));
216: setComponentCenter(pnl);
217:
218: super .initComponents();
219: }
220:
221: void open() {
222: super .open();
223: println("<html><body><table border=1>");
224:
225: if (cbxHeader.isSelected()) {
226: print("<tr>");
227: for (int col = 0; col < view.getColumnCount(); col++) {
228: print("<th>" + view.getColumnName(col) + "</th>");
229: }
230: println("</tr>");
231: }
232: }
233:
234: void handle(Object[] vals) {
235: print("<tr>");
236: for (int i = 0; i < vals.length; i++) {
237: String val = vals[i] == null ? "null" : vals[i]
238: .toString();
239: print("<td>" + val + "</td>");
240: }
241: println("</tr>");
242: }
243:
244: void close() {
245: println("</table></body></html>");
246: super .close();
247: }
248: }
249:
250: private class SqlChoice extends AbstractChoice {
251: JCheckBox cbxDelete;
252: JTextField txtTable;
253:
254: String insert = null;
255:
256: SqlChoice(String tname) {
257: super ();
258: txtTable.setText(tname);
259: }
260:
261: void initComponents() {
262: JPanel pnl1 = new JPanel(new FlowLayout(FlowLayout.LEFT));
263: pnl1.add(new JLabel("table name:"));
264: pnl1.add(txtTable = new JTextField(10));
265:
266: cbxDelete = new JCheckBox("with delete statement");
267:
268: JPanel pnl2 = new JPanel(new GridLayout(2, 1));
269: pnl2.add(pnl1);
270: pnl2.add(cbxDelete);
271:
272: JPanel pnl3 = new JPanel(new FlowLayout(FlowLayout.LEFT));
273: setComponentCenter(pnl3);
274: pnl3.add(pnl2);
275:
276: super .initComponents();
277: }
278:
279: void open() {
280: super .open();
281:
282: if (cbxDelete.isSelected()) {
283: println("DELETE FROM " + txtTable.getText() + ";");
284: }
285:
286: StringBuffer buffer = new StringBuffer("INSERT INTO "
287: + txtTable.getText() + " (");
288: for (int col = 0; col < view.getColumnCount(); col++) {
289: buffer.append(view.getColumnName(col) + ",");
290: }
291: buffer.deleteCharAt(buffer.length() - 1);
292: insert = buffer.toString() + ")";
293: }
294:
295: void handle(Object[] vals) {
296: StringBuffer buffer = new StringBuffer();
297: for (int i = 0; i < vals.length; i++) {
298: // String val = vals[i] == null ? "null" : vals[i].toString();
299: buffer.append(toSQLValue(vals[i], i) + ",");
300: }
301: buffer.deleteCharAt(buffer.length() - 1);
302: println(insert + " VALUES (" + buffer.toString() + ");");
303: }
304:
305: private String toSQLValue(Object value, int col) {
306: if (value == null)
307: return "null";
308:
309: switch (MaskExport.this .view.getColumnType(col)) {
310: case Types.CHAR:
311: case Types.VARCHAR:
312: value = Text
313: .replaceText(value.toString(), "\'", "\\\'");
314: return "'" + value.toString() + "'";
315: case Types.DATE:
316: return "{d '" + value.toString() + "'}";
317: case Types.TIME:
318: return "{t '" + value.toString() + "'}";
319: case Types.TIMESTAMP:
320: return "{ts '" + value.toString() + "'}";
321: default:
322: return value.toString();
323: }
324: }
325: }
326:
327: private class TxtChoice extends AbstractChoice {
328: JCheckBox cbxHeader;
329: JCheckBox cbxNull;
330: JCheckBox cbxTrim;
331:
332: JRadioButton rbTab;
333: JRadioButton rbOther;
334:
335: JTextField txtDelimiter;
336:
337: void initComponents() {
338: JPanel pnl1 = new JPanel(new FlowLayout(FlowLayout.LEFT));
339: pnl1.add(cbxHeader = new JCheckBox("with header"));
340: pnl1.add(cbxNull = new JCheckBox("null if blanks"));
341: pnl1.add(cbxTrim = new JCheckBox("trim value"));
342:
343: JPanel pnl2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
344: pnl2.add(new JLabel("delimiter:"));
345: pnl2.add(rbTab = new JRadioButton("tab", true));
346: pnl2.add(rbOther = new JRadioButton("other"));
347: pnl2.add(txtDelimiter = new JTextField(";", 5));
348: txtDelimiter.setEditable(false);
349: txtDelimiter.setEnabled(false);
350:
351: JPanel pnl3 = new JPanel(new GridLayout(2, 1));
352: pnl3.add(pnl1);
353: pnl3.add(pnl2);
354:
355: JPanel pnl4 = new JPanel(new FlowLayout(FlowLayout.LEFT));
356: setComponentCenter(pnl4);
357: pnl4.add(pnl3);
358:
359: ButtonGroup bg = new ButtonGroup();
360: bg.add(rbTab);
361: bg.add(rbOther);
362:
363: rbTab.addItemListener(new ItemListener() {
364: public void itemStateChanged(ItemEvent e) {
365: txtDelimiter.setEditable(!rbTab.isSelected());
366: txtDelimiter.setEnabled(!rbTab.isSelected());
367: }
368: });
369:
370: super .initComponents();
371: }
372:
373: private String getDelimiter() {
374: if (rbTab.isSelected())
375: return "\t";
376: return txtDelimiter.getText();
377: }
378:
379: void open() {
380: super .open();
381:
382: if (cbxHeader.isSelected()) {
383: StringBuffer buffer = new StringBuffer();
384: for (int col = 0; col < view.getColumnCount(); col++) {
385: buffer.append(view.getColumnName(col)
386: + getDelimiter());
387: }
388: if (buffer.length() > 0)
389: buffer.deleteCharAt(buffer.length() - 1);
390: println(buffer.toString());
391: }
392: }
393:
394: void handle(Object[] vals) {
395: StringBuffer buffer = new StringBuffer();
396: for (int i = 0; i < vals.length; i++) {
397: String val = vals[i] == null ? "null" : vals[i]
398: .toString();
399: buffer.append(val + getDelimiter());
400: }
401: if (buffer.length() > 0)
402: buffer.deleteCharAt(buffer.length() - 1);
403: println(buffer.toString());
404: }
405: }
406: }
|