001: package net.sourceforge.squirrel_sql.fw.gui.action;
002:
003: import net.sourceforge.squirrel_sql.fw.gui.GUIUtils;
004: import net.sourceforge.squirrel_sql.fw.util.StringManager;
005: import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
006:
007: import javax.swing.*;
008: import java.io.File;
009: import java.nio.charset.Charset;
010: import java.nio.charset.IllegalCharsetNameException;
011: import java.awt.event.*;
012: import java.awt.*;
013: import java.util.prefs.Preferences;
014:
015: public class TableExportCsvController {
016: private static final String PREF_KEY_CSV_FILE = "SquirrelSQL.csvexport.csvfile";
017: private static final String PREF_KEY_CSV_ENCODING = "SquirrelSQL.csvexport.csvencoding";
018: private static final String PREF_KEY_WITH_HEADERS = "SquirrelSQL.csvexport.withColumnHeaders";
019: private static final String PREF_KEY_SEPERATOR_TAB = "SquirrelSQL.csvexport.sepearatorTab";
020: private static final String PREF_KEY_SEPERATOR_CHAR = "SquirrelSQL.csvexport.sepearatorChar";
021: private static final String PREF_KEY_EXPORT_COMPLETE = "SquirrelSQL.csvexport.exportcomplete";
022: private static final String PREF_KEY_USE_GLOBAL_PREFS_FORMATING = "SquirrelSQL.csvexport.useGlobalPrefsFomating";
023: private static final String PREF_KEY_EXECUTE_COMMAND = "SquirrelSQL.csvexport.executeCommand";
024: private static final String PREF_KEY_COMMAND = "SquirrelSQL.csvexport.commandString";
025: private static final String PREF_KEY_FORMAT_CSV = "SquirrelSQL.csvexport.formatCSV";
026: private static final String PREF_KEY_FORMAT_XLS = "SquirrelSQL.csvexport.formatXLS";
027:
028: private static final StringManager s_stringMgr = StringManagerFactory
029: .getStringManager(TableExportCsvController.class);
030:
031: private TableExportCsvDlg _dlg;
032: private boolean _ok = false;
033: public static final int EXPORT_FORMAT_CSV = 0;
034: public static final int EXPORT_FORMAT_XLS = 1;
035:
036: TableExportCsvController() {
037: _dlg = new TableExportCsvDlg();
038:
039: initDlg();
040:
041: initListeners();
042:
043: _dlg.txtSeparatorChar.addKeyListener(new KeyAdapter() {
044: public void keyTyped(KeyEvent e) {
045: onSeparatorCharChanged(e);
046: }
047: });
048:
049: _dlg.getRootPane().setDefaultButton(_dlg.btnOk);
050: installEscapeClose();
051:
052: _dlg.pack();
053:
054: GUIUtils.centerWithinParent(_dlg);
055:
056: _dlg.setVisible(true);
057:
058: }
059:
060: private void onSeparatorCharChanged(KeyEvent e) {
061: SwingUtilities.invokeLater(new Runnable() {
062: public void run() {
063: String text = _dlg.txtSeparatorChar.getText();
064: if (null != text && 1 < text.length()) {
065: _dlg.txtSeparatorChar.setText(text.substring(0, 1));
066: Toolkit.getDefaultToolkit().beep();
067: }
068: }
069: });
070:
071: }
072:
073: private void initListeners() {
074: _dlg.btnOk.addActionListener(new ActionListener() {
075: public void actionPerformed(ActionEvent e) {
076: onOK();
077: }
078: });
079:
080: _dlg.btnCancel.addActionListener(new ActionListener() {
081: public void actionPerformed(ActionEvent e) {
082: closeDlg();
083: }
084: });
085:
086: _dlg.radFormatCSV.addActionListener(new ActionListener() {
087: public void actionPerformed(ActionEvent e) {
088: onFormat(true);
089: }
090: });
091:
092: _dlg.radFormatXLS.addActionListener(new ActionListener() {
093: public void actionPerformed(ActionEvent e) {
094: onFormat(true);
095: }
096: });
097:
098: _dlg.chkSeparatorTab.addActionListener(new ActionListener() {
099: public void actionPerformed(ActionEvent e) {
100: onFormat(false);
101: }
102: });
103:
104: _dlg.chkExecCommand.addActionListener(new ActionListener() {
105: public void actionPerformed(ActionEvent e) {
106: onChkExecCommand();
107: }
108: });
109:
110: _dlg.btnFile.addActionListener(new ActionListener() {
111: public void actionPerformed(ActionEvent e) {
112: onFile();
113: }
114:
115: });
116:
117: _dlg.btnCommandFile.addActionListener(new ActionListener() {
118: public void actionPerformed(ActionEvent e) {
119: onCommandFile();
120: }
121: });
122: }
123:
124: private void onFormat(boolean replaceEnding) {
125:
126: if (_dlg.radFormatCSV.isSelected()) {
127: _dlg.lblSeparator.setEnabled(true);
128: _dlg.chkSeparatorTab.setEnabled(true);
129: _dlg.txtSeparatorChar.setEnabled(true);
130: _dlg.lblCharset.setEnabled(true);
131: _dlg.charsets.setEnabled(true);
132:
133: if (_dlg.chkSeparatorTab.isSelected()) {
134: _dlg.txtSeparatorChar.setText(null);
135: _dlg.txtSeparatorChar.setEnabled(false);
136: _dlg.lblSeparator.setEnabled(false);
137: } else {
138: _dlg.txtSeparatorChar.setEnabled(true);
139: _dlg.lblSeparator.setEnabled(true);
140: }
141:
142: if (replaceEnding) {
143: replaceFileEnding();
144: }
145: } else if (_dlg.radFormatXLS.isSelected()) {
146: _dlg.lblSeparator.setEnabled(false);
147: _dlg.lblCharset.setEnabled(false);
148: _dlg.chkSeparatorTab.setEnabled(false);
149: _dlg.txtSeparatorChar.setEnabled(false);
150: _dlg.charsets.setEnabled(false);
151: if (replaceEnding) {
152: replaceFileEnding();
153: }
154: } else {
155: throw new IllegalStateException("No valid output format");
156: }
157: }
158:
159: private void replaceFileEnding() {
160: String newEnding;
161:
162: if (_dlg.radFormatCSV.isSelected()) {
163: newEnding = "csv";
164: } else if (_dlg.radFormatXLS.isSelected()) {
165: newEnding = "xls";
166: } else {
167: throw new IllegalStateException("No valid output format");
168: }
169:
170: String file = _dlg.txtFile.getText();
171: if (null == file
172: || 0 == file.trim().length()
173: || file.toUpperCase().endsWith(
174: "." + newEnding.toUpperCase())) {
175: return;
176: }
177:
178: file = file.trim();
179:
180: String newFile;
181:
182: if (-1 == file.lastIndexOf(".")
183: || file.lastIndexOf(".") < file
184: .lastIndexOf(File.separator)) {
185: newFile = file + "." + newEnding;
186: } else if (file.lastIndexOf(".") > file
187: .lastIndexOf(File.separator)) {
188: newFile = file.substring(0, file.lastIndexOf(".")) + "."
189: + newEnding;
190: } else {
191: newFile = file;
192: }
193:
194: _dlg.txtFile.setText(newFile);
195: }
196:
197: private void onCommandFile() {
198: JFileChooser chooser = new JFileChooser(System.getProperties()
199: .getProperty("user.home"));
200: chooser
201: .setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
202:
203: // i18n[TableExportCsvController.commandChooserTitel=Choose command executable]
204: chooser
205: .setDialogTitle(s_stringMgr
206: .getString("TableExportCsvController.commandChooserTitel"));
207:
208: // i18n[TableExportCsvController.commandChooserButton=Choose]
209: if (JFileChooser.APPROVE_OPTION != chooser
210: .showDialog(
211: _dlg,
212: s_stringMgr
213: .getString("TableExportCsvController.commandChooserButton"))) {
214: return;
215: }
216:
217: if (null != chooser.getSelectedFile()) {
218: _dlg.txtCommand.setText(chooser.getSelectedFile().getPath()
219: + " %file");
220: }
221: }
222:
223: private void onFile() {
224: JFileChooser chooser = null;
225:
226: String csvFileName = _dlg.txtFile.getText();
227: if (null != csvFileName && 0 < csvFileName.trim().length()) {
228: File csvFile = new File(csvFileName);
229:
230: File parentFile = csvFile.getParentFile();
231: if (null != parentFile && parentFile.exists()) {
232: chooser = new JFileChooser(parentFile);
233: }
234: }
235:
236: if (null == chooser) {
237: chooser = new JFileChooser(System.getProperties()
238: .getProperty("user.home"));
239: }
240:
241: chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
242:
243: // i18n[TableExportCsvController.fileChooserTitel=Choose export file]
244: chooser
245: .setDialogTitle(s_stringMgr
246: .getString("TableExportCsvController.fileChooserTitel"));
247:
248: // i18n[TableExportCsvController.fileChooserButton=Choose]
249: if (JFileChooser.APPROVE_OPTION != chooser
250: .showDialog(
251: _dlg,
252: s_stringMgr
253: .getString("TableExportCsvController.fileChooserButton"))) {
254: return;
255: }
256:
257: if (null != chooser.getSelectedFile()) {
258: _dlg.txtFile.setText(chooser.getSelectedFile().getPath());
259: }
260:
261: }
262:
263: private void onOK() {
264: String csvFileName = _dlg.txtFile.getText();
265: if (null == csvFileName || 0 == csvFileName.trim().length()) {
266: // i18n[TableExportCsvController.noFile=You must provide a export file name.]
267: String msg = s_stringMgr
268: .getString("TableExportCsvController.noFile");
269: JOptionPane.showMessageDialog(_dlg, msg);
270: return;
271: }
272:
273: if (false == _dlg.chkSeparatorTab.isSelected()) {
274: String sepChar = _dlg.txtSeparatorChar.getText();
275: if (null == sepChar || 1 != sepChar.trim().length()) {
276: // i18n[TableExportCsvController.invalidSeparator=You must provide a single separator character or check "Use tab" to use the tab character.]
277: String msg = s_stringMgr
278: .getString("TableExportCsvController.invalidSeparator");
279: JOptionPane.showMessageDialog(_dlg, msg);
280: return;
281: }
282: }
283:
284: if (_dlg.chkExecCommand.isSelected()) {
285: String command = _dlg.txtCommand.getText();
286: if (null == command || 0 == command.trim().length()) {
287: // i18n[TableExportCsvController.noCommand=You must provide a command string or uncheck "Execute command".]
288: String msg = s_stringMgr
289: .getString("TableExportCsvController.noCommand");
290: JOptionPane.showMessageDialog(_dlg, msg);
291: return;
292: }
293: }
294:
295: if (new File(csvFileName).exists()) {
296: // i18n[TableExportCsvController.replaceFile=The export file already exisits. Would you like to replace it?]
297: String msg = s_stringMgr
298: .getString("TableExportCsvController.replaceFile");
299: if (JOptionPane.OK_OPTION != JOptionPane.showConfirmDialog(
300: _dlg, msg)) {
301: return;
302: }
303: }
304:
305: writePrefs();
306: closeDlg();
307: _ok = true;
308: }
309:
310: private void writePrefs() {
311: Preferences.userRoot().put(PREF_KEY_CSV_FILE,
312: _dlg.txtFile.getText());
313: Preferences.userRoot().put(PREF_KEY_CSV_ENCODING,
314: _dlg.charsets.getSelectedItem().toString());
315: Preferences.userRoot().putBoolean(PREF_KEY_WITH_HEADERS,
316: _dlg.chkWithHeaders.isSelected());
317: Preferences.userRoot().putBoolean(PREF_KEY_FORMAT_CSV,
318: _dlg.radFormatCSV.isSelected());
319: Preferences.userRoot().putBoolean(PREF_KEY_FORMAT_XLS,
320: _dlg.radFormatXLS.isSelected());
321: Preferences.userRoot().putBoolean(PREF_KEY_SEPERATOR_TAB,
322: _dlg.chkSeparatorTab.isSelected());
323: Preferences.userRoot().put(PREF_KEY_SEPERATOR_CHAR,
324: _dlg.txtSeparatorChar.getText());
325: Preferences.userRoot().putBoolean(PREF_KEY_EXPORT_COMPLETE,
326: _dlg.radComplete.isSelected());
327: Preferences.userRoot().putBoolean(
328: PREF_KEY_USE_GLOBAL_PREFS_FORMATING,
329: _dlg.radUseGlobalPrefsFormating.isSelected());
330: Preferences.userRoot().putBoolean(PREF_KEY_EXECUTE_COMMAND,
331: _dlg.chkExecCommand.isSelected());
332: Preferences.userRoot().put(PREF_KEY_COMMAND,
333: _dlg.txtCommand.getText());
334: }
335:
336: private void initDlg() {
337: _dlg.txtFile.setText(Preferences.userRoot().get(
338: PREF_KEY_CSV_FILE, null));
339: _dlg.charsets
340: .setSelectedItem(Preferences.userRoot().get(
341: PREF_KEY_CSV_ENCODING,
342: Charset.defaultCharset().name()));
343: _dlg.chkWithHeaders.setSelected(Preferences.userRoot()
344: .getBoolean(PREF_KEY_WITH_HEADERS, true));
345:
346: _dlg.chkSeparatorTab.setSelected(Preferences.userRoot()
347: .getBoolean(PREF_KEY_SEPERATOR_TAB, false));
348:
349: if (false == _dlg.chkSeparatorTab.isSelected()) {
350: _dlg.txtSeparatorChar.setText(Preferences.userRoot().get(
351: PREF_KEY_SEPERATOR_CHAR, ","));
352: }
353:
354: if (Preferences.userRoot()
355: .getBoolean(PREF_KEY_FORMAT_CSV, true)) {
356: _dlg.radFormatCSV.setSelected(true);
357: } else if (Preferences.userRoot().getBoolean(
358: PREF_KEY_FORMAT_XLS, false)) {
359: _dlg.radFormatXLS.setSelected(true);
360: } else {
361: _dlg.radFormatCSV.setSelected(true);
362: }
363:
364: onFormat(false);
365:
366: if (Preferences.userRoot().getBoolean(PREF_KEY_EXPORT_COMPLETE,
367: true)) {
368: _dlg.radComplete.setSelected(true);
369: } else {
370: _dlg.radSelection.setSelected(true);
371: }
372:
373: if (Preferences.userRoot().getBoolean(
374: PREF_KEY_USE_GLOBAL_PREFS_FORMATING, true)) {
375: _dlg.radUseGlobalPrefsFormating.setSelected(true);
376: } else {
377: _dlg.radUseDefaultFormating.setSelected(true);
378: }
379:
380: _dlg.chkExecCommand.setSelected(Preferences.userRoot()
381: .getBoolean(PREF_KEY_EXECUTE_COMMAND, false));
382: onChkExecCommand();
383:
384: _dlg.txtCommand.setText(Preferences.userRoot().get(
385: PREF_KEY_COMMAND, "openoffice.org-2.0 -calc %file"));
386: }
387:
388: private void onChkExecCommand() {
389: _dlg.txtCommand.setEnabled(_dlg.chkExecCommand.isSelected());
390: _dlg.btnCommandFile
391: .setEnabled(_dlg.chkExecCommand.isSelected());
392: }
393:
394: private void installEscapeClose() {
395: AbstractAction closeAction = new AbstractAction() {
396: public void actionPerformed(ActionEvent actionEvent) {
397: closeDlg();
398: }
399: };
400: KeyStroke escapeStroke = KeyStroke.getKeyStroke(
401: KeyEvent.VK_ESCAPE, 0);
402: _dlg.getRootPane().getInputMap(
403: JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(
404: escapeStroke, "CloseAction");
405: _dlg.getRootPane().getInputMap(
406: JComponent.WHEN_IN_FOCUSED_WINDOW).put(escapeStroke,
407: "CloseAction");
408: _dlg.getRootPane().getInputMap(JComponent.WHEN_FOCUSED).put(
409: escapeStroke, "CloseAction");
410: _dlg.getRootPane().getActionMap().put("CloseAction",
411: closeAction);
412: }
413:
414: private void closeDlg() {
415: _dlg.setVisible(false);
416: _dlg.dispose();
417: }
418:
419: boolean isOK() {
420: return _ok;
421: }
422:
423: File getFile() {
424: return new File(_dlg.txtFile.getText());
425: }
426:
427: String getSeparatorChar() {
428: if (_dlg.chkSeparatorTab.isSelected()) {
429: return "\t";
430: } else {
431: return _dlg.txtSeparatorChar.getText();
432: }
433: }
434:
435: Charset getCSVCharset() {
436: try {
437: return Charset.forName(_dlg.charsets.getSelectedItem()
438: .toString());
439: } catch (IllegalCharsetNameException icne) {
440: return Charset.defaultCharset();
441: }
442: }
443:
444: boolean includeHeaders() {
445: return _dlg.chkWithHeaders.isSelected();
446: }
447:
448: boolean exportComplete() {
449: return _dlg.radComplete.isSelected();
450: }
451:
452: boolean useGloablPrefsFormatting() {
453: return _dlg.radUseGlobalPrefsFormating.isSelected();
454: }
455:
456: String getCommand() {
457: if (_dlg.chkExecCommand.isSelected()) {
458: // Copied from Java Doc Matcher.replaceAll:
459: //
460: // Note that backslashes (\) and dollar signs ($) in the replacement string
461: // may cause the results to be different than if it
462: // were being treated as a literal replacement string.
463: // Dollar signs may be treated as references to
464: // captured subsequences as described above, and
465: // backslashes are used to escape literal characters in the replacement string.
466: return _dlg.txtCommand.getText().replaceAll(
467: "%file",
468: _dlg.txtFile.getText().replaceAll("\\\\",
469: "\\\\\\\\"));
470: } else {
471: return null;
472: }
473: }
474:
475: public int getExportFormat() {
476: if (_dlg.radFormatCSV.isSelected()) {
477: return EXPORT_FORMAT_CSV;
478: } else if (_dlg.radFormatXLS.isSelected()) {
479: return EXPORT_FORMAT_XLS;
480: } else {
481: throw new IllegalStateException("No valid output format");
482: }
483:
484: }
485: }
|