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

📄 mainframe.java

📁 TestDataBuilder是一个采用Java编写的
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
package com.testDataBuilder.ui.main;

import java.awt.BorderLayout;
import java.awt.Component;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.TreePath;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.dom4j.DocumentException;
import org.xml.sax.SAXException;
import com.testDataBuilder.config.AutoConfigEngine;
import com.testDataBuilder.config.ColumnConfig;
import com.testDataBuilder.config.DatabaseConfig;
import com.testDataBuilder.config.TableConfig;
import com.testDataBuilder.config.TestDataConfig;
import com.testDataBuilder.config.WorkspaceDataCache;
import com.testDataBuilder.core.DatabaseUtil;
import com.testDataBuilder.dbMetaInfo.Database;
import com.testDataBuilder.dbMetaInfo.Table;
import com.testDataBuilder.exception.BaseException;
import com.testDataBuilder.resources.RM;
import com.testDataBuilder.ui.core.CenterFrame;
import com.testDataBuilder.ui.core.JTDButton;
import com.testDataBuilder.ui.core.JTextFieldEx;
import com.testDataBuilder.ui.database.ConfigDBDialog;
import com.testDataBuilder.ui.help.AboutDialog;
import com.testDataBuilder.ui.help.Helper;
import com.testDataBuilder.ui.help.UpgradeDialog;
import com.testDataBuilder.ui.role.GenerateDataDialog;
import com.testDataBuilder.ui.role.RoleDialog;
import com.testDataBuilder.ui.main.others.AppPropertyDialog;
import com.testDataBuilder.ui.main.others.JTDBMenuBar;
import com.testDataBuilder.ui.main.others.JTDBToolBar;
import com.testDataBuilder.ui.main.queryConsole.QueryPanel;
import com.testDataBuilder.ui.preference.PreferenceDialog;
import com.testDataBuilder.ui.project.JProjectDirChooser;
import com.testDataBuilder.ui.project.ProjectDialog;
import com.testDataBuilder.ui.project.RecentProjects;
import com.testDataBuilder.util.Global;
import java.io.File;
import java.io.IOException;
import javax.swing.JCheckBox;
import java.awt.Rectangle;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.JTextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.FocusEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JTabbedPane;
import java.awt.Toolkit;
import com.testDataBuilder.ui.main.dataModel.DataModelPanel;


public class MainFrame extends CenterFrame {

    static Logger logger = Logger.getLogger(MainFrame.class);
    
	private static final long serialVersionUID = 1L;

	private JPanel jContentPane = null;

    private JTDBToolBar tdbToolBar = null;
    
	private JSplitPane jSplitPane = null;

    private JLabel msgLine = null;
    
	private WorkspaceDataCache workspaceDataCache = WorkspaceDataCache.getInstance();
    
    public Global G = Global.getInstance();  //  @jve:decl-index=0:
    
	public String getProjectConfigFile(){
	    return getWorkspace() + Global.SEP + Global.PROJECT_CONFIG;
    }

    public Database getDatabase(){
        return this.getJLeftTree().getDatabase();
    }
    
	public void enableControl(boolean enable){
		this.getCbxCloseIDAutoInsert().setEnabled(enable);
		this.getTxtCount().setEnabled(enable);
		this.getCbxOnError().setEnabled(enable);
		this.getTxtInit().setEnabled(enable);
		this.getTxtDestroy().setEnabled(enable);
		this.getColumnTable().setEnabled(enable);
		this.getBtnAutoConfig().setEnabled(enable);
		this.getBtnAdd().setEnabled(enable);
		this.getBtnDel().setEnabled(enable);
        this.getBtnSave().setEnabled(enable);
        this.getBtnClearColumnConfig().setEnabled(enable);
	}
 

    private void resetRefValues(){
		List<String> allTypes = this.getAllRefRoles4List();
		this.getColumnTable().setRefs(allTypes);
	}
	
