001: package net.sourceforge.squirrel_sql.client.session.properties;
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.Component;
022: import java.awt.Font;
023: import java.awt.GridBagConstraints;
024: import java.awt.GridBagLayout;
025: import java.awt.Insets;
026: import java.awt.event.ActionEvent;
027: import java.awt.event.ActionListener;
028:
029: import javax.swing.*;
030: import javax.swing.event.ChangeEvent;
031: import javax.swing.event.ChangeListener;
032:
033: import net.sourceforge.squirrel_sql.fw.gui.FontChooser;
034: import net.sourceforge.squirrel_sql.fw.gui.FontInfo;
035: import net.sourceforge.squirrel_sql.fw.gui.IntegerField;
036: import net.sourceforge.squirrel_sql.fw.gui.MultipleLineLabel;
037: import net.sourceforge.squirrel_sql.fw.util.StringManager;
038: import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
039:
040: import net.sourceforge.squirrel_sql.client.IApplication;
041: import net.sourceforge.squirrel_sql.client.preferences.INewSessionPropertiesPanel;
042: import net.sourceforge.squirrel_sql.client.session.ISession;
043:
044: /**
045: * This panel allows the user to tailor SQL settings for a session.
046: *
047: * @author <A HREF="mailto:colbell@users.sourceforge.net">Colin Bell</A>
048: */
049: public class SessionSQLPropertiesPanel implements
050: INewSessionPropertiesPanel, ISessionPropertiesPanel {
051: /** Internationalized strings for this class. */
052: private static final StringManager s_stringMgr = StringManagerFactory
053: .getStringManager(SessionSQLPropertiesPanel.class);
054:
055: /** Application API. */
056: private final IApplication _app;
057:
058: /** The actual GUI panel that allows user to do the maintenance. */
059: private final SQLPropertiesPanel _myPanel;
060:
061: private final JScrollPane _scrolledMyPanel;
062:
063: /** Session properties object being maintained. */
064: private SessionProperties _props;
065:
066: /**
067: * ctor specifying the Application API.
068: *
069: * @param app Application API.
070: *
071: * @throws IllegalArgumentException
072: * Thrown if <tt>null</tt> <tt>IApplication</tt>
073: * passed.
074: */
075: public SessionSQLPropertiesPanel(IApplication app,
076: boolean newSessionProperties)
077: throws IllegalArgumentException {
078: super ();
079: if (app == null) {
080: throw new IllegalArgumentException(
081: "Null IApplication passed");
082: }
083: _app = app;
084: _myPanel = new SQLPropertiesPanel(app, newSessionProperties);
085: _scrolledMyPanel = new JScrollPane(_myPanel);
086: }
087:
088: public void initialize(IApplication app) {
089: _props = _app.getSquirrelPreferences().getSessionProperties();
090: _myPanel.loadData(_props);
091: }
092:
093: public void initialize(IApplication app, ISession session)
094: throws IllegalArgumentException {
095: if (session == null) {
096: throw new IllegalArgumentException("Null ISession passed");
097: }
098: _props = session.getProperties();
099: _myPanel.loadData(_props);
100: }
101:
102: public Component getPanelComponent() {
103: return _scrolledMyPanel;
104: }
105:
106: public String getTitle() {
107: return s_stringMgr.getString("SessionSQLPropertiesPanel.sql");
108: }
109:
110: public String getHint() {
111: return getTitle();
112: }
113:
114: public void applyChanges() {
115: _myPanel.applyChanges(_props);
116: }
117:
118: private static final class SQLPropertiesPanel extends JPanel {
119: private JCheckBox _abortOnErrorChk = new JCheckBox(s_stringMgr
120: .getString("SessionSQLPropertiesPanel.abortonerror"));
121: private JCheckBox _writeSQLErrorsToLogChk = new JCheckBox(
122: s_stringMgr
123: .getString("SessionSQLPropertiesPanel.writesqlerrorstolog"));
124: private JCheckBox _loadColumsInBackgroundChk = new JCheckBox(
125: s_stringMgr
126: .getString("SessionSQLPropertiesPanel.loadColumsInBackground"));
127: private JCheckBox _autoCommitChk = new JCheckBox(s_stringMgr
128: .getString("SessionSQLPropertiesPanel.autocommit"));
129: private JCheckBox _commitOnClose = new JCheckBox(s_stringMgr
130: .getString("SessionSQLPropertiesPanel.commitonclose"));
131: private IntegerField _sqlNbrRowsToShowField = new IntegerField(
132: 5);
133: private JCheckBox _sqlLimitRowsChk = new JCheckBox(s_stringMgr
134: .getString("SessionSQLPropertiesPanel.limitrows"));
135: private JTextField _stmtSepField = new JTextField(5);
136: private JTextField _solCommentField = new JTextField(2);
137: // i18n[SessionSQLPropertiesPanel.removeMultiLineComment=Remove multi line comment (/*...*/) from SQL before sending to database]
138: private JCheckBox _removeMultiLineComment = new JCheckBox(
139: s_stringMgr
140: .getString("SessionSQLPropertiesPanel.removeMultiLineComment"));
141:
142: private JCheckBox _limitSQLResultTabsChk = new JCheckBox(
143: s_stringMgr
144: .getString("SessionSQLPropertiesPanel.limitsqlresulttabs"));
145: private IntegerField _limitSQLResultTabsField = new IntegerField(
146: 5);
147:
148: /** Label displaying the selected font. */
149: private JLabel _fontLbl = new JLabel();
150:
151: /** Button to select font. */
152: private FontButton _fontBtn = new FontButton(s_stringMgr
153: .getString("SessionSQLPropertiesPanel.font"), _fontLbl);
154:
155: private JCheckBox _shareSQLHistoryChk = new JCheckBox(
156: s_stringMgr
157: .getString("SessionSQLPropertiesPanel.sharesqlhistory"));
158: private JCheckBox _limitSQLHistoryComboSizeChk = new JCheckBox(
159: s_stringMgr
160: .getString("SessionSQLPropertiesPanel.limitsqlhistorysize"));
161: private IntegerField _limitSQLHistoryComboSizeField = new IntegerField(
162: 5);
163: private JCheckBox _showResultsMetaChk = new JCheckBox(
164: s_stringMgr
165: .getString("SessionSQLPropertiesPanel.showresultsmd"));
166:
167: /**
168: * This object will update the status of the GUI controls as the user
169: * makes changes.
170: */
171: private final ControlMediator _controlMediator = new ControlMediator();
172: private boolean _newSessionProperties;
173:
174: SQLPropertiesPanel(IApplication app,
175: boolean newSessionProperties) {
176: super ();
177: _newSessionProperties = newSessionProperties;
178: createGUI();
179: }
180:
181: void loadData(SessionProperties props) {
182: _abortOnErrorChk.setSelected(props.getAbortOnError());
183: _writeSQLErrorsToLogChk.setSelected(props
184: .getWriteSQLErrorsToLog());
185: _loadColumsInBackgroundChk.setSelected(props
186: .getLoadColumnsInBackground());
187:
188: _autoCommitChk.setSelected(props.getAutoCommit());
189: _commitOnClose.setSelected(props
190: .getCommitOnClosingConnection());
191: _sqlNbrRowsToShowField.setInt(props.getSQLNbrRowsToShow());
192: _sqlLimitRowsChk.setSelected(props.getSQLLimitRows());
193: _stmtSepField.setText(props.getSQLStatementSeparator());
194: _solCommentField.setText(props.getStartOfLineComment());
195: _removeMultiLineComment.setSelected(props
196: .getRemoveMultiLineComment());
197:
198: _shareSQLHistoryChk.setSelected(props.getSQLShareHistory());
199: _limitSQLHistoryComboSizeChk.setSelected(props
200: .getLimitSQLEntryHistorySize());
201: _limitSQLHistoryComboSizeField.setInt(props
202: .getSQLEntryHistorySize());
203:
204: _limitSQLResultTabsChk.setSelected(props
205: .getLimitSQLResultTabs());
206: _limitSQLResultTabsField.setInt(props
207: .getSqlResultTabLimit());
208:
209: _showResultsMetaChk.setSelected(props
210: .getShowResultsMetaData());
211:
212: FontInfo fi = props.getFontInfo();
213: if (fi == null) {
214: fi = new FontInfo(UIManager.getFont("TextArea.font"));
215: }
216: _fontLbl.setText(fi.toString());
217: _fontBtn.setSelectedFont(fi.createFont());
218:
219: updateControlStatus();
220: }
221:
222: void applyChanges(SessionProperties props) {
223: props.setAbortOnError(_abortOnErrorChk.isSelected());
224: props.setWriteSQLErrorsToLog(_writeSQLErrorsToLogChk
225: .isSelected());
226: props.setLoadColumnsInBackground(_loadColumsInBackgroundChk
227: .isSelected());
228: props.setAutoCommit(_autoCommitChk.isSelected());
229: props.setCommitOnClosingConnection(_commitOnClose
230: .isSelected());
231: props.setSQLNbrRowsToShow(_sqlNbrRowsToShowField.getInt());
232: props.setSQLLimitRows(_sqlLimitRowsChk.isSelected());
233: props.setSQLStatementSeparator(_stmtSepField.getText());
234: props.setStartOfLineComment(_solCommentField.getText());
235: props.setRemoveMultiLineComment(_removeMultiLineComment
236: .isSelected());
237:
238: props.setFontInfo(_fontBtn.getFontInfo());
239:
240: props.setSQLShareHistory(_shareSQLHistoryChk.isSelected());
241: props
242: .setLimitSQLEntryHistorySize(_limitSQLHistoryComboSizeChk
243: .isSelected());
244: props.setSQLEntryHistorySize(_limitSQLHistoryComboSizeField
245: .getInt());
246:
247: props.setLimitSQLResultTabs(_limitSQLResultTabsChk
248: .isSelected());
249:
250: if (0 >= _limitSQLResultTabsField.getInt()) {
251: props.setSqlResultTabLimit(15);
252: } else {
253: props.setSqlResultTabLimit(_limitSQLResultTabsField
254: .getInt());
255: }
256:
257: props.setShowResultsMetaData(_showResultsMetaChk
258: .isSelected());
259: }
260:
261: private void updateControlStatus() {
262: _commitOnClose.setEnabled(!_autoCommitChk.isSelected());
263:
264: _sqlNbrRowsToShowField.setEnabled(_sqlLimitRowsChk
265: .isSelected());
266:
267: _limitSQLResultTabsField.setEnabled(_limitSQLResultTabsChk
268: .isSelected());
269:
270: // If this session doesn't share SQL history with other sessions
271: // then disable the controls that relate to SQL History.
272: final boolean shareSQLHistory = _shareSQLHistoryChk
273: .isSelected();
274:
275: }
276:
277: private void createGUI() {
278: setLayout(new GridBagLayout());
279: final GridBagConstraints gbc = new GridBagConstraints();
280: gbc.anchor = GridBagConstraints.WEST;
281: gbc.fill = GridBagConstraints.HORIZONTAL;
282: gbc.insets = new Insets(4, 4, 4, 4);
283:
284: gbc.gridx = 0;
285: gbc.gridy = 0;
286: add(createSQLPanel(), gbc);
287:
288: ++gbc.gridy;
289: add(createFontPanel(), gbc);
290:
291: ++gbc.gridy;
292: add(createSQLHistoryPanel(), gbc);
293: }
294:
295: private JPanel createSQLPanel() {
296: final JPanel pnl = new JPanel(new GridBagLayout());
297: pnl.setBorder(BorderFactory.createTitledBorder(s_stringMgr
298: .getString("SessionSQLPropertiesPanel.sql")));
299: final GridBagConstraints gbc = new GridBagConstraints();
300: gbc.fill = GridBagConstraints.HORIZONTAL;
301: gbc.insets = new Insets(4, 4, 4, 4);
302: gbc.anchor = GridBagConstraints.CENTER;
303:
304: _autoCommitChk.addChangeListener(_controlMediator);
305: _sqlLimitRowsChk.addChangeListener(_controlMediator);
306: _sqlNbrRowsToShowField.setColumns(5);
307: _stmtSepField.setColumns(5);
308:
309: _limitSQLResultTabsChk.addChangeListener(_controlMediator);
310: _limitSQLResultTabsField.setColumns(5);
311:
312: gbc.gridx = 0;
313: gbc.gridy = 0;
314: gbc.gridwidth = 2;
315: pnl.add(_autoCommitChk, gbc);
316:
317: gbc.gridx += 2;
318: gbc.gridwidth = GridBagConstraints.REMAINDER;
319: pnl.add(_commitOnClose, gbc);
320:
321: ++gbc.gridy; // new line
322: gbc.gridx = 0;
323: gbc.gridwidth = 3;
324: pnl.add(_showResultsMetaChk, gbc);
325:
326: ++gbc.gridy; // new line
327: gbc.gridx = 0;
328: gbc.gridwidth = 2;
329: pnl.add(_sqlLimitRowsChk, gbc);
330: gbc.gridwidth = 1;
331: gbc.gridx += 2;
332: pnl.add(_sqlNbrRowsToShowField, gbc);
333: ++gbc.gridx;
334: gbc.gridwidth = GridBagConstraints.REMAINDER;
335: pnl.add(new JLabel(s_stringMgr
336: .getString("SessionSQLPropertiesPanel.rows")), gbc);
337:
338: ++gbc.gridy; // new line
339: gbc.gridx = 0;
340: gbc.gridwidth = 2;
341: pnl.add(_limitSQLResultTabsChk, gbc);
342: gbc.gridwidth = 1;
343: gbc.gridx += 2;
344: pnl.add(_limitSQLResultTabsField, gbc);
345: ++gbc.gridx;
346: gbc.gridwidth = GridBagConstraints.REMAINDER;
347: pnl.add(new JLabel(s_stringMgr
348: .getString("SessionSQLPropertiesPanel.tabs")), gbc);
349:
350: ++gbc.gridy; // new line
351: gbc.gridx = 0;
352: gbc.gridwidth = GridBagConstraints.REMAINDER;
353: pnl.add(_abortOnErrorChk, gbc);
354:
355: ++gbc.gridy; // new line
356: gbc.gridx = 0;
357: gbc.gridwidth = GridBagConstraints.REMAINDER;
358: pnl.add(_writeSQLErrorsToLogChk, gbc);
359:
360: //////////////////////////////////////////////
361: // belong together
362: Insets defaultInsets = gbc.insets;
363:
364: gbc.insets = (Insets) defaultInsets.clone();
365: gbc.insets.bottom = 0;
366:
367: ++gbc.gridy; // new line
368: gbc.gridx = 0;
369: gbc.gridwidth = GridBagConstraints.REMAINDER;
370: String loadColsInBackgroundDescription = s_stringMgr
371: .getString("SessionSQLPropertiesPanel.loadColsInBackgroundDescription");
372: pnl.add(new MultipleLineLabel(
373: loadColsInBackgroundDescription), gbc);
374:
375: gbc.insets = (Insets) defaultInsets.clone();
376: gbc.insets.top = 0;
377:
378: ++gbc.gridy; // new line
379: gbc.gridx = 0;
380: gbc.gridwidth = GridBagConstraints.REMAINDER;
381: pnl.add(_loadColumsInBackgroundChk, gbc);
382:
383: gbc.insets = defaultInsets;
384: //
385: /////////////////////////////////////////////
386:
387: ++gbc.gridy; // new line
388: gbc.gridx = 0;
389: gbc.gridwidth = 1;
390: pnl.add(new JLabel(s_stringMgr
391: .getString("SessionSQLPropertiesPanel.stmtsep")),
392: gbc);
393: ++gbc.gridx;
394: pnl.add(_stmtSepField, gbc);
395: ++gbc.gridx;
396: pnl
397: .add(
398: new RightLabel(
399: s_stringMgr
400: .getString("SessionSQLPropertiesPanel.solcomment")),
401: gbc);
402: ++gbc.gridx;
403: pnl.add(_solCommentField, gbc);
404:
405: ++gbc.gridy; // new line
406: gbc.gridx = 0;
407: gbc.gridwidth = 4;
408: pnl.add(_removeMultiLineComment, gbc);
409:
410: return pnl;
411: }
412:
413: private JPanel createFontPanel() {
414: JPanel pnl = new JPanel();
415: pnl
416: .setBorder(BorderFactory
417: .createTitledBorder(s_stringMgr
418: .getString("SessionSQLPropertiesPanel.sqlentryarea")));
419: pnl.setLayout(new GridBagLayout());
420: final GridBagConstraints gbc = new GridBagConstraints();
421: gbc.fill = GridBagConstraints.HORIZONTAL;
422: gbc.insets = new Insets(4, 4, 4, 4);
423:
424: _fontBtn.addActionListener(new FontButtonListener());
425:
426: gbc.gridx = 0;
427: gbc.gridy = 0;
428: pnl.add(_fontBtn, gbc);
429:
430: ++gbc.gridx;
431: gbc.weightx = 1.0;
432: pnl.add(_fontLbl, gbc);
433:
434: return pnl;
435: }
436:
437: private JPanel createSQLHistoryPanel() {
438: _shareSQLHistoryChk.addChangeListener(_controlMediator);
439: _limitSQLHistoryComboSizeChk
440: .addChangeListener(_controlMediator);
441:
442: JPanel pnl = new JPanel(new GridBagLayout());
443: pnl
444: .setBorder(BorderFactory
445: .createTitledBorder(s_stringMgr
446: .getString("SessionSQLPropertiesPanel.sqlhistory")));
447: final GridBagConstraints gbc = new GridBagConstraints();
448: gbc.fill = GridBagConstraints.HORIZONTAL;
449: gbc.insets = new Insets(4, 4, 4, 4);
450: gbc.anchor = GridBagConstraints.WEST;
451:
452: gbc.gridx = 0;
453: gbc.gridy = 0;
454: pnl.add(_shareSQLHistoryChk, gbc);
455:
456: ++gbc.gridy;
457: pnl.add(_limitSQLHistoryComboSizeChk, gbc);
458:
459: ++gbc.gridx;
460: pnl.add(_limitSQLHistoryComboSizeField, gbc);
461:
462: return pnl;
463: }
464:
465: private static final class RightLabel extends JLabel {
466: RightLabel(String title) {
467: super (title, SwingConstants.RIGHT);
468: }
469: }
470:
471: private static final class FontButton extends JButton {
472: private FontInfo _fi;
473: private JLabel _lbl;
474: private Font _font;
475:
476: FontButton(String text, JLabel lbl) {
477: super (text);
478: _lbl = lbl;
479: }
480:
481: FontInfo getFontInfo() {
482: return _fi;
483: }
484:
485: Font getSelectedFont() {
486: return _font;
487: }
488:
489: void setSelectedFont(Font font) {
490: _font = font;
491: if (_fi == null) {
492: _fi = new FontInfo(font);
493: } else {
494: _fi.setFont(font);
495: }
496: }
497: }
498:
499: private static final class FontButtonListener implements
500: ActionListener {
501: public void actionPerformed(ActionEvent evt) {
502: if (evt.getSource() instanceof FontButton) {
503: FontButton btn = (FontButton) evt.getSource();
504: FontInfo fi = btn.getFontInfo();
505: Font font = null;
506: if (fi != null) {
507: font = fi.createFont();
508: }
509: font = new FontChooser().showDialog(font);
510: if (font != null) {
511: btn.setSelectedFont(font);
512: btn._lbl.setText(new FontInfo(font).toString());
513: }
514: }
515: }
516: }
517:
518: /**
519: * This class will update the status of the GUI controls as the user
520: * makes changes.
521: */
522: private final class ControlMediator implements ChangeListener,
523: ActionListener {
524: public void stateChanged(ChangeEvent evt) {
525: updateControlStatus();
526: }
527:
528: public void actionPerformed(ActionEvent evt) {
529: updateControlStatus();
530: }
531: }
532: }
533: }
|