package com.supcon.httpscfgtools;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.text.MessageFormat;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import javax.swing.JToolBar;
/**
*
* @ClassName: MainWindow
* @Description: 配置工具主窗口
* @author: zhanglei
* @date:2016年9月20日 下午1:16:41
*/
public class MainWindow {
private JFrame frame;
private JTextField dbTextField;
private JTextField urlTextField;
private JTextField unameTextField;
private JPasswordField pwdTextField;
private static String dbcfg;
private static String mqcfg;
private static String serviceName;
private String driver;
//private static final Log logger = LogFactory.getLog("tools");
private JTextField mqHostTextField;
private JTextField mqPortTextField;
private final JToolBar toolBar = new JToolBar();
/**
* Launch the application.
*/
public static void main(String[] args) {
// dbcfg = args[0];
// mqcfg = args[1];
// serviceName = args[2];
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MainWindow window = new MainWindow();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public MainWindow() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setIconImage(Toolkit.getDefaultToolkit().getImage(MainWindow.class.getResource("/icons/leftlogo.jpg")));
frame.setTitle("振动监测平台配置工具");
frame.setBounds(100, 100, 684, 446);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
toolBar.setBounds(0, 0, 668, 24);
frame.getContentPane().add(toolBar);
JButton startButton = new JButton("");
startButton.setToolTipText("启动" + serviceName + "服务");
startButton.setIcon(new ImageIcon(MainWindow.class.getResource("/icons/start.gif")));
toolBar.add(startButton);
JButton stopButton = new JButton("");
stopButton.setToolTipText("停止" + serviceName + "服务");
stopButton.setIcon(new ImageIcon(MainWindow.class.getResource("/icons/stop.gif")));
toolBar.add(stopButton);
JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
tabbedPane.setBounds(0, 27, 668, 380);
frame.getContentPane().add(tabbedPane);
JPanel mqPanel = new JPanel();
tabbedPane.addTab("ActiveMQ配置", null, mqPanel, null);
mqPanel.setLayout(null);
JLabel mqHostLabel = new JLabel("主 机 地 址:");
mqHostLabel.setFont(new Font("微软雅黑", Font.PLAIN, 12));
mqHostLabel.setBounds(110, 120, 80, 15);
mqPanel.add(mqHostLabel);
mqHostTextField = new JTextField();
mqHostTextField.setFont(new Font("Consolas", Font.PLAIN, 12));
mqHostTextField.setColumns(45);
mqHostTextField.setBounds(235, 117, 309, 21);
mqPanel.add(mqHostTextField);
JLabel mqPortLabel = new JLabel("端 口:");
mqPortLabel.setFont(new Font("微软雅黑", Font.PLAIN, 12));
mqPortLabel.setBounds(110, 182, 80, 15);
mqPanel.add(mqPortLabel);
mqPortTextField = new JTextField();
mqPortTextField.setFont(new Font("Consolas", Font.PLAIN, 12));
mqPortTextField.setColumns(45);
mqPortTextField.setBounds(235, 179, 309, 21);
mqPanel.add(mqPortTextField);
JButton mqSaveButton = new JButton("保存文件");
mqSaveButton.setFont(new Font("微软雅黑", Font.PLAIN, 12));
mqSaveButton.setBounds(279, 265, 93, 23);
mqPanel.add(mqSaveButton);
JPanel dbPanel = new JPanel();
tabbedPane.addTab("数据库配置", null, dbPanel, null);
dbPanel.setLayout(null);
JLabel dbLabel = new JLabel("数据库名称:");
dbLabel.setFont(new Font("微软雅黑", Font.PLAIN, 12));
dbLabel.setBounds(103, 38, 80, 15);
dbPanel.add(dbLabel);
dbTextField = new JTextField();
dbTextField.setFont(new Font("Consolas", Font.PLAIN, 12));
dbTextField.setBounds(228, 35, 309, 21);
dbPanel.add(dbTextField);
dbTextField.setColumns(45);
JLabel urlLabel = new JLabel("数据库url:");
urlLabel.setFont(new Font("微软雅黑", Font.PLAIN, 12));
urlLabel.setBounds(103, 88, 80, 15);
dbPanel.add(urlLabel);
urlTextField = new JTextField();
urlTextField.setFont(new Font("Consolas", Font.PLAIN, 12));
urlTextField.setColumns(45);
urlTextField.setBounds(228, 85, 309, 21);
dbPanel.add(urlTextField);
JLabel unameLabel = new JLabel("用 户 名:");
unameLabel.setFont(new Font("微软雅黑", Font.PLAIN, 12));
unameLabel.setBounds(103, 137, 80, 15);
dbPanel.add(unameLabel);
unameTextField = new JTextField();
unameTextField.setFont(new Font("Consolas", Font.PLAIN, 12));
unameTextField.setColumns(45);
unameTextField.setBounds(228, 134, 309, 21);
dbPanel.add(unameTextField);
JLabel pwdLabel = new JLabel("密 码:");
pwdLabel.setFont(new Font("微软雅黑", Font.PLAIN, 12));
pwdLabel.setBounds(103, 187, 80, 15);
dbPanel.add(pwdLabel);
pwdTextField = new JPasswordField();
pwdTextField.setBounds(228, 184, 309, 21);
dbPanel.add(pwdTextField);
JButton dbTestButton = new JButton("测试连接");
dbTestButton.setFont(new Font("微软雅黑", Font.PLAIN, 12));
dbTestButton.setBounds(110, 287, 93, 23);
dbPanel.add(dbTestButton);
JButton saveButton = new JButton("保存文件");
saveButton.setFont(new Font("微软雅黑", Font.PLAIN, 12));
saveButton.setBounds(257, 287, 93, 23);
dbPanel.add(saveButton);
JButton dbInitButton = new JButton("初始化数据库");
dbInitButton.setFont(new Font("微软雅黑", Font.PLAIN, 12));
dbInitButton.setBounds(407, 287, 113, 23);
dbPanel.add(dbInitButton);
initCfgInfo();
startButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
startService();
}
});
stopButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
stopService();
}
});
mqSaveButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
saveMQCfg();
}
});
dbTestButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
connectDb();
}
});
saveButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
saveDBCfg();
}
});
dbInitButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
initDb();
}
});
JPanel aboutPanel = new JPanel();
tabbedPane.addTab("关于", null, aboutPanel, null);
aboutPanel.setLayou