001: package net.sourceforge.squirrel_sql.client.preferences;
002:
003: /*
004: * Copyright (C) 2001-2003 Colin Bell
005: * colbell@users.sourceforge.net
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: */
021: import java.awt.Dimension;
022: import java.awt.GridBagConstraints;
023: import java.awt.GridBagLayout;
024: import java.awt.event.ActionEvent;
025: import java.awt.event.ActionListener;
026: import java.awt.event.KeyEvent;
027: import java.util.ArrayList;
028: import java.util.Iterator;
029: import java.util.List;
030: import java.util.prefs.Preferences;
031:
032: import javax.swing.*;
033:
034: import net.sourceforge.squirrel_sql.fw.gui.GUIUtils;
035: import net.sourceforge.squirrel_sql.fw.util.StringManager;
036: import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
037: import net.sourceforge.squirrel_sql.fw.util.log.ILogger;
038: import net.sourceforge.squirrel_sql.fw.util.log.LoggerController;
039:
040: import net.sourceforge.squirrel_sql.client.IApplication;
041: import net.sourceforge.squirrel_sql.client.gui.BaseInternalFrame;
042: import net.sourceforge.squirrel_sql.client.gui.builders.UIFactory;
043: import net.sourceforge.squirrel_sql.client.plugin.PluginInfo;
044: import net.sourceforge.squirrel_sql.client.session.properties.GeneralSessionPropertiesPanel;
045: import net.sourceforge.squirrel_sql.client.session.properties.SessionObjectTreePropertiesPanel;
046: import net.sourceforge.squirrel_sql.client.session.properties.SessionSQLPropertiesPanel;
047:
048: // JASON: Rename to NewSessionPropertiesInternalFrame
049: public class NewSessionPropertiesSheet extends BaseInternalFrame {
050: private static final long serialVersionUID = 1L;
051:
052: /** Internationalized strings for this class. */
053: private static final StringManager s_stringMgr = StringManagerFactory
054: .getStringManager(NewSessionPropertiesSheet.class);
055:
056: /** Logger for this class. */
057: private static final ILogger s_log = LoggerController
058: .createLogger(NewSessionPropertiesSheet.class);
059:
060: /** Singleton instance of this class. */
061: private static NewSessionPropertiesSheet s_instance;
062:
063: /** Frame title. */
064: private JLabel _titleLbl = new JLabel();
065:
066: private IApplication _app;
067: private List<INewSessionPropertiesPanel> _panels = new ArrayList<INewSessionPropertiesPanel>();
068:
069: public static final String PREF_KEY_NEW_SESSION_PROPS_SHEET_WIDTH = "Squirrel.newSessionPropsSheetWidth";
070: public static final String PREF_KEY_NEW_SESSION_PROPS_SHEET_HEIGHT = "Squirrel.newSessionPropsSheetHeight";
071:
072: private NewSessionPropertiesSheet(IApplication app) {
073: super (s_stringMgr.getString("NewSessionPropertiesSheet.title"),
074: true);
075: _app = app;
076: setDefaultCloseOperation(HIDE_ON_CLOSE);
077:
078: createGUI();
079: for (Iterator<INewSessionPropertiesPanel> it = _panels
080: .iterator(); it.hasNext();) {
081: INewSessionPropertiesPanel pnl = it.next();
082: pnl.initialize(_app);
083: }
084:
085: setSize(getDimension());
086: app.getMainFrame().addInternalFrame(this , true, null);
087: GUIUtils.centerWithinDesktop(this );
088: setVisible(true);
089: }
090:
091: private Dimension getDimension() {
092: return new Dimension(Preferences.userRoot().getInt(
093: PREF_KEY_NEW_SESSION_PROPS_SHEET_WIDTH, 500),
094: Preferences.userRoot().getInt(
095: PREF_KEY_NEW_SESSION_PROPS_SHEET_HEIGHT, 600));
096: }
097:
098: /**
099: * Show the Preferences dialog
100: *
101: * @param app Application API.
102: *
103: * @throws IllegalArgumentException
104: * Thrown if a <TT>null</TT> <TT>IApplication</TT> object passed.
105: */
106: public static synchronized void showSheet(IApplication app) {
107: if (s_instance == null) {
108: s_instance = new NewSessionPropertiesSheet(app);
109: } else {
110: s_instance.moveToFront();
111: }
112: }
113:
114: public void dispose() {
115: Dimension size = getSize();
116: Preferences.userRoot().putInt(
117: PREF_KEY_NEW_SESSION_PROPS_SHEET_WIDTH, size.width);
118: Preferences.userRoot().putInt(
119: PREF_KEY_NEW_SESSION_PROPS_SHEET_HEIGHT, size.height);
120:
121: synchronized (getClass()) {
122: s_instance = null;
123: }
124: super .dispose();
125: }
126:
127: /**
128: * Set title of this frame. Ensure that the title label
129: * matches the frame title.
130: *
131: * @param title New title text.
132: */
133: public void setTitle(String title) {
134: super .setTitle(title);
135: _titleLbl.setText(title);
136: }
137:
138: private void performClose() {
139: dispose();
140: }
141:
142: /**
143: * OK button pressed so save changes.
144: */
145: private void performOk() {
146: final boolean isDebug = s_log.isDebugEnabled();
147: long start = 0;
148: for (Iterator<INewSessionPropertiesPanel> it = _panels
149: .iterator(); it.hasNext();) {
150: if (isDebug) {
151: start = System.currentTimeMillis();
152: }
153: INewSessionPropertiesPanel pnl = it.next();
154: pnl.applyChanges();
155: if (isDebug) {
156: s_log.debug("Panel " + pnl.getTitle()
157: + " applied changes in "
158: + (System.currentTimeMillis() - start) + "ms");
159: }
160: }
161:
162: dispose();
163: }
164:
165: private void createGUI() {
166: setDefaultCloseOperation(DISPOSE_ON_CLOSE);
167:
168: // This is a tool window.
169: GUIUtils.makeToolWindow(this , true);
170:
171: // Add panels for core Squirrel functionality.
172: _panels.add(new GeneralSessionPropertiesPanel());
173: _panels.add(new SessionObjectTreePropertiesPanel(_app));
174: _panels.add(new SessionSQLPropertiesPanel(_app, true));
175:
176: // Go thru all loaded plugins asking for panels.
177: PluginInfo[] plugins = _app.getPluginManager()
178: .getPluginInformation();
179: for (int plugIdx = 0; plugIdx < plugins.length; ++plugIdx) {
180: PluginInfo pi = plugins[plugIdx];
181: if (pi.isLoaded()) {
182: INewSessionPropertiesPanel[] pnls = pi.getPlugin()
183: .getNewSessionPropertiesPanels();
184: if (pnls != null && pnls.length > 0) {
185: for (int pnlIdx = 0; pnlIdx < pnls.length; ++pnlIdx) {
186: _panels.add(pnls[pnlIdx]);
187: }
188: }
189: }
190: }
191:
192: // Add all panels to the tabbed pane.
193: final JTabbedPane tabPane = UIFactory.getInstance()
194: .createTabbedPane();
195: for (Iterator<INewSessionPropertiesPanel> it = _panels
196: .iterator(); it.hasNext();) {
197: INewSessionPropertiesPanel pnl = it.next();
198: String winTitle = pnl.getTitle();
199: String hint = pnl.getHint();
200: tabPane.addTab(winTitle, null, pnl.getPanelComponent(),
201: hint);
202: }
203:
204: final JPanel contentPane = new JPanel(new GridBagLayout());
205: contentPane.setBorder(BorderFactory.createEmptyBorder(5, 10, 5,
206: 10));
207: setContentPane(contentPane);
208:
209: GridBagConstraints gbc = new GridBagConstraints();
210:
211: gbc.gridwidth = 1;
212: gbc.fill = GridBagConstraints.BOTH;
213: gbc.weightx = 1;
214:
215: gbc.gridx = 0;
216: gbc.gridy = 0;
217: contentPane.add(_titleLbl, gbc);
218: ++gbc.gridy;
219: gbc.weighty = 1;
220: contentPane.add(tabPane, gbc);
221:
222: ++gbc.gridy;
223: gbc.weighty = 0;
224: contentPane.add(createButtonsPanel(), gbc);
225:
226: AbstractAction closeAction = new AbstractAction() {
227: private static final long serialVersionUID = 1L;
228:
229: public void actionPerformed(ActionEvent actionEvent) {
230: performClose();
231: }
232: };
233: KeyStroke escapeStroke = KeyStroke.getKeyStroke(
234: KeyEvent.VK_ESCAPE, 0);
235: getRootPane().getInputMap(
236: JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(
237: escapeStroke, "CloseAction");
238: getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)
239: .put(escapeStroke, "CloseAction");
240: getRootPane().getInputMap(JComponent.WHEN_FOCUSED).put(
241: escapeStroke, "CloseAction");
242: getRootPane().getActionMap().put("CloseAction", closeAction);
243: }
244:
245: private JPanel createButtonsPanel() {
246: JPanel pnl = new JPanel();
247:
248: JButton okBtn = new JButton(s_stringMgr
249: .getString("NewSessionPropertiesSheet.ok"));
250: okBtn.addActionListener(new ActionListener() {
251: public void actionPerformed(ActionEvent evt) {
252: performOk();
253: }
254: });
255: JButton closeBtn = new JButton(s_stringMgr
256: .getString("NewSessionPropertiesSheet.close"));
257: closeBtn.addActionListener(new ActionListener() {
258: public void actionPerformed(ActionEvent evt) {
259: performClose();
260: }
261: });
262:
263: pnl.add(okBtn);
264: pnl.add(closeBtn);
265:
266: GUIUtils
267: .setJButtonSizesTheSame(new JButton[] { okBtn, closeBtn });
268: getRootPane().setDefaultButton(okBtn);
269:
270: return pnl;
271: }
272: }
|