package org.crazyit.mysql.ui;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JToolBar;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;
import org.crazyit.mysql.object.GlobalContext;
import org.crazyit.mysql.object.ViewObject;
import org.crazyit.mysql.object.list.AbstractData;
import org.crazyit.mysql.object.list.ProcedureData;
import org.crazyit.mysql.object.list.TableData;
import org.crazyit.mysql.object.list.ViewData;
import org.crazyit.mysql.object.tree.ConnectionNode;
import org.crazyit.mysql.object.tree.Database;
import org.crazyit.mysql.object.tree.ProcedureNode;
import org.crazyit.mysql.object.tree.RootNode;
import org.crazyit.mysql.object.tree.ServerConnection;
import org.crazyit.mysql.object.tree.TableNode;
import org.crazyit.mysql.object.tree.ViewNode;
import org.crazyit.mysql.ui.list.ListCellRenderer;
import org.crazyit.mysql.ui.tree.TreeCellRenderer;
import org.crazyit.mysql.ui.tree.TreeListener;
import org.crazyit.mysql.util.MySQLUtil;
/**
* 管理器的主界面
*
* @author yangenxiong yangenxiong2009@gmail.com
* @version 1.0
* <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
* <br>Copyright (C), 2009-2010, yangenxiong
* <br>This program is protected by copyright laws.
*/
@SuppressWarnings("serial")
public class MainFrame extends JFrame {
//主界面的分隔Pane
private JSplitPane mainPane;
//树的JScrollPane
private JScrollPane treePane;
//主界面的连接树, 树的第一层子节点是各个连接,第二层子节点是该连接下的数据库列表,
//第三层子节点是表、视图等
private JTree tree;
//树的Model
private DefaultTreeModel treeModel;
//JList的JScrollPane
private JScrollPane dataPane;
//表、视图等存放的JList
private JList dataList;
//空的数据
private Object[] emptyData = new Object[]{};
//工具栏
private JToolBar toolBar = new JToolBar();
//新连接界面
private ConnectionFrame connectionFrame;
//新建数据库界面
private DatabaseFrame databaseFrame;
//新连接
private Action newConnection = new AbstractAction("新连接", new ImageIcon("images/connection.gif")) {
public void actionPerformed(ActionEvent e) {
newConnection();
}
};
//表
private Action table = new AbstractAction("表", new ImageIcon("images/table.gif")) {
public void actionPerformed(ActionEvent e) {
viewTables();
}
};
//视图
private Action view = new AbstractAction("视图", new ImageIcon("images/view.gif")) {
public void actionPerformed(ActionEvent e) {
viewViews();
}
};
//存储过程
private Action procedure = new AbstractAction("存储过程", new ImageIcon("images/procedure.gif")) {
public void actionPerformed(ActionEvent e) {
viewProcedures();
}
};
//查询
private Action query = new AbstractAction("查询", new ImageIcon("images/query.gif")) {
public void actionPerformed(ActionEvent e) {
query();
}
};
//弹出菜单
JPopupMenu menu = new JPopupMenu();
//表的菜单
private JMenuItem addTableItem = new JMenuItem("新建表", new ImageIcon("images/add-table.gif"));
private JMenuItem editTableItem = new JMenuItem("编辑表", new ImageIcon("images/edit-table.gif"));
private JMenuItem deleteTableItem = new JMenuItem("删除表", new ImageIcon("images/delete-table.gif"));
private JMenuItem dumpTableItem = new JMenuItem("导出表", null);
private JMenuItem refresh = new JMenuItem("刷 新", new ImageIcon("images/refresh.gif"));
//视图菜单
private JMenuItem addViewItem = new JMenuItem("新建视图", new ImageIcon("images/add-view.gif"));
private JMenuItem editViewItem = new JMenuItem("编辑视图", new ImageIcon("images/edit-view.gif"));
private JMenuItem dropViewItem = new JMenuItem("删除视图", new ImageIcon("images/delete-view.gif"));
//存储过程菜单
private JMenuItem addProcedureItem = new JMenuItem("新建存储过程", new ImageIcon("images/add-procedure.gif"));
private JMenuItem editProcedureItem = new JMenuItem("编辑存储过程", new ImageIcon("images/edit-procedure.gif"));
private JMenuItem dropProcedureItem = new JMenuItem("删除存储过程", new ImageIcon("images/delete-procedure.gif"));
//树的弹出菜单
JPopupMenu treeMenu = new JPopupMenu();
private JMenuItem closeConnection = new JMenuItem("关闭连接", null);
private JMenuItem removeConnection = new JMenuItem("删除连接", null);
private JMenuItem closeDatabase = new JMenuItem("关闭数据库", null);
private JMenuItem newDatabase = new JMenuItem("新建数据库", null);
private JMenuItem removeDatabase = new JMenuItem("删除数据库", null);
private JMenuItem executeSQLFile = new JMenuItem("执行SQL文件", null);
private JMenuItem dumpSQLFile = new JMenuItem("导出SQL文件", null);
//全局上下文对象
private GlobalContext ctx;
//当前列表前所显示数据种类
private ViewObject currentView;
public MainFrame(GlobalContext ctx) {
this.ctx = ctx;
//初始化树对象
createTree();
this.treePane = new JScrollPane(this.tree);
//初始化表、视图的JList
this.dataList = createList();
this.dataPane = new JScrollPane(dataList);
this.dataPane.setPreferredSize(new Dimension(600, 400));
this.mainPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, this.treePane,
this.dataPane);
this.mainPane.setDividerLocation(170);
this.add(this.mainPane);
//创建工具栏
createToolBar();
this.add(this.toolBar, BorderLayout.NORTH);
this.setLocation(150, 100);
this.pack();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("MySQL管理器");
//初始化各个界面
this.connectionFrame = new ConnectionFrame(this.ctx, this);
initMenuItemListeners();
}
//初始化右键菜单
private void initMenuItemListeners() {
this.addTableItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
newTable();
}
});
this.editTableItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
editTable();
}
});
this.deleteTableItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
dropTable();
}
});
this.dumpTableItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
dumpTable();
}
});
this.addViewItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
newView();
}
});
this.editViewItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
editView();
}
});
this.dropViewItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
dropView();
}
});
this.addProcedureItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
newProcedure();
}
});
this.editProcedureItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
editProcedure();
}
});
this.dropProcedureItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
dropProcedure();
}
});
this.refresh.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
refreshDataList();
}
});
this.closeConnection.addActionListener(new ActionListener(){