📄 connectionpanel.java
字号:
advancedPanel.add(advTxPanel, BorderLayout.SOUTH); tabPane = new JTabbedPane(JTabbedPane.BOTTOM); tabPane.addTab("Basic", mainPanel); tabPane.addTab("Advanced", advancedPanel); add(tabPane, BorderLayout.CENTER); } /** * Retrieves and populates the drivers list. */ protected void buildDriversList() { jdbcDrivers = JDBCProperties.getDriversVector(); int size = jdbcDrivers.size(); String[] driverNames = new String[size + 1]; driverNames[0] = "Select..."; for (int i = 0; i < size; i++) { driverNames[i+1] = jdbcDrivers.elementAt(i).toString(); } if (driverCombo == null) { DynamicComboBoxModel model = new DynamicComboBoxModel(); model.setElements(driverNames); driverCombo = new JComboBox(model); } else { DynamicComboBoxModel model = (DynamicComboBoxModel)driverCombo.getModel(); model.setElements(driverNames); driverCombo.setModel(model); selectDriver(); } } /** * Action performed upon selection of the Apply button * when selecting a tx isolation level. */ public void transactionLevelChanged() { try { applyTransactionLevel(true); if (databaseConnection.getTransactionIsolation() == -1) { return; } String txLevel = txCombo.getSelectedItem().toString(); GUIUtilities.displayInformationMessage( "The transaction isolation level " + txLevel + " was applied successfully."); } catch (DataSourceException e) { GUIUtilities.displayWarningMessage( "The selected isolation level could not be applied.\n" + "The JDBC driver returned:\n\n" + e.getMessage() + "\n\n"); } catch (Exception e) {} } /** * Applies the tx level on open connections of the type selected. */ private void applyTransactionLevel(boolean reloadProperties) throws DataSourceException { // set the tx level from the combo selection getTransactionIsolationLevel(); int isolationLevel = databaseConnection.getTransactionIsolation(); // apply to open connections ConnectionManager. setTransactionIsolationLevel(databaseConnection, isolationLevel); if (reloadProperties) { controller.updateDatabaseProperties(); } } /** * Acion implementation on selection of the Connect button. */ public void connect() { // ---------------------------- // some validation // make sure a name has been entered if (nameField.getText().trim().length() == 0) { GUIUtilities.displayErrorMessage("You must enter a name for this connection"); return; } if (ConnectionProperties.nameExists(databaseConnection, nameField.getText())) { GUIUtilities.displayErrorMessage("The name entered for this connection already exists"); return; } // check a driver is selected if (driverCombo.getSelectedIndex() == 0) { GUIUtilities.displayErrorMessage("You must select a driver"); return; } // check if we have a url - if not check the port is valid if (urlField.getText().trim().length() == 0) { if (portField.getText().trim().length() > 0) { char[] portChars = portField.getText().toCharArray(); for (int i = 0; i < portChars.length; i++) { if (!Character.isDigit(portChars[i])) { GUIUtilities.displayErrorMessage("Invalid port number"); return; } } } } // otherwise - good to proceed // populate the object with field values populateConnectionObject(); try { // connect GUIUtilities.showWaitCursor(); boolean connected = SystemUtilities.connect(databaseConnection); if (connected) { // apply the tx level if supplied try { applyTransactionLevel(false); } catch (DataSourceException e) { GUIUtilities.displayWarningMessage( "The selected isolation level could not be applied.\n" + "The JDBC driver returned:\n\n" + e.getMessage() + "\n\n"); } } } catch (DataSourceException e) { StringBuffer sb = new StringBuffer(); sb.append("The connection to the database could not be established."); sb.append("\nPlease ensure all required fields have been entered "); sb.append("correctly and try again.\n\nThe system returned:\n"); sb.append(e.getExtendedMessage()); GUIUtilities.displayExceptionErrorDialog(sb.toString(), e); } finally { GUIUtilities.showNormalCursor(); } } /** * Saves the driver info and mods the tree node value * when the name field loses focus. */ public void focusLost(FocusEvent e) { tabViewDeselected(); } public void focusGained(FocusEvent e) {} /** * Informed by a tree selection, this readies the form for * a new connection object and value change. */ protected void selectionChanging() {} /** * Indicates the panel is being de-selected in the pane */ public boolean tabViewDeselected() { nameField.removeFocusListener(this); populateConnectionObject(); save(); return true; } /** * Indicates the panel is being selected in the pane */ public boolean tabViewSelected() { nameField.addFocusListener(this); enableFields(databaseConnection.isConnected()); return true; } /** * Checks the current selection for a name change * to be propagated back to the tree view. */ private void checkNameUpdate() { String oldName = databaseConnection.getName(); String newName = nameField.getText().trim(); if (!oldName.equals(newName)) { databaseConnection.setName(newName); controller.nodeNameValueChanged(metaObject); } } /** * Acion implementation on selection of the Disconnect button. */ public void disconnect() { try { SystemUtilities.disconnect(databaseConnection); } catch (DataSourceException e) { GUIUtilities.displayErrorMessage( "Error disconnecting from data source:\n" + e.getMessage()); } } /** * Retrieves the values from the jdbc properties table * and stores them within the current database connection. */ private void storeJdbcProperties() { Properties properties = databaseConnection.getJdbcProperties(); if (properties == null) { properties = new Properties(); } else { properties.clear(); } for (int i = 0; i < advancedProperties.length; i++) { String key = advancedProperties[i][0]; String value = advancedProperties[i][1]; if (!MiscUtils.isNull(key) && !MiscUtils.isNull(value)) { properties.setProperty(key, value); } } databaseConnection.setJdbcProperties(properties); } /** * Sets the values of the current database connection * within the jdbc properties table. */ private void setJdbcProperties() { advancedProperties = new String[20][2]; Properties properties = databaseConnection.getJdbcProperties(); if (properties == null || properties.size() == 0) { model.fireTableDataChanged(); return; } int count = 0; for (Enumeration i = properties.propertyNames(); i.hasMoreElements();) { String name = (String)i.nextElement(); if (!name.equalsIgnoreCase("password")) { advancedProperties[count][0] = name; advancedProperties[count][1] = properties.getProperty(name); count++; } } model.fireTableDataChanged(); } /** * Indicates a connection has been established. * * @param the connection properties object */ public void connected(DatabaseConnection databaseConnection) { populateConnectionFields(databaseConnection); save(); } /** * Saves the connection info to file. */ protected boolean saveConnections() { try { int saved = ConnectionProperties.saveConnections(); } catch (ValidationException e) { GUIUtilities.displayErrorMessage(e.getMessage()); return false; } return true; /* if (saved == 0) { GUIUtilities.displayErrorMessage("You must enter a name for this connection"); } */ } /** * Indicates a connection has been closed. * * @param the connection properties object */ public void disconnected(DatabaseConnection databaseConnection) { enableFields(false); } /** * Enables/disables fields as specified. */ private void enableFields(boolean enable) { txApplyButton.setEnabled(enable); connectButton.setEnabled(!enable); disconnectButton.setEnabled(enable); if (enable) { int count = SystemUtilities.getOpenConnectionCount(databaseConnection); statusLabel.setText("Connected [ " + count + (count > 1 ? " connections open ]" : " connection open ]") ); } else { statusLabel.setText("Not Connected"); } paintStatusLabel(); setEncryptPassword(); } /** * Changes the state of the save and encrypt password * check boxes depending on the whether the encrypt * check box is selected. */ public void setEncryptPassword() { boolean encrypt = encryptPwdCheck.isSelected(); if (encrypt && !savePwdCheck.isSelected()) { savePwdCheck.setSelected(encrypt); } } /** * Changes the state of the encrypt password check * box depending on the whether the save password
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -