/* * To change this template, choose Tools | Templates * and open the template in the editor. */ /* * DbConfigurationDialog.java * * Created on 2011-12-01, 17:44:14 */ package dbmigrate.gui; import java.awt.Dimension; import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.Properties; import javax.swing.DefaultComboBoxModel; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPasswordField; import javax.swing.JTextField; import dbmigrate.exceptions.ConnectException; import dbmigrate.model.db.DbConnector; // CHECKSTYLE:OFF /** * * @author zyxist */ public class DbConfigurationDialog extends javax.swing.JDialog { public DbConnector getConnector() { return connector; } public void setConnector(DbConnector connector) { this.connector = connector; } private DbConnector connector; private JTextField hostnameText; private JTextField usernameText; private JPasswordField passwordText; private JTextField databaseText; private JTextField sidText; private JComboBox dbTypeCombo; /** Creates new form DbConfigurationDialog */ public DbConfigurationDialog(java.awt.Frame parent, boolean modal) { super(parent, true); this.setResizable(false); this.setPreferredSize(new Dimension(350, 250)); this.initComponents(); this.setTitle("Connection properties"); // create and load default properties Properties defaultProps = new Properties(); FileInputStream in; try { in = new FileInputStream("dbmigrate.properties"); defaultProps.load(in); in.close(); this.hostnameText.setText(defaultProps.getProperty("hostname", "")); this.usernameText.setText(defaultProps.getProperty("username", "")); this.databaseText.setText(defaultProps.getProperty("database", "")); this.passwordText.setText(defaultProps.getProperty("password", "")); this.sidText.setText(defaultProps.getProperty("sid", "")); this.dbTypeCombo.setSelectedIndex(Integer.valueOf(defaultProps .getProperty("dbType", "0"))); } catch (FileNotFoundException e) { } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { Font fontStyle = new Font("Verdana", Font.PLAIN, 11); setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); JLabel lblHostname = new JLabel("Hostname"); lblHostname.setBounds(54, 42, 72, 15); JLabel lblUsername = new JLabel("Username"); lblUsername.setBounds(54, 72, 72, 15); JLabel lblPassword = new JLabel("Password"); lblPassword.setBounds(54, 102, 70, 15); JLabel lblDatabase = new JLabel("Database"); lblDatabase.setBounds(54, 132, 69, 15); this.hostnameText = new JTextField(); this.hostnameText.setBounds(157, 42, 114, 22); this.hostnameText.setColumns(10); this.usernameText = new JTextField(); this.usernameText.setBounds(157, 72, 114, 22); this.usernameText.setColumns(10); this.passwordText = new JPasswordField(); this.passwordText.setBounds(157, 102, 114, 22); this.passwordText.setColumns(10); this.databaseText = new JTextField(); this.databaseText.setBounds(157, 132, 114, 22); this.databaseText.setColumns(10); this.usernameText.setFont(fontStyle); this.hostnameText.setFont(fontStyle); this.passwordText.setFont(fontStyle); this.databaseText.setFont(fontStyle); this.getContentPane().setLayout(null); this.getContentPane().add(lblHostname); this.getContentPane().add(this.hostnameText); this.getContentPane().add(lblPassword); this.getContentPane().add(lblUsername); this.getContentPane().add(lblDatabase); this.getContentPane().add(this.databaseText); this.getContentPane().add(this.passwordText); this.getContentPane().add(this.usernameText); final JLabel sidLabel = new JLabel("Sid"); sidLabel.setBounds(54, 162, 69, 15); getContentPane().add(sidLabel); sidText = new JTextField(); sidText.setFont(fontStyle); sidText.setColumns(10); sidText.setBounds(157, 162, 114, 22); getContentPane().add(sidText); JLabel dbTypeLabel = new JLabel("DB type"); dbTypeLabel.setBounds(54, 12, 85, 15); getContentPane().add(dbTypeLabel); dbTypeCombo = new JComboBox(); dbTypeCombo.setModel(new DefaultComboBoxModel(new String[] { "Postgres", "Oracle" })); dbTypeCombo.setSelectedIndex(0); dbTypeCombo.setBounds(157, 6, 114, 24); getContentPane().add(dbTypeCombo); dbTypeCombo.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if (dbTypeCombo.getSelectedIndex() == 0) { sidText.setVisible(false); sidLabel.setVisible(false); } else { sidText.setVisible(true); sidLabel.setVisible(true); } } }); JButton okButton = new JButton("Ok"); okButton.setBounds(54, 187, 117, 25); this.getContentPane().add(okButton); okButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { try { if (dbTypeCombo.getSelectedIndex() == 0) { DbConfigurationDialog.this.connector .setConnectionParams(DbConnector.POSTGRESQL_DB, DbConfigurationDialog.this.hostnameText .getText(), DbConfigurationDialog.this.databaseText .getText(), DbConfigurationDialog.this.usernameText .getText(), DbConfigurationDialog.this.passwordText .getText()); } else { DbConfigurationDialog.this.connector .setConnectionParams(DbConnector.ORACLE_DB, DbConfigurationDialog.this.hostnameText .getText(), DbConfigurationDialog.this.databaseText .getText(), DbConfigurationDialog.this.usernameText .getText(), DbConfigurationDialog.this.passwordText .getText(), DbConfigurationDialog.this.sidText .getText()); } DbConfigurationDialog.this.connector.getConnection(); DbConfigurationDialog.this.saveProperties(); DbConfigurationDialog.this.setVisible(false); } catch (ConnectException exception) { JOptionPane.showMessageDialog(null, exception.getMessage(), "Connection problem", JOptionPane.WARNING_MESSAGE); } } }); JButton cancelButton = new JButton("Cancel"); cancelButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { saveProperties(); DbConfigurationDialog.this.setVisible(false); } }); cancelButton.setBounds(183, 187, 117, 25); this.getContentPane().add(cancelButton); this.pack(); }// </editor-fold> private void saveProperties() { FileOutputStream out; try { out = new FileOutputStream("dbmigrate.properties"); Properties defaultProps = new Properties(); defaultProps.setProperty("hostname", this.hostnameText.getText()); defaultProps.setProperty("username", this.usernameText.getText()); defaultProps.setProperty("password", this.passwordText.getText()); defaultProps.setProperty("database", this.databaseText.getText()); defaultProps.setProperty("sid", this.sidText.getText()); defaultProps.setProperty("dbType", String.valueOf(this.dbTypeCombo.getSelectedIndex())); defaultProps.store(out, "---No Comment---"); out.close(); } catch (FileNotFoundException e) { } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { } } }