001: /*
002: * ImportStringVerifier.java
003: *
004: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
005: *
006: * Copyright 2002-2008, Thomas Kellerer
007: * No part of this code maybe reused without the permission of the author
008: *
009: * To contact the author please send an email to: support@sql-workbench.net
010: *
011: */
012: package workbench.gui.sql;
013:
014: import java.awt.BorderLayout;
015: import java.awt.Color;
016: import java.awt.Dimension;
017: import java.awt.Frame;
018: import java.io.BufferedReader;
019: import java.io.StringReader;
020: import java.util.Iterator;
021: import java.util.List;
022: import javax.swing.JPanel;
023: import javax.swing.JScrollPane;
024: import javax.swing.JTextArea;
025: import javax.swing.JTextField;
026: import javax.swing.border.Border;
027: import javax.swing.border.EmptyBorder;
028: import workbench.WbManager;
029: import workbench.db.ColumnIdentifier;
030: import workbench.db.importer.TextFileParser;
031: import workbench.gui.WbSwingUtilities;
032: import workbench.gui.components.ValidatingDialog;
033: import workbench.gui.dialogs.dataimport.GeneralImportOptionsPanel;
034: import workbench.gui.dialogs.dataimport.ImportOptions;
035: import workbench.gui.dialogs.dataimport.TextImportOptions;
036: import workbench.gui.dialogs.dataimport.TextOptionsPanel;
037: import workbench.resource.ResourceMgr;
038: import workbench.resource.Settings;
039: import workbench.storage.ResultInfo;
040: import workbench.util.ClipboardFile;
041: import workbench.util.StringUtil;
042:
043: /**
044: * This class checks the content of an input string and tries to match
045: * it against a ResultInfo.
046: *
047: * @author support@sql-workbench.net
048: */
049: public class ImportStringVerifier {
050: private ResultInfo target;
051: private String content;
052: private JPanel optionsPanel;
053: private TextOptionsPanel textOptions;
054: private GeneralImportOptionsPanel generalOptions;
055: private boolean columnNamesMatched = false;
056: private boolean columnCountMatched = false;
057:
058: public ImportStringVerifier(String data, ResultInfo result) {
059: this .target = result;
060: this .content = data;
061: }
062:
063: /**
064: * Check the contents of the data string if it matches
065: * the structure of our target ResultInfo.
066: * The user will be prompted to correct the problems
067: * if possible.
068: * The following things are checked:
069: * <ul>
070: * <li>the input data has to have a header line which defines the columns</li>
071: * <li>If there is no header line (i.e. no matching columns found) then
072: * the import is OK, if the column count is the same</li>
073: * <li>at least one column from the input data must occur in the ResultInfo</li>
074: * </ul>
075: *
076: * @return false if the data cannot be imported
077: */
078: public boolean checkData() {
079: ClipboardFile f = new ClipboardFile(this .content);
080: TextFileParser parser = new TextFileParser(f);
081: parser.setContainsHeader(true);
082: if (this .textOptions != null) {
083: // textOptions != null then we have displayed the options
084: // dialog to the user. If the ClipBoard does not contain
085: // a header line, we simply assume that it matches the c
086: // columns from the result set.
087: if (!textOptions.getContainsHeader())
088: return true;
089:
090: parser.setDelimiter(textOptions.getTextDelimiter());
091: }
092: List cols = parser.getColumnsFromFile();
093:
094: int matchingColumns = 0;
095: Iterator itr = cols.iterator();
096: while (itr.hasNext()) {
097: ColumnIdentifier col = (ColumnIdentifier) itr.next();
098: if (target.findColumn(col.getColumnName()) > -1) {
099: matchingColumns++;
100: }
101: }
102:
103: this .columnCountMatched = (cols.size() == target
104: .getColumnCount());
105: this .columnNamesMatched = (matchingColumns > 0);
106: return (columnCountMatched || columnNamesMatched);
107: }
108:
109: private void createOptionsPanel() {
110: if (this .optionsPanel != null)
111: return;
112:
113: this .textOptions = new TextOptionsPanel();
114: this .textOptions.restoreSettings("clipboard");
115: if (this .columnCountMatched && !this .columnNamesMatched) {
116: textOptions.setContainsHeader(false);
117: }
118:
119: this .generalOptions = new GeneralImportOptionsPanel();
120: this .generalOptions.setModeSelectorEnabled(false);
121: this .generalOptions.setEncodingVisible(false);
122: this .generalOptions.restoreSettings("clipboard");
123:
124: this .optionsPanel = new JPanel(new BorderLayout());
125: this .optionsPanel.add(generalOptions, BorderLayout.NORTH);
126: this .optionsPanel.add(textOptions, BorderLayout.SOUTH);
127: }
128:
129: /**
130: * If no columns are found, then most probably the (default) column
131: * delimiter is not correct, so let the user supply the import options
132: */
133: public boolean showOptionsDialog() {
134: createOptionsPanel();
135: JPanel p = new JPanel(new BorderLayout(0, 5));
136:
137: JTextArea preview = new JTextArea();
138: StringBuilder s = getLines(content, 15);
139: int l = s.length();
140: for (int i = 0; i < l; i++) {
141: if (s.charAt(i) == '\t')
142: s.setCharAt(i, '\u00bb');
143: }
144: preview.setText(s.toString());
145: preview.setFont(Settings.getInstance().getEditorFont());
146: preview.setEditable(false);
147: preview.setDisabledTextColor(Color.BLACK);
148: preview.setCaretPosition(0);
149:
150: JScrollPane scroll = new JScrollPane(preview);
151: Dimension d = new Dimension(350, 250);
152: preview.setMaximumSize(d);
153: scroll.setMaximumSize(d);
154: scroll.setPreferredSize(d);
155:
156: JTextField msg = new JTextField();
157: msg.setEnabled(false);
158: msg.setText(ResourceMgr.getString("MsgClipFormat"));
159: msg.setBackground(Color.WHITE);
160: msg.setDisabledTextColor(Color.BLACK);
161: Border b = new EmptyBorder(4, 2, 4, 2);
162: msg.setBorder(b);
163:
164: p.add(this .optionsPanel, BorderLayout.EAST);
165: p.add(msg, BorderLayout.NORTH);
166: p.add(scroll, BorderLayout.CENTER);
167:
168: Frame f = WbManager.getInstance().getCurrentWindow();
169: ValidatingDialog dialog = new ValidatingDialog(f, "Import", p);
170: WbSwingUtilities.center(dialog, f);
171: dialog.setVisible(true);
172: boolean ok = !dialog.isCancelled();
173: textOptions.saveSettings("clipboard");
174: return ok;
175: }
176:
177: public ImportOptions getImportOptions() {
178: return generalOptions;
179: }
180:
181: public TextImportOptions getTextImportOptions() {
182: return textOptions;
183: }
184:
185: public final StringBuilder getLines(String s, int lineCount) {
186: StringBuilder result = new StringBuilder(lineCount * 100);
187: try {
188: BufferedReader r = new BufferedReader(new StringReader(s));
189: int lines = 0;
190: String line = r.readLine();
191: while (line != null && lines < lineCount) {
192: result.append(line);
193: result.append('\n');
194: lines++;
195: line = r.readLine();
196: }
197: } catch (Exception e) {
198: e.printStackTrace();
199: }
200: return result;
201: }
202:
203: }
|