    /**
     * 返回用于List显示的类型。
     * <p><code>getAllRefRoles4List</code></p>
     * @return
     * @author LiuXiaojie 2007-12-3
     */
    public List<String> getAllRefRoles4List(){
    	List<String> tempList = null;
    	TableConfig curTableConfig = this.getCurTableConfig();
    	if(curTableConfig != null){
    		tempList = curTableConfig.getRoleFactory().getAllRefRoles4List();
    	}else{
    		tempList = new ArrayList<String>();
    	}
    	
        return tempList;        
    }
	//private DatabaseUtil dbUtil = null;  //  @jve:decl-index=0:

	private JScrollPane leftPane = null;

	private JDBTree jLeftTree = null;

	private JPanel configPanel = null;

	private JCheckBox cbxGenerate = null;

	private JLabel jLabel = null;

	private JTextField txtCount = null;

	private JLabel jLabel1 = null;

	private JComboBox cbxOnError = null;

	private JLabel jLabel2 = null;

	private JScrollPane initPanel = null;

	private JTextArea txtInit = null;

	private JLabel jLabel3 = null;

	private JScrollPane destroyPane = null;

	private JTextArea txtDestroy = null;

	private JScrollPane columnPane = null;

	private ColumnTable columnTable = null;

	private JLabel jLabel4 = null;

	private JCheckBox cbxCloseIDAutoInsert = null;

	private JTDButton btnAutoConfig = null;

	private JTDButton btnAdd = null;

	private JTDButton btnDel = null;

    private QueryPanel queryPanel = null;
    
    @Override
    public JTDBMenuBar getJMenuBar() {
        return (JTDBMenuBar)super.getJMenuBar();
    }

    public void configDBAndInitWorkspace(String configName, boolean changeWorkspace) throws IOException, BaseException{
        
        boolean result = false;
        try {
            result = this.configAndReconnect(configName);
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(this, RM.R("label.MainFrame.error.connectDatabase") + ex.getMessage()); //$NON-NLS-1$
        }

        if(result &&  configName.equals(TestDataConfig.DEFAULT)){
            if(changeWorkspace){
                this.initWorkspaceFromDB();
            }else{
                this.refreshDataFromDB();           
            }
        }
    }
    
    public boolean configDB(String configName) throws BaseException{
        
        DatabaseConfig databaseConfig = null;
        
        if(configName!= null){       
            databaseConfig = this.getDatabaseConfig(configName);
        }
        
        ConfigDBDialog dbDialog = new ConfigDBDialog(MainFrame.this,databaseConfig);
        dbDialog.setModal(true);
        dbDialog.setVisible(true);
        
        if(dbDialog.isOk()){
            this.getTestDataConfig().saveOrUpdateDatabaseConfig(dbDialog.getDatabaseConfig());
            if(configName == null){
                newConfigName = dbDialog.getTxtName().getText().trim();
            }else{
                newConfigName = null;
            }
        }
        return dbDialog.isOk();
        
    }
    private String newConfigName = null;
    
    public DatabaseConfig getDatabaseConfig(String configName){
        DatabaseConfig databaseConfig = null;
        TestDataConfig testDataConfig = this.getTestDataConfig();
        if(configName.equals(TestDataConfig.DEFAULT)){
            databaseConfig = testDataConfig.getDefDatabaseConfig();
        }else{
            databaseConfig = testDataConfig.getDatabaseConfigs().get(configName);
        }
        return databaseConfig;
    }
    
    private void bindToTree(Database database) {
        if(database != null ){
            this.getJLeftTree().setDatabase(database);
        }
        if(this.workspaceDataCache != null){
        	workspaceDataCache.setDatabase(database);
        }else{
        	logger.error("%%%%%%%%%%%  workspaceDatCache is null %%%%%%%%%");
        }
        this.getJLeftTree().setData(database);
        TestDataConfig tdc = this.getTestDataConfig();
        if(tdc != null && tdc.getDatabaseConfigs() != null){
            this.getJLeftTree().setDatabaseConfigs(tdc.getDatabaseConfigs());            
        }
        this.getJLeftTree().updateUI();
    }
    
	/**
	 * This method initializes jSplitPane	
	 * 	
	 * @return javax.swing.JSplitPane	
	 */
	private JSplitPane getJSplitPane() {
		if (jSplitPane == null) {
			jSplitPane = new JSplitPane();
			jSplitPane.setDividerSize(3);
			jSplitPane.setLeftComponent(getLeftPane());
            jSplitPane.setRightComponent(getJTabbedPane());
			jSplitPane.setDividerLocation(200);			
			jSplitPane.setResizeWeight(0.4);
		}
		return jSplitPane;
	}

