📄 connectiondialog.java
字号:
package com.jimw.mysqlplus;
import java.util.Set;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ConnectionDialog implements ActionListener {
static String[] ConnectOptionNames = { "Connect" };
JPanel connectionPanel;
JTextField userNameField;
JTextField passwordField;
JTextField serverField;
JTextField driverField;
JComboBox aliasList;
HashMap cfgMap;
public ConnectionDialog(HashMap cfgMap) {
this.cfgMap = cfgMap;
createConnectionDialog();
}
private void createConnectionDialog() {
JLabel userNameLabel = new JLabel("User name: ", JLabel.RIGHT);
userNameField = new JTextField();
JLabel passwordLabel = new JLabel("Password: ", JLabel.RIGHT);
passwordField = new JTextField();
JLabel serverLabel = new JLabel("Database URL: ", JLabel.RIGHT);
serverField = new JTextField();
JLabel driverLabel = new JLabel("Driver: ", JLabel.RIGHT);
driverField = new JTextField();
driverField.setEditable(false);
JLabel aliasLabel = new JLabel("Database Alias: ", JLabel.RIGHT);
aliasList = new JComboBox(cfgMap.keySet().toArray());
aliasList.setEditable(false);
aliasList.addActionListener(this);
connectionPanel = new JPanel(false);
connectionPanel.setLayout(new BoxLayout(connectionPanel,
BoxLayout.X_AXIS));
JPanel namePanel = new JPanel(false);
namePanel.setLayout(new GridLayout(0, 1));
namePanel.add(userNameLabel);
namePanel.add(passwordLabel);
namePanel.add(serverLabel);
namePanel.add(driverLabel);
namePanel.add(aliasLabel);
JPanel fieldPanel = new JPanel(false);
fieldPanel.setLayout(new GridLayout(0, 1));
fieldPanel.add(userNameField);
fieldPanel.add(passwordField);
fieldPanel.add(serverField);
fieldPanel.add(driverField);
fieldPanel.add(aliasList);
connectionPanel.add(namePanel);
connectionPanel.add(fieldPanel);
}
public JDBCAdapter show() {
Object key = aliasList.getSelectedItem();
CfgStru cfgStru = (CfgStru)cfgMap.get(key);
userNameField.setText(cfgStru.username);
passwordField.setText(cfgStru.password);
serverField.setText(cfgStru.serverURL);
driverField.setText(cfgStru.driver);
if(JOptionPane.showOptionDialog(null, connectionPanel, "connection...",
JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE,
null, ConnectOptionNames, ConnectOptionNames[0]) == 0) {
String url = serverField.getText().trim();
String driver = driverField.getText().trim();
String username = userNameField.getText().trim();
String password = passwordField.getText().trim();
if ( "".equals(username) || "".equals(url) ) {
JOptionPane.showMessageDialog(null,"Error:Some filed is null!");
return null;
}
return connect(url,driver,username,password);
} else
return null;
}
private JDBCAdapter connect(String url,String driver,String username,String password) {
JDBCAdapter dataBase = new JDBCAdapter(url,driver,username,password);
return dataBase;
}
public String getAlias() {
return (String)aliasList.getSelectedItem() ;
}
public void actionPerformed(ActionEvent e) {
JComboBox cb = (JComboBox)e.getSource();
Object key = cb.getSelectedItem();
CfgStru cfgStru = (CfgStru)cfgMap.get(key);
userNameField.setText(cfgStru.username);
passwordField.setText(cfgStru.password);
serverField.setText(cfgStru.serverURL);
driverField.setText(cfgStru.driver);
}
}
class HistoryListDlg extends JDialog {
final String title = "Display History Command";
JList historyList;
DefaultListModel listModel;
MySqlPlusFrame frame;
HistoryListDlg(MySqlPlusFrame frame) {
super(frame, true);
this.frame = frame;
this.setTitle(title);
final JButton getButton = new JButton("Get");
getButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
getOneCommand();
}
});
final JButton closeButton = new JButton("Close");
closeButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setVisible(false);
}
});
listModel = new DefaultListModel();
historyList = new JList(listModel);
historyList.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
historyList.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
getButton.doClick();
}
}
});
JScrollPane listScroller = new JScrollPane(historyList);
JLabel titleLabel = new JLabel(title+":");
JPanel listPane = new JPanel();
listPane.setLayout(new BorderLayout());
listPane.add(titleLabel,BorderLayout.NORTH);
listPane.add(listScroller,BorderLayout.CENTER);
JPanel buttonPane = new JPanel();
buttonPane.add(getButton);
buttonPane.add(closeButton);
Container contentPane = getContentPane();
contentPane.add(listPane, BorderLayout.CENTER);
contentPane.add(buttonPane, BorderLayout.SOUTH);
pack();
}
public void setList(HashSet set) {
listModel.clear();
Iterator iterator = set.iterator();
while (iterator.hasNext()) {
listModel.addElement(iterator.next());
}
}
void getOneCommand() {
int index = historyList.getSelectedIndex();
if (index == -1) return;
String str = (String)historyList.getSelectedValue();
((InternalFrame)MySqlPlusFrame.internalFrameMap.get(new Integer(MySqlPlusFrame.activeWindow))).addCommand(str);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -