001: /*
002: * ExportOptionsPanel.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.dialogs.export;
013:
014: import java.awt.BorderLayout;
015: import java.awt.CardLayout;
016: import java.awt.event.ActionEvent;
017: import java.awt.event.ActionListener;
018: import java.util.List;
019: import javax.swing.JComboBox;
020: import javax.swing.JLabel;
021: import javax.swing.JOptionPane;
022: import javax.swing.JPanel;
023: import javax.swing.SwingUtilities;
024: import javax.swing.border.Border;
025: import javax.swing.border.CompoundBorder;
026: import javax.swing.border.EmptyBorder;
027: import workbench.db.ColumnIdentifier;
028: import workbench.db.WbConnection;
029: import workbench.db.exporter.DataExporter;
030: import workbench.db.exporter.PoiHelper;
031: import workbench.gui.WbSwingUtilities;
032: import workbench.gui.components.ColumnSelectorPanel;
033: import workbench.gui.components.DividerBorder;
034: import workbench.interfaces.EncodingSelector;
035: import workbench.log.LogMgr;
036: import workbench.resource.ResourceMgr;
037: import workbench.resource.Settings;
038: import workbench.storage.ResultInfo;
039: import workbench.util.SqlUtil;
040:
041: /**
042: *
043: * @author support@sql-workbench.net
044: *
045: */
046: public class ExportOptionsPanel extends JPanel implements
047: EncodingSelector, ActionListener {
048: private GeneralExportOptionsPanel generalOptions;
049: private JPanel typePanel;
050: private CardLayout card;
051: private JComboBox typeSelector;
052: private TextOptionsPanel textOptions;
053: private SqlOptionsPanel sqlOptions;
054: private HtmlOptionsPanel htmlOptions;
055: private XmlOptionsPanel xmlOptions;
056: private SpreadSheetOptionsPanel odsOptions;
057: private SpreadSheetOptionsPanel xlsOptions;
058: private SpreadSheetOptionsPanel xlsxOptions;
059: private int currentType = -1;
060: private List<ColumnIdentifier> selectedColumns;
061: private Object columnSelectEventSource;
062: private ColumnSelectorPanel columnSelectorPanel;
063: private ResultInfo dataStoreColumns;
064: private String query;
065: private WbConnection dbConnection;
066: private boolean poiAvailable = false;
067:
068: private final String ODS_ITEM = ResourceMgr.getString("TxtOdsName");
069: private final String XLS_ITEM = ResourceMgr.getString("TxtXlsName");
070: private final String XLSX_ITEM = "XLS (XML)";
071:
072: public ExportOptionsPanel() {
073: this (null);
074: }
075:
076: public ExportOptionsPanel(ResultInfo columns) {
077: super ();
078: this .setLayout(new BorderLayout());
079: boolean allowColumnSelection = (columns != null);
080: this .dataStoreColumns = columns;
081: this .generalOptions = new GeneralExportOptionsPanel();
082: generalOptions.allowSelectColumns(allowColumnSelection);
083: if (allowColumnSelection) {
084: generalOptions.showSelectColumnsLabel();
085: this .columnSelectEventSource = generalOptions
086: .addColumnSelectListener(this );
087: }
088: JPanel p = new JPanel();
089: p.setLayout(new BorderLayout());
090: p.add(this .generalOptions, BorderLayout.CENTER);
091:
092: JPanel s = new JPanel(new BorderLayout(2, 2));
093: Border b = new CompoundBorder(DividerBorder.BOTTOM_DIVIDER,
094: new EmptyBorder(0, 0, 5, 0));
095: s.setBorder(b);
096:
097: poiAvailable = PoiHelper.isPoiAvailable();
098:
099: typeSelector = new JComboBox();
100: typeSelector.addItem("Text");
101: typeSelector.addItem("SQL");
102: typeSelector.addItem("XML");
103: typeSelector.addItem(ODS_ITEM);
104: typeSelector.addItem("HTML");
105: typeSelector.addItem(XLSX_ITEM);
106: if (poiAvailable) {
107: typeSelector.addItem(XLS_ITEM);
108: }
109:
110: JLabel type = new JLabel("Type");
111: s.add(type, BorderLayout.WEST);
112: s.add(typeSelector, BorderLayout.CENTER);
113: p.add(s, BorderLayout.SOUTH);
114:
115: this .add(p, BorderLayout.NORTH);
116:
117: this .textOptions = new TextOptionsPanel();
118: this .typePanel = new JPanel();
119: this .card = new CardLayout();
120: this .typePanel.setLayout(card);
121: this .typePanel.add(this .textOptions, "text");
122:
123: this .sqlOptions = new SqlOptionsPanel(columns);
124: this .typePanel.add(this .sqlOptions, "sql");
125:
126: xmlOptions = new XmlOptionsPanel();
127: this .typePanel.add(xmlOptions, "xml");
128:
129: odsOptions = new SpreadSheetOptionsPanel("ods");
130: this .typePanel.add(odsOptions, "ods");
131:
132: htmlOptions = new HtmlOptionsPanel();
133: this .typePanel.add(htmlOptions, "html");
134:
135: xlsxOptions = new SpreadSheetOptionsPanel("xlsx");
136: this .typePanel.add(xlsxOptions, "xlsx");
137:
138: if (poiAvailable) {
139: xlsOptions = new SpreadSheetOptionsPanel("xls");
140: this .typePanel.add(xlsOptions, "xls");
141: }
142:
143: this .add(typePanel, BorderLayout.CENTER);
144: typeSelector.addActionListener(this );
145: }
146:
147: public void setQuerySql(String sql, WbConnection con) {
148: this .query = sql;
149: this .dbConnection = con;
150: this .dataStoreColumns = null;
151: generalOptions.allowSelectColumns(true);
152: generalOptions.showRetrieveColumnsLabel();
153: if (this .columnSelectEventSource == null) {
154: this .columnSelectEventSource = generalOptions
155: .addColumnSelectListener(this );
156: }
157: }
158:
159: public void setIncludeSqlUpdate(boolean flag) {
160: this .sqlOptions.setIncludeUpdate(flag);
161: }
162:
163: public void setIncludeSqlDeleteInsert(boolean flag) {
164: this .sqlOptions.setIncludeDeleteInsert(flag);
165: }
166:
167: public List<ColumnIdentifier> getColumnsToExport() {
168: return this .selectedColumns;
169: }
170:
171: public void saveSettings() {
172: this .generalOptions.saveSettings();
173: this .sqlOptions.saveSettings();
174: this .textOptions.saveSettings();
175: this .htmlOptions.saveSettings();
176: this .xmlOptions.saveSettings();
177: this .odsOptions.saveSettings();
178: this .xlsxOptions.saveSettings();
179: if (this .xlsOptions != null) {
180: this .xlsOptions.saveSettings();
181: }
182: Settings.getInstance().setProperty("workbench.export.type",
183: Integer.toString(this .currentType));
184: }
185:
186: public void restoreSettings() {
187: this .generalOptions.restoreSettings();
188: this .sqlOptions.restoreSettings();
189: this .textOptions.restoreSettings();
190: this .htmlOptions.restoreSettings();
191: this .xmlOptions.restoreSettings();
192: this .odsOptions.restoreSettings();
193: this .xlsxOptions.restoreSettings();
194: if (this .xlsOptions != null) {
195: this .xlsOptions.restoreSettings();
196: }
197: int type = Settings.getInstance().getIntProperty(
198: "workbench.export.type", -1);
199: this .setExportType(type);
200: }
201:
202: /**
203: * Sets the displayed options according to
204: * DataExporter.EXPORT_XXXX types
205: */
206: public void setExportType(int type) {
207: switch (type) {
208: case DataExporter.EXPORT_HTML:
209: setTypeHtml();
210: break;
211: case DataExporter.EXPORT_SQL:
212: setTypeSql();
213: break;
214: case DataExporter.EXPORT_TXT:
215: setTypeText();
216: break;
217: case DataExporter.EXPORT_XML:
218: setTypeXml();
219: break;
220: case DataExporter.EXPORT_ODS:
221: setTypeOds();
222: break;
223: case DataExporter.EXPORT_XLSX:
224: setTypeXlsX();
225: break;
226: case DataExporter.EXPORT_XLS:
227: if (poiAvailable)
228: setTypeXls();
229: break;
230: }
231: }
232:
233: public int getExportType() {
234: return this .currentType;
235: }
236:
237: private void showTextOptions() {
238: this .card.show(this .typePanel, "text");
239: }
240:
241: public void setTypeText() {
242: showTextOptions();
243: this .currentType = DataExporter.EXPORT_TXT;
244: typeSelector.setSelectedItem("Text");
245: }
246:
247: private void showSqlOptions() {
248: this .card.show(this .typePanel, "sql");
249: }
250:
251: public void setTypeSql() {
252: showSqlOptions();
253: this .currentType = DataExporter.EXPORT_SQL;
254: typeSelector.setSelectedItem("SQL");
255: }
256:
257: private void showXmlOptions() {
258: this .card.show(this .typePanel, "xml");
259: }
260:
261: public void setTypeXml() {
262: showXmlOptions();
263: this .currentType = DataExporter.EXPORT_XML;
264: typeSelector.setSelectedItem("XML");
265: }
266:
267: private void showHtmlOptions() {
268: this .card.show(this .typePanel, "html");
269: }
270:
271: public void setTypeHtml() {
272: showHtmlOptions();
273: this .currentType = DataExporter.EXPORT_HTML;
274: typeSelector.setSelectedItem("HTML");
275: }
276:
277: private void showOdsOptions() {
278: this .card.show(this .typePanel, "ods");
279: }
280:
281: public void setTypeOds() {
282: showOdsOptions();
283: this .currentType = DataExporter.EXPORT_ODS;
284: typeSelector.setSelectedItem(ODS_ITEM);
285: }
286:
287: private void showXlsOptions() {
288: this .card.show(this .typePanel, "xls");
289: }
290:
291: private void showXlsXOptions() {
292: this .card.show(this .typePanel, "xlsx");
293: }
294:
295: public void setTypeXls() {
296: showXlsOptions();
297: this .currentType = DataExporter.EXPORT_XLS;
298: typeSelector.setSelectedItem(XLS_ITEM);
299: }
300:
301: public void setTypeXlsX() {
302: showXlsXOptions();
303: this .currentType = DataExporter.EXPORT_XLSX;
304: typeSelector.setSelectedItem(XLSX_ITEM);
305: }
306:
307: public SpreadSheetOptions getXlsOptions() {
308: return xlsOptions;
309: }
310:
311: public SpreadSheetOptions getXlsXOptions() {
312: return xlsxOptions;
313: }
314:
315: public SpreadSheetOptions getOdsOptions() {
316: return odsOptions;
317: }
318:
319: public XmlOptions getXmlOptions() {
320: return xmlOptions;
321: }
322:
323: public HtmlOptions getHtmlOptions() {
324: return htmlOptions;
325: }
326:
327: public SqlOptions getSqlOptions() {
328: return sqlOptions;
329: }
330:
331: public ExportOptions getExportOptions() {
332: return generalOptions;
333: }
334:
335: public TextOptions getTextOptions() {
336: return textOptions;
337: }
338:
339: public String getEncoding() {
340: return generalOptions.getEncoding();
341: }
342:
343: public void setEncoding(String enc) {
344: generalOptions.setEncoding(enc);
345: }
346:
347: public void actionPerformed(ActionEvent event) {
348: if (event.getSource() == this .typeSelector) {
349: Object item = typeSelector.getSelectedItem();
350: String itemValue = item.toString();
351: int type = -1;
352:
353: this .card.show(this .typePanel, itemValue);
354:
355: if ("text".equalsIgnoreCase(itemValue)) {
356: type = DataExporter.EXPORT_TXT;
357: showTextOptions();
358: } else if ("sql".equalsIgnoreCase(itemValue)) {
359: type = DataExporter.EXPORT_SQL;
360: showSqlOptions();
361: } else if ("xml".equalsIgnoreCase(itemValue)) {
362: type = DataExporter.EXPORT_XML;
363: showXmlOptions();
364: } else if (item == ODS_ITEM) {
365: type = DataExporter.EXPORT_ODS;
366: showOdsOptions();
367: } else if (item == XLSX_ITEM) {
368: type = DataExporter.EXPORT_XLSX;
369: showXlsXOptions();
370: } else if (item == XLS_ITEM && poiAvailable) {
371: type = DataExporter.EXPORT_XLS;
372: showXlsOptions();
373: } else if ("html".equalsIgnoreCase(itemValue)) {
374: type = DataExporter.EXPORT_HTML;
375: showHtmlOptions();
376: }
377:
378: this .currentType = type;
379: firePropertyChange("exportType", -1, type);
380: } else if (event.getSource() == this .columnSelectEventSource) {
381: this .selectColumns();
382: }
383: }
384:
385: private void retrieveQueryColumns() {
386: try {
387: WbSwingUtilities.showWaitCursor(this );
388: this .dataStoreColumns = SqlUtil.getResultInfoFromQuery(
389: this .query, this .dbConnection);
390: sqlOptions.setResultInfo(this .dataStoreColumns);
391: } catch (Exception e) {
392: this .dataStoreColumns = null;
393: LogMgr.logError(
394: "ExportOptionsPanel.retrieveQueryColumns()",
395: "Could not retrieve query columns", e);
396: } finally {
397: WbSwingUtilities.showDefaultCursor(this );
398: }
399: }
400:
401: private void selectColumns() {
402: if (this .dataStoreColumns == null) {
403: if (this .query != null) {
404: retrieveQueryColumns();
405: }
406: }
407: if (this .dataStoreColumns == null)
408: return;
409:
410: if (this .columnSelectorPanel == null) {
411: this .columnSelectorPanel = new ColumnSelectorPanel(
412: this .dataStoreColumns.getColumns());
413: this .columnSelectorPanel.selectAll();
414: } else {
415: this .columnSelectorPanel
416: .selectColumns(this .selectedColumns);
417: }
418:
419: int choice = JOptionPane
420: .showConfirmDialog(
421: SwingUtilities.getWindowAncestor(this ),
422: this .columnSelectorPanel,
423: ResourceMgr
424: .getString("MsgSelectColumnsWindowTitle"),
425: JOptionPane.OK_CANCEL_OPTION,
426: JOptionPane.PLAIN_MESSAGE);
427:
428: if (choice == JOptionPane.OK_OPTION) {
429: this.selectedColumns = this.columnSelectorPanel
430: .getSelectedColumns();
431: }
432: }
433:
434: }
|