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.FileInputStream;
028: import java.io.FileNotFoundException;
029: import java.io.IOException;
030: import java.util.Vector;
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.environment.Application;
042:
043: public class MaskImport extends AbstractMaskPerform {
044: private AbstractChoice iChoice;
045:
046: public void setEnabled(boolean b) {
047: super .setEnabled(b);
048: for (int i = 0; i < iChoice.getComponentCount(); i++)
049: iChoice.getComponent(i).setEnabled(b);
050: }
051:
052: void setType(short type, String tname, String fname) {
053: if (iChoice != null)
054: remove(iChoice);
055:
056: progress.setValue(0);
057: progress.setMaximum(0);
058:
059: if (type == TXT) {
060: if (!fname.endsWith(".txt"))
061: fname = fname + ".txt";
062: setComponentCenter(iChoice = new TxtChoice());
063: }
064:
065: lblFile.setText("file: " + fname);
066: }
067:
068: // -----------------------------------------------------------------------------------------
069: // ?????????????????????????????????????????????????????????????????????????????????????????
070: // -----------------------------------------------------------------------------------------
071: void init() {
072: super .init();
073:
074: progress.setValue(0);
075: progress.setMaximum(iChoice.open());
076: }
077:
078: void next() {
079: String line = iChoice.nextln();
080:
081: Object[] rowdata = new Object[view.getColumnCount()];
082: iChoice.flush(line, rowdata);
083:
084: view.addRow(rowdata, true);
085: progress.setValue(progress.getValue() + line.length() + 1);
086: }
087:
088: boolean finished() {
089: if (progress.getValue() == progress.getMaximum()) {
090: iChoice.close();
091:
092: view.onTableChanged(true);
093: view.getControl().doRefreshStatus();
094:
095: btnStop.setEnabled(false);
096: lblMsg.setText("ready!");
097:
098: return true;
099: }
100:
101: return false;
102: }
103:
104: // -----------------------------------------------------------------------------------------
105: // -----------------------------------------------------------------------------------------
106: private abstract class AbstractChoice extends BorderLayoutPanel {
107: private FileInputStream stream;
108:
109: JRadioButton rbAll;
110: JRadioButton rbUser;
111:
112: JTextField txtInterval;
113:
114: AbstractChoice() {
115: setBorder(new TitledBorder("options"));
116: }
117:
118: /*
119: void initComponents()
120: {
121: JPanel pnl = new JPanel(new FlowLayout(FlowLayout.LEFT));
122: setComponentSouth(pnl);
123:
124: pnl.add(new JLabel("records:"));
125: pnl.add(rbAll = new JRadioButton("all",true));
126: pnl.add(rbUser = new JRadioButton("define:"));
127: pnl.add(txtInterval = new JTextField("1..",8));
128: txtInterval.setEditable(false);
129: txtInterval.setEnabled(false);
130:
131: ButtonGroup bg = new ButtonGroup();
132: ItemListener il = new ItemListener()
133: {
134: public void itemStateChanged(ItemEvent e)
135: {
136: txtInterval.setEditable(rbUser.isSelected());
137: txtInterval.setEnabled(rbUser.isSelected());
138:
139: if(rbAll.isSelected())
140: {
141: txtInterval.setText("1..");
142: }
143: }
144: };
145:
146: bg.add(rbAll);
147: bg.add(rbUser);
148:
149: rbAll.addItemListener(il);
150: rbUser.addItemListener(il);
151: }
152: */
153: int open() {
154: try {
155: stream = new FileInputStream(MaskImport.this .lblFile
156: .getText().substring(6));
157: return stream.available();
158: } catch (FileNotFoundException e) {
159: Application.println(e, true);
160: } catch (IOException e) {
161: Application.println(e, true);
162: }
163:
164: return 0;
165: }
166:
167: void close() {
168: try {
169: stream.close();
170: } catch (IOException e) {
171: Application.println(e, true);
172: }
173: }
174:
175: abstract void flush(String line, Object[] rowdata);
176:
177: String nextln() {
178: try {
179: String line = new String("");
180:
181: int nChar;
182: while ((nChar = stream.read()) != -1
183: && (char) nChar != '\n')
184: line = line + (char) nChar;
185:
186: return line;
187: } catch (IOException e) {
188: Application.println(e, true);
189: }
190:
191: return null;
192: }
193: }
194:
195: private class TxtChoice extends AbstractChoice {
196: JCheckBox cbxHeader;
197: JCheckBox cbxNull;
198: JCheckBox cbxTrim;
199:
200: JRadioButton rbTab;
201: JRadioButton rbOther;
202:
203: JTextField txtDelimiter;
204:
205: TxtChoice() {
206: super ();
207:
208: JPanel pnl1 = new JPanel(new GridLayout(3, 1));
209: pnl1.add(cbxHeader = new JCheckBox("with header"));
210: pnl1.add(cbxNull = new JCheckBox("null if blanks"));
211: pnl1.add(cbxTrim = new JCheckBox("trim value"));
212:
213: JPanel pnl2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
214: pnl2.add(new JLabel("delimiter:"));
215: pnl2.add(rbTab = new JRadioButton("tab", true));
216: pnl2.add(rbOther = new JRadioButton("other"));
217: pnl2.add(txtDelimiter = new JTextField(";", 5));
218: txtDelimiter.setEditable(false);
219: txtDelimiter.setEnabled(false);
220:
221: JPanel pnl3 = new JPanel(new GridLayout(2, 1));
222: pnl3.add(pnl1);
223: pnl3.add(pnl2);
224:
225: JPanel pnl4 = new JPanel(new FlowLayout(FlowLayout.LEFT));
226: setComponentCenter(pnl4);
227: pnl4.add(pnl3);
228:
229: ButtonGroup bg = new ButtonGroup();
230: bg.add(rbTab);
231: bg.add(rbOther);
232:
233: rbTab.addItemListener(new ItemListener() {
234: public void itemStateChanged(ItemEvent e) {
235: txtDelimiter.setEditable(!rbTab.isSelected());
236: txtDelimiter.setEnabled(!rbTab.isSelected());
237: }
238: });
239: }
240:
241: private String getDelimiter() {
242: if (rbTab.isSelected())
243: return "\t";
244: return txtDelimiter.getText();
245: }
246:
247: int open() {
248: int bytes = super .open();
249:
250: if (cbxHeader.isSelected()) {
251: String line = nextln();
252: bytes = bytes - (line.length() + 1);
253: }
254:
255: return bytes;
256: }
257:
258: void flush(String line, Object[] rowdata) {
259: Vector vRow = new Vector();
260:
261: int bix = 0;
262: int eix = 0;
263:
264: while ((eix = line.indexOf(getDelimiter(), bix)) != -1) {
265: String value = line.substring(bix, eix);
266: bix = eix + getDelimiter().length();
267:
268: if (cbxNull.isSelected() && value.trim().length() == 0)
269: value = null;
270: if (cbxTrim.isSelected() && value != null)
271: value = value.trim();
272:
273: vRow.addElement(value);
274: }
275:
276: if (bix < line.length() - 1) {
277: String value = line.substring(bix);
278:
279: if (cbxNull.isSelected() && value.trim().length() == 0)
280: value = null;
281: if (cbxTrim.isSelected() && value != null)
282: value = value.trim();
283:
284: vRow.addElement(value);
285: }
286:
287: vRow.toArray(rowdata);
288: }
289: }
290: }
|