001: package org.dbbrowser.ui.panel.connectioninformationwindow;
002:
003: import infrastructure.internationalization.InternationalizationManager;
004: import infrastructure.propertymanager.PropertyManagementException;
005: import infrastructure.propertymanager.PropertyManager;
006: import java.awt.event.ActionEvent;
007: import java.awt.event.ActionListener;
008: import java.awt.event.MouseListener;
009: import java.util.ArrayList;
010: import javax.swing.*;
011: import javax.swing.table.AbstractTableModel;
012: import org.dbbrowser.drivermanager.ConnectionInfo;
013: import org.dbbrowser.drivermanager.ConnectionInfos;
014: import org.dbbrowser.ui.UIControllerForQueries;
015: import org.dbbrowser.ui.panel.ButtonsPanel;
016: import org.dbbrowser.ui.widget.Button;
017: import org.dbbrowser.util.TableSorter;
018:
019: public class KnownConnectionsPanel extends JPanel implements
020: ActionListener {
021: private static final String TITLE = InternationalizationManager
022: .getInstance().getMessage("dbbrowser-ui",
023: "dbbrowser-ui-dbbrowser-window-title-label", null);;
024:
025: private static final long serialVersionUID = UIControllerForQueries.version;
026:
027: private static final String CONNECT_BUTTON_ICON_FILENAME = PropertyManager
028: .getInstance().getProperty(
029: "dbbrowser-ui-connection-info-window-connect-icon");
030: private static final String DELETE_CONNECTION_INFO_BUTTON_ICON_FILENAME = PropertyManager
031: .getInstance()
032: .getProperty(
033: "dbbrowser-ui-connection-info-window-delete-connection-info-icon");
034: private static final String EDIT_CONNECTION_INFO_BUTTON_ICON_FILENAME = PropertyManager
035: .getInstance()
036: .getProperty(
037: "dbbrowser-ui-connection-info-window-edit-connection-info-icon");
038: private static final String USE_MASTER_PASSWORD_TO_ENCRYPT_CONNECTION_INFO = PropertyManager
039: .getInstance()
040: .getProperty(
041: "dbbrowser-ui-connection-info-window-use-master-password");
042: private static final String USE_MASTER_PASSWORD_TO_ENCRYPT_CONNECTION_INFO_LABEL = InternationalizationManager
043: .getInstance()
044: .getMessage(
045: "dbbrowser-ui",
046: "dbbrowser-ui-known-connections-panel-use-master-password-label",
047: null);
048:
049: private JTable tableOfKnownConnections = null;
050: private TableSorter sorter = null;
051: private ConnectionInfos connectionInfos = null;
052: private static final int COLUMN_COUNT = 3;
053: private KnownConnectionsTableModel knownConnectionsTableModel = null;
054: private JCheckBox checkBoxToUseMasterPassword = new JCheckBox(
055: USE_MASTER_PASSWORD_TO_ENCRYPT_CONNECTION_INFO_LABEL);
056:
057: public KnownConnectionsPanel(ConnectionInfos connectionInfos,
058: ActionListener listener,
059: MouseListener knownConnectionsTableMouseListener) {
060: this .tableOfKnownConnections = new JTable();
061: this .tableOfKnownConnections
062: .addMouseListener(knownConnectionsTableMouseListener);
063: JScrollPane pane = new JScrollPane(tableOfKnownConnections);
064: this .add(pane);
065:
066: this .connectionInfos = connectionInfos;
067: String panelTitle = InternationalizationManager.getInstance()
068: .getMessage("dbbrowser-ui",
069: "dbbrowser-ui-known-connections-panel-title",
070: null);
071: this .setBorder(BorderFactory.createTitledBorder(panelTitle));
072:
073: this .setLayout(new BoxLayout(this , BoxLayout.PAGE_AXIS));
074:
075: //Set the table attributes
076: knownConnectionsTableModel = new KnownConnectionsTableModel();
077: sorter = new TableSorter(knownConnectionsTableModel);
078:
079: sorter.setTableHeader(tableOfKnownConnections.getTableHeader());
080: this .tableOfKnownConnections.setModel(sorter);
081:
082: //Add the checkbox to use master password or not
083: if ("true"
084: .equals(USE_MASTER_PASSWORD_TO_ENCRYPT_CONNECTION_INFO)) {
085: this .checkBoxToUseMasterPassword.setSelected(true);
086: }
087: this .checkBoxToUseMasterPassword.addActionListener(this );
088:
089: //if there are no connection infos, allow the user to choose whether to use a master password or not
090: if (this .connectionInfos.getListOfConnectionInfos().size() != 0) {
091: this .checkBoxToUseMasterPassword.setEnabled(false);
092: }
093:
094: JPanel p = new JPanel();
095: p.add(this .checkBoxToUseMasterPassword);
096:
097: this .add(p);
098:
099: //Select the first row if there are more than one rows
100: if (this .tableOfKnownConnections.getRowCount() > 0) {
101: this .tableOfKnownConnections.setRowSelectionInterval(0, 0);
102: }
103:
104: //Setup buttons panel
105: String connectButtonLabel = InternationalizationManager
106: .getInstance()
107: .getMessage(
108: "dbbrowser-ui",
109: "dbbrowser-ui-known-connections-panel-connect-button-label",
110: null);
111: Button connectButton = new Button(connectButtonLabel, listener,
112: connectButtonLabel, new ImageIcon(
113: CONNECT_BUTTON_ICON_FILENAME), Boolean.TRUE);
114: String deleteConnectionInfoButtonLabel = InternationalizationManager
115: .getInstance()
116: .getMessage(
117: "dbbrowser-ui",
118: "dbbrowser-ui-known-connections-panel-delete-connection-info-button-label",
119: null);
120: Button deleteConnectionInfoButton = new Button(
121: deleteConnectionInfoButtonLabel, listener,
122: deleteConnectionInfoButtonLabel, new ImageIcon(
123: DELETE_CONNECTION_INFO_BUTTON_ICON_FILENAME),
124: Boolean.FALSE);
125: String editConnectionInfoButtonLabel = InternationalizationManager
126: .getInstance()
127: .getMessage(
128: "dbbrowser-ui",
129: "dbbrowser-ui-known-connections-panel-edit-connection-info-button-label",
130: null);
131: Button editConnectionInfoButton = new Button(
132: editConnectionInfoButtonLabel, listener,
133: editConnectionInfoButtonLabel, new ImageIcon(
134: EDIT_CONNECTION_INFO_BUTTON_ICON_FILENAME),
135: Boolean.FALSE);
136:
137: ArrayList listOfButtons = new ArrayList();
138: listOfButtons.add(connectButton);
139: listOfButtons.add(deleteConnectionInfoButton);
140: listOfButtons.add(editConnectionInfoButton);
141: ButtonsPanel buttons = new ButtonsPanel(listOfButtons);
142:
143: this .add(buttons);
144: }
145:
146: public void actionPerformed(ActionEvent e) {
147: try {
148: if (this .checkBoxToUseMasterPassword.isSelected()) {
149: PasswordInputWindow piw = new PasswordInputWindow(
150: Boolean.TRUE);
151: piw.show();
152:
153: if (piw.getPassword() != null) {
154: //Set the master password
155: this .connectionInfos.setMasterPassword(piw
156: .getPassword());
157: PropertyManager
158: .getInstance()
159: .setProperty(
160: "dbbrowser-ui-connection-info-window-use-master-password",
161: "true");
162: } else {
163: this .checkBoxToUseMasterPassword.setSelected(false);
164: PropertyManager
165: .getInstance()
166: .setProperty(
167: "dbbrowser-ui-connection-info-window-use-master-password",
168: "true");
169: }
170: } else {
171: //Get the connection info
172: PropertyManager
173: .getInstance()
174: .setProperty(
175: "dbbrowser-ui-connection-info-window-use-master-password",
176: "false");
177: }
178: } catch (PropertyManagementException exc) {
179: String errorMessage = InternationalizationManager
180: .getInstance()
181: .getMessage(
182: "dbbrowser-ui",
183: "dbbrowser-ui-known-connections-panel-master-password-set-failed-error-message",
184: null);
185: JOptionPane.showMessageDialog(null, errorMessage, TITLE,
186: JOptionPane.ERROR_MESSAGE);
187: }
188: }
189:
190: public Integer getSelectedRow() {
191: int selectedRow = this .tableOfKnownConnections.getSelectedRow();
192:
193: Integer selectedRowInteger = null;
194: if (selectedRow >= 0) {
195: selectedRowInteger = new Integer(this .sorter
196: .modelIndex(selectedRow));
197: }
198: return selectedRowInteger;
199: }
200:
201: public void update() {
202: this .knownConnectionsTableModel.fireTableDataChanged();
203:
204: //if there are any connection infos, disable the checkbox
205: if (this .connectionInfos.getListOfConnectionInfos().size() > 0) {
206: this .checkBoxToUseMasterPassword.setEnabled(false);
207: }
208: }
209:
210: private class KnownConnectionsTableModel extends AbstractTableModel {
211: private static final long serialVersionUID = 1l;
212: private String[] columnNames = new String[COLUMN_COUNT];
213:
214: private KnownConnectionsTableModel() {
215: String column1 = InternationalizationManager
216: .getInstance()
217: .getMessage(
218: "dbbrowser-ui",
219: "dbbrowser-ui-known-connections-panel-table-columnheader-name",
220: null);
221: String column2 = InternationalizationManager
222: .getInstance()
223: .getMessage(
224: "dbbrowser-ui",
225: "dbbrowser-ui-known-connections-panel-table-columnheader-databaseurl",
226: null);
227: String column3 = InternationalizationManager
228: .getInstance()
229: .getMessage(
230: "dbbrowser-ui",
231: "dbbrowser-ui-known-connections-panel-table-columnheader-lastused",
232: null);
233:
234: columnNames[0] = column1;
235: columnNames[1] = column2;
236: columnNames[2] = column3;
237: }
238:
239: public String getColumnName(int col) {
240: return columnNames[col];
241: }
242:
243: public int getRowCount() {
244: return connectionInfos.getListOfConnectionInfos().size();
245: }
246:
247: public int getColumnCount() {
248: return COLUMN_COUNT;
249: }
250:
251: public Object getValueAt(int row, int col) {
252: ConnectionInfo ci = null;
253: String cellValue = null;
254: switch (col) {
255: case 0:
256: ci = (ConnectionInfo) connectionInfos
257: .getListOfConnectionInfos().get(row);
258: cellValue = ci.getName();
259: break;
260: case 1:
261: ci = (ConnectionInfo) connectionInfos
262: .getListOfConnectionInfos().get(row);
263: cellValue = ci.getDatabaseURL();
264: break;
265: case 2:
266: ci = (ConnectionInfo) connectionInfos
267: .getListOfConnectionInfos().get(row);
268: if (ci.getLastUsed() == null) {
269: cellValue = "";
270: } else {
271: cellValue = ci.getLastUsed().toString();
272: }
273: break;
274: }
275: return cellValue;
276: }
277: }
278: }
|