	/**
	 * This method initializes leftPane	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getLeftPane() {
		if (leftPane == null) {
			leftPane = new JScrollPane();
			leftPane.setViewportView(getJLeftTree());
		}
		return leftPane;
	}
	/**
	 * This method initializes jLeftTree	
	 * 	
	 * @return javax.swing.JTree	
	 */
	private JDBTree getJLeftTree() {
		if (jLeftTree == null) {
			jLeftTree = new JDBTree(this);
			jLeftTree.addMouseListener(new MouseAdapter(){

				public void mouseClicked(MouseEvent e) {
					MainFrame.this.treeMouseClicked(e);
				}
			});
			TreeSelectionListener tsl = new TreeSelectionListener(){

				public void valueChanged(TreeSelectionEvent e) {
					MainFrame.this.treeSelectionChanged(e);
				}
				
			}; 
			jLeftTree.addTreeSelectionListener(tsl);
		}
		return jLeftTree;
	}
	
    protected void treeSelectionChanged(TreeSelectionEvent e) {
		TreePath treePath = e.getPath();
		if(treePath != null){			
			this.treeSelectChanged(treePath.getPath());	
		}
	}
    
	protected void treeMouseClicked(MouseEvent e) {
        JDBTree DBTree = (JDBTree) e.getComponent();
        TreePath treePath = null;
        if (e.getButton() == 3)/* 右键 */{
            treePath = DBTree.getPathForLocation(e.getX(), e.getY());
            if(treePath == null){
                return;
            }
            DBTree.setSelectionPath(treePath);
        }
       treePath = DBTree.getSelectionPath();
        if (treePath == null) {
            return;
        }

        Object[] paths = treePath.getPath();
       // this.treeSelectChanged(paths);
        if (e.getButton() == 3)/* 右键 */{
           if(DBTree.isSelTable(paths)){
               
           }else if(DBTree.isSelDatabase(paths)){
               this.showDBPopupMenu(DBTree, e.getX(), e.getY());
           }else if(DBTree.isSelConnectionPool(paths)){
               this.showConnectionPoolMenu(DBTree, e.getX(), e.getY());
           }else if(DBTree.isSelConnectConfig(paths)){
               this.showConnectMenu(DBTree, e.getX(), e.getY());
           }
        }
    }
    
	protected void treeSelectChanged(Object[] paths) {
		
        if (this.getJLeftTree().isSelTableOrTableField(paths)) {
			String tableName = paths[JDBTree.LEVEL_TABLE].toString();
			Table selTable = (Table)paths[JDBTree.LEVEL_TABLE];
			
			Table curSelTable = this.getJLeftTree().getDatabase().getSelTable();
			if (curSelTable != selTable) {
                this.setCurSelTable(selTable);
				File configFile = workspaceDataCache.getTableConfigFile(selTable
						.getTableName());
				this.getCbxGenerate().setSelected(configFile.exists());
				showConfig(tableName);
			}
		} else {
            this.setCurSelTable(null);
			this.getCbxGenerate().setSelected(false);
			showConfig(null);
		}
        
        if(this.getJLeftTree().isSelConnectConfig(paths)){
            DatabaseConfig curSelDatabaseConfig = (DatabaseConfig) paths[JDBTree.LEVEL_CONNECT]; 
            this.setCurSelDatabaseConfig(curSelDatabaseConfig);
        }else{
            this.setCurSelDatabaseConfig(null);
        }

		if (this.getCurSelTable() != null) {
			this.getColumnTable().setFields(this.getCurSelTable().getAllFields());
		}

		this.cbxGenerate.setEnabled(this.getCurSelTable() != null);
	}
	
    protected void showDBPopupMenu(Component invoker, int x, int y) {
        JPopupMenu pupupMenu = getJMenuBar().getDBPopupMenu();
        pupupMenu.show(invoker, x, y);
    }

⌨️ 快捷键说明

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