001: /*
002: * ConnectionPropertiesEditor.java
003: *
004: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
005: *
006: * Copyright 2002-2008, Thomas Kellerer
007: * No part of this code maybe reused without the permission of the author
008: *
009: * To contact the author please send an email to: support@sql-workbench.net
010: *
011: */
012: package workbench.gui.profiles;
013:
014: import java.awt.BorderLayout;
015: import java.awt.Dimension;
016: import java.awt.Window;
017: import java.sql.Types;
018: import java.util.Enumeration;
019: import java.util.Properties;
020: import javax.swing.JCheckBox;
021:
022: import javax.swing.JPanel;
023: import javax.swing.JScrollPane;
024:
025: import workbench.db.ConnectionProfile;
026: import workbench.gui.actions.DeleteListEntryAction;
027: import workbench.gui.actions.NewListEntryAction;
028: import workbench.gui.components.DataStoreTableModel;
029: import workbench.gui.components.TableColumnOptimizer;
030: import workbench.gui.components.ValidatingDialog;
031: import workbench.gui.components.WbTable;
032: import workbench.gui.components.WbToolbar;
033: import workbench.interfaces.FileActions;
034: import workbench.resource.ResourceMgr;
035: import workbench.storage.DataStore;
036: import workbench.util.StringUtil;
037:
038: /**
039: *
040: * @author support@sql-workbench.net
041: */
042: public class ConnectionPropertiesEditor extends JPanel implements
043: FileActions {
044: private DataStore propData;
045: private WbTable propTable;
046:
047: private NewListEntryAction newItem;
048: private DeleteListEntryAction deleteItem;
049: private JCheckBox copyProps;
050:
051: public ConnectionPropertiesEditor(ConnectionProfile profile) {
052: String[] cols = new String[] {
053: ResourceMgr.getString("TxtConnDataPropName"),
054: ResourceMgr.getString("TxtConnDataPropValue") };
055: int[] types = new int[] { Types.VARCHAR, Types.VARCHAR };
056: int[] sizes = new int[] { 15, 5 };
057:
058: this .propData = new DataStore(cols, types, sizes);
059: this .propData.setAllowUpdates(true);
060: Properties source = profile.getConnectionProperties();
061:
062: if (source != null) {
063: Enumeration keys = source.propertyNames();
064: while (keys.hasMoreElements()) {
065: String key = (String) keys.nextElement();
066: String value = source.getProperty(key);
067: int row = this .propData.addRow();
068: this .propData.setValue(row, 0, key);
069: this .propData.setValue(row, 1, value);
070: }
071: }
072: this .propTable = new WbTable();
073:
074: this .propTable.setModel(new DataStoreTableModel(this .propData));
075: TableColumnOptimizer optimizer = new TableColumnOptimizer(
076: this .propTable);
077: optimizer.optimizeAllColWidth(100, -1, true);
078:
079: this .setLayout(new BorderLayout());
080: JScrollPane scroll = new JScrollPane(this .propTable);
081:
082: WbToolbar toolbar = new WbToolbar();
083: toolbar.addDefaultBorder();
084: this .newItem = new NewListEntryAction(this );
085: this .deleteItem = new DeleteListEntryAction(this );
086:
087: toolbar.add(this .newItem);
088: toolbar.add(this .deleteItem);
089: this .add(toolbar, BorderLayout.NORTH);
090: this .add(scroll, BorderLayout.CENTER);
091: copyProps = new JCheckBox(ResourceMgr
092: .getString("LblCpProps2System"));
093: copyProps.setToolTipText(ResourceMgr
094: .getDescription("LblCpProps2System"));
095: this .add(copyProps, BorderLayout.SOUTH);
096: this .copyProps.setSelected(profile
097: .getCopyExtendedPropsToSystem());
098: }
099:
100: public boolean getCopyToSystem() {
101: return this .copyProps.isSelected();
102: }
103:
104: public Properties getProperties() {
105: Properties props = new Properties();
106: this .propTable.stopEditing();
107: int count = this .propData.getRowCount();
108: for (int row = 0; row < count; row++) {
109: String key = this .propData.getValueAsString(row, 0);
110: if (StringUtil.isEmptyString(key))
111: continue;
112: String value = this .propData.getValueAsString(row, 1);
113: props.setProperty(key.trim(), (value == null ? "" : value
114: .trim()));
115: }
116: return props;
117: }
118:
119: public void deleteItem() throws Exception {
120: this .propTable.deleteRow();
121: }
122:
123: public void newItem(boolean copyCurrent) throws Exception {
124: this .propTable.addRow();
125: this .propTable.getSelectionModel().clearSelection();
126: }
127:
128: public void saveItem() throws Exception {
129: }
130:
131: public static void editProperties(Window parent,
132: ConnectionProfile profile) {
133: ConnectionPropertiesEditor editor = new ConnectionPropertiesEditor(
134: profile);
135: Dimension d = new Dimension(300, 250);
136: editor.setMinimumSize(d);
137: editor.setPreferredSize(d);
138:
139: boolean ok = ValidatingDialog.showConfirmDialog(parent, editor,
140: ResourceMgr.getString("TxtEditConnPropsWindowTitle"));
141: if (ok) {
142: profile.setConnectionProperties(editor.getProperties());
143: profile.setCopyExtendedPropsToSystem(editor
144: .getCopyToSystem());
145: }
146: }
147: }
|