001: package net.sourceforge.squirrel_sql.client.session.properties;
002:
003: /*
004: * Copyright (C) 2001-2004 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.List;
029: import java.util.prefs.Preferences;
030:
031: import javax.swing.AbstractAction;
032: import javax.swing.BorderFactory;
033: import javax.swing.JButton;
034: import javax.swing.JComponent;
035: import javax.swing.JLabel;
036: import javax.swing.JPanel;
037: import javax.swing.JTabbedPane;
038: import javax.swing.KeyStroke;
039:
040: import net.sourceforge.squirrel_sql.client.IApplication;
041: import net.sourceforge.squirrel_sql.client.gui.WindowManager;
042: import net.sourceforge.squirrel_sql.client.gui.builders.UIFactory;
043: import net.sourceforge.squirrel_sql.client.gui.session.BaseSessionInternalFrame;
044: import net.sourceforge.squirrel_sql.client.gui.session.SessionInternalFrame;
045: import net.sourceforge.squirrel_sql.client.plugin.SessionPluginInfo;
046: import net.sourceforge.squirrel_sql.client.preferences.NewSessionPropertiesSheet;
047: import net.sourceforge.squirrel_sql.client.session.ISession;
048: import net.sourceforge.squirrel_sql.fw.gui.GUIUtils;
049: import net.sourceforge.squirrel_sql.fw.util.StringManager;
050: import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
051: import net.sourceforge.squirrel_sql.fw.util.log.ILogger;
052: import net.sourceforge.squirrel_sql.fw.util.log.LoggerController;
053:
054: public class SessionPropertiesSheet extends BaseSessionInternalFrame {
055: private static final StringManager s_stringMgr = StringManagerFactory
056: .getStringManager(SessionPropertiesSheet.class);
057:
058: /**
059: * This interface defines locale specific strings. This should be
060: * replaced with a property file.
061: */
062: private interface i18n {
063:
064: // i18n[sessionPropertiesSheet.sessionProperties=- Session Properties]
065: String TITLE = s_stringMgr
066: .getString("sessionPropertiesSheet.sessionProperties");
067: }
068:
069: /** Logger for this class. */
070: private static final ILogger s_log = LoggerController
071: .createLogger(SessionPropertiesSheet.class);
072:
073: private final List<ISessionPropertiesPanel> _panels = new ArrayList<ISessionPropertiesPanel>();
074:
075: private JTabbedPane _tabbedPane;
076:
077: /** Frame title. */
078: private JLabel _titleLbl = new JLabel();
079:
080: public SessionPropertiesSheet(ISession session) {
081: super (session, session.getTitle() + " " + i18n.TITLE, true);
082: createGUI();
083: for (ISessionPropertiesPanel pnl : _panels) {
084: pnl.initialize(getSession().getApplication(), getSession());
085: }
086:
087: setSize(getDimension());
088: }
089:
090: private Dimension getDimension() {
091: return new Dimension(
092: Preferences
093: .userRoot()
094: .getInt(
095: NewSessionPropertiesSheet.PREF_KEY_NEW_SESSION_PROPS_SHEET_WIDTH,
096: 500),
097: Preferences
098: .userRoot()
099: .getInt(
100: NewSessionPropertiesSheet.PREF_KEY_NEW_SESSION_PROPS_SHEET_HEIGHT,
101: 600));
102: }
103:
104: public void selectTabIndex(int index) {
105: int tabCount = _tabbedPane.getTabCount();
106:
107: if (0 <= index && index < tabCount) {
108: _tabbedPane.setSelectedIndex(index);
109: }
110: }
111:
112: /**
113: * Set title of this frame. Ensure that the title label
114: * matches the frame title.
115: *
116: * @param newTitle New title text.
117: */
118: public void setTitle(String newTitle) {
119: super .setTitle(newTitle);
120: if (_titleLbl != null) {
121: _titleLbl.setText(newTitle);
122: }
123: }
124:
125: private void performClose() {
126: dispose();
127: }
128:
129: /**
130: * OK button pressed. Edit data and if ok save to aliases model
131: * and then close dialog.
132: */
133: private void performOk() {
134: final boolean isDebug = s_log.isDebugEnabled();
135: long start = 0;
136: for (ISessionPropertiesPanel pnl : _panels) {
137: if (isDebug) {
138: start = System.currentTimeMillis();
139: }
140: pnl.applyChanges();
141: if (pnl instanceof SessionObjectTreePropertiesPanel) {
142: SessionObjectTreePropertiesPanel otPanel = (SessionObjectTreePropertiesPanel) pnl;
143: if (otPanel.isObjectTreeRefreshNeeded()) {
144: WindowManager wm = _session.getApplication()
145: .getWindowManager();
146: BaseSessionInternalFrame[] frames = wm
147: .getAllFramesOfSession(_session
148: .getIdentifier());
149: for (int i = 0; i < frames.length; i++) {
150: BaseSessionInternalFrame frame = frames[i];
151: try {
152: if (frame instanceof SessionInternalFrame) {
153: SessionInternalFrame sif = (SessionInternalFrame) frame;
154: sif.getObjectTreeAPI()
155: .refreshSelectedNodes();
156: }
157: } catch (Exception e) {
158: s_log.error(
159: "Unexpected exception while attempting to "
160: + "refresh object tree: "
161: + e.getMessage(), e);
162: }
163: }
164: }
165: }
166: if (isDebug) {
167: s_log.debug("Panel " + pnl.getTitle()
168: + " applied changes in "
169: + (System.currentTimeMillis() - start) + "ms");
170: }
171: }
172:
173: dispose();
174: }
175:
176: public void dispose() {
177: Dimension size = getSize();
178: Preferences
179: .userRoot()
180: .putInt(
181: NewSessionPropertiesSheet.PREF_KEY_NEW_SESSION_PROPS_SHEET_WIDTH,
182: size.width);
183: Preferences
184: .userRoot()
185: .putInt(
186: NewSessionPropertiesSheet.PREF_KEY_NEW_SESSION_PROPS_SHEET_HEIGHT,
187: size.height);
188:
189: super .dispose(); //To change body of overridden methods use File | Settings | File Templates.
190: }
191:
192: private void createGUI() {
193: setDefaultCloseOperation(DISPOSE_ON_CLOSE);
194:
195: // TODO: Setup title correctly.
196: // setTitle(getTitle() + ": " + _session.getSessionSheet().getTitle());
197:
198: // This is a tool window.
199: GUIUtils.makeToolWindow(this , true);
200:
201: final IApplication app = getSession().getApplication();
202:
203: // Property panels for SQuirreL.
204: _panels.add(new GeneralSessionPropertiesPanel());
205: _panels.add(new SessionObjectTreePropertiesPanel(app));
206: _panels.add(new SessionSQLPropertiesPanel(app, false));
207:
208: // Go thru all plugins attached to this session asking for panels.
209: SessionPluginInfo[] plugins = app.getPluginManager()
210: .getPluginInformation(getSession());
211: for (int i = 0; i < plugins.length; ++i) {
212: SessionPluginInfo spi = plugins[i];
213: if (spi.isLoaded()) {
214: ISessionPropertiesPanel[] pnls = spi.getSessionPlugin()
215: .getSessionPropertiesPanels(getSession());
216: if (pnls != null && pnls.length > 0) {
217: for (int pnlIdx = 0; pnlIdx < pnls.length; ++pnlIdx) {
218: _panels.add(pnls[pnlIdx]);
219: }
220: }
221: }
222: }
223:
224: // Add all panels to the tabbed panel.
225: _tabbedPane = UIFactory.getInstance().createTabbedPane();
226: for (ISessionPropertiesPanel pnl : _panels) {
227: String pnlTitle = pnl.getTitle();
228: String hint = pnl.getHint();
229: _tabbedPane.addTab(pnlTitle, null, pnl.getPanelComponent(),
230: hint);
231: }
232:
233: final JPanel contentPane = new JPanel(new GridBagLayout());
234: contentPane.setBorder(BorderFactory.createEmptyBorder(5, 10, 5,
235: 10));
236: setContentPane(contentPane);
237:
238: GridBagConstraints gbc = new GridBagConstraints();
239:
240: gbc.gridwidth = 1;
241:
242: gbc.gridx = 0;
243: gbc.gridy = 0;
244:
245: gbc.fill = GridBagConstraints.BOTH;
246: gbc.weightx = 1;
247: contentPane.add(_titleLbl, gbc);
248:
249: ++gbc.gridy;
250: gbc.weighty = 1;
251: contentPane.add(_tabbedPane, gbc);
252:
253: ++gbc.gridy;
254: gbc.weighty = 0;
255: contentPane.add(createButtonsPanel(), gbc);
256:
257: AbstractAction closeAction = new AbstractAction() {
258: public void actionPerformed(ActionEvent actionEvent) {
259: performClose();
260: }
261: };
262: KeyStroke escapeStroke = KeyStroke.getKeyStroke(
263: KeyEvent.VK_ESCAPE, 0);
264: getRootPane().getInputMap(
265: JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(
266: escapeStroke, "CloseAction");
267: getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)
268: .put(escapeStroke, "CloseAction");
269: getRootPane().getInputMap(JComponent.WHEN_FOCUSED).put(
270: escapeStroke, "CloseAction");
271: getRootPane().getActionMap().put("CloseAction", closeAction);
272: }
273:
274: private JPanel createButtonsPanel() {
275: JPanel pnl = new JPanel();
276:
277: // i18n[sessionPropertiesSheet.ok=OK]
278: JButton okBtn = new JButton(s_stringMgr
279: .getString("sessionPropertiesSheet.ok"));
280: okBtn.addActionListener(new ActionListener() {
281: public void actionPerformed(ActionEvent evt) {
282: performOk();
283: }
284: });
285:
286: // i18n[sessionPropertiesSheet.close=Close]
287: JButton closeBtn = new JButton(s_stringMgr
288: .getString("sessionPropertiesSheet.close"));
289: closeBtn.addActionListener(new ActionListener() {
290: public void actionPerformed(ActionEvent evt) {
291: performClose();
292: }
293: });
294:
295: pnl.add(okBtn);
296: pnl.add(closeBtn);
297:
298: GUIUtils
299: .setJButtonSizesTheSame(new JButton[] { okBtn, closeBtn });
300: getRootPane().setDefaultButton(okBtn);
301:
302: return pnl;
303: }
304: }
|