001: /*
002: * The contents of this file are subject to the Mozilla Public License
003: * Version 1.1 (the "License"); you may not use this file except in
004: * compliance with the License. You may obtain a copy of the License at
005: * http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
009: * License for the specific language governing rights and limitations
010: * under the License.
011: *
012: * The Original Code is iSQL-Viewer, A Mutli-Platform Database Tool.
013: *
014: * The Initial Developer of the Original Code is iSQL-Viewer, A Mutli-Platform Database Tool.
015: * Portions created by Mark A. Kobold are Copyright (C) 2000-2007. All Rights Reserved.
016: *
017: * Contributor(s):
018: * Mark A. Kobold [mkobold <at> isqlviewer <dot> com].
019: *
020: * If you didn't download this code from the following link, you should check
021: * if you aren't using an obsolete version: http://www.isqlviewer.com
022: */
023: package org.isqlviewer.ui;
024:
025: import java.awt.AWTEvent;
026: import java.awt.Frame;
027: import java.awt.Rectangle;
028: import java.awt.event.WindowEvent;
029: import java.util.prefs.BackingStoreException;
030: import java.util.prefs.Preferences;
031:
032: import javax.swing.ImageIcon;
033: import javax.swing.JFrame;
034: import javax.swing.JMenuBar;
035: import javax.swing.JPanel;
036:
037: import org.apache.log4j.BasicConfigurator;
038: import org.isqlviewer.swing.SwingUtilities;
039: import org.isqlviewer.swing.WizardPanel;
040: import org.isqlviewer.swing.action.SwingEventManager;
041: import org.isqlviewer.util.IsqlToolkit;
042:
043: /**
044: * @author Markus A. Kobold <mkobold at sprintpcs dot com>
045: * @version 1.0
046: */
047: public class ApplicationFrame extends JFrame {
048:
049: private static final long serialVersionUID = -3603727788974494075L;
050: private static final String WINDOW_ID = "main-window";
051:
052: private JMenuBar menuBar = new JMenuBar();
053: private WizardPanel contentPanel = new WizardPanel();
054: private ApplicationView defaultView = null;
055: private SwingEventManager swingEventManager = new SwingEventManager();
056: private Preferences applicationPreferences = null;
057:
058: public ApplicationFrame() {
059:
060: super ("iSQL-Viewer v.3.0.0");
061: Preferences userRoot = Preferences.userRoot();
062: applicationPreferences = userRoot.node(IsqlToolkit
063: .getRootPreferencesNode());
064: setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
065: enableEvents(AWTEvent.KEY_EVENT_MASK
066: | AWTEvent.MOUSE_EVENT_MASK);
067: swingEventManager.start();
068: BasicConfigurator.configure();
069: try {
070: initializeUI();
071: } catch (Throwable t) {
072: throw new RuntimeException(t);
073: }
074: if (!SwingUtilities.isMacOS()) {
075: ImageIcon iconImage = (ImageIcon) SwingUtilities
076: .loadIconResource("application", 128);
077: setIconImage(iconImage.getImage());
078: }
079: }
080:
081: @Override
082: protected void processWindowEvent(WindowEvent e) {
083:
084: switch (e.getID()) {
085: case WindowEvent.WINDOW_OPENED:
086: retrieveWindowPrefs(WINDOW_ID, this );
087: defaultView.initializeView();
088: String jre = System
089: .getProperty("java.specification.version");
090: if ("1.5".equals(jre)) {
091: validateTree();
092: }
093: break;
094: case WindowEvent.WINDOW_CLOSING:
095: storeWindowPrefs(WINDOW_ID, this );
096: defaultView.disposeView(applicationPreferences);
097: try {
098: applicationPreferences.sync();
099: } catch (BackingStoreException error) {
100: error.printStackTrace(System.err);
101: }
102: break;
103: }
104: super .processWindowEvent(e);
105: }
106:
107: private synchronized void initializeUI() throws Exception {
108:
109: setContentPane(contentPanel);
110: setJMenuBar(menuBar);
111: JdbcWorkbench workbench = new JdbcWorkbench();
112: JPanel applicationView = new JPanel();
113: workbench.doLayout(applicationView, applicationPreferences,
114: swingEventManager);
115: workbench.configureMenubar(menuBar);
116: contentPanel.add("root-view", applicationView);
117: pack();
118: defaultView = workbench;
119: }
120:
121: public void storeWindowPrefs(String name, Frame window) {
122:
123: Preferences windowsRoot = applicationPreferences
124: .node("windows");
125: Preferences windowPrefs = windowsRoot.node(name);
126: Rectangle rect = window.getBounds();
127: windowPrefs.putInt("x", rect.x);
128: windowPrefs.putInt("y", rect.y);
129: windowPrefs.putInt("width", rect.width);
130: windowPrefs.putInt("height", rect.height);
131: windowPrefs.putBoolean("maximized",
132: (window.getExtendedState() == Frame.MAXIMIZED_BOTH));
133: }
134:
135: public boolean retrieveWindowPrefs(String name, Frame window) {
136:
137: Preferences windowRoot = applicationPreferences.node("windows");
138: try {
139: if (!windowRoot.nodeExists(name)) {
140: window.setBounds(new Rectangle(0, 0, 800, 600));
141: SwingUtilities.centerFrameOnScreen(this );
142: return false;
143: }
144: } catch (BackingStoreException e) {
145: return false;
146: }
147: Preferences windowPrefs = windowRoot.node(name);
148: int x, y, width, height;
149: x = windowPrefs.getInt("x", 0);
150: y = windowPrefs.getInt("y", 0);
151: width = windowPrefs.getInt("width", 800);
152: height = windowPrefs.getInt("height", 600);
153: window.setBounds(new Rectangle(x, y, width, height));
154: if (windowPrefs.getBoolean("maximized", false)) {
155: window.setExtendedState(Frame.MAXIMIZED_BOTH);
156: } else if (x == 0 && y == 0) {
157: SwingUtilities.centerFrameOnScreen(this );
158: }
159: return true;
160: }
161:
162: public SwingEventManager getSwingEventManager() {
163:
164: return swingEventManager;
165: }
166: }
|