001: /*
002: * SqlOptionsPanel.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.event.ActionListener;
015: import java.util.ArrayList;
016: import java.util.List;
017: import javax.swing.ComboBoxModel;
018: import javax.swing.DefaultComboBoxModel;
019: import javax.swing.JOptionPane;
020: import javax.swing.SwingUtilities;
021: import workbench.db.ColumnIdentifier;
022: import workbench.db.TableIdentifier;
023: import workbench.gui.components.ColumnSelectorPanel;
024: import workbench.gui.components.KeyColumnSelectorPanel;
025: import workbench.resource.ResourceMgr;
026: import workbench.resource.Settings;
027: import workbench.storage.ResultInfo;
028:
029: /**
030: *
031: * @author support@sql-workbench.net
032: */
033: public class SqlOptionsPanel extends javax.swing.JPanel implements
034: SqlOptions, ActionListener {
035: private List keyColumns;
036: private ColumnSelectorPanel columnSelectorPanel;
037: private ResultInfo tableColumns;
038:
039: public SqlOptionsPanel(ResultInfo info) {
040: initComponents();
041: setResultInfo(info);
042: List<String> types = Settings.getInstance()
043: .getLiteralTypeList();
044: ComboBoxModel model = new DefaultComboBoxModel(types.toArray());
045: literalTypes.setModel(model);
046: }
047:
048: public void setResultInfo(ResultInfo info) {
049: this .tableColumns = info;
050:
051: boolean hasColumns = tableColumns != null;
052: boolean keysPresent = (info == null ? false : info
053: .hasPkColumns());
054: this .selectKeys.setEnabled(hasColumns);
055:
056: this .setIncludeDeleteInsert(keysPresent);
057: this .setIncludeUpdate(keysPresent);
058:
059: if (info != null) {
060: TableIdentifier table = info.getUpdateTable();
061: if (table != null) {
062: this .alternateTable.setText(table.getTableName());
063: } else {
064: this .alternateTable.setText("target_table");
065: }
066: }
067: }
068:
069: public void saveSettings() {
070: Settings s = Settings.getInstance();
071: s.setProperty("workbench.export.sql.commitevery", this
072: .getCommitEvery());
073: s.setProperty("workbench.export.sql.createtable", this
074: .getCreateTable());
075: s.setProperty("workbench.export.sql.saveas.dateliterals", this
076: .getDateLiteralType());
077: }
078:
079: public void restoreSettings() {
080: Settings s = Settings.getInstance();
081: this .setCommitEvery(s.getIntProperty(
082: "workbench.export.sql.commitevery", 0));
083: this .setCreateTable(s
084: .getBoolProperty("workbench.export.sql.createtable"));
085: String def = s.getProperty(
086: "workbench.export.sql.default.dateliterals", "dbms");
087: String type = s.getProperty(
088: "workbench.export.sql.saveas.dateliterals", def);
089: this .literalTypes.setSelectedItem(type);
090: }
091:
092: public String getDateLiteralType() {
093: return (String) literalTypes.getSelectedItem();
094: }
095:
096: public String getAlternateUpdateTable() {
097: String s = alternateTable.getText();
098: if (s != null && s.trim().length() > 0)
099: return s.trim();
100: return null;
101: }
102:
103: public void setAlternateUpdateTable(String table) {
104: this .alternateTable
105: .setText((table == null ? "" : table.trim()));
106: }
107:
108: public int getCommitEvery() {
109: int result = -1;
110: try {
111: String value = this .commitCount.getText();
112: if (value != null && value.length() > 0) {
113: result = Integer.parseInt(value);
114: } else {
115: result = 0;
116: }
117: } catch (Exception e) {
118: e.printStackTrace();
119: }
120: return result;
121: }
122:
123: public boolean updateEnabled() {
124: return useUpdate.isEnabled();
125: }
126:
127: public boolean deleteInsertEnabled() {
128: return useDeleteInsert.isEnabled();
129: }
130:
131: public boolean isSqlAllowed() {
132: return updateEnabled() || deleteInsertEnabled();
133: }
134:
135: public void setIncludeUpdate(boolean flag) {
136: useUpdate.setEnabled(flag);
137: }
138:
139: public void setIncludeDeleteInsert(boolean flag) {
140: useDeleteInsert.setEnabled(flag);
141: }
142:
143: public boolean getCreateInsert() {
144: if (useInsert.isSelected())
145: return true;
146: return false;
147: }
148:
149: public boolean getCreateUpdate() {
150: if (useUpdate.isEnabled())
151: return useUpdate.isSelected();
152: return false;
153: }
154:
155: public boolean getCreateDeleteInsert() {
156: if (useDeleteInsert.isEnabled())
157: return useDeleteInsert.isSelected();
158: return false;
159: }
160:
161: public boolean getCreateTable() {
162: return createTable.isSelected();
163: }
164:
165: public void setCommitEvery(int value) {
166: if (value > 0) {
167: this .commitCount.setText(Integer.toString(value));
168: } else {
169: this .commitCount.setText("");
170: }
171: }
172:
173: public void setCreateInsert() {
174: this .useInsert.setSelected(true);
175: }
176:
177: public void setCreateUpdate() {
178: if (this .useUpdate.isEnabled())
179: this .useUpdate.setSelected(true);
180: }
181:
182: public void setCreateDeleteInsert() {
183: if (this .useDeleteInsert.isEnabled())
184: this .useDeleteInsert.setSelected(true);
185: }
186:
187: public void setCreateTable(boolean flag) {
188: this .createTable.setSelected(flag);
189: }
190:
191: public List getKeyColumns() {
192: return keyColumns;
193: }
194:
195: private void selectColumns() {
196: if (this .tableColumns == null)
197: return;
198:
199: if (this .columnSelectorPanel == null) {
200: this .columnSelectorPanel = new KeyColumnSelectorPanel(
201: this .tableColumns.getColumns(), this .tableColumns
202: .getUpdateTable());
203: } else {
204: this .columnSelectorPanel.selectColumns(this .keyColumns);
205: }
206:
207: int choice = JOptionPane
208: .showConfirmDialog(
209: SwingUtilities.getWindowAncestor(this ),
210: this .columnSelectorPanel,
211: ResourceMgr
212: .getString("MsgSelectKeyColumnsWindowTitle"),
213: JOptionPane.OK_CANCEL_OPTION,
214: JOptionPane.PLAIN_MESSAGE);
215:
216: if (choice == JOptionPane.OK_OPTION) {
217: this .keyColumns = null;
218:
219: List selected = this .columnSelectorPanel
220: .getSelectedColumns();
221: int size = selected.size();
222: this .keyColumns = new ArrayList(size);
223: for (int i = 0; i < size; i++) {
224: ColumnIdentifier col = (ColumnIdentifier) selected
225: .get(i);
226: this .keyColumns.add(col.getColumnName());
227: }
228:
229: boolean keysPresent = (size > 0);
230: this .setIncludeDeleteInsert(keysPresent);
231: this .setIncludeUpdate(keysPresent);
232: }
233: }
234:
235: /** This method is called from within the constructor to
236: * initialize the form.
237: * WARNING: Do NOT modify this code. The content of this method is
238: * always regenerated by the Form Editor.
239: */
240: // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
241: private void initComponents() {
242: java.awt.GridBagConstraints gridBagConstraints;
243:
244: typeGroup = new javax.swing.ButtonGroup();
245: createTable = new javax.swing.JCheckBox();
246: useUpdate = new javax.swing.JRadioButton();
247: useInsert = new javax.swing.JRadioButton();
248: alternateTable = new javax.swing.JTextField();
249: jLabel1 = new javax.swing.JLabel();
250: useDeleteInsert = new javax.swing.JRadioButton();
251: selectKeys = new javax.swing.JButton();
252: jPanel1 = new javax.swing.JPanel();
253: jLabel2 = new javax.swing.JLabel();
254: literalTypes = new javax.swing.JComboBox();
255: jPanel2 = new javax.swing.JPanel();
256: commitLabel = new javax.swing.JLabel();
257: commitCount = new javax.swing.JTextField();
258:
259: setLayout(new java.awt.GridBagLayout());
260:
261: createTable.setText(ResourceMgr
262: .getString("LblExportIncludeCreateTable"));
263: createTable.setToolTipText(ResourceMgr
264: .getDescription("LblExportIncludeCreateTable"));
265: gridBagConstraints = new java.awt.GridBagConstraints();
266: gridBagConstraints.gridx = 0;
267: gridBagConstraints.gridy = 1;
268: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
269: gridBagConstraints.weightx = 1.0;
270: gridBagConstraints.insets = new java.awt.Insets(2, 0, 0, 0);
271: add(createTable, gridBagConstraints);
272:
273: typeGroup.add(useUpdate);
274: useUpdate.setText(ResourceMgr.getString("LblExportSqlUpdate"));
275: useUpdate.setToolTipText(ResourceMgr
276: .getDescription("LblExportSqlUpdate"));
277: useUpdate.setEnabled(false);
278: gridBagConstraints = new java.awt.GridBagConstraints();
279: gridBagConstraints.gridx = 0;
280: gridBagConstraints.gridy = 3;
281: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
282: add(useUpdate, gridBagConstraints);
283:
284: typeGroup.add(useInsert);
285: useInsert.setSelected(true);
286: useInsert.setText(ResourceMgr.getString("LblExportSqlInsert"));
287: useInsert.setToolTipText(ResourceMgr
288: .getDescription("LblExportSqlInsert"));
289: gridBagConstraints = new java.awt.GridBagConstraints();
290: gridBagConstraints.gridx = 0;
291: gridBagConstraints.gridy = 2;
292: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
293: gridBagConstraints.insets = new java.awt.Insets(3, 0, 0, 0);
294: add(useInsert, gridBagConstraints);
295:
296: alternateTable.setColumns(15);
297: gridBagConstraints = new java.awt.GridBagConstraints();
298: gridBagConstraints.gridx = 0;
299: gridBagConstraints.gridy = 7;
300: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
301: gridBagConstraints.insets = new java.awt.Insets(2, 4, 0, 0);
302: add(alternateTable, gridBagConstraints);
303:
304: jLabel1.setText(ResourceMgr.getString("LblUseExportTableName"));
305: gridBagConstraints = new java.awt.GridBagConstraints();
306: gridBagConstraints.gridx = 0;
307: gridBagConstraints.gridy = 6;
308: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
309: gridBagConstraints.insets = new java.awt.Insets(5, 4, 0, 0);
310: add(jLabel1, gridBagConstraints);
311:
312: typeGroup.add(useDeleteInsert);
313: useDeleteInsert.setText(ResourceMgr
314: .getString("LblExportSqlDeleteInsert"));
315: useDeleteInsert.setToolTipText(ResourceMgr
316: .getDescription("LblExportSqlDeleteInsert"));
317: useDeleteInsert.setEnabled(false);
318: gridBagConstraints = new java.awt.GridBagConstraints();
319: gridBagConstraints.gridx = 0;
320: gridBagConstraints.gridy = 4;
321: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
322: add(useDeleteInsert, gridBagConstraints);
323:
324: selectKeys
325: .setText(ResourceMgr.getString("LblSelectKeyColumns"));
326: selectKeys.setToolTipText(ResourceMgr
327: .getDescription("LblSelectKeyColumns"));
328: selectKeys.addActionListener(this );
329: gridBagConstraints = new java.awt.GridBagConstraints();
330: gridBagConstraints.gridx = 0;
331: gridBagConstraints.gridy = 5;
332: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
333: gridBagConstraints.insets = new java.awt.Insets(0, 4, 0, 0);
334: add(selectKeys, gridBagConstraints);
335:
336: jPanel1.setLayout(new java.awt.BorderLayout(10, 0));
337:
338: jLabel2.setText(ResourceMgr.getString("LblLiteralType"));
339: jPanel1.add(jLabel2, java.awt.BorderLayout.WEST);
340:
341: literalTypes.setToolTipText(ResourceMgr
342: .getDescription("LblLiteralType"));
343: jPanel1.add(literalTypes, java.awt.BorderLayout.CENTER);
344:
345: gridBagConstraints = new java.awt.GridBagConstraints();
346: gridBagConstraints.gridx = 0;
347: gridBagConstraints.gridy = 8;
348: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
349: gridBagConstraints.weighty = 1.0;
350: gridBagConstraints.insets = new java.awt.Insets(6, 4, 0, 0);
351: add(jPanel1, gridBagConstraints);
352:
353: jPanel2.setLayout(new java.awt.BorderLayout(10, 0));
354:
355: commitLabel.setText(ResourceMgr
356: .getString("LblExportCommitEvery"));
357: jPanel2.add(commitLabel, java.awt.BorderLayout.WEST);
358:
359: commitCount.setColumns(5);
360: jPanel2.add(commitCount, java.awt.BorderLayout.CENTER);
361:
362: gridBagConstraints = new java.awt.GridBagConstraints();
363: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
364: gridBagConstraints.insets = new java.awt.Insets(0, 4, 0, 0);
365: add(jPanel2, gridBagConstraints);
366: }
367:
368: // Code for dispatching events from components to event handlers.
369:
370: public void actionPerformed(java.awt.event.ActionEvent evt) {
371: if (evt.getSource() == selectKeys) {
372: SqlOptionsPanel.this .selectKeysActionPerformed(evt);
373: }
374: }// </editor-fold>//GEN-END:initComponents
375:
376: private void selectKeysActionPerformed(
377: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_selectKeysActionPerformed
378: selectColumns();
379: }//GEN-LAST:event_selectKeysActionPerformed
380:
381: // Variables declaration - do not modify//GEN-BEGIN:variables
382: public javax.swing.JTextField alternateTable;
383: public javax.swing.JTextField commitCount;
384: public javax.swing.JLabel commitLabel;
385: public javax.swing.JCheckBox createTable;
386: public javax.swing.JLabel jLabel1;
387: public javax.swing.JLabel jLabel2;
388: public javax.swing.JPanel jPanel1;
389: public javax.swing.JPanel jPanel2;
390: public javax.swing.JComboBox literalTypes;
391: public javax.swing.JButton selectKeys;
392: public javax.swing.ButtonGroup typeGroup;
393: public javax.swing.JRadioButton useDeleteInsert;
394: public javax.swing.JRadioButton useInsert;
395: public javax.swing.JRadioButton useUpdate;
396: // End of variables declaration//GEN-END:variables
397:
398: }
|