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 class InsertExportPanel extends ExportPanel {
046:
047: private static final Formatter charFormatter = new CharFieldFormatter() {
048: public String toString() {
049: return "<Copy Value>";
050: }
051: };
052:
053: private static final Formatter numFormatter = new NumberFieldFormatter() {
054: public String toString() {
055: return "<Copy Value>";
056: }
057: };
058:
059: private static final Formatter nullFormatter = new Formatter() {
060:
061: public String getFormattedString(Object o) {
062: return "NULL";
063: }
064:
065: public String toString() {
066: return "NULL";
067: }
068: };
069:
070: private static class TimestampFormatter implements Formatter {
071:
072: public final static int TIME_ESCAPE = 0;
073:
074: public final static int DATE_ESCAPE = 1;
075:
076: public final static int TIMESTAMP_ESCAPE = 2;
077:
078: public final static int TIME_STRING = 3;
079:
080: public final static int DATE_STRING = 4;
081:
082: public final static int TIMESTAMP_STRING = 5;
083:
084: private int formatStyle = TIMESTAMP_STRING;
085:
086: private final static SimpleDateFormat TIME_FORMATTER = new SimpleDateFormat(
087: "HH:mm:ss");
088:
089: private final static SimpleDateFormat DATE_FORMATTER = new SimpleDateFormat(
090: "yyyy-MM-dd");
091:
092: private final static SimpleDateFormat TIMESTAMP_FORMATTER = new SimpleDateFormat(
093: "yyyy-MM-dd HH:mm:ss");
094:
095: public TimestampFormatter() {
096: }
097:
098: public TimestampFormatter(int formatStyle) {
099: this .formatStyle = formatStyle;
100: }
101:
102: public String getFormattedString(Object obj) {
103: if (obj == null)
104: return "NULL";
105:
106: java.util.Date date = (java.util.Date) obj;
107:
108: switch (formatStyle) {
109: case TIME_ESCAPE:
110: return "{t '" + TIME_FORMATTER.format(date) + "'}";
111: case DATE_ESCAPE:
112: return "{d '" + DATE_FORMATTER.format(date) + "'}";
113: case TIMESTAMP_ESCAPE:
114: return "{ts '" + TIMESTAMP_FORMATTER.format(date)
115: + "'}";
116: case TIME_STRING:
117: return "'" + TIME_FORMATTER.format(date) + "'";
118: case DATE_STRING:
119: return "'" + DATE_FORMATTER.format(date) + "'";
120: default:
121: return "'" + TIMESTAMP_FORMATTER.format(date) + "'";
122:
123: }
124: }
125:
126: public String toString() {
127:
128: switch (formatStyle) {
129: case TIME_ESCAPE:
130: return "{t 'hh:mm:ss'}";
131: case DATE_ESCAPE:
132: return "{d 'YYYY-MM-DD'}";
133: case TIMESTAMP_ESCAPE:
134: return "{ts 'YYYY-MM-DD hh:mm:ss'}";
135: case TIME_STRING:
136: return "'hh:mm:ss'";
137: case DATE_STRING:
138: return "'YYYY-MM-DD'";
139: default: // TIMESTAMP_STRING
140: return "'YYYY-MM-DD hh:mm:ss'";
141:
142: }
143: }
144:
145: }
146:
147: private static final Object[] charChoices = { "", charFormatter,
148: nullFormatter };
149:
150: private static final Object[] numChoices = { "", numFormatter,
151: nullFormatter };
152:
153: private static final Object[] binChoices = { "", nullFormatter };
154:
155: private static final Object[] dateTimeChoices = {
156: "",
157: nullFormatter,
158: new TimestampFormatter(TimestampFormatter.TIMESTAMP_STRING),
159: new TimestampFormatter(TimestampFormatter.DATE_STRING),
160: new TimestampFormatter(TimestampFormatter.TIME_STRING),
161: new TimestampFormatter(TimestampFormatter.TIMESTAMP_ESCAPE),
162: new TimestampFormatter(TimestampFormatter.DATE_ESCAPE),
163: new TimestampFormatter(TimestampFormatter.TIME_ESCAPE) };
164:
165: public InsertExportPanel(TableInfo ti) {
166: super (ti);
167:
168: }
169:
170: protected Object[] getFormatterChoices(int type) {
171: if (DBUtil.isCharType(type)) {
172: return charChoices;
173: } else if (DBUtil.isNumericType(type)) {
174: return numChoices;
175: } else if (DBUtil.isDateTimeType(type)) {
176: return dateTimeChoices;
177: } else {
178: return binChoices;
179: }
180:
181: }
182:
183: }
|