001: /*
002: * ImportOptionsPanel.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.dataimport;
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 javax.swing.JComboBox;
019: import javax.swing.JLabel;
020: import javax.swing.JPanel;
021: import javax.swing.border.Border;
022: import javax.swing.border.CompoundBorder;
023: import javax.swing.border.EmptyBorder;
024: import workbench.db.importer.ProducerFactory;
025: import workbench.gui.components.DividerBorder;
026: import workbench.interfaces.EncodingSelector;
027: import workbench.resource.Settings;
028:
029: /**
030: *
031: * @author support@sql-workbench.net
032: *
033: */
034: public class ImportOptionsPanel extends JPanel implements
035: EncodingSelector, ActionListener {
036: private JPanel typePanel;
037: private CardLayout card;
038: private JComboBox typeSelector;
039: private GeneralImportOptionsPanel generalOptions;
040: private TextOptionsPanel textOptions;
041: private XmlOptionsPanel xmlOptions;
042: private int currentType = -1;
043:
044: public ImportOptionsPanel() {
045: super ();
046: this .setLayout(new BorderLayout());
047: this .generalOptions = new GeneralImportOptionsPanel();
048: JPanel p = new JPanel();
049: p.setLayout(new BorderLayout());
050: p.add(this .generalOptions, BorderLayout.CENTER);
051:
052: JPanel s = new JPanel(new BorderLayout(2, 2));
053: Border b = new CompoundBorder(DividerBorder.BOTTOM_DIVIDER,
054: new EmptyBorder(0, 0, 5, 0));
055: s.setBorder(b);
056: typeSelector = new JComboBox();
057: typeSelector.addItem("Text");
058: typeSelector.addItem("XML");
059: JLabel type = new JLabel("Type");
060: s.add(type, BorderLayout.WEST);
061: s.add(typeSelector, BorderLayout.CENTER);
062: p.add(s, BorderLayout.SOUTH);
063:
064: this .add(p, BorderLayout.NORTH);
065:
066: this .textOptions = new TextOptionsPanel();
067: this .typePanel = new JPanel();
068: this .card = new CardLayout();
069: this .typePanel.setLayout(card);
070: this .typePanel.add(this .textOptions, "text");
071:
072: this .xmlOptions = new XmlOptionsPanel();
073: this .typePanel.add(this .xmlOptions, "xml");
074:
075: this .add(typePanel, BorderLayout.CENTER);
076: typeSelector.addActionListener(this );
077: }
078:
079: public void allowImportTypeSelection(boolean flag) {
080:
081: }
082:
083: public void allowImportModeSelection(boolean flag) {
084: this .generalOptions.setModeSelectorEnabled(flag);
085: }
086:
087: public void saveSettings() {
088: this .generalOptions.saveSettings();
089: this .textOptions.saveSettings();
090: this .xmlOptions.saveSettings();
091: Settings.getInstance().setProperty("workbench.import.type",
092: Integer.toString(this .currentType));
093: }
094:
095: public void restoreSettings() {
096: this .generalOptions.restoreSettings();
097: this .textOptions.restoreSettings();
098: this .xmlOptions.restoreSettings();
099: int type = Settings.getInstance().getIntProperty(
100: "workbench.import.type", -1);
101: this .setImportType(type);
102: }
103:
104: /**
105: * Sets the displayed options according to
106: * DataExporter.EXPORT_XXXX types
107: */
108: public void setImportType(int type) {
109: switch (type) {
110: case ProducerFactory.IMPORT_TEXT:
111: setTypeText();
112: break;
113: case ProducerFactory.IMPORT_XML:
114: setTypeXml();
115: break;
116: }
117: }
118:
119: public int getImportType() {
120: return this .currentType;
121: }
122:
123: public void setTypeText() {
124: this .card.show(this .typePanel, "text");
125: this .currentType = ProducerFactory.IMPORT_TEXT;
126: typeSelector.setSelectedIndex(0);
127: }
128:
129: public void setTypeXml() {
130: this .card.show(this .typePanel, "xml");
131: this .currentType = ProducerFactory.IMPORT_XML;
132: typeSelector.setSelectedIndex(1);
133: }
134:
135: public ImportOptions getGeneralOptions() {
136: return this .generalOptions;
137: }
138:
139: public TextImportOptions getTextOptions() {
140: return textOptions;
141: }
142:
143: public XmlImportOptions getXmlOptions() {
144: return xmlOptions;
145: }
146:
147: public String getEncoding() {
148: return generalOptions.getEncoding();
149: }
150:
151: public void setEncoding(String enc) {
152: generalOptions.setEncoding(enc);
153: }
154:
155: public void actionPerformed(ActionEvent event) {
156: if (event.getSource() == this .typeSelector) {
157: String item = typeSelector.getSelectedItem().toString()
158: .toLowerCase();
159: int oldType = this .currentType;
160:
161: this .card.show(this .typePanel, item);
162:
163: if ("text".equals(item))
164: this .currentType = ProducerFactory.IMPORT_TEXT;
165: else if ("xml".equals(item))
166: this .currentType = ProducerFactory.IMPORT_XML;
167: firePropertyChange("exportType", oldType, this.currentType);
168: }
169: }
170:
171: }
|