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

📄 mainframe.java

📁 TestDataBuilder是一个采用Java编写的
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
    
    
    protected void showConnectionPoolMenu(Component invoker, int x, int y){
        JPopupMenu pupupMenu = getJMenuBar().getConnectionPoolPopupMenu();
        pupupMenu.show(invoker, x, y);
    }

    
    public void addConnectConfig() {
        try {
            this.addNewConnectionConfig();
            tabbedPaneSelChanged();
            this.getJLeftTree().updateUI();
        } catch (Exception e) {
            logger.error("addConnectConfig", e); //$NON-NLS-1$
            JOptionPane.showMessageDialog(this,RM.R("label.MainFrame.error.addNewConfig") + e.getMessage()); //$NON-NLS-1$
        } 
    }

    public void disconnectAll() {
        this.getTestDataConfig().disconnectAll();
        tabbedPaneSelChanged();
        this.getJLeftTree().updateUI();
    }

    public void disconnect() {
        if(this.getCurSelDatabaseConfig() != null){
            String configName = this.getCurSelDatabaseConfig().getName();
            this.getTestDataConfig().disconnectDatabase(configName);
            tabbedPaneSelChanged();         
            this.getJLeftTree().updateUI();
        }
    }

    public void showConnectMenu(Component invoker, int x, int y){
        if(invoker instanceof JDBTree){           
            JPopupMenu pupupMenu = getJMenuBar().getConnectionPopupMenu();
            pupupMenu.show(invoker, x, y);
        }
    }
    
    public void delelteConfig() {
        if(this.getTestDataConfig() != null && this.getCurSelDatabaseConfig() != null){
            try {    
                if(JOptionPane.showConfirmDialog(this, RM.R("label.MainFrame.deleteConnectConfirm"))==JOptionPane.OK_OPTION ){ //$NON-NLS-1$
                    int[] rows = this.getJLeftTree().getSelectionRows();
                    int selRow = rows[0];
                    //TreePath selPath = this.getJLeftTree().getSelectionPath();
                    TreePath parentPath = this.getJLeftTree().getSelectionPath().getParentPath();
                    if(this.getTestDataConfig().deleteDatabaseConfig(
                                    this.getCurSelDatabaseConfig().getName())){
                        this.getJLeftTree().updateUI();
                        this.getJLeftTree().expandPath(parentPath);
                        
                        int rowCount = this.getJLeftTree().getRowCount();
                        if(selRow == rowCount){
                            selRow = rowCount - 1;
                        }
                        this.getJLeftTree().setSelectionRow(selRow);
                        this.getJLeftTree().updateUI();
                    }
                }
            } catch (BaseException e) {
               JOptionPane.showMessageDialog(this, RM.R("label.MainFrame.error.deleteConfig") + e.getMessage()); //$NON-NLS-1$
            }
        }
    }

    public boolean configAndReconnect() throws BaseException, SQLException {
        if(this.getCurSelDatabaseConfig() != null){
            String configName = this.getCurSelDatabaseConfig().getName();
            return configAndReconnect(configName);
        }
        
        return false;
    }

    protected boolean configAndReconnect(String configName) throws BaseException, SQLException {       
        boolean result = this.configDB(configName);
        if(result){
            this.getTestDataConfig().disconnectDatabase(configName);
            if(configName == null){
                configName = newConfigName;
            }
            this.getTestDataConfig().connectDatabase(configName);
        }
        return result;
        
    }
    
    public boolean addNewConnectionConfig() throws SQLException, BaseException{
       return configAndReconnect(null);
    }
    
    public TestDataConfig getTestDataConfig(){
        if(this.getWorkspaceDataCache() != null){
            return this.getWorkspaceDataCache().getTestDataConfig();
        }
        return null;
    }
    
    public void connectAll() {
        try{
            this.getTestDataConfig().connectAll();
            tabbedPaneSelChanged();
        }catch(SQLException ex){
            JOptionPane.showMessageDialog(this, RM.R("label.MainFrame.error.connectDatabase") + ex.getMessage()); //$NON-NLS-1$
        }
        this.getJLeftTree().updateUI();
    }

    public void connect() {
        try{
            if(this.getCurSelDatabaseConfig() != null){
                this.getTestDataConfig().connectDatabase(this.getCurSelDatabaseConfig().getName());
                tabbedPaneSelChanged();
            }
        }catch(SQLException ex){
            JOptionPane.showMessageDialog(this, RM.R("label.MainFrame.error.connectDatabase") + ex.getMessage()); //$NON-NLS-1$
        }
        this.getJLeftTree().updateUI();
    }

    public void autoConfigAll() {
        try{
        this.getJLeftTree().autoConfigAll();
        }catch(IOException ex){
            logger.error("autoConfigAll", ex); //$NON-NLS-1$
            JOptionPane.showMessageDialog(this, RM.R("label.MainFrame.error.saveAutoConfig") + ex.getMessage()); //$NON-NLS-1$
            
        }
    }
    
    public void deleteAllConfig(){
        int result = JOptionPane.showConfirmDialog(this, RM.R("label.MainFrame.deleteAllConfig.confirm"),
                        RM.R("label.MainFrame.deleteAllConfig.confirm.title"),
                        JOptionPane.YES_NO_OPTION);
        if(result == JOptionPane.YES_OPTION){
            getWorkspaceDataCache().removeAndBankupAllTableConfig();
            this.getJLeftTree().updateUI();
        }
    }

	private void showConfig(String tableName) {
		if(tableName == null){
			dataExchange(null, true);
			this.getColumnTable().setRefs(new ArrayList(0));
		}else{
			File configFile = workspaceDataCache.getTableConfigFile(tableName);
			if(configFile.exists()){
				try {
					TableConfig tableConfig = this.getWorkspaceDataCache().getTableConfig(tableName);					
					dataExchange(tableConfig, true);
				} catch (BaseException e) {
					logger.error("showConfig", e); //$NON-NLS-1$
					JOptionPane.showMessageDialog(this, RM.R("label.MainFrame.error.parseConfigFile") + e.getMessage()); //$NON-NLS-1$
				}
			}else{
				dataExchange(null, true);
			}
			resetRefValues();
		}
	}


	private void dataExchange(TableConfig tableConfig, boolean isToControl){
		if(isToControl){
            boolean configExist = (tableConfig != null);
            //this.cbxGenerate.setSelected(configExist);
			if(!configExist){
				this.cbxCloseIDAutoInsert.setSelected(false);
				this.txtCount.setText(null);
				this.cbxOnError.setSelectedItem(0);
				this.txtInit.setText(null);
				this.txtDestroy.setText(null);
				this.columnTable.setData(null);
			}else{
				this.cbxCloseIDAutoInsert.setSelected(tableConfig.isCloseIdAutoInsert());
				this.txtCount.setText(tableConfig.getCount() + ""); //$NON-NLS-1$
				this.cbxOnError.setSelectedItem(tableConfig.getOnError());
				this.txtInit.setText(tableConfig.getInit());
				this.txtDestroy.setText(tableConfig.getDestroy());
                List<ColumnConfig> tempColumnConfig = TableConfig.cloneColumnConfigs(tableConfig.getColumnConfigs());
				this.columnTable.setData(tempColumnConfig);
			}
		}else{
			if(this.getCurSelTable() != null){
				tableConfig.setTableName(this.getCurSelTable().getTableName());
                tableConfig.setOnError((String)cbxOnError.getSelectedItem());
				tableConfig.setCloseIdAutoInsert(cbxCloseIDAutoInsert.isSelected());
				tableConfig.setCount(Integer.valueOf(txtCount.getText()));
				tableConfig.setInit(txtInit.getText().trim());
				tableConfig.setDestroy(txtDestroy.getText().trim());
				List<ColumnConfig> tempColumnConfigs = TableConfig.cloneColumnConfigs(this.columnTable.getData());
				tableConfig.setColumnConfigs(tempColumnConfigs);
			}
		}
	}
	
	public static void printPath(Object[] paths){
		System.out.println("==============================");
		for(Object path : paths){
			System.out.println(path);
		}
	}


	/**
	 * This method initializes rightPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getConfigPanel() {
		if(configPanel == null) {
			jLabel4 = new JLabel();
			jLabel4.setBounds(new Rectangle(19, 203, 49, 18));
			jLabel4.setText(RM.R("label.MainFrame.ColumnConfig")); //$NON-NLS-1$
			jLabel3 = new JLabel();
			jLabel3.setBounds(new Rectangle(20, 137, 45, 20));
			jLabel3.setText(RM.R("label.MainFrame.Clear")); //$NON-NLS-1$
			jLabel2 = new JLabel();
			jLabel2.setBounds(new Rectangle(20, 73, 51, 20));
			jLabel2.setText(RM.R("label.MainFrame.Init")); //$NON-NLS-1$
			jLabel1 = new JLabel();
			jLabel1.setBounds(new Rectangle(250, 44, 49, 21));
			jLabel1.setText(RM.R("label.MainFrame.OnError")); //$NON-NLS-1$
			jLabel = new JLabel();
			jLabel.setBounds(new Rectangle(20, 44, 49, 20));
			jLabel.setText(RM.R("label.MainFrame.Rows")); //$NON-NLS-1$
			configPanel = new JPanel();
			configPanel.setLayout(null);
			configPanel.add(getCbxGenerate(), null);
			configPanel.add(jLabel, null);
			configPanel.add(getTxtCount(), null);
			configPanel.add(jLabel1, null);
			configPanel.add(getCbxOnError(), null);
			configPanel.add(jLabel2, null);
			configPanel.add(getInitPanel(), null);
			configPanel.add(jLabel3, null);
			configPanel.add(getDestroyPane(), null);
			configPanel.add(getColumnPane(), null);
			configPanel.add(jLabel4, null);
			configPanel.add(getCbxCloseIDAutoInsert(), null);
			configPanel.add(getBtnAutoConfig(), null);
			configPanel.add(getBtnAdd(), null);
			configPanel.add(getBtnDel(), null);
			configPanel.add(getBtnSave(), null);
			configPanel.add(getBtnClose(), null);
			configPanel.add(getBtnClearColumnConfig(), null);
            configPanel.addComponentListener(new ComponentAdapter(){
               @Override
                public void componentResized(ComponentEvent e) {
                    MainFrame.this.frameResized(e);                    
                } 
            });
		}
		return configPanel;
	}
    
    public static final int rightSpace = 20;
    
	protected void frameResized(ComponentEvent e) {
        int rightPaneWidth = getConfigPanel().getWidth();
        if(rightPaneWidth <  500){
            rightPaneWidth = 500;
        }
        int rightPaneHeight = getConfigPanel().getHeight();
        if(rightPaneHeight < 350){
            rightPaneHeight = 350;
        }
        int btnCloseX  = rightPaneWidth - getBtnSave().getWidth() - rightSpace;
        int btnSaveX = btnCloseX - rightSpace - getBtnClose().getWidth();
         
        this.getInitPanel().setSize( rightPaneWidth - getInitPanel().getX() - rightSpace, getInitPanel().getHeight());
        this.getDestroyPane().setSize(rightPaneWidth - getDestroyPane().getX() - rightSpace, getDestroyPane().getHeight());
        this.getColumnPane().setSize(rightPaneWidth - getColumnPane().getX() - rightSpace, rightPaneHeight - 250);
        this.getBtnSave().setLocation(btnSaveX, rightPaneHeight - 35);
        this.getBtnClose().setLocation(btnCloseX, rightPaneHeight - 35);

        this.paintComponents(this.getGraphics());
    }


    /**
	 * This method initializes cbxGenerate	
	 * 	
	 * @return javax.swing.JCheckBox	
	 */
	private JCheckBox getCbxGenerate() {
		if (cbxGenerate == null) {
			cbxGenerate = new JCheckBox();
			cbxGenerate.setBounds(new Rectangle(18, 17, 159, 20));
			cbxGenerate.setEnabled(false);
			cbxGenerate.setText(RM.R("label.MainFrame.generateTestData")); //$NON-NLS-1$
			cbxGenerate.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					MainFrame.this.roleActionPerformed(e);
				}
                
			});     
            cbxGenerate.addChangeListener(new ChangeListener(){
                public void stateChanged(ChangeEvent e) {
                    MainFrame.this.roleStateChanged(e);
                }
                
            });
		}
		return cbxGenerate;
	}


	protected void roleStateChanged(ChangeEvent e) {
        boolean isSelected = this.getCbxGenerate().isSelected();
        this.enableControl(isSelected);
    }


    protected void roleActionPerformed(ActionEvent e) {
		boolean isSelected = this.getCbxGenerate().isSelected();
        String tableName = this.getCurSelTable().getTableName();
        File configFile = workspaceDataCache.getTableConfigFile(this.getCurSelTable().getTableName());
        
        String configFileName = configFile.getAbsolutePath();
        String bankupConfigFileName = configFileName + ".bank"; //$NON-NLS-1$
        
        //取消配置。
        if(!isSelected){
        	this.getWorkspaceDataCache().removeAndBankupTableConfig(tableName);   
        }else/*选中,表示生成*/ {
            
            File bankupConfigFile = new File(bankupConfigFileName);
            if(bankupConfigFile.exists()){
                if(!bankupConfigFile.renameTo(configFile)){
                  logger.error("error on rename file [" + bankupConfigFileName  //$NON-NLS-1$
                                  + "] to [" + configFileName + "]"); //$NON-NLS-1$ //$NON-NLS-2$
                }
            }else if(!configFile.exists()){
                try {
                    String tableConfigContent = TableConfig.CONIFG_DEF_VALUE.replaceAll("tableName", //$NON-NLS-1$
                                    this.getCurSelTable().getTableName());
                    FileUtils.writeStringToFile(configFile, tableConfigContent);
                } catch (IOException e1) {
                    logger.error("MainFrame", e1); //$NON-NLS-1$
                    JOptionPane.showMessageDialog(this,RM.R("label.MainFrame.error.saveConfigFile") +  e1.getMessage()); //$NON-NLS-1$
                }
            }
            
        }
        
        this.showConfig(tableName);
        
		//enableControl(isSelected);
		if(this.txtCount.getText().trim().equals("")){ //$NON-NLS-1$
			this.txtCount.setText("" + Global.getInstance().P.getDefaultRowToGenerate()); //$NON-NLS-1$
		}
	}


	/**
	 * This method initializes txtCount	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getTxtCount() {
		if (txtCount == null) {
			txtCount = new JTextFieldEx();
			txtCount.setBounds(new Rectangle(87, 43, 137, 21));
			txtCount.addFocusListener(new java.awt.event.FocusAdapter() {
				public void focusLost(java.awt.event.FocusEvent e) {
					MainFrame.this.txtCountLostFocus(e);
				}
			});
		}
		return txtCount;
	}


	protected void txtCountLostFocus(FocusEvent e) {
		try{
			Integer.valueOf(this.txtCount.getText().trim());
		}catch (Exception ex){
			JOptionPane.showMessageDialog(this, RM.R("label.MainFrame.InvalidNumberInfo")); //$NON-NLS-1$
			this.txtCount.setText("100"); //$NON-NLS-1$
			this.txtCount.grabFocus();

⌨️ 快捷键说明

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