?? connectioninfodialog.java
字號:
package com.tanghan.plugin.dbviews.dialog;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import com.tanghan.db.util.DBConnection;
import com.tanghan.db.util.Driver;
import com.tanghan.plugin.TanghanPlugin;
import com.tanghan.util.DealString;
public class ConnectionInfoDialog {
private static ResourceBundle res = TanghanPlugin.getDefault().getResourceBundle();
// private HashMap data = null;
// private HashMap items = null;
private DBConnection dbConn;
/**輸入框*/
private Text connNameText;
private String jdbcDriverName = "";
private Text jdbcDrvierClassText;
private Text jdbcURLText;
private Text userNameText;
private Text passwordText;
private Button okButton;
/**驅動程序列表*/
private Combo driversCombo;
private List driverList;
Shell shell;
public ConnectionInfoDialog(Shell parent) {
shell = new Shell(parent, SWT.DIALOG_TRIM | SWT.PRIMARY_MODAL );
shell.setLayout(new GridLayout());
}
public DBConnection open(DBConnection data,List driverList) {
if(driverList!=null){
this.driverList = driverList;
}else{
this.driverList = new ArrayList();
}
createTextWidgets();
createControlButtons();
if(data!=null){
this.dbConn = (DBConnection)data.clone();
}else{
this.dbConn = new DBConnection();
}
initData();
shell.pack();
shell.open();
Display display = shell.getDisplay();
while(!shell.isDisposed()){
if(!display.readAndDispatch())
display.sleep();
}
return this.dbConn;
}
private void initData(){
connNameText.setText(dbConn.getConnectionName());
jdbcDrvierClassText.setText(dbConn.getJdbcDriverClass());
jdbcURLText.setText(dbConn.getDatabaseURL());
jdbcDriverName = dbConn.getJdbcDriverName();
userNameText.setText(dbConn.getUserName());
passwordText.setText(dbConn.getPassword());
if(dbConn.isConnected()){
connNameText.setEnabled(false);
connNameText.setToolTipText(dbConn.getConnectionName());
jdbcDrvierClassText.setEnabled(false);
jdbcDrvierClassText.setToolTipText(dbConn.getJdbcDriverClass());
jdbcURLText.setEnabled(false);
jdbcURLText.setToolTipText(dbConn.getDatabaseURL());
userNameText.setEnabled(false);
userNameText.setToolTipText(dbConn.getUserName());
passwordText.setEnabled(false);
passwordText.setToolTipText(dbConn.getPassword());
okButton.setEnabled(false);
}
//初始化驅動程序列表
for(int i =0;i<driverList.size();i++){
Driver driver = (Driver)driverList.get(i);
driversCombo.add(driver.getDriverName(),i);
}
driversCombo.setText(this.dbConn.getJdbcDriverName());
}
private void createTextWidgets() {
Composite composite = new Composite(shell, SWT.NULL);
composite.getShell().setText(res.getString("Tree.View.Action.NewConn.Name"));
composite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
GridLayout layout= new GridLayout();
layout.numColumns = 2;
composite.setLayout(layout);
connNameText = addTextLine(composite,res.getString("Connection.Name"),100);
// jdbcDriverNameText = addTextLine(composite,res.getString("JDBC.Name"),100);
addDriverComboLine(composite,res.getString("Driver.Name"));
jdbcDrvierClassText = addTextLine(composite,res.getString("Driver.ClassName"),200);
jdbcURLText = addTextLine(composite,res.getString("JDBC.DatabaseURL"),200);
userNameText = addTextLine(composite,res.getString("JDBC.UserName"),50);
passwordText = addTextLine(composite,res.getString("JDBC.Password"),50);
passwordText.setEchoChar('*');
}
private void createControlButtons() {
Composite composite = new Composite(shell, SWT.NULL);
composite.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_CENTER));
GridLayout layout = new GridLayout();
layout.numColumns = 2;
composite.setLayout(layout);
okButton = new Button(composite, SWT.PUSH);
okButton.setText(res.getString("OK"));
okButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
// if(MessageDialog.openConfirm(parent.getShell(),res.getString("TanghanPlugin.Message.ifDeleteTitle"),res.getString("TanghanPlugin.Message.ifDelete"))){
if(checkData()){
dbConn.setConnectionName(connNameText.getText().trim());
dbConn.setJdbcDriverClass(jdbcDrvierClassText.getText().trim());
dbConn.setJdbcDriverName(jdbcDriverName);
dbConn.setDatabaseURL(jdbcURLText.getText().trim());
dbConn.setUserName(userNameText.getText().trim());
dbConn.setPassword(passwordText.getText().trim());
shell.close();
}
}
});
Button cancelButton = new Button(composite, SWT.PUSH);
cancelButton.setText(res.getString("Cancel"));
cancelButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
dbConn = null;
shell.close();
}
});
shell.setDefaultButton(okButton);
}
private void addDriverComboLine(Composite composite,String lableText){
Label label = new Label(composite, SWT.RIGHT);
label.setText(lableText);
driversCombo = new Combo(composite, SWT.READ_ONLY|SWT.DROP_DOWN);
driversCombo.addSelectionListener(new SelectionAdapter(){
public void widgetSelected(SelectionEvent e){
int select = driversCombo.getSelectionIndex();
if(select>-1&&select<driverList.size()){
Driver driver = (Driver)driverList.get(select);
jdbcDriverName=driver.getDriverName();
jdbcDrvierClassText.setText(driver.getDriverClass());
jdbcURLText.setText(driver.getURLPrefix());
}
}
});
}
/**添加輸入行*/
private Text addTextLine(Composite composite,String lableText,int textSize){
Label label = new Label(composite, SWT.RIGHT);
label.setText(lableText);
Text text = new Text(composite, SWT.BORDER);
GridData gridData = new GridData();
gridData.widthHint = textSize;
text.setLayoutData(gridData);
return text;
}
/**數據檢查*/
private boolean checkData(){
if(DealString.equalsIgnoreCase(this.connNameText.getText(),"")){
MessageDialog.openError(this.shell,res.getString("TanghanPlugin.DataInput.Error"),res.getString("TanghanPlugin.Error.DBConnection.Name.Null"));
return false;
}
return true;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -