⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dbconnectiondialog.java

📁 报表设计软件,很好的
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/**
 * ========================================================================================
 * JFreeReport Designer : a graphical designer for JFreeReport - a free Java report library
 * ========================================================================================
 *
 * Project Info:  http://www.jfree.org/jfreereport/index.html
 * Project Lead:  Thomas Morgner (taquera@sherito.org);
 *
 * (C) Copyright 2003, by Heiko Evermann (heiko@evermann.de) and Contributors.
 *
 * This library is free software; you can redistribute it and/or modify it under the terms
 * of the GNU Lesser General Public License as published by the Free Software Foundation;
 * either version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along with this
 * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 */

package org.jfree.designer.db;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Enumeration;
import java.util.Vector;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.jfree.layout.FormatLayout;
import org.jfree.report.modules.gui.base.components.ActionButton;

/**
 * This dialog needs a better way to define the values used in the wizzard. I removed it
 * until that better way is implemented!
 */
public final class DBConnectionDialog
        extends JDialog
{
  private final class WizardAction
          extends AbstractAction
  {
    /**
     * Defines an <code>Action</code> object with a default description string and default
     * icon.
     */
    public WizardAction ()
    {
      putValue(Action.NAME, "Wizard");
    }

    /**
     * Invoked when an action occurs.
     */
    public final void actionPerformed (final ActionEvent e)
    {
      onBtWizard();
    }
  }

  private final class TestConnectionAction
          extends AbstractAction
  {
    /**
     * Defines an <code>Action</code> object with a default description string and default
     * icon.
     */
    public TestConnectionAction ()
    {
      putValue(Action.NAME, "Test");
    }

    /**
     * Invoked when an action occurs.
     */
    public final void actionPerformed (final ActionEvent e)
    {
      onBtTest();
    }
  }

  private final class SaveConnectionAction
          extends AbstractAction
  {
    /**
     * Defines an <code>Action</code> object with a default description string and default
     * icon.
     */
    public SaveConnectionAction ()
    {
      putValue(Action.NAME, "Save");
    }

    /**
     * Invoked when an action occurs.
     */
    public final void actionPerformed (final ActionEvent e)
    {
      onBtSave();

    }
  }

  private final class CancelAction
          extends AbstractAction
  {
    /**
     * Defines an <code>Action</code> object with a default description string and default
     * icon.
     */
    public CancelAction ()
    {
      putValue(Action.NAME, "Cancel");
    }

    /**
     * Invoked when an action occurs.
     */
    public final void actionPerformed (final ActionEvent e)
    {
      onBtCancel();
    }
  }

  private JComboBox comboDriverList;
  private JTextField editURL;
  private JTextField editUsername;
  private JTextField editPassword;
  private JTextField editDatabase;
  private JTextField editName;
  private JCheckBox checkBoxSavePwd;
  private ConnectionEntry result;

  public DBConnectionDialog (final JDialog parent)
  {
    super(parent, true); // modal dialog
    initDialog();
  }

  private void initDialog ()
  {

    final JPanel editPanel = new JPanel();

    final int[] columnFormat = {FormatLayout.LC, // "name", editfield
                                FormatLayout.LC, // "JDBC Driver" + combobox
                                FormatLayout.LCB, // "JDBC URL" + editfield + button "wizard"
                                FormatLayout.LC, // "Database" + editfield
                                FormatLayout.LC, // "User Name" + editfield
                                FormatLayout.LCB,
                                // "password" + editfield + checkbox "store password"
    };
    final FormatLayout layout = new FormatLayout(6, columnFormat);
    editPanel.setLayout(layout);

    final JLabel labelName = new JLabel("Name");
    final JLabel labelDriver = new JLabel("JDBC Driver");
    final JLabel labelURL = new JLabel("JDBC URL");
    final JLabel labelDatabase = new JLabel("Database");
    final JLabel labelUserName = new JLabel("User name");
    final JLabel labelPassword = new JLabel("Password");

    comboDriverList = new JComboBox();
    editURL = new JTextField();
    editURL.setMinimumSize(new java.awt.Dimension(200, 20));
    // otherwise it looks much too small
    editURL.setPreferredSize(new java.awt.Dimension(200, 20));

    editUsername = new JTextField();
    editPassword = new JTextField();
    editDatabase = new JTextField();
    editName = new JTextField();
    checkBoxSavePwd = new JCheckBox("save Password");
    final JButton btWizard = new ActionButton(new WizardAction());

    editPanel.add(labelName);
    editPanel.add(editName);
    editPanel.add(labelDriver);
    editPanel.add(comboDriverList);
    editPanel.add(labelURL);
    editPanel.add(editURL);
    editPanel.add(btWizard);
    editPanel.add(labelDatabase);
    editPanel.add(editDatabase);
    editPanel.add(labelUserName);
    editPanel.add(editUsername);
    editPanel.add(labelPassword);
    editPanel.add(editPassword);
    editPanel.add(checkBoxSavePwd);

    final JPanel buttonPanel = new JPanel();
    buttonPanel.setLayout(new GridLayout(1, 4, 5, 5));
    buttonPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
    buttonPanel.add(new ActionButton(new TestConnectionAction()));
    buttonPanel.add(new ActionButton(new SaveConnectionAction()));
    buttonPanel.add(new ActionButton(new CancelAction()));

    final Container pane = getContentPane();
    pane.setLayout(new BorderLayout(5, 5));
    pane.add(editPanel, BorderLayout.NORTH);
    pane.add(buttonPanel, BorderLayout.SOUTH);

    setTitle("New/Modify connection");
    pack();
  }

  private void onBtTest ()
  {
    try
    {
      Class.forName((String) comboDriverList.getSelectedItem()).newInstance();
      final Connection conn =
              DriverManager.getConnection(this.editURL.getText(),
                      this.editUsername.getText(),
                      this.editPassword.getText());
      // if you forget this, and your JDBC source only supports one connection, you get into trouble
      conn.close();
      JOptionPane.showMessageDialog(this,
              "Your connection seems to be valid.",
              "Success",
              JOptionPane.PLAIN_MESSAGE);
    }
    catch (NoClassDefFoundError ex)
    {
      JOptionPane.showMessageDialog(this,
              "NoClassDefFoundError!!\nCheck your classpath!",
              "Error",
              JOptionPane.ERROR_MESSAGE);
    }
    catch (ClassNotFoundException ex)
    {
      JOptionPane.showMessageDialog(this,
              "ClassNotFoundError:\n"
              + ex.getMessage()
              + "\n\nCheck your classpath!",
              "Error",
              JOptionPane.ERROR_MESSAGE);
    }
    catch (SQLException ex)
    {
      JOptionPane.showMessageDialog(this,
              "SQL problems:\n"
              + ex.getMessage()
              + "\n"
              + this.editURL.getText(),
              "Error",
              JOptionPane.ERROR_MESSAGE);
    }
    catch (Exception ex)
    {
      JOptionPane.showMessageDialog(this,
              "General problem:\n"
              + ex.getMessage()
              + "\n\nPlease check datasource name",
              "Error",
              JOptionPane.ERROR_MESSAGE);
    }
  }

  private void onBtSave ()
  {
    // Fill DBConnectionEntry...
    if (editName.getText().trim().length() == 0)
    {

      JOptionPane.showMessageDialog(this,
              "Please enter a name for the connection entry",
              "Data incomplete",
              JOptionPane.WARNING_MESSAGE);
      return;
    }

    result = new ConnectionEntry
            ((String) comboDriverList.getSelectedItem(),

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -