0001: package org.dbbrowser.ui;
0002:
0003: import infrastructure.internationalization.InternationalizationManager;
0004: import infrastructure.lookandfeel.LookAndFeelHandler;
0005: import infrastructure.propertymanager.PropertyManagementException;
0006: import infrastructure.propertymanager.PropertyManager;
0007: import infrastructure.logging.Log;
0008: import java.awt.BorderLayout;
0009: import java.awt.Component;
0010: import java.awt.Image;
0011: import java.awt.event.ActionEvent;
0012: import java.awt.event.ActionListener;
0013: import java.awt.event.ItemEvent;
0014: import java.awt.event.ItemListener;
0015: import java.io.File;
0016: import java.io.FileNotFoundException;
0017: import java.io.IOException;
0018: import java.util.HashMap;
0019: import java.util.Iterator;
0020: import java.util.List;
0021: import java.util.Map;
0022: import java.util.Observable;
0023: import java.util.Observer;
0024: import javax.swing.AbstractAction;
0025: import javax.swing.Action;
0026: import javax.swing.Icon;
0027: import javax.swing.ImageIcon;
0028: import javax.swing.JButton;
0029: import javax.swing.JCheckBoxMenuItem;
0030: import javax.swing.JComponent;
0031: import javax.swing.JFileChooser;
0032: import javax.swing.JFrame;
0033: import javax.swing.JMenu;
0034: import javax.swing.JMenuBar;
0035: import javax.swing.JMenuItem;
0036: import javax.swing.JOptionPane;
0037: import javax.swing.JTabbedPane;
0038: import javax.swing.JToolBar;
0039: import javax.swing.KeyStroke;
0040: import javax.swing.SwingUtilities;
0041: import javax.swing.event.ChangeEvent;
0042: import javax.swing.event.ChangeListener;
0043: import org.dbbrowser.db.engine.exception.DBEngineException;
0044: import org.dbbrowser.drivermanager.ConnectionInfo;
0045: import org.dbbrowser.drivermanager.DriverManagerException;
0046: import org.dbbrowser.ui.helper.KeyBindingsSerializer;
0047: import org.dbbrowser.ui.panel.dbbrowserwindow.DBBrowserConnectionInstancePanel;
0048: import org.dbbrowser.ui.widget.MemoryMonitor;
0049: import org.dbbrowser.help.HelpManager;
0050:
0051: public class DBBrowserWindow implements ActionListener, ItemListener,
0052: ChangeListener, Observer {
0053: private JFrame dbBrowserFrame = new JFrame();
0054: private static final String TITLE = InternationalizationManager
0055: .getInstance().getMessage("dbbrowser-ui",
0056: "dbbrowser-ui-dbbrowser-window-title-label", null);
0057: private JMenuBar menuBar = new JMenuBar();
0058: private JToolBar toolBar = new JToolBar();
0059:
0060: //Labels for file menu
0061: private static final String fileMenuLabel = InternationalizationManager
0062: .getInstance().getMessage("dbbrowser-ui",
0063: "dbbrowser-ui-dbbrowser-window-file-menu-label",
0064: null);
0065: private static final String fileMenuConnectLabel = InternationalizationManager
0066: .getInstance()
0067: .getMessage(
0068: "dbbrowser-ui",
0069: "dbbrowser-ui-dbbrowser-window-file-menu-connect-label",
0070: null);
0071: private static final String fileMenuDropConnectionLabel = InternationalizationManager
0072: .getInstance()
0073: .getMessage(
0074: "dbbrowser-ui",
0075: "dbbrowser-ui-dbbrowser-window-file-menu-drop-connection-label",
0076: null);
0077: private static final String fileMenuBatchRunLabel = InternationalizationManager
0078: .getInstance()
0079: .getMessage(
0080: "dbbrowser-ui",
0081: "dbbrowser-ui-dbbrowser-window-file-menu-batch-run-label",
0082: null);
0083: private static final String fileMenuSetLookandFeelLabel = InternationalizationManager
0084: .getInstance()
0085: .getMessage(
0086: "dbbrowser-ui",
0087: "dbbrowser-ui-dbbrowser-window-file-menu-lookandfeel-label",
0088: null);
0089: private static final String fileMenuExitLabel = InternationalizationManager
0090: .getInstance()
0091: .getMessage(
0092: "dbbrowser-ui",
0093: "dbbrowser-ui-dbbrowser-window-file-menu-exit-label",
0094: null);
0095:
0096: //Labels for Edit menu
0097: private String editMenuLabel = InternationalizationManager
0098: .getInstance().getMessage("dbbrowser-ui",
0099: "dbbrowser-ui-dbbrowser-window-edit-menu-label",
0100: null);
0101: private String editMenuPreferencesLabel = InternationalizationManager
0102: .getInstance()
0103: .getMessage(
0104: "dbbrowser-ui",
0105: "dbbrowser-ui-dbbrowser-window-edit-menu-preferences-label",
0106: null);
0107:
0108: //Labels for Tools menu
0109: private static final String toolsMenuLabel = InternationalizationManager
0110: .getInstance().getMessage("dbbrowser-ui",
0111: "dbbrowser-ui-dbbrowser-window-tools-menu-label",
0112: null);
0113: private static final String toolsMenuReverseEngineerLabel = InternationalizationManager
0114: .getInstance()
0115: .getMessage(
0116: "dbbrowser-ui",
0117: "dbbrowser-ui-dbbrowser-window-tools-menu-paste-label",
0118: null);
0119: private static final String toolsMenuCommitLabel = InternationalizationManager
0120: .getInstance()
0121: .getMessage(
0122: "dbbrowser-ui",
0123: "dbbrowser-ui-dbbrowser-browser-view-record-window-commit-button-label",
0124: null);
0125: private static final String toolsMenuRollbackLabel = InternationalizationManager
0126: .getInstance()
0127: .getMessage(
0128: "dbbrowser-ui",
0129: "dbbrowser-ui-dbbrowser-browser-view-record-window-rollback-button-label",
0130: null);
0131:
0132: //Labels for View menu
0133: private static final String viewMenuLabel = InternationalizationManager
0134: .getInstance().getMessage("dbbrowser-ui",
0135: "dbbrowser-ui-dbbrowser-window-view-menu-label",
0136: null);
0137: private static final String viewMenuRefreshLabel = InternationalizationManager
0138: .getInstance()
0139: .getMessage(
0140: "dbbrowser-ui",
0141: "dbbrowser-ui-dbbrowser-window-view-menu-refresh-label",
0142: null);
0143: private static final String viewMenuShowToolbarLabel = InternationalizationManager
0144: .getInstance()
0145: .getMessage(
0146: "dbbrowser-ui",
0147: "dbbrowser-ui-dbbrowser-window-view-menu-showtoolbar-label",
0148: null);
0149: private static final String viewMenuShowSQLLogLabel = InternationalizationManager
0150: .getInstance()
0151: .getMessage(
0152: "dbbrowser-ui",
0153: "dbbrowser-ui-preferences-window-show-sql-log-label",
0154: null);
0155: private static final String viewMenuShowMemoryMonitorLabel = InternationalizationManager
0156: .getInstance()
0157: .getMessage(
0158: "dbbrowser-ui",
0159: "dbbrowser-ui-dbbrowser-window-view-menu-show-memory-monitor-label",
0160: null);
0161:
0162: //Labels for Help menu
0163: private static final String helpMenuLabel = InternationalizationManager
0164: .getInstance().getMessage("dbbrowser-ui",
0165: "dbbrowser-ui-dbbrowser-window-help-menu-label",
0166: null);
0167: private static final String helpMenuAboutLabel = InternationalizationManager
0168: .getInstance()
0169: .getMessage(
0170: "dbbrowser-ui",
0171: "dbbrowser-ui-dbbrowser-window-help-menu-about-label",
0172: null);
0173: private static final String helpMenuCommentsFeedbackLabel = InternationalizationManager
0174: .getInstance()
0175: .getMessage(
0176: "dbbrowser-ui",
0177: "dbbrowser-ui-dbbrowser-window-help-menu-comments-feedback-label",
0178: null);
0179: private static final String helpMenuHelpLabel = InternationalizationManager
0180: .getInstance()
0181: .getMessage(
0182: "dbbrowser-ui",
0183: "dbbrowser-ui-dbbrowser-window-help-menu-help-label",
0184: null);
0185:
0186: private static final String connectIconFileName = PropertyManager
0187: .getInstance()
0188: .getProperty(
0189: "dbbrowser-ui-connection-info-window-test-connection-icon");
0190: private static final String dropConnectionIconFileName = PropertyManager
0191: .getInstance()
0192: .getProperty(
0193: "dbbrowser-ui-dbbrowser-window-sql-tab-remove-sql-tab-button-icon");
0194: private static final String exitIconFileName = PropertyManager
0195: .getInstance()
0196: .getProperty(
0197: "dbbrowser-ui-dbbrowser-window-file-menu-exit-icon");
0198: private static final String lookAndFeelIconFileName = PropertyManager
0199: .getInstance()
0200: .getProperty(
0201: "dbbrowser-ui-dbbrowser-window-toolbar-lookandfeel-icon");
0202: private static final String importIconFileName = PropertyManager
0203: .getInstance()
0204: .getProperty(
0205: "dbbrowser-ui-dbbrowser-window-toolbar-runbatchfile-icon");
0206: private static final String refreshIconFileName = PropertyManager
0207: .getInstance()
0208: .getProperty(
0209: "dbbrowser-ui-dbbrowser-window-toolbar-refresh-icon");
0210: private static final String aboutIconFileName = PropertyManager
0211: .getInstance()
0212: .getProperty(
0213: "dbbrowser-ui-dbbrowser-window-help-about-menu-icon");
0214: private static final String viewToolbarIconFileName = PropertyManager
0215: .getInstance()
0216: .getProperty(
0217: "dbbrowser-ui-dbbrowser-window-view-menu-toolbar-icon");
0218: private static final String viewSQLLogIconFileName = PropertyManager
0219: .getInstance()
0220: .getProperty(
0221: "dbbrowser-ui-dbbrowser-window-view-menu-show-sql-log-icon");
0222: private static final String viewMemoryMonitorIconFileName = PropertyManager
0223: .getInstance().getProperty("dbbrowser-ui-good-signal-icon");
0224: private static final String preferencesIconFileName = PropertyManager
0225: .getInstance()
0226: .getProperty(
0227: "dbbrowser-ui-dbbrowser-window-toolbar-preferences-icon");
0228: private static final String reverseEngineerIconFileName = PropertyManager
0229: .getInstance()
0230: .getProperty(
0231: "dbbrowser-ui-dbbrowser-window-toolbar-reverse-engineer-icon");
0232: private static final String commitIconFileName = PropertyManager
0233: .getInstance()
0234: .getProperty(
0235: "dbbrowser-ui-dbbrowser-window-sql-tab-commit-button-icon");
0236: private static final String rollbackIconFileName = PropertyManager
0237: .getInstance()
0238: .getProperty(
0239: "dbbrowser-ui-dbbrowser-window-sql-tab-rollback-button-icon");
0240: private static final String helpIconFileName = PropertyManager
0241: .getInstance().getProperty(
0242: "dbbrowser-ui-dbbrowser-window-toolbar-help-icon");
0243: private static final String commentsFeedbackIconFileName = PropertyManager
0244: .getInstance()
0245: .getProperty(
0246: "dbbrowser-ui-dbbrowser-window-help-comments-feedback-menu-icon");
0247: private static final String windowIconFileName = PropertyManager
0248: .getInstance().getProperty(
0249: "dbbrowser-ui-dbbrowser-window-icon-filename");
0250: private Image windowIcon = (new ImageIcon(windowIconFileName))
0251: .getImage();
0252:
0253: private static final String connectionInstanceTabIconFilename = PropertyManager
0254: .getInstance()
0255: .getProperty(
0256: "dbbrowser-ui-dbbrowser-window-connection-instance-tab-icon");
0257: private static final String USE_MASTER_PASSWORD_TO_ENCRYPT_CONNECTION_INFO = PropertyManager
0258: .getInstance()
0259: .getProperty(
0260: "dbbrowser-ui-connection-info-window-use-master-password");
0261:
0262: private JTabbedPane tabbedPaneForConnectionInstance = new JTabbedPane();
0263: private DBBrowserConnectionInstancePanel dbBrowserConnectionInstancePanel = null;
0264: private SQLLogUI sqlLogUI = new SQLLogUI();
0265: private Map mapOfActionNameToAction = new HashMap();
0266: private PreferencesWindow preferencesWindow = null;
0267:
0268: public DBBrowserWindow() {
0269: this .tabbedPaneForConnectionInstance
0270: .setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
0271: initialize();
0272: addMenuBar();
0273: addToolBar();
0274: this .dbBrowserFrame.getContentPane().add(
0275: this .tabbedPaneForConnectionInstance);
0276: setupActionMaps();
0277: }
0278:
0279: private void setupActionMaps() {
0280: //Get all the possible actions and add them to the map
0281: List listOfActions = KeyBinding.getAllActions();
0282: Iterator i = listOfActions.iterator();
0283: while (i.hasNext()) {
0284: String anActionString = (String) i.next();
0285: mapOfActionNameToAction.put(anActionString,
0286: new DBBrowserKeyAction());
0287: }
0288: }
0289:
0290: public void update(Observable o, Object args) {
0291: initializeKeyBindings();
0292: }
0293:
0294: private void initializeKeyBindings() {
0295: //Load the key bindings
0296: try {
0297: List listOfKeyBindings = KeyBindingsSerializer
0298: .deserializeKeyBindings();
0299:
0300: Iterator i = listOfKeyBindings.iterator();
0301: while (i.hasNext()) {
0302: KeyBinding keyBinding = (KeyBinding) i.next();
0303:
0304: //Get the action
0305: Action anAction = (Action) mapOfActionNameToAction
0306: .get(keyBinding.getAction());
0307:
0308: //Set the action command to tell this action apart from other actions
0309: anAction.putValue("ACTION", keyBinding.getAction());
0310:
0311: this .dbBrowserConnectionInstancePanel.getInputMap(
0312: JComponent.WHEN_IN_FOCUSED_WINDOW).put(
0313: KeyStroke.getKeyStroke(keyBinding.getKeyCode()
0314: .intValue(), keyBinding
0315: .getModifierCode().intValue()),
0316: keyBinding.getDescription());
0317: this .dbBrowserConnectionInstancePanel.getActionMap()
0318: .put(keyBinding.getDescription(), anAction);
0319: }
0320: } catch (FileNotFoundException exc) {
0321: //Ignore if key bindings have not been set
0322: } catch (IOException exc) {
0323: String[] params = new String[] { exc.getMessage() };
0324: String errorMessage = InternationalizationManager
0325: .getInstance()
0326: .getMessage(
0327: "dbbrowser-ui",
0328: "dbbrowser-ui-preferences-window-key-bindings-save-error-message",
0329: params);
0330: JOptionPane.showMessageDialog(null, errorMessage, TITLE,
0331: JOptionPane.ERROR_MESSAGE);
0332: }
0333: }
0334:
0335: private class DBBrowserKeyAction extends AbstractAction {
0336: public void actionPerformed(ActionEvent e) {
0337: //Get the discriminator
0338: String action = (String) super .getValue("ACTION");
0339: Log.getInstance().debugMessage(
0340: "Pressed key for action: " + action,
0341: DBBrowserWindow.class.getName());
0342:
0343: if (KeyBinding.HELP.equals(action)) {
0344: HelpManager.getInstance()
0345: .getActionListenerForHelpEvents()
0346: .actionPerformed(e);
0347: }
0348:
0349: if (KeyBinding.FILE_CONNECT.equals(action)) {
0350: connect();
0351: }
0352:
0353: if (KeyBinding.EDIT_PREFERENCES.equals(action)) {
0354: editPreferences();
0355: }
0356:
0357: if (KeyBinding.FILE_DROP_CONNECTION.equals(action)) {
0358: dropConnection();
0359: }
0360:
0361: if (KeyBinding.FILE_EXIT.equals(action)) {
0362: exit();
0363: }
0364:
0365: if (KeyBinding.FILE_OPEN_SQL_SCRIPT_FILE.equals(action)) {
0366: batchRun();
0367: }
0368:
0369: if (KeyBinding.TOOLS_COMMIT.equals(action)) {
0370: commit();
0371: }
0372:
0373: if (KeyBinding.TOOLS_ROLLBACK.equals(action)) {
0374: rollback();
0375: }
0376: }
0377: }
0378:
0379: public void stateChanged(ChangeEvent e) {
0380: if (e.getSource() instanceof JTabbedPane) {
0381: //Get the tabbed pane which generated this change event
0382: JTabbedPane tp = (JTabbedPane) e.getSource();
0383:
0384: //Get the title of the selected tab
0385: int selectedIndex = tp.getSelectedIndex();
0386: if (selectedIndex != -1) {
0387: String title = tp.getTitleAt(selectedIndex);
0388:
0389: //Set the title
0390: this .dbBrowserFrame.setTitle(title + " - " + TITLE);
0391: } else {
0392: //Tabbed Pane has no tab
0393: //Set the title
0394: this .dbBrowserFrame.setTitle(TITLE);
0395: }
0396: }
0397: }
0398:
0399: private void initialize() {
0400: this .dbBrowserFrame.setTitle(TITLE);
0401: this .dbBrowserFrame.setSize(800, 600);
0402: this .dbBrowserFrame.setLocationRelativeTo(null);
0403: this .dbBrowserFrame
0404: .setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
0405: this .dbBrowserFrame.setIconImage(windowIcon);
0406: }
0407:
0408: private void addMenuBar() {
0409: //Setup file menu
0410: JMenu fileMenu = new JMenu(fileMenuLabel);
0411:
0412: JMenuItem fileConnectMenuItem = new JMenuItem(
0413: fileMenuConnectLabel,
0414: new ImageIcon(connectIconFileName));
0415: fileConnectMenuItem.addActionListener(this );
0416: fileMenu.add(fileConnectMenuItem);
0417: JMenuItem fileDropConnectionMenuItem = new JMenuItem(
0418: fileMenuDropConnectionLabel, new ImageIcon(
0419: dropConnectionIconFileName));
0420: fileDropConnectionMenuItem.addActionListener(this );
0421: fileMenu.add(fileDropConnectionMenuItem);
0422: fileMenu.addSeparator();
0423:
0424: JMenuItem fileBatchRunMenuItem = new JMenuItem(
0425: fileMenuBatchRunLabel,
0426: new ImageIcon(importIconFileName));
0427: fileBatchRunMenuItem.addActionListener(this );
0428: fileMenu.add(fileBatchRunMenuItem);
0429: fileMenu.addSeparator();
0430: JMenuItem fileExitMenuItem = new JMenuItem(fileMenuExitLabel,
0431: new ImageIcon(exitIconFileName));
0432: fileExitMenuItem.addActionListener(this );
0433: fileMenu.add(fileExitMenuItem);
0434:
0435: //Setup edit menu
0436: JMenu editMenu = new JMenu(editMenuLabel);
0437: JMenuItem editMenuPreferencesItem = new JMenuItem(
0438: editMenuPreferencesLabel, new ImageIcon(
0439: preferencesIconFileName));
0440: editMenuPreferencesItem.addActionListener(this );
0441: editMenu.add(editMenuPreferencesItem);
0442:
0443: //Setup tools menu
0444: JMenu toolsMenu = new JMenu(toolsMenuLabel);
0445: JMenuItem toolsMenuReverseEngineerItem = new JMenuItem(
0446: toolsMenuReverseEngineerLabel, new ImageIcon(
0447: reverseEngineerIconFileName));
0448: toolsMenuReverseEngineerItem.addActionListener(this );
0449: toolsMenu.add(toolsMenuReverseEngineerItem);
0450: toolsMenu.addSeparator();
0451: JMenuItem toolsMenuCommitItem = new JMenuItem(
0452: toolsMenuCommitLabel, new ImageIcon(commitIconFileName));
0453: toolsMenuCommitItem.addActionListener(this );
0454: toolsMenu.add(toolsMenuCommitItem);
0455: JMenuItem toolsMenuRollbackItem = new JMenuItem(
0456: toolsMenuRollbackLabel, new ImageIcon(
0457: rollbackIconFileName));
0458: toolsMenuRollbackItem.addActionListener(this );
0459: toolsMenu.add(toolsMenuRollbackItem);
0460:
0461: //Setup view menu
0462: JMenu viewMenu = new JMenu(viewMenuLabel);
0463: JMenuItem viewMenuRefreshItem = new JMenuItem(
0464: viewMenuRefreshLabel,
0465: new ImageIcon(refreshIconFileName));
0466: viewMenuRefreshItem.addActionListener(this );
0467: viewMenu.add(viewMenuRefreshItem);
0468: viewMenu.addSeparator();
0469: JMenuItem fileSetLookAndFeelMenuItem = new JMenuItem(
0470: fileMenuSetLookandFeelLabel, new ImageIcon(
0471: lookAndFeelIconFileName));
0472: fileSetLookAndFeelMenuItem.addActionListener(this );
0473: viewMenu.add(fileSetLookAndFeelMenuItem);
0474: viewMenu.addSeparator();
0475: JCheckBoxMenuItem viewMenuShowToolbarItem = new JCheckBoxMenuItem(
0476: viewMenuShowToolbarLabel, new ImageIcon(
0477: viewToolbarIconFileName));
0478: viewMenuShowToolbarItem.addItemListener(this );
0479: viewMenuShowToolbarItem
0480: .setActionCommand(viewMenuShowToolbarLabel);
0481:
0482: //Menu item is selected if it is selected in properties
0483: //Add the toolbar to the frame if it is set in the properties
0484: String showToolbar = PropertyManager.getInstance().getProperty(
0485: "dbbrowser-ui-dbbrowser-window-show-toolbar");
0486: if ("true".equals(showToolbar)) {
0487: viewMenuShowToolbarItem.setSelected(true);
0488: }
0489: viewMenu.add(viewMenuShowToolbarItem);
0490:
0491: //Add menu item for toggle SQL on or off
0492: JCheckBoxMenuItem viewMenuShowSQLLogItem = null;
0493: String showSQLLog = PropertyManager.getInstance().getProperty(
0494: "dbbrowser-ui-dbbrowser-window-show-sql-log");
0495: if ("true".equals(showSQLLog)) {
0496: viewMenuShowSQLLogItem = new JCheckBoxMenuItem(
0497: viewMenuShowSQLLogLabel, new ImageIcon(
0498: viewSQLLogIconFileName), true);
0499: viewMenuShowSQLLogItem.setSelected(true);
0500: } else {
0501: viewMenuShowSQLLogItem = new JCheckBoxMenuItem(
0502: viewMenuShowSQLLogLabel, new ImageIcon(
0503: viewSQLLogIconFileName));
0504: viewMenuShowSQLLogItem.setSelected(false);
0505: }
0506:
0507: viewMenuShowSQLLogItem.addItemListener(this );
0508: viewMenuShowSQLLogItem
0509: .setActionCommand(viewMenuShowSQLLogLabel);
0510:
0511: viewMenu.add(viewMenuShowSQLLogItem);
0512:
0513: //Add the view memory monitor label
0514: JMenuItem viewMenuShowMemoryMonitorMenuItem = new JMenuItem(
0515: viewMenuShowMemoryMonitorLabel, new ImageIcon(
0516: viewMemoryMonitorIconFileName));
0517: viewMenuShowMemoryMonitorMenuItem.addActionListener(this );
0518: viewMenuShowMemoryMonitorMenuItem
0519: .setActionCommand(viewMenuShowMemoryMonitorLabel);
0520: viewMenu.add(viewMenuShowMemoryMonitorMenuItem);
0521:
0522: //Setup help menu
0523: JMenu helpMenu = new JMenu(helpMenuLabel);
0524: JMenuItem helpMenuHelpItem = new JMenuItem(helpMenuHelpLabel,
0525: new ImageIcon(helpIconFileName));
0526: helpMenuHelpItem.addActionListener(HelpManager.getInstance()
0527: .getActionListenerForHelpEvents());
0528: helpMenu.add(helpMenuHelpItem);
0529: JMenuItem helpMenuCommentsFeedbackItem = new JMenuItem(
0530: helpMenuCommentsFeedbackLabel, new ImageIcon(
0531: commentsFeedbackIconFileName));
0532: helpMenuCommentsFeedbackItem.addActionListener(this );
0533: helpMenu.add(helpMenuCommentsFeedbackItem);
0534: JMenuItem helpMenuAboutItem = new JMenuItem(helpMenuAboutLabel,
0535: new ImageIcon(aboutIconFileName));
0536: helpMenuAboutItem.addActionListener(this );
0537: helpMenu.add(helpMenuAboutItem);
0538:
0539: //Setup the menu bar for the frame
0540: this .menuBar.add(fileMenu);
0541: this .menuBar.add(editMenu);
0542: this .menuBar.add(toolsMenu);
0543: this .menuBar.add(viewMenu);
0544: this .menuBar.add(helpMenu);
0545: this .dbBrowserFrame.setJMenuBar(this .menuBar);
0546: }
0547:
0548: private void addToolBar() {
0549: //Set the connect button on the toolbar
0550: Icon iconForConnect = new ImageIcon(connectIconFileName);
0551: JButton buttonToConnect = new JButton(fileMenuConnectLabel,
0552: iconForConnect);
0553: buttonToConnect.addActionListener(this );
0554: this .toolBar.add(buttonToConnect);
0555:
0556: //Set the connect button on the toolbar
0557: Icon iconToDropConnection = new ImageIcon(
0558: dropConnectionIconFileName);
0559: JButton buttonToDropConnection = new JButton(
0560: fileMenuDropConnectionLabel, iconToDropConnection);
0561: buttonToDropConnection.addActionListener(this );
0562: this .toolBar.add(buttonToDropConnection);
0563:
0564: //Set the look and feel button on the toolbar
0565: Icon iconToSetLookAndFeel = new ImageIcon(
0566: lookAndFeelIconFileName);
0567: JButton buttonToSetLookAndFeel = new JButton(
0568: fileMenuSetLookandFeelLabel, iconToSetLookAndFeel);
0569: buttonToSetLookAndFeel.addActionListener(this );
0570: //this.toolBar.add( buttonToSetLookAndFeel );
0571:
0572: //Set the import button on the toolbar
0573: Icon iconForImport = new ImageIcon(importIconFileName);
0574: JButton importButton = new JButton(fileMenuBatchRunLabel,
0575: iconForImport);
0576: importButton.addActionListener(this );
0577: this .toolBar.add(importButton);
0578:
0579: //Set the Reverse enginner button on the toolbar
0580: // String reverseEngineerIconFileName = PropertyManager.getInstance().getProperty("dbbrowser-ui-dbbrowser-window-toolbar-reverse-engineer-icon");
0581: // Icon iconForReverseEngineer = new ImageIcon( reverseEngineerIconFileName );
0582: // JButton reverseEngineerButton = new JButton( toolsMenuReverseEngineerLabel, iconForReverseEngineer );
0583: // reverseEngineerButton.addActionListener( this );
0584: // this.toolBar.add( reverseEngineerButton );
0585:
0586: //Commit button
0587: Icon iconForCommitButton = new ImageIcon(commitIconFileName);
0588: JButton commitButton = new JButton(toolsMenuCommitLabel,
0589: iconForCommitButton);
0590: commitButton.addActionListener(this );
0591: this .toolBar.add(commitButton);
0592:
0593: //Rollback button
0594: Icon iconForRollbackButton = new ImageIcon(rollbackIconFileName);
0595: JButton rollbackButton = new JButton(toolsMenuRollbackLabel,
0596: iconForRollbackButton);
0597: rollbackButton.addActionListener(this );
0598: this .toolBar.add(rollbackButton);
0599:
0600: //Set the refresh button on the toolbar
0601: Icon iconForRefresh = new ImageIcon(refreshIconFileName);
0602: JButton refereshButton = new JButton(viewMenuRefreshLabel,
0603: iconForRefresh);
0604: refereshButton.addActionListener(this );
0605: this .toolBar.add(refereshButton);
0606:
0607: //Add the toolbar to the frame if it is set in the properties
0608: String showToolbar = PropertyManager.getInstance().getProperty(
0609: "dbbrowser-ui-dbbrowser-window-show-toolbar");
0610: if ("true".equals(showToolbar)) {
0611: this .dbBrowserFrame.getContentPane().add(this .toolBar,
0612: BorderLayout.PAGE_START);
0613: }
0614: }
0615:
0616: public void setupTabbedPane(ConnectionInfo connectionInfo,
0617: String masterPassword) throws DriverManagerException,
0618: DBEngineException {
0619: //If the connection is already in use, drop it and reconnect
0620: boolean connectionAlreadyinUse = false;
0621: Component[] tabs = this .tabbedPaneForConnectionInstance
0622: .getComponents();
0623: for (int i = 0; i < tabs.length; i++) {
0624: Object o = tabs[i];
0625: if (o != null
0626: && o instanceof DBBrowserConnectionInstancePanel) {
0627: DBBrowserConnectionInstancePanel dbBrowserConnectionInstancePanel = (DBBrowserConnectionInstancePanel) tabs[i];
0628: String connectionInfoName = dbBrowserConnectionInstancePanel
0629: .getConnectionInfoName();
0630: if (connectionInfo.getName().equals(connectionInfoName)) {
0631: connectionAlreadyinUse = true;
0632: break;
0633: }
0634: }
0635: }
0636:
0637: if (connectionAlreadyinUse) {
0638: this .tabbedPaneForConnectionInstance
0639: .remove(this .dbBrowserConnectionInstancePanel);
0640: }
0641:
0642: //Start the ui controller
0643: UIControllerForQueries uiControllerForQueries = new UIControllerForQueries();
0644: uiControllerForQueries.setup(connectionInfo, masterPassword);
0645: UIControllerForUpdates uiControllerForUpdates = new UIControllerForUpdates();
0646: uiControllerForUpdates.setup(connectionInfo, masterPassword);
0647: UIControllerForRawSQL uiControllerForRawSQL = new UIControllerForRawSQL();
0648: uiControllerForRawSQL.setup(connectionInfo, masterPassword);
0649:
0650: this .dbBrowserConnectionInstancePanel = new DBBrowserConnectionInstancePanel(
0651: uiControllerForQueries, uiControllerForUpdates,
0652: uiControllerForRawSQL);
0653: this .tabbedPaneForConnectionInstance.addTab(connectionInfo
0654: .getName(), new ImageIcon(
0655: connectionInstanceTabIconFilename),
0656: this .dbBrowserConnectionInstancePanel, connectionInfo
0657: .getName());
0658:
0659: //Set the title of the dbbrowser window
0660: this .dbBrowserFrame.setTitle(connectionInfo.getName() + " - "
0661: + TITLE);
0662:
0663: //Update the UI
0664: this .tabbedPaneForConnectionInstance.updateUI();
0665:
0666: //Add the change listener which is used to change the title of dbbroweser window
0667: this .tabbedPaneForConnectionInstance.addChangeListener(this );
0668:
0669: //Setup key bindings
0670: this .initializeKeyBindings();
0671: }
0672:
0673: public void showSQLLog() {
0674: //If SQL Log is required, show it
0675: String showSQLLog = PropertyManager.getInstance().getProperty(
0676: "dbbrowser-ui-dbbrowser-window-show-sql-log");
0677: if ("true".equals(showSQLLog)) {
0678: this .sqlLogUI.show();
0679: Log.getInstance().debugMessage(
0680: "SQL Logger UI has been started",
0681: this .getClass().getName());
0682: }
0683:
0684: this .dbBrowserFrame.setVisible(true);
0685: }
0686:
0687: public void actionPerformed(ActionEvent e) {
0688: //Set the look and feel if look and feel button pressed
0689: if (fileMenuSetLookandFeelLabel.equals(e.getActionCommand())) {
0690: setLookAndFeel();
0691: }
0692:
0693: //Refresh data in a table
0694: if (viewMenuRefreshLabel.equals(e.getActionCommand())) {
0695: refresh();
0696: }
0697:
0698: //Exit
0699: if (fileMenuExitLabel.equals(e.getActionCommand())) {
0700: //Exit
0701: exit();
0702: }
0703:
0704: //Batch run
0705: if (fileMenuBatchRunLabel.equals(e.getActionCommand())) {
0706: batchRun();
0707: }
0708:
0709: //Reverse Engineer
0710: if (toolsMenuReverseEngineerLabel.equals(e.getActionCommand())) {
0711: String UNIMPLEMENTED_FEATURE_MESSAGE = InternationalizationManager
0712: .getInstance()
0713: .getMessage(
0714: "dbbrowser-ui",
0715: "dbbrowser-ui-dbbrowser-unimplemented-feature",
0716: null);
0717: JOptionPane.showMessageDialog(null,
0718: UNIMPLEMENTED_FEATURE_MESSAGE, TITLE,
0719: JOptionPane.INFORMATION_MESSAGE);
0720: }
0721:
0722: //Change preferences
0723: if (editMenuPreferencesLabel.equals(e.getActionCommand())) {
0724: editPreferences();
0725: }
0726:
0727: //Setup another connection
0728: if (fileMenuConnectLabel.equals(e.getActionCommand())) {
0729: connect();
0730: }
0731:
0732: //Show about window
0733: if (helpMenuAboutLabel.equals(e.getActionCommand())) {
0734: AboutWindow aboutWindow = new AboutWindow();
0735: aboutWindow.show();
0736: }
0737:
0738: //Show about window
0739: if (helpMenuCommentsFeedbackLabel.equals(e.getActionCommand())) {
0740: CommentsFeedbackWindow commentsFeedbackWindow = new CommentsFeedbackWindow();
0741: commentsFeedbackWindow.show();
0742: }
0743:
0744: //Close the connection tab
0745: if (fileMenuDropConnectionLabel.equals(e.getActionCommand())) {
0746: dropConnection();
0747: }
0748:
0749: //Commit
0750: if (toolsMenuCommitLabel.equals(e.getActionCommand())) {
0751: commit();
0752: }
0753:
0754: //Rollback
0755: if (toolsMenuRollbackLabel.equals(e.getActionCommand())) {
0756: rollback();
0757: }
0758:
0759: //Show memory monitor
0760: if (viewMenuShowMemoryMonitorLabel.equals(e.getActionCommand())) {
0761: showMemoryMonitor();
0762: }
0763: }
0764:
0765: private void showMemoryMonitor() {
0766: MemoryMonitor memoryMonitor = new MemoryMonitor();
0767: memoryMonitor.show();
0768: }
0769:
0770: public void itemStateChanged(ItemEvent e) {
0771: Object o = e.getItem();
0772: JMenuItem mi = (JMenuItem) o;
0773: String actionCommand = mi.getActionCommand();
0774:
0775: if (viewMenuShowToolbarLabel.equals(actionCommand)) {
0776: if (e.getStateChange() == ItemEvent.DESELECTED) {
0777: this .dbBrowserFrame.remove(this .toolBar);
0778: //Persist the new property
0779: try {
0780: PropertyManager
0781: .getInstance()
0782: .setProperty(
0783: "dbbrowser-ui-dbbrowser-window-show-toolbar",
0784: "false");
0785: } catch (PropertyManagementException exc) {
0786: String errorMessage = InternationalizationManager
0787: .getInstance()
0788: .getMessage(
0789: "dbbrowser-ui",
0790: "dbbrowser-ui-dbbrowser-window-save-property-failed-message",
0791: null);
0792: JOptionPane.showMessageDialog(null, errorMessage,
0793: TITLE, JOptionPane.ERROR_MESSAGE);
0794: }
0795: } else {
0796: this .dbBrowserFrame.getContentPane().add(this .toolBar,
0797: BorderLayout.PAGE_START);
0798: //Persist the new property
0799: try {
0800: PropertyManager
0801: .getInstance()
0802: .setProperty(
0803: "dbbrowser-ui-dbbrowser-window-show-toolbar",
0804: "true");
0805: } catch (PropertyManagementException exc) {
0806: String errorMessage = InternationalizationManager
0807: .getInstance()
0808: .getMessage(
0809: "dbbrowser-ui",
0810: "dbbrowser-ui-dbbrowser-window-save-property-failed-message",
0811: null);
0812: JOptionPane.showMessageDialog(null, errorMessage,
0813: TITLE, JOptionPane.ERROR_MESSAGE);
0814: }
0815: }
0816:
0817: //Get the top level ancestor and ask it to update itself
0818: Component c = SwingUtilities.getRoot(this .dbBrowserFrame);
0819: if (c instanceof JFrame) {
0820: SwingUtilities.updateComponentTreeUI(c);
0821: }
0822: }
0823:
0824: if (viewMenuShowSQLLogLabel.equals(actionCommand)) {
0825: if (e.getStateChange() == ItemEvent.DESELECTED) {
0826: //Persist the new property
0827: try {
0828: PropertyManager
0829: .getInstance()
0830: .setProperty(
0831: "dbbrowser-ui-dbbrowser-window-show-sql-log",
0832: "false");
0833: this .sqlLogUI.hide();
0834: Log.getInstance().debugMessage(
0835: "SQL Logger UI has been closed",
0836: this .getClass().getName());
0837: } catch (PropertyManagementException exc) {
0838: String errorMessage = InternationalizationManager
0839: .getInstance()
0840: .getMessage(
0841: "dbbrowser-ui",
0842: "dbbrowser-ui-dbbrowser-window-save-property-failed-message",
0843: null);
0844: JOptionPane.showMessageDialog(null, errorMessage,
0845: TITLE, JOptionPane.ERROR_MESSAGE);
0846: }
0847: } else {
0848: //Persist the new property
0849: try {
0850: PropertyManager
0851: .getInstance()
0852: .setProperty(
0853: "dbbrowser-ui-dbbrowser-window-show-sql-log",
0854: "true");
0855: this .sqlLogUI.show();
0856: Log.getInstance().debugMessage(
0857: "SQL Logger UI has been started",
0858: this .getClass().getName());
0859: } catch (PropertyManagementException exc) {
0860: String errorMessage = InternationalizationManager
0861: .getInstance()
0862: .getMessage(
0863: "dbbrowser-ui",
0864: "dbbrowser-ui-dbbrowser-window-save-property-failed-message",
0865: null);
0866: JOptionPane.showMessageDialog(null, errorMessage,
0867: TITLE, JOptionPane.ERROR_MESSAGE);
0868: }
0869: }
0870: }
0871: }
0872:
0873: private void refresh() {
0874: this .dbBrowserConnectionInstancePanel = (DBBrowserConnectionInstancePanel) this .tabbedPaneForConnectionInstance
0875: .getSelectedComponent();
0876:
0877: //if there is a connection panel, then refresh it
0878: if (this .dbBrowserConnectionInstancePanel != null) {
0879: this .dbBrowserConnectionInstancePanel.refresh();
0880: }
0881: }
0882:
0883: private void exit() {
0884: System.exit(0);
0885: }
0886:
0887: private void batchRun() {
0888: //Get the file to run as batch file
0889: JFileChooser jfc = new JFileChooser(".");
0890: jfc.setMultiSelectionEnabled(false);
0891: int ans = jfc.showOpenDialog(null);
0892:
0893: if (JFileChooser.APPROVE_OPTION == ans) {
0894: File fileToOpen = jfc.getSelectedFile();
0895: this .dbBrowserConnectionInstancePanel = (DBBrowserConnectionInstancePanel) this .tabbedPaneForConnectionInstance
0896: .getSelectedComponent();
0897:
0898: //if there is a connection panel, then refresh it
0899: if (this .dbBrowserConnectionInstancePanel != null) {
0900: this .dbBrowserConnectionInstancePanel
0901: .runBatchFile(fileToOpen);
0902: }
0903: }
0904: }
0905:
0906: private void editPreferences() {
0907: this .preferencesWindow = new PreferencesWindow(this );
0908: this .preferencesWindow.show();
0909: }
0910:
0911: private void connect() {
0912: ConnectionInformationWindow connectionInformationWindow = new ConnectionInformationWindow(
0913: this );
0914: connectionInformationWindow.show();
0915: }
0916:
0917: private void dropConnection() {
0918: String dropConnectionConfirmMessage = InternationalizationManager
0919: .getInstance()
0920: .getMessage(
0921: "dbbrowser-ui",
0922: "dbbrowser-ui-dbbrowser-drop-connection-confirm-message",
0923: null);
0924: int ans = JOptionPane.showConfirmDialog(null,
0925: dropConnectionConfirmMessage, TITLE,
0926: JOptionPane.OK_CANCEL_OPTION);
0927:
0928: if (ans == JOptionPane.OK_OPTION) {
0929: int selectedConnectionInstanceTabIndex = this .tabbedPaneForConnectionInstance
0930: .getSelectedIndex();
0931: if (selectedConnectionInstanceTabIndex != -1) {
0932: this .tabbedPaneForConnectionInstance
0933: .removeTabAt(selectedConnectionInstanceTabIndex);
0934: }
0935: }
0936: }
0937:
0938: private void commit() {
0939: DBBrowserConnectionInstancePanel dbBrowserConnectionInstancePanel = (DBBrowserConnectionInstancePanel) this .tabbedPaneForConnectionInstance
0940: .getSelectedComponent();
0941:
0942: //if there is a connection panel, then refresh it
0943: if (this .dbBrowserConnectionInstancePanel != null) {
0944: dbBrowserConnectionInstancePanel.commit();
0945: }
0946: }
0947:
0948: private void rollback() {
0949: DBBrowserConnectionInstancePanel dbBrowserConnectionInstancePanel = (DBBrowserConnectionInstancePanel) this .tabbedPaneForConnectionInstance
0950: .getSelectedComponent();
0951:
0952: //if there is a connection panel, then refresh it
0953: if (this .dbBrowserConnectionInstancePanel != null) {
0954: dbBrowserConnectionInstancePanel.rollback();
0955: }
0956: }
0957:
0958: private void setLookAndFeel() {
0959: String chooseLookAndFeelLabel = InternationalizationManager
0960: .getInstance()
0961: .getMessage(
0962: "dbbrowser-ui",
0963: "dbbrowser-ui-dbbrowser-window-choose-lookandfeel-message",
0964: null);
0965: Object[] installedLookAndFeels = LookAndFeelHandler
0966: .getInstance().getAllInstalledLookAndFeels();
0967: String currentLookandFeel = LookAndFeelHandler.getInstance()
0968: .getCurrentLookAndFeel();
0969: Object response = JOptionPane.showInputDialog(
0970: this .dbBrowserFrame, chooseLookAndFeelLabel, TITLE,
0971: JOptionPane.PLAIN_MESSAGE, null, installedLookAndFeels,
0972: currentLookandFeel);
0973:
0974: if (response != null) {
0975: String newLookAndFeel = (String) response;
0976: LookAndFeelHandler.getInstance().setLookAndFeel(
0977: newLookAndFeel);
0978: //Get the top level ancestor and ask it to update itself
0979: Component c = SwingUtilities.getRoot(this .dbBrowserFrame);
0980: if (c instanceof JFrame) {
0981: SwingUtilities.updateComponentTreeUI(c);
0982: }
0983:
0984: //Persist the changes
0985: try {
0986: PropertyManager.getInstance().setProperty(
0987: "dbbrowser.ui.lookandfeel", newLookAndFeel);
0988: } catch (PropertyManagementException exc) {
0989: String errorMessage = InternationalizationManager
0990: .getInstance()
0991: .getMessage(
0992: "dbbrowser-ui",
0993: "dbbrowser-ui-dbbrowser-window-save-property-failed-message",
0994: null);
0995: JOptionPane.showMessageDialog(null, errorMessage,
0996: TITLE, JOptionPane.ERROR_MESSAGE);
0997: }
0998: }
0999: }
1000: }
|