001: package org.jsqltool.replication.gui;
002:
003: import javax.swing.*;
004: import java.awt.*;
005: import java.awt.event.*;
006: import javax.swing.event.*;
007: import java.io.*;
008: import java.util.*;
009:
010: import org.jsqltool.replication.*;
011: import org.jsqltool.gui.*;
012: import org.jsqltool.utils.ImageLoader;
013: import org.jsqltool.utils.Options;
014: import org.jsqltool.conn.gui.ConnectionFrame;
015: import org.jsqltool.conn.DbConnection;
016: import org.jsqltool.conn.DbConnectionUtil;
017:
018: /**
019: * <p>Title: JSqlTool Project</p>
020: * <p>Description: Window used to define a data replication profile.
021: * </p>
022: * <p>Copyright: Copyright (C) 2006 Mauro Carniel</p>
023: *
024: * <p> This file is part of JSqlTool project.
025: * This library is free software; you can redistribute it and/or
026: * modify it under the terms of the (LGPL) Lesser General Public
027: * License as published by the Free Software Foundation;
028: *
029: * GNU LESSER GENERAL PUBLIC LICENSE
030: * Version 2.1, February 1999
031: *
032: * This library is distributed in the hope that it will be useful,
033: * but WITHOUT ANY WARRANTY; without even the implied warranty of
034: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
035: * Library General Public License for more details.
036: *
037: * You should have received a copy of the GNU Library General Public
038: * License along with this library; if not, write to the Free
039: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
040: *
041: * The author may be contacted at:
042: * maurocarniel@tin.it</p>
043: *
044: * @author Mauro Carniel
045: * @version 1.0
046: */
047: public class ReplicationDialog extends JDialog {
048: JPanel mainPanel = new JPanel();
049: GridBagLayout gridBagLayout1 = new GridBagLayout();
050: JLabel nameLabel = new JLabel();
051: JTextField nameTF = new JTextField();
052: JLabel srcLabel = new JLabel();
053: JComboBox srcComboBox = new JComboBox();
054: JLabel destLabel = new JLabel();
055: JComboBox destComboBox = new JComboBox();
056: JPanel buttonsPanel = new JPanel();
057: JButton okButton = new JButton();
058: JButton cancelButton = new JButton();
059: JLabel tablesLabel = new JLabel();
060: JScrollPane tablesScrollPane = new JScrollPane();
061: JList tablesList = new JList();
062: JCheckBox recreateCheckBox = new JCheckBox();
063:
064: public static final int INSERT = 0;
065: public static final int EDIT = 1;
066:
067: /** current window mode (edit/insert)*/
068: private int mode;
069:
070: /** current replication profile */
071: private ReplicationProfile profile = null;
072:
073: /** parent frame */
074: private ReplicationFrame parentFrame = null;
075:
076: /** main frame */
077: private MainFrame frame = null;
078:
079: /** connections defined */
080: private ArrayList conns = null;
081:
082: public ReplicationDialog(MainFrame frame,
083: ReplicationFrame parentFrame, ReplicationProfile profile,
084: int mode) {
085: super (frame, Options.getInstance().getResource(
086: "data replication profile"), true);
087: this .frame = frame;
088: this .parentFrame = parentFrame;
089: this .profile = profile;
090: this .mode = mode;
091: init();
092: try {
093: jbInit();
094: setSize(500, 400);
095: Dimension screenSize = Toolkit.getDefaultToolkit()
096: .getScreenSize();
097: Dimension frameSize = this .getSize();
098: if (frameSize.height > screenSize.height) {
099: frameSize.height = screenSize.height;
100: }
101: if (frameSize.width > screenSize.width) {
102: frameSize.width = screenSize.width;
103: }
104: this .setLocation((screenSize.width - frameSize.width) / 2,
105: (screenSize.height - frameSize.height) / 2);
106: setDefaultCloseOperation(this .DISPOSE_ON_CLOSE);
107: setVisible(true);
108: } catch (Exception ex) {
109: ex.printStackTrace();
110: }
111: }
112:
113: public ReplicationDialog() {
114: try {
115: jbInit();
116: setSize(500, 400);
117: } catch (Exception ex) {
118: ex.printStackTrace();
119: }
120: }
121:
122: /**
123: * Load combo-box connections and set window content according to the window mode.
124: */
125: private void init() {
126: try {
127: // load combo-box connections...
128: ConnectionFrame f = new ConnectionFrame();
129: conns = f.getConnections();
130: DbConnection dbConn = null;
131: DbConnection srcDbConn = null;
132: for (int i = 0; i < conns.size(); i++) {
133: dbConn = (DbConnection) conns.get(i);
134: srcComboBox.addItem(dbConn.getName());
135: destComboBox.addItem(dbConn.getName());
136: if (mode == EDIT
137: && profile.getSourceDatabase().equals(
138: dbConn.getName())) {
139: srcDbConn = dbConn;
140: }
141: }
142: srcComboBox.setSelectedIndex(-1);
143: destComboBox.setSelectedIndex(-1);
144:
145: // set window content according to the window mode...
146: if (mode == EDIT) {
147: nameTF.setText(profile.getName());
148: srcComboBox
149: .setSelectedItem(profile.getSourceDatabase());
150: destComboBox.setSelectedItem(profile.getDestDatabase());
151: recreateCheckBox.setSelected(profile
152: .isRecreateTablesContent());
153: DefaultListModel model = initTablesList(srcDbConn);
154: int[] indexes = new int[profile.getTablesList().size()];
155: for (int i = 0; i < profile.getTablesList().size(); i++) {
156: indexes[i] = model.indexOf(profile.getTablesList()
157: .get(i));
158: }
159: tablesList.setSelectedIndices(indexes);
160: nameTF.setEditable(false);
161: srcComboBox.setEnabled(false);
162: destComboBox.setEnabled(false);
163: }
164: } catch (Exception ex) {
165: ex.printStackTrace();
166: JOptionPane.showMessageDialog(frame, Options.getInstance()
167: .getResource("error when initializing window")
168: + ":\n" + ex.getMessage(), Options.getInstance()
169: .getResource("error"), JOptionPane.ERROR_MESSAGE);
170: }
171: }
172:
173: /**
174: * Fill in the tables list, according to the selected source database.
175: * @param dbConn source database descriptor
176: * @return tables list model
177: */
178: private DefaultListModel initTablesList(DbConnection dbConn) {
179: DefaultListModel model = new DefaultListModel();
180: if (dbConn == null) {
181: tablesList.setModel(model);
182: tablesList.revalidate();
183: tablesList.repaint();
184: return model;
185: }
186:
187: DbConnectionUtil dbUtil = new DbConnectionUtil(frame, dbConn);
188: java.util.List tables = dbUtil.getTables(dbConn.getUsername(),
189: "TABLE");
190: for (int i = 0; i < tables.size(); i++)
191: model.addElement(tables.get(i));
192: tablesList.setModel(model);
193: tablesList.revalidate();
194: tablesList.repaint();
195: return model;
196: }
197:
198: private void jbInit() throws Exception {
199: mainPanel.setLayout(gridBagLayout1);
200: nameLabel.setText(Options.getInstance().getResource(
201: "profile name"));
202: nameTF.setColumns(20);
203: srcLabel.setText(Options.getInstance().getResource(
204: "source database"));
205: destLabel.setText(Options.getInstance().getResource(
206: "target database"));
207: buttonsPanel.setBorder(BorderFactory.createEtchedBorder());
208: mainPanel.setBorder(BorderFactory.createEtchedBorder());
209: okButton.setMnemonic(Options.getInstance().getResource(
210: "okbutton.mnemonic").charAt(0));
211: okButton.setText(Options.getInstance().getResource(
212: "okbutton.text"));
213: okButton
214: .addActionListener(new ReplicationDialog_okButton_actionAdapter(
215: this ));
216: cancelButton.setMnemonic(Options.getInstance().getResource(
217: "cancelbutton.mnemonic").charAt(0));
218: cancelButton.setText(Options.getInstance().getResource(
219: "cancelbutton.text"));
220: cancelButton
221: .addActionListener(new ReplicationDialog_cancelButton_actionAdapter(
222: this ));
223: destComboBox
224: .addItemListener(new ReplicationDialog_destComboBox_itemAdapter(
225: this ));
226: srcComboBox
227: .addItemListener(new ReplicationDialog_srcComboBox_itemAdapter(
228: this ));
229: tablesLabel.setText(Options.getInstance().getResource(
230: "source tables"));
231: recreateCheckBox.setText(Options.getInstance().getResource(
232: "re-create tables content"));
233: getContentPane().add(mainPanel);
234: mainPanel.add(nameLabel, new GridBagConstraints(0, 0, 1, 1,
235: 0.0, 0.0, GridBagConstraints.WEST,
236: GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
237: mainPanel.add(nameTF, new GridBagConstraints(1, 0, 3, 1, 1.0,
238: 0.0, GridBagConstraints.NORTHWEST,
239: GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
240: mainPanel.add(srcLabel, new GridBagConstraints(0, 1, 1, 1, 0.0,
241: 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE,
242: new Insets(5, 5, 5, 5), 0, 0));
243: mainPanel.add(srcComboBox, new GridBagConstraints(1, 1, 1, 1,
244: 1.0, 0.0, GridBagConstraints.NORTHWEST,
245: GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5),
246: 0, 0));
247: mainPanel.add(destLabel, new GridBagConstraints(2, 1, 1, 1,
248: 0.0, 0.0, GridBagConstraints.WEST,
249: GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
250: mainPanel.add(destComboBox, new GridBagConstraints(3, 1, 1, 1,
251: 1.0, 0.0, GridBagConstraints.NORTHWEST,
252: GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5),
253: 0, 0));
254: this .getContentPane().add(buttonsPanel, BorderLayout.SOUTH);
255: buttonsPanel.add(okButton, null);
256: buttonsPanel.add(cancelButton, null);
257: mainPanel
258: .add(tablesLabel, new GridBagConstraints(0, 2, 4, 1,
259: 0.0, 0.0, GridBagConstraints.NORTHWEST,
260: GridBagConstraints.NONE,
261: new Insets(15, 5, 5, 5), 0, 0));
262: mainPanel.add(tablesScrollPane, new GridBagConstraints(0, 3, 4,
263: 1, 1.0, 1.0, GridBagConstraints.NORTHWEST,
264: GridBagConstraints.BOTH, new Insets(0, 5, 5, 5), 0, 0));
265: mainPanel.add(recreateCheckBox, new GridBagConstraints(0, 4, 4,
266: 1, 0.0, 0.0, GridBagConstraints.NORTHWEST,
267: GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
268: tablesScrollPane.getViewport().add(tablesList, null);
269: }
270:
271: void okButton_actionPerformed(ActionEvent e) {
272: if (nameTF.getText().trim().length() == 0) {
273: JOptionPane.showMessageDialog(frame, Options.getInstance()
274: .getResource("you must set a profile name"),
275: Options.getInstance().getResource("attention"),
276: JOptionPane.ERROR_MESSAGE);
277: return;
278: }
279: if (srcComboBox.getSelectedIndex() == -1
280: || destComboBox.getSelectedIndex() == -1) {
281: JOptionPane
282: .showMessageDialog(
283: frame,
284: Options
285: .getInstance()
286: .getResource(
287: "you must set a source and target database"),
288: Options.getInstance().getResource(
289: "attention"),
290: JOptionPane.ERROR_MESSAGE);
291: return;
292: }
293: if (tablesList.getSelectedIndices().length == 0) {
294: JOptionPane.showMessageDialog(frame, Options.getInstance()
295: .getResource("you must select at least one table"),
296: Options.getInstance().getResource("attention"),
297: JOptionPane.ERROR_MESSAGE);
298: return;
299: }
300:
301: // save the current profile...
302: profile.setName(nameTF.getText().trim());
303: profile.setSourceDatabase(srcComboBox.getSelectedItem()
304: .toString());
305: profile.setDestDatabase(destComboBox.getSelectedItem()
306: .toString());
307: profile.setRecreateTablesContent(recreateCheckBox.isSelected());
308: ArrayList tables = new ArrayList();
309: for (int i = 0; i < tablesList.getSelectedValues().length; i++)
310: tables.add(tablesList.getSelectedValues()[i].toString());
311: profile.setTablesList(tables);
312: parentFrame.updateList(profile, mode == EDIT);
313: setVisible(false);
314: }
315:
316: void cancelButton_actionPerformed(ActionEvent e) {
317: setVisible(false);
318: }
319:
320: void destComboBox_itemStateChanged(ItemEvent e) {
321: if (e.getStateChange() == e.SELECTED
322: && destComboBox.getSelectedIndex() != -1) {
323: if (srcComboBox.getSelectedIndex() != -1
324: && srcComboBox.getSelectedItem().equals(
325: destComboBox.getSelectedItem())) {
326: JOptionPane
327: .showMessageDialog(
328: frame,
329: Options
330: .getInstance()
331: .getResource(
332: "you cannot set the same connection for source and target database"),
333: Options.getInstance().getResource(
334: "attention"),
335: JOptionPane.ERROR_MESSAGE);
336: destComboBox.setSelectedIndex(-1);
337: }
338: }
339:
340: }
341:
342: void srcComboBox_itemStateChanged(ItemEvent e) {
343: if (e.getStateChange() == e.SELECTED
344: && srcComboBox.getSelectedIndex() != -1) {
345: if (destComboBox.getSelectedIndex() != -1
346: && destComboBox.getSelectedItem().equals(
347: srcComboBox.getSelectedItem())) {
348: JOptionPane
349: .showMessageDialog(
350: frame,
351: Options
352: .getInstance()
353: .getResource(
354: "you cannot set the same connection for source and target database"),
355: Options.getInstance().getResource(
356: "attention"),
357: JOptionPane.ERROR_MESSAGE);
358: srcComboBox.setSelectedIndex(-1);
359: return;
360: }
361: initTablesList(srcComboBox.getSelectedIndex() == -1 ? null
362: : (DbConnection) conns.get(srcComboBox
363: .getSelectedIndex()));
364: }
365:
366: }
367:
368: }
369:
370: class ReplicationDialog_okButton_actionAdapter implements
371: java.awt.event.ActionListener {
372: ReplicationDialog adaptee;
373:
374: ReplicationDialog_okButton_actionAdapter(ReplicationDialog adaptee) {
375: this .adaptee = adaptee;
376: }
377:
378: public void actionPerformed(ActionEvent e) {
379: adaptee.okButton_actionPerformed(e);
380: }
381: }
382:
383: class ReplicationDialog_cancelButton_actionAdapter implements
384: java.awt.event.ActionListener {
385: ReplicationDialog adaptee;
386:
387: ReplicationDialog_cancelButton_actionAdapter(
388: ReplicationDialog adaptee) {
389: this .adaptee = adaptee;
390: }
391:
392: public void actionPerformed(ActionEvent e) {
393: adaptee.cancelButton_actionPerformed(e);
394: }
395: }
396:
397: class ReplicationDialog_destComboBox_itemAdapter implements
398: java.awt.event.ItemListener {
399: ReplicationDialog adaptee;
400:
401: ReplicationDialog_destComboBox_itemAdapter(ReplicationDialog adaptee) {
402: this .adaptee = adaptee;
403: }
404:
405: public void itemStateChanged(ItemEvent e) {
406: adaptee.destComboBox_itemStateChanged(e);
407: }
408: }
409:
410: class ReplicationDialog_srcComboBox_itemAdapter implements
411: java.awt.event.ItemListener {
412: ReplicationDialog adaptee;
413:
414: ReplicationDialog_srcComboBox_itemAdapter(ReplicationDialog adaptee) {
415: this .adaptee = adaptee;
416: }
417:
418: public void itemStateChanged(ItemEvent e) {
419: adaptee.srcComboBox_itemStateChanged(e);
420: }
421: }
|