001: package org.dbbrowser.ui;
002:
003: import infrastructure.internationalization.InternationalizationManager;
004: import infrastructure.propertymanager.PropertyManager;
005: import java.awt.Component;
006: import java.awt.Dimension;
007: import java.awt.GridLayout;
008: import java.awt.event.ActionEvent;
009: import java.awt.event.ActionListener;
010: import java.awt.event.KeyAdapter;
011: import java.awt.event.KeyEvent;
012: import java.io.FileNotFoundException;
013: import java.io.IOException;
014: import java.util.ArrayList;
015: import java.util.List;
016: import java.util.Observer;
017: import javax.swing.BorderFactory;
018: import javax.swing.BoxLayout;
019: import javax.swing.Icon;
020: import javax.swing.ImageIcon;
021: import javax.swing.JCheckBox;
022: import javax.swing.JComboBox;
023: import javax.swing.JLabel;
024: import javax.swing.JList;
025: import javax.swing.JOptionPane;
026: import javax.swing.JPanel;
027: import javax.swing.JScrollPane;
028: import javax.swing.JTable;
029: import javax.swing.JTextField;
030: import javax.swing.KeyStroke;
031: import javax.swing.ListCellRenderer;
032: import javax.swing.table.AbstractTableModel;
033: import org.dbbrowser.ui.helper.KeyBindingsSerializer;
034: import org.dbbrowser.ui.panel.ButtonsPanel;
035: import org.dbbrowser.ui.widget.Button;
036: import org.dbbrowser.util.TableSorter;
037:
038: public class EditKeyBindingsPanel extends JPanel implements
039: ActionListener {
040: private static final String TITLE = InternationalizationManager
041: .getInstance().getMessage("dbbrowser-ui",
042: "dbbrowser-ui-dbbrowser-window-title-label", null);
043:
044: private static final String EXISTING_KEY_BINDINGS_PANEL_TITLE = InternationalizationManager
045: .getInstance()
046: .getMessage(
047: "dbbrowser-ui",
048: "dbbrowser-ui-preferences-window-existing-key-bindings-panel-title",
049: null);
050: private static final String NEW_KEY_BINDINGS_PANEL_TITLE = InternationalizationManager
051: .getInstance()
052: .getMessage(
053: "dbbrowser-ui",
054: "dbbrowser-ui-preferences-window-new-key-bindings-panel-title",
055: null);
056: private static final String DESCRIPTION_LABEL = InternationalizationManager
057: .getInstance()
058: .getMessage(
059: "dbbrowser-ui",
060: "dbbrowser-ui-preferences-window-key-bindings-table-header-description-label",
061: null);
062: private static final String KEY_LABEL = InternationalizationManager
063: .getInstance()
064: .getMessage(
065: "dbbrowser-ui",
066: "dbbrowser-ui-preferences-window-key-bindings-table-header-key-label",
067: null);
068: private static final String ACTION_LABEL = InternationalizationManager
069: .getInstance()
070: .getMessage(
071: "dbbrowser-ui",
072: "dbbrowser-ui-preferences-window-key-bindings-table-header-action-label",
073: null);
074:
075: private static final String SHOW_SQL_LOG_LABEL = InternationalizationManager
076: .getInstance()
077: .getMessage(
078: "dbbrowser-ui",
079: "dbbrowser-ui-preferences-window-show-sql-log-label",
080: null);
081: private static final String SORT_COLUMNS_LABEL = InternationalizationManager
082: .getInstance().getMessage("dbbrowser-ui",
083: "dbbrowser-ui-preferences-window-sort-columns",
084: null);
085:
086: private static final String EDIT_KEY_BINDINGS_BUTTON_LABEL = InternationalizationManager
087: .getInstance()
088: .getMessage(
089: "dbbrowser-ui",
090: "dbbrowser-ui-preferences-window-edit-key-bindings-panel-edit-key-bindings-button-label",
091: null);
092: private static final String EDIT_KEY_BINDINGS_ICON_FILENAME = PropertyManager
093: .getInstance()
094: .getProperty(
095: "dbbrowser-ui-connection-info-window-edit-connection-info-icon");
096: private static final String DELETE_KEY_BINDINGS_BUTTON_LABEL = InternationalizationManager
097: .getInstance()
098: .getMessage(
099: "dbbrowser-ui",
100: "dbbrowser-ui-preferences-window-edit-key-bindings-panel-delete-key-bindings-button-label",
101: null);
102: private static final String DELETE_KEY_BINDINGS_ICON_FILENAME = PropertyManager
103: .getInstance()
104: .getProperty(
105: "dbbrowser-ui-connection-info-window-delete-connection-info-icon");
106: private static final String SAVE_CHANGES_BUTTON_LABEL = InternationalizationManager
107: .getInstance()
108: .getMessage(
109: "dbbrowser-ui",
110: "dbbrowser-ui-preferences-window-edit-key-bindings-panel-save-changes-button-label",
111: null);
112: private static final String SAVE_CHANGES_ICON_FILENAME = PropertyManager
113: .getInstance()
114: .getProperty(
115: "dbbrowser-ui-view-record-window-update-record-icon");
116:
117: private Icon iconForSaveChanges = new ImageIcon(
118: SAVE_CHANGES_ICON_FILENAME);
119: private Icon iconForEditKeyBindings = new ImageIcon(
120: EDIT_KEY_BINDINGS_ICON_FILENAME);
121: private Icon iconForDeleteKeyBindings = new ImageIcon(
122: DELETE_KEY_BINDINGS_ICON_FILENAME);
123:
124: private JTable tableForKeyBindings = new JTable();
125: private JScrollPane paneForKeyBindings = new JScrollPane(
126: tableForKeyBindings);
127:
128: private JTextField textFieldForDescription = new JTextField("");
129: private JTextField textFieldForKey = new JTextField("");
130: private JComboBox boxForActions = new JComboBox(KeyBinding
131: .getAllActions().toArray());
132:
133: private JTextField textFieldForSQLStatementTerminator = new JTextField(
134: "");
135: private JCheckBox checkBoxForAutoCommit = new JCheckBox();
136: private JCheckBox checkBoxForShowSQL = new JCheckBox();
137: private JCheckBox checkBoxForSortColumns = new JCheckBox();
138:
139: //Icons for actions
140: private static final String RUN_SQL_ICON_FILENAME = PropertyManager
141: .getInstance()
142: .getProperty(
143: "dbbrowser-ui-dbbrowser-window-sql-tab-run-sql-icon");
144: private static final String connectIconFileName = PropertyManager
145: .getInstance()
146: .getProperty(
147: "dbbrowser-ui-connection-info-window-test-connection-icon");
148: private static final String dropConnectionIconFileName = PropertyManager
149: .getInstance()
150: .getProperty(
151: "dbbrowser-ui-dbbrowser-window-sql-tab-remove-sql-tab-button-icon");
152: private static final String exitIconFileName = PropertyManager
153: .getInstance()
154: .getProperty(
155: "dbbrowser-ui-dbbrowser-window-file-menu-exit-icon");
156: private static final String lookAndFeelIconFileName = PropertyManager
157: .getInstance()
158: .getProperty(
159: "dbbrowser-ui-dbbrowser-window-toolbar-lookandfeel-icon");
160: private static final String importIconFileName = PropertyManager
161: .getInstance()
162: .getProperty(
163: "dbbrowser-ui-dbbrowser-window-toolbar-runbatchfile-icon");
164: private static final String refreshIconFileName = PropertyManager
165: .getInstance()
166: .getProperty(
167: "dbbrowser-ui-dbbrowser-window-toolbar-refresh-icon");
168: private static final String aboutIconFileName = PropertyManager
169: .getInstance()
170: .getProperty(
171: "dbbrowser-ui-dbbrowser-window-help-about-menu-icon");
172: private static final String viewToolbarIconFileName = PropertyManager
173: .getInstance()
174: .getProperty(
175: "dbbrowser-ui-dbbrowser-window-view-menu-toolbar-icon");
176: private static final String viewSQLLogIconFileName = PropertyManager
177: .getInstance()
178: .getProperty(
179: "dbbrowser-ui-dbbrowser-window-view-menu-show-sql-log-icon");
180: private static final String preferencesIconFileName = PropertyManager
181: .getInstance()
182: .getProperty(
183: "dbbrowser-ui-dbbrowser-window-toolbar-preferences-icon");
184: private static final String reverseEngineerIconFileName = PropertyManager
185: .getInstance()
186: .getProperty(
187: "dbbrowser-ui-dbbrowser-window-toolbar-reverse-engineer-icon");
188: private static final String commitIconFileName = PropertyManager
189: .getInstance()
190: .getProperty(
191: "dbbrowser-ui-dbbrowser-window-sql-tab-commit-button-icon");
192: private static final String rollbackIconFileName = PropertyManager
193: .getInstance()
194: .getProperty(
195: "dbbrowser-ui-dbbrowser-window-sql-tab-rollback-button-icon");
196: private static final String helpIconFileName = PropertyManager
197: .getInstance().getProperty(
198: "dbbrowser-ui-dbbrowser-window-toolbar-help-icon");
199: private static final String commentsFeedbackIconFileName = PropertyManager
200: .getInstance()
201: .getProperty(
202: "dbbrowser-ui-dbbrowser-window-help-comments-feedback-menu-icon");
203: private static final String windowIconFileName = PropertyManager
204: .getInstance().getProperty(
205: "dbbrowser-ui-dbbrowser-window-icon-filename");
206:
207: private TableSorter tableSorter = null;
208: private List listOfKeyBindings = null;
209: private Observer observer = null;
210:
211: public EditKeyBindingsPanel(Observer observer) {
212: this .observer = observer;
213: initialize();
214: }
215:
216: public void actionPerformed(ActionEvent e) {
217: //Handle Save changes
218: if (SAVE_CHANGES_BUTTON_LABEL.equals(e.getActionCommand())) {
219: String description = this .textFieldForDescription.getText();
220: KeyStroke keyStroke = KeyStroke
221: .getKeyStroke(this .textFieldForKey.getText());
222:
223: //Check both description and keystroke have been set
224: if ((description != null && (!"".equals(description)))
225: && keyStroke != null) {
226: int keyCode = keyStroke.getKeyCode();
227: int modifier = keyStroke.getModifiers();
228: String action = (String) this .boxForActions
229: .getSelectedItem();
230:
231: KeyBinding keyBinding = new KeyBinding(description,
232: new Integer(keyCode), new Integer(modifier),
233: action);
234:
235: //if the list already contains a key binding with this description, then replace it with this one
236: if (this .listOfKeyBindings.contains(keyBinding)) {
237: this .listOfKeyBindings.remove(keyBinding);
238: }
239:
240: this .listOfKeyBindings.add(keyBinding);
241:
242: //Save the changes to XML file
243: try {
244: KeyBindingsSerializer
245: .serializeKeyBindings(this .listOfKeyBindings);
246:
247: this .textFieldForDescription.setText("");
248: this .textFieldForKey.setText("");
249: this .boxForActions.setSelectedIndex(0);
250:
251: ((AbstractTableModel) this .tableSorter
252: .getTableModel()).fireTableDataChanged();
253:
254: //Update the observer
255: this .observer.update(null, null);
256: } catch (IOException exc) {
257: String[] params = new String[] { exc.getMessage() };
258: String errorMessage = InternationalizationManager
259: .getInstance()
260: .getMessage(
261: "dbbrowser-ui",
262: "dbbrowser-ui-preferences-window-key-bindings-save-error-message",
263: params);
264: JOptionPane.showMessageDialog(null, errorMessage,
265: TITLE, JOptionPane.ERROR_MESSAGE);
266: }
267: } else {
268: String errorMessage = InternationalizationManager
269: .getInstance()
270: .getMessage(
271: "dbbrowser-ui",
272: "dbbrowser-ui-preferences-window-key-bindings-values-not-entered-error-message",
273: null);
274: JOptionPane.showMessageDialog(null, errorMessage,
275: TITLE, JOptionPane.WARNING_MESSAGE);
276: }
277: }
278:
279: //Handle Save changes
280: if (EDIT_KEY_BINDINGS_BUTTON_LABEL.equals(e.getActionCommand())) {
281: Integer selectedRow = getSelectedRow();
282: //if the source is a row in the table
283: if (selectedRow != null) {
284: KeyBinding keyBinding = (KeyBinding) this .listOfKeyBindings
285: .get(selectedRow.intValue());
286: this .textFieldForDescription.setText(keyBinding
287: .getDescription());
288: this .textFieldForKey
289: .setText(KeyEvent.getKeyText(keyBinding
290: .getKeyCode().intValue()));
291: this .boxForActions.setSelectedItem(keyBinding
292: .getAction());
293: } else {
294: String errorMessage = InternationalizationManager
295: .getInstance()
296: .getMessage(
297: "dbbrowser-ui",
298: "dbbrowser-ui-preferences-window-key-bindings-key-not-selected-error-message",
299: null);
300: JOptionPane.showMessageDialog(null, errorMessage,
301: TITLE, JOptionPane.WARNING_MESSAGE);
302: }
303: }
304:
305: if (DELETE_KEY_BINDINGS_BUTTON_LABEL.equals(e
306: .getActionCommand())) {
307: Integer selectedRow = getSelectedRow();
308: //if the source is a row in the table
309: if (selectedRow != null) {
310: this .listOfKeyBindings.remove(selectedRow.intValue());
311: this .textFieldForDescription.setText("");
312: this .textFieldForKey.setText("");
313: this .boxForActions.setSelectedIndex(0);
314:
315: try {
316: KeyBindingsSerializer
317: .serializeKeyBindings(this .listOfKeyBindings);
318: ((AbstractTableModel) this .tableSorter
319: .getTableModel()).fireTableDataChanged();
320: } catch (IOException exc) {
321: String[] params = new String[] { exc.getMessage() };
322: String errorMessage = InternationalizationManager
323: .getInstance()
324: .getMessage(
325: "dbbrowser-ui",
326: "dbbrowser-ui-preferences-window-key-bindings-save-error-message",
327: params);
328: JOptionPane.showMessageDialog(null, errorMessage,
329: TITLE, JOptionPane.ERROR_MESSAGE);
330: }
331: } else {
332: String errorMessage = InternationalizationManager
333: .getInstance()
334: .getMessage(
335: "dbbrowser-ui",
336: "dbbrowser-ui-preferences-window-key-bindings-key-not-selected-error-message",
337: null);
338: JOptionPane.showMessageDialog(null, errorMessage,
339: TITLE, JOptionPane.WARNING_MESSAGE);
340: }
341: }
342: }
343:
344: public Integer getSelectedRow() {
345: int selectedRow = this .tableForKeyBindings.getSelectedRow();
346:
347: Integer selectedRowInteger = null;
348: if (selectedRow >= 0) {
349: selectedRowInteger = new Integer(this .tableSorter
350: .modelIndex(selectedRow));
351: }
352: return selectedRowInteger;
353: }
354:
355: private void initialize() {
356: //Load the key bindings
357: try {
358: this .listOfKeyBindings = KeyBindingsSerializer
359: .deserializeKeyBindings();
360: } catch (FileNotFoundException exc) {
361: //Ignore if they have not been set
362: this .listOfKeyBindings = new ArrayList();
363: } catch (IOException exc) {
364: String[] params = new String[] { exc.getMessage() };
365: String errorMessage = InternationalizationManager
366: .getInstance()
367: .getMessage(
368: "dbbrowser-ui",
369: "dbbrowser-ui-preferences-window-key-bindings-save-error-message",
370: params);
371: JOptionPane.showMessageDialog(null, errorMessage, TITLE,
372: JOptionPane.ERROR_MESSAGE);
373: this .listOfKeyBindings = new ArrayList();
374: }
375:
376: //Setup the panel
377: this .setLayout(new BoxLayout(this , BoxLayout.PAGE_AXIS));
378:
379: //Panel for existing key bindings
380: JPanel panelForExistingKeyBindings = new JPanel();
381: panelForExistingKeyBindings.setBorder(BorderFactory
382: .createTitledBorder(EXISTING_KEY_BINDINGS_PANEL_TITLE));
383: panelForExistingKeyBindings.setLayout(new BoxLayout(
384: panelForExistingKeyBindings, BoxLayout.PAGE_AXIS));
385:
386: //Setup table for existing key bindings
387: this .tableSorter = new TableSorter(new KeyBindingsTableModel());
388: this .tableSorter.setTableHeader(this .tableForKeyBindings
389: .getTableHeader());
390: this .tableForKeyBindings.setModel(tableSorter);
391: panelForExistingKeyBindings.add(this .paneForKeyBindings);
392:
393: //Build the list of buttons to delete selected key binding
394: List listOfButtons = new ArrayList();
395: Button editKeyBindingsButton = new Button(
396: EDIT_KEY_BINDINGS_BUTTON_LABEL, this ,
397: EDIT_KEY_BINDINGS_BUTTON_LABEL, iconForEditKeyBindings,
398: Boolean.FALSE);
399: listOfButtons.add(editKeyBindingsButton);
400: Button deleteKeyBindingsButton = new Button(
401: DELETE_KEY_BINDINGS_BUTTON_LABEL, this ,
402: DELETE_KEY_BINDINGS_BUTTON_LABEL,
403: iconForDeleteKeyBindings, Boolean.FALSE);
404: listOfButtons.add(deleteKeyBindingsButton);
405:
406: //Setup the navigation panel
407: ButtonsPanel butonsPanel = new ButtonsPanel(listOfButtons);
408: panelForExistingKeyBindings.add(butonsPanel);
409:
410: this .add(panelForExistingKeyBindings);
411:
412: //Panel for new key bindings
413: JPanel panelForNewKeyBindings = new JPanel();
414: panelForNewKeyBindings.setBorder(BorderFactory
415: .createTitledBorder(NEW_KEY_BINDINGS_PANEL_TITLE));
416: panelForNewKeyBindings.setLayout(new BoxLayout(
417: panelForNewKeyBindings, BoxLayout.PAGE_AXIS));
418:
419: //Textfield and label for description
420: JPanel panelForFormFields = new JPanel();
421: panelForFormFields.setPreferredSize(new Dimension(100, 100));
422: panelForFormFields.setLayout(new GridLayout(3, 2));
423:
424: JLabel labelForDescription = new JLabel(DESCRIPTION_LABEL);
425: panelForFormFields.add(labelForDescription);
426: panelForFormFields.add(textFieldForDescription);
427:
428: JLabel labelForKey = new JLabel(KEY_LABEL);
429: panelForFormFields.add(labelForKey);
430: textFieldForKey.addKeyListener(new KeyTextFieldKeyListener());
431: panelForFormFields.add(textFieldForKey);
432:
433: JLabel labelForAction = new JLabel(ACTION_LABEL);
434: panelForFormFields.add(labelForAction);
435:
436: //Set the renderer for combo box
437: boxForActions.setRenderer(new KeyBindingsComboBoxRenderer());
438: panelForFormFields.add(boxForActions);
439:
440: //Add form fields
441: panelForNewKeyBindings.add(panelForFormFields);
442:
443: //Build the list of buttons to save new key binding
444: listOfButtons = new ArrayList();
445: Button firstRecordButton = new Button(
446: SAVE_CHANGES_BUTTON_LABEL, this ,
447: SAVE_CHANGES_BUTTON_LABEL, iconForSaveChanges,
448: Boolean.FALSE);
449: listOfButtons.add(firstRecordButton);
450:
451: //Setup the navigation panel
452: butonsPanel = new ButtonsPanel(listOfButtons);
453: panelForNewKeyBindings.add(butonsPanel);
454:
455: this .add(panelForNewKeyBindings);
456: }
457:
458: private class KeyBindingsTableModel extends AbstractTableModel {
459: private static final long serialVersionUID = 1l;
460: private String[] columnNames = new String[3];
461:
462: private KeyBindingsTableModel() {
463: columnNames[0] = DESCRIPTION_LABEL;
464: columnNames[1] = KEY_LABEL;
465: columnNames[2] = ACTION_LABEL;
466: }
467:
468: public String getColumnName(int col) {
469: return columnNames[col];
470: }
471:
472: public int getRowCount() {
473: return listOfKeyBindings.size();
474: }
475:
476: public int getColumnCount() {
477: return 3;
478: }
479:
480: public Object getValueAt(int row, int col) {
481: KeyBinding keyBinding = null;
482: String cellValue = null;
483: switch (col) {
484: case 0:
485: keyBinding = (KeyBinding) listOfKeyBindings.get(row);
486: cellValue = keyBinding.getDescription();
487: break;
488: case 1:
489: keyBinding = (KeyBinding) listOfKeyBindings.get(row);
490: cellValue = KeyEvent.getKeyText(keyBinding.getKeyCode()
491: .intValue());// + " - " + keyBinding.getModifierCode().intValue();
492: break;
493: case 2:
494: keyBinding = (KeyBinding) listOfKeyBindings.get(row);
495: cellValue = keyBinding.getAction();
496: break;
497: }
498: return cellValue;
499: }
500: }
501:
502: private class KeyBindingsComboBoxRenderer implements
503: ListCellRenderer {
504: public Component getListCellRendererComponent(JList list,
505: Object value, int index, boolean isSelected,
506: boolean cellHasFocus) {
507: String iconFileName = null;
508: switch (index) {
509: case 0:
510: iconFileName = helpIconFileName;
511: break;
512: case 1:
513: iconFileName = connectIconFileName;
514: break;
515: case 2:
516: iconFileName = dropConnectionIconFileName;
517: break;
518: case 3:
519: iconFileName = importIconFileName;
520: break;
521: case 4:
522: iconFileName = exitIconFileName;
523: break;
524: case 5:
525: iconFileName = preferencesIconFileName;
526: break;
527: case 6:
528: iconFileName = commitIconFileName;
529: break;
530: case 7:
531: iconFileName = rollbackIconFileName;
532: break;
533: }
534: JLabel label = new JLabel(value.toString());
535:
536: if (iconFileName != null) {
537: label.setIcon(new ImageIcon(iconFileName));
538: }
539: return label;
540: }
541: }
542:
543: private class KeyTextFieldKeyListener extends KeyAdapter {
544: public void keyReleased(KeyEvent e) {
545: e.consume();
546: }
547:
548: public void keyTyped(KeyEvent e) {
549: e.consume();
550: }
551:
552: public void keyPressed(KeyEvent e) {
553: textFieldForKey
554: .setText(KeyEvent.getKeyText(e.getKeyCode()));
555: e.consume();
556: }
557: }
558: }
|