Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- import java.util.ArrayList;
- import java.sql.DriverManager;
- import java.sql.Connection;
- import java.sql.SQLException;
- public class DataSourceWizard extends JPanel implements ActionListener
- {
- private CardLayout layout;
- private JPanel pnlCards;
- private JPanel pnlControls;
- private JButton btnOk;
- private JButton btnCancel;
- private JButton btnNext;
- private JButton btnPrevious;
- private int cardIndex = 0;
- private javax.swing.Timer timer = new javax.swing.Timer(100, this);
- private DataSourceWizardPanel wpnlDataSource;
- private JLabel lblDataSource;
- private DefaultComboBoxModel mdlDataSource;
- private JComboBox cmbDataSource;
- private JLabel lblConnectionString;
- private JTextField txtConnectionString;
- private JLabel lblUsername;
- private JTextField txtUsername;
- private JLabel lblPassword;
- private JTextField txtPassword;
- private JButton btnTestConnection;
- private DataSourceWizardPanel wpnlConnectionSettings;
- public DataSourceWizard()
- {
- this.initializeComponent();
- this.timer.start();
- }
- private final void initializeComponent()
- {
- int yComponent = 20;
- this.layout = new CardLayout();
- this.pnlCards = new JPanel(this.layout);
- this.pnlControls = new JPanel(new FlowLayout(FlowLayout.RIGHT));
- this.btnOk = new JButton("Finish");
- this.btnCancel = new JButton("Cancel");
- this.btnNext = new JButton("Next »");
- this.btnPrevious = new JButton("« Back");
- //
- // The Data Source panel
- //
- this.wpnlDataSource = new DataSourceWizardPanel(this,
- "Data source setup",
- "Choose the data source you want your data to be extracted from.");
- this.lblDataSource = new JLabel("Data Source:");
- this.mdlDataSource = new DefaultComboBoxModel();
- this.cmbDataSource = new JComboBox(this.mdlDataSource);
- this.lblConnectionString = new JLabel("Connection string:");
- this.txtConnectionString = new JTextField("jdbc:odbc:");
- this.lblUsername = new JLabel("Username:");
- this.txtUsername = new JTextField();
- this.lblPassword = new JLabel("Password:");
- this.txtPassword = new JPasswordField();
- this.btnTestConnection = new JButton("Test Connection");
- this.lblDataSource.setDisplayedMnemonic(KeyEvent.VK_D);
- this.lblConnectionString.setDisplayedMnemonic(KeyEvent.VK_C);
- this.lblUsername.setDisplayedMnemonic(KeyEvent.VK_U);
- this.lblPassword.setDisplayedMnemonic(KeyEvent.VK_P);
- this.lblDataSource.setLabelFor(this.cmbDataSource);
- this.lblConnectionString.setLabelFor(this.txtConnectionString);
- this.lblUsername.setLabelFor(this.txtUsername);
- this.lblPassword.setLabelFor(this.txtPassword);
- this.btnTestConnection.setMnemonic(KeyEvent.VK_T);
- this.btnTestConnection.setDefaultCapable(true);
- this.btnTestConnection.addActionListener(this);
- this.lblDataSource.setBounds(20, yComponent, 150, 20);
- this.cmbDataSource.setBounds(160, yComponent, 180, 20);
- this.lblConnectionString.setBounds(20, yComponent += 25, 150, 20);
- this.txtConnectionString.setBounds(160, yComponent, 180, 20);
- this.lblUsername.setBounds(20, yComponent += 25, 150, 20);
- this.txtUsername.setBounds(160, yComponent, 180, 20);
- this.lblPassword.setBounds(20, yComponent += 25, 150, 20);
- this.txtPassword.setBounds(160, yComponent, 180, 20);
- this.btnTestConnection.setBounds(160, yComponent += 25, 120, 20);
- this.wpnlDataSource.addComponent(this.lblDataSource);
- this.wpnlDataSource.addComponent(this.cmbDataSource);
- this.wpnlDataSource.addComponent(this.lblConnectionString);
- this.wpnlDataSource.addComponent(this.txtConnectionString);
- this.wpnlDataSource.addComponent(this.lblUsername);
- this.wpnlDataSource.addComponent(this.txtUsername);
- this.wpnlDataSource.addComponent(this.lblPassword);
- this.wpnlDataSource.addComponent(this.txtPassword);
- this.wpnlDataSource.addComponent(this.btnTestConnection);
- this.mdlDataSource.addElement("JDBC");
- this.wpnlDataSource.pnlContent.setPreferredSize(new Dimension(
- this.wpnlDataSource.pnlContent.getPreferredSize().width, yComponent += 25));
- //
- // The Connection Settings panel
- //
- this.wpnlConnectionSettings = new DataSourceWizardPanel(this,
- "Connection settings",
- "Miscellaneous options required for a connection to be established.");
- //
- // The Controls panel
- //
- this.pnlControls.add(this.btnPrevious);
- this.pnlControls.add(this.btnNext);
- this.pnlControls.add(this.btnCancel);
- this.pnlControls.add(this.btnOk);
- this.btnPrevious.addActionListener(this);
- this.btnNext.addActionListener(this);
- this.pnlCards.add(this.wpnlDataSource);
- this.pnlCards.add(this.wpnlConnectionSettings);
- this.setLayout(new BorderLayout());
- this.add(this.pnlCards, BorderLayout.CENTER);
- this.add(this.pnlControls, BorderLayout.SOUTH);
- }
- public void actionPerformed(ActionEvent e)
- {
- if(e.getSource() == this.timer)
- {
- if(this.cardIndex <= 0)
- {
- this.cardIndex = 0;
- this.btnPrevious.setEnabled(false);
- }
- else if(this.cardIndex > 0)
- this.btnPrevious.setEnabled(true);
- if(this.cardIndex >= this.pnlCards.getComponents().length - 1)
- {
- this.cardIndex = this.pnlCards.getComponents().length - 1;
- this.btnNext.setEnabled(false);
- }
- else if(this.cardIndex < this.pnlCards.getComponents().length - 1)
- this.btnNext.setEnabled(true);
- }
- else if(e.getSource() == this.btnPrevious &&
- this.cardIndex > 0)
- {
- this.layout.previous(this.pnlCards);
- this.cardIndex--;
- }
- else if(e.getSource() == this.btnNext &&
- this.cardIndex < this.pnlCards.getComponents().length-1)
- {
- boolean isNextAllowed = false;
- try
- {
- switch(this.cardIndex)
- {
- case 0:
- isNextAllowed = this.cmbDataSource.
- getSelectedItem().equals("JDBC") &&
- this.txtConnectionString.getText().
- startsWith("jdbc:odbc:");
- break;
- default:
- isNextAllowed = true;
- break;
- }
- }
- catch(Exception ex){ isNextAllowed = false; }
- if(isNextAllowed)
- {
- this.layout.next(this.pnlCards);
- this.cardIndex++;
- }
- }
- else if(e.getSource() == this.btnTestConnection)
- {
- boolean testPassed = false;
- try
- {
- Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
- Connection connection = DriverManager.getConnection(
- this.txtConnectionString.getText().trim(),
- this.txtUsername.getText().trim(),
- this.txtPassword.getText().trim());
- connection.close();
- testPassed = true;
- }
- catch(ClassNotFoundException ex)
- {
- testPassed = false;
- }
- catch(SQLException ex)
- {
- testPassed = false;
- }
- if(testPassed)
- {
- JOptionPane.showMessageDialog((JFrame)null,
- "A connection was successfully established with the provided data source.",
- "Test Passed", JOptionPane.INFORMATION_MESSAGE);
- }
- else
- {
- JOptionPane.showMessageDialog((JFrame)null,
- "A connection could not be established with the provided data source.",
- "Test Failed", JOptionPane.ERROR_MESSAGE);
- }
- }
- }
- public static void main(String[] args)
- {
- JFrame frame = new JFrame("Test");
- frame.setSize(400, 350);
- frame.setLocationRelativeTo(null);
- frame.getContentPane().add(new DataSourceWizard());
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setVisible(true);
- }
- }
- class DataSourceWizardPanel extends JPanel
- {
- private Font defaultFont = new Font("Trebuchet MS", Font.PLAIN, 11);
- private JLabel lblName = null;
- private JLabel lblDescription = null;
- private JPanel pnlHeader = null;
- public JPanel pnlContent = null;
- private DataSourceWizard wizard = null;
- private ArrayList<Component> components = null;
- private JScrollPane spnContent = null;
- public DataSourceWizardPanel(DataSourceWizard wizard)
- {
- this.wizard = wizard;
- this.initializeComponent();
- this.setFont(new Font("Trebuchet MS", Font.PLAIN, 11));
- }
- public DataSourceWizardPanel(DataSourceWizard wizard, String name, String description)
- {
- this(wizard);
- this.lblName.setText(name);
- this.lblDescription.setText("<HTML>" + description);
- }
- private final void initializeComponent()
- {
- this.lblName = new JLabel("Untitled");
- this.lblDescription = new JLabel("<HTML>No description provided");
- this.pnlHeader = new JPanel(new BorderLayout());
- this.pnlContent = new JPanel(null);
- this.components = new ArrayList<Component>();
- this.spnContent = new JScrollPane(this.pnlContent);
- //this.pnlHeader.setPreferredSize(new Dimension(
- // this.pnlHeader.getPreferredSize().width, 90));
- this.pnlHeader.setBackground(new Color(255, 255, 255, 200));
- this.pnlHeader.setBorder(BorderFactory.createCompoundBorder(
- BorderFactory.createLineBorder(new Color(50, 50, 50, 100), 1),
- BorderFactory.createEmptyBorder(5, 5, 5, 5)
- ));
- this.pnlHeader.add(this.lblName, BorderLayout.NORTH);
- this.pnlHeader.add(this.lblDescription, BorderLayout.CENTER);
- this.spnContent.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
- super.setLayout(new BorderLayout());
- super.add(this.pnlHeader, BorderLayout.NORTH);
- super.add(this.spnContent, BorderLayout.CENTER);
- }
- public void addComponent(JComponent component)
- {
- if(this.defaultFont != null) component.setFont(this.defaultFont);
- this.pnlContent.add(component);
- }
- public void setFont(Font font)
- {
- try
- {
- this.defaultFont = font;
- super.setFont(font);
- for(Component component : components)
- {
- if(component != null)
- component.setFont(font);
- }
- if(this.lblName != null)
- {
- this.lblName.setFont(new Font(font.getName(), Font.BOLD, font.getSize()));
- }
- if(this.lblDescription != null) this.lblDescription.setFont(font);
- if(this.pnlHeader != null) this.pnlHeader.setFont(font);
- if(this.pnlContent != null) this.pnlContent.setFont(font);
- }
- catch(Exception ex)
- {
- }
- }
- }
Add Comment
Please, Sign In to add comment