001: /*
002: * Copyright (C) 2007 Rob Manning
003: * manningr@users.sourceforge.net
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: */
019: package net.sourceforge.squirrel_sql.client.plugin.gui;
020:
021: import java.awt.Component;
022: import java.awt.GridBagConstraints;
023: import java.awt.GridBagLayout;
024: import java.awt.Insets;
025: import java.awt.event.ActionEvent;
026: import java.awt.event.ActionListener;
027:
028: import javax.swing.JCheckBox;
029: import javax.swing.JLabel;
030: import javax.swing.JPanel;
031: import javax.swing.JTextField;
032: import javax.swing.border.Border;
033: import javax.swing.border.CompoundBorder;
034: import javax.swing.border.EmptyBorder;
035: import javax.swing.border.TitledBorder;
036:
037: import net.sourceforge.squirrel_sql.client.plugin.PluginQueryTokenizerPreferencesManager;
038: import net.sourceforge.squirrel_sql.fw.preferences.IQueryTokenizerPreferenceBean;
039: import net.sourceforge.squirrel_sql.fw.util.StringManager;
040: import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
041:
042: /**
043: * This can be used (or subclassed) to provide a plugin-specific preference
044: * panel geared toward storing custom QueryTokenizer preferences. A plugin
045: * can instantiate this class giving it an IQueryTokenizerPreferenceBean with
046: * which to load and store preference information. The databaseName is used to
047: * tailor some of the tooltips for a specific database.
048: *
049: * @author manningr
050: */
051: public class PluginQueryTokenizerPreferencesPanel extends JPanel {
052:
053: private static final long serialVersionUID = 1L;
054:
055: /** Internationalized strings for this class. */
056: private static final StringManager s_stringMgr = StringManagerFactory
057: .getStringManager(PluginQueryTokenizerPreferencesPanel.class);
058:
059: static interface i18n {
060:
061: //i18n[PreferencesPanel.useCustomQTLabel=Use Custom Query Tokenizer]
062: String USE_CUSTOM_QT_LABEL = s_stringMgr
063: .getString("PreferencesPanel.useCustomQTLabel");
064:
065: //i18n[PreferencesPanel.removeMultiLineCommentLabel=Remove multi-line
066: //comments (/*...*/) from SQL before executing
067: String REMOVE_ML_COMMENT_LABEL = s_stringMgr
068: .getString("PreferencesPanel.removeMultiLineCommentLabel");
069:
070: //i18n[PreferencesPanel.removeMultiLineCommentLabelTipText=]
071: String REMOVE_ML_COMMENT_LABEL_TT = s_stringMgr
072: .getString("PreferencesPanel.removeMultiLineCommentLabelTipText");
073:
074: //i18n[PreferencesPanel.statementSeparatorLabel=Statement Separator]
075: String STMT_SEP_LABEL = s_stringMgr
076: .getString("PreferencesPanel.statementSeparatorLabel");
077:
078: //i18n[PreferencesPanel.statementSeparatorToolTip=When multiple
079: //statements are selected, use this to separate them into single
080: //statements.]
081: String STMT_SEP_LABEL_TT = s_stringMgr
082: .getString("PreferencesPanel.statementSeparatorToolTip");
083:
084: //i18n[PreferencesPanel.lineCommentLabel=Start of line comment]
085: String LINE_COMMENT_LABEL = s_stringMgr
086: .getString("PreferencesPanel.lineCommentLabel");
087:
088: //i18n[PreferencesPanel.lineCommentToolTip=Sequence of characters at
089: //the beginning of a line to indicate that whole line is a comment]
090: String LINE_COMMENT_LABEL_TT = s_stringMgr
091: .getString("PreferencesPanel.lineCommentToolTip");
092:
093: //i18n[PreferencesPanel.procedureSeparatorLabel=Procedure/Function
094: //Separator]
095: String PROC_SEP_LABEL = s_stringMgr
096: .getString("PreferencesPanel.procedureSeparatorLabel");
097:
098: //i18n[PreferencesPanel.procedureSeparatorToolTip=Multiple statements
099: //within a procedure or function can be terminated with this.]
100: String PROC_SEP_LABEL_TT = s_stringMgr
101: .getString("PreferencesPanel.procedureSeparatorToolTip");
102:
103: }
104:
105: protected PluginQueryTokenizerPreferencesManager _prefsManager = null;
106:
107: protected JCheckBox useCustomQTCheckBox = null;
108:
109: protected JLabel useCustomQTLabel = null;
110:
111: protected JCheckBox removeMultiLineCommentCheckBox = null;
112:
113: protected JTextField lineCommentTextField = null;
114:
115: protected JLabel lineCommentLabel = null;
116:
117: protected JLabel procedureSeparatorLabel = null;
118:
119: protected JTextField procedureSeparatorTextField = null;
120:
121: protected JLabel statementSeparatorLabel = null;
122:
123: protected JTextField statementSeparatorTextField = null;
124:
125: /** the name of the database we are storing preferences for. */
126: protected String _databaseName = null;
127:
128: protected boolean _showProcSep = true;
129:
130: /** the last row of widgets that was added to the grid */
131: protected int lastY = 0;
132:
133: /**
134: * Construct a new PreferencesPanel.
135: * @param databaseName
136: */
137: public PluginQueryTokenizerPreferencesPanel(
138: PluginQueryTokenizerPreferencesManager prefsMgr,
139: String databaseName) {
140: this (prefsMgr, databaseName, true);
141: }
142:
143: /**
144: * Construct a new PreferencesPanel.
145: * @param prefsMgr the query tokenizer preferences manager.
146: * @param databaseName the name of the database to use as the panel title.
147: * @param showProcedureSeparator whether or not the custom tokenizer needs
148: * procedure separator preference.
149: */
150: public PluginQueryTokenizerPreferencesPanel(
151: PluginQueryTokenizerPreferencesManager prefsMgr,
152: String databaseName, boolean showProcedureSeparator) {
153: super ();
154: _prefsManager = prefsMgr;
155: _databaseName = databaseName;
156: _showProcSep = showProcedureSeparator;
157: createGUI();
158: loadData();
159: }
160:
161: private void createGUI() {
162: this .setLayout(new GridBagLayout());
163: GridBagConstraints c = new GridBagConstraints();
164: c.gridx = 0; // Column 0
165: c.gridy = 0; // Row 0
166: c.fill = GridBagConstraints.BOTH;
167: c.weightx = 1;
168: c.weighty = .40;
169: add(createTopPanel(), c);
170: }
171:
172: protected JPanel createTopPanel() {
173: JPanel result = new JPanel(new GridBagLayout());
174: //i18n[PreferencesPanel.borderLabel={0} Script Settings]
175: String borderLabel = s_stringMgr.getString(
176: "PreferencesPanel.borderLabel", _databaseName);
177: result.setBorder(getTitledBorder(borderLabel));
178:
179: addUseCustomQTCheckBox(result, 0, lastY++);
180:
181: addLineCommentLabel(result, 0, lastY);
182: addLineCommentTextField(result, 1, lastY++);
183:
184: addStatementSeparatorLabel(result, 0, lastY);
185: addStatementSeparatorTextField(result, 1, lastY++);
186:
187: if (_showProcSep) {
188: addProcedureSeparatorLabel(result, 0, lastY);
189: addProcedureSeparatorTextField(result, 1, lastY++);
190: }
191:
192: addRemoveMultiLineCommentCheckBox(result, 0, lastY++);
193:
194: return result;
195: }
196:
197: private void addUseCustomQTCheckBox(JPanel panel, int col, int row) {
198: GridBagConstraints c = new GridBagConstraints();
199: c.gridx = col;
200: c.gridy = row;
201: c.insets = new Insets(5, 5, 0, 0);
202: c.anchor = GridBagConstraints.WEST;
203: c.gridwidth = 2; // Span across two columns
204: useCustomQTCheckBox = new JCheckBox(i18n.USE_CUSTOM_QT_LABEL);
205:
206: //i18n[PreferencesPanel.useCustomQTToolTip=Gives enhanced capabilities
207: //over the default query tokenizer for handling {0} scripts]
208: String USE_CUSTOM_QT_TOOLTIP = s_stringMgr.getString(
209: "PreferencesPanel.useCustomQTToolTip", _databaseName);
210:
211: useCustomQTCheckBox.setToolTipText(USE_CUSTOM_QT_TOOLTIP);
212: useCustomQTCheckBox.addActionListener(new UseQTHandler());
213: panel.add(useCustomQTCheckBox, c);
214: }
215:
216: private void addRemoveMultiLineCommentCheckBox(JPanel panel,
217: int col, int row) {
218: GridBagConstraints c = new GridBagConstraints();
219: c.gridx = col;
220: c.gridy = row;
221: c.gridwidth = 2; // Span across two columns
222: c.insets = new Insets(5, 30, 0, 0);
223: String cbLabel = i18n.REMOVE_ML_COMMENT_LABEL;
224: removeMultiLineCommentCheckBox = new JCheckBox(cbLabel);
225: removeMultiLineCommentCheckBox
226: .setToolTipText(i18n.REMOVE_ML_COMMENT_LABEL_TT);
227: panel.add(removeMultiLineCommentCheckBox, c);
228: }
229:
230: private void addStatementSeparatorLabel(JPanel panel, int col,
231: int row) {
232: GridBagConstraints c = new GridBagConstraints();
233: c.gridx = col;
234: c.gridy = row;
235: c.insets = new Insets(5, 35, 0, 0);
236: c.anchor = GridBagConstraints.WEST;
237: statementSeparatorLabel = new JLabel(i18n.STMT_SEP_LABEL);
238: statementSeparatorLabel.setHorizontalAlignment(JLabel.LEFT);
239: statementSeparatorLabel.setToolTipText(i18n.STMT_SEP_LABEL_TT);
240: panel.add(statementSeparatorLabel, c);
241: }
242:
243: private void addStatementSeparatorTextField(JPanel panel, int col,
244: int row) {
245: GridBagConstraints c = new GridBagConstraints();
246: c.gridx = col;
247: c.gridy = row;
248: c.ipadx = 40; // Increases component width by 40 pixels
249: c.insets = new Insets(5, 5, 0, 0);
250: c.anchor = GridBagConstraints.WEST;
251: statementSeparatorTextField = new JTextField(10);
252:
253: statementSeparatorTextField
254: .setHorizontalAlignment(JTextField.RIGHT);
255: statementSeparatorTextField
256: .setToolTipText(i18n.STMT_SEP_LABEL_TT);
257: panel.add(statementSeparatorTextField, c);
258: }
259:
260: private void addLineCommentLabel(JPanel panel, int col, int row) {
261: GridBagConstraints c = new GridBagConstraints();
262: c.gridx = col;
263: c.gridy = row;
264: c.insets = new Insets(5, 35, 0, 0);
265: c.anchor = GridBagConstraints.WEST;
266: lineCommentLabel = new JLabel(i18n.LINE_COMMENT_LABEL);
267: lineCommentLabel.setHorizontalAlignment(JLabel.LEFT);
268: lineCommentLabel.setToolTipText(i18n.LINE_COMMENT_LABEL_TT);
269: panel.add(lineCommentLabel, c);
270: }
271:
272: private void addLineCommentTextField(JPanel panel, int col, int row) {
273: GridBagConstraints c = new GridBagConstraints();
274: c.gridx = col;
275: c.gridy = row;
276: c.ipadx = 40; // Increases component width by 40 pixels
277: c.insets = new Insets(5, 5, 0, 0);
278: c.anchor = GridBagConstraints.WEST;
279: lineCommentTextField = new JTextField(10);
280: lineCommentTextField.setHorizontalAlignment(JTextField.RIGHT);
281: lineCommentTextField.setToolTipText(i18n.LINE_COMMENT_LABEL_TT);
282: panel.add(lineCommentTextField, c);
283: }
284:
285: private void addProcedureSeparatorLabel(JPanel panel, int col,
286: int row) {
287: GridBagConstraints c = new GridBagConstraints();
288: c.gridx = col;
289: c.gridy = row;
290: c.insets = new Insets(5, 35, 0, 0);
291: procedureSeparatorLabel = new JLabel(i18n.PROC_SEP_LABEL);
292: procedureSeparatorLabel.setHorizontalAlignment(JLabel.RIGHT);
293: procedureSeparatorLabel.setToolTipText(i18n.PROC_SEP_LABEL_TT);
294: panel.add(procedureSeparatorLabel, c);
295: }
296:
297: private void addProcedureSeparatorTextField(JPanel panel, int col,
298: int row) {
299: GridBagConstraints c = new GridBagConstraints();
300: c.gridx = col;
301: c.gridy = row;
302: c.ipadx = 40; // Increases component width by 20 pixels
303: c.insets = new Insets(5, 5, 0, 0);
304: c.anchor = GridBagConstraints.WEST;
305: procedureSeparatorTextField = new JTextField(10);
306: procedureSeparatorTextField
307: .setHorizontalAlignment(JTextField.RIGHT);
308: procedureSeparatorTextField
309: .setToolTipText(i18n.PROC_SEP_LABEL_TT);
310: panel.add(procedureSeparatorTextField, c);
311: }
312:
313: private Border getTitledBorder(String title) {
314: CompoundBorder border = new CompoundBorder(new EmptyBorder(10,
315: 10, 10, 10), new TitledBorder(title));
316: return border;
317: }
318:
319: protected void loadData() {
320: IQueryTokenizerPreferenceBean _prefs = _prefsManager
321: .getPreferences();
322: removeMultiLineCommentCheckBox.setSelected(_prefs
323: .isRemoveMultiLineComments());
324: lineCommentTextField.setText(_prefs.getLineComment());
325: statementSeparatorTextField.setText(_prefs
326: .getStatementSeparator());
327: if (_showProcSep) {
328: procedureSeparatorTextField.setText(_prefs
329: .getProcedureSeparator());
330: }
331: useCustomQTCheckBox.setSelected(_prefs
332: .isInstallCustomQueryTokenizer());
333: updatePreferenceState();
334: }
335:
336: protected void save() {
337: IQueryTokenizerPreferenceBean _prefs = _prefsManager
338: .getPreferences();
339: _prefs
340: .setRemoveMultiLineComments(removeMultiLineCommentCheckBox
341: .isSelected());
342: _prefs.setLineComment(lineCommentTextField.getText());
343: _prefs.setStatementSeparator(statementSeparatorTextField
344: .getText());
345: if (_showProcSep) {
346: _prefs.setProcedureSeparator(procedureSeparatorTextField
347: .getText());
348: }
349: _prefs.setInstallCustomQueryTokenizer(useCustomQTCheckBox
350: .isSelected());
351: _prefsManager.savePrefs();
352: }
353:
354: /* (non-Javadoc)
355: * @see net.sourceforge.squirrel_sql.client.util.IOptionPanel#applyChanges()
356: */
357: public void applyChanges() {
358: save();
359: }
360:
361: /* (non-Javadoc)
362: * @see net.sourceforge.squirrel_sql.client.util.IOptionPanel#getPanelComponent()
363: */
364: public Component getPanelComponent() {
365: return this ;
366: }
367:
368: private void updatePreferenceState() {
369: if (useCustomQTCheckBox.isSelected()) {
370: removeMultiLineCommentCheckBox.setEnabled(true);
371: lineCommentTextField.setEnabled(true);
372: lineCommentLabel.setEnabled(true);
373: statementSeparatorTextField.setEnabled(true);
374: statementSeparatorLabel.setEnabled(true);
375: if (_showProcSep) {
376: procedureSeparatorLabel.setEnabled(true);
377: procedureSeparatorTextField.setEnabled(true);
378: }
379: } else {
380: removeMultiLineCommentCheckBox.setEnabled(false);
381: lineCommentTextField.setEnabled(false);
382: lineCommentLabel.setEnabled(false);
383: statementSeparatorTextField.setEnabled(false);
384: statementSeparatorLabel.setEnabled(false);
385: if (_showProcSep) {
386: procedureSeparatorLabel.setEnabled(false);
387: procedureSeparatorTextField.setEnabled(false);
388: }
389: }
390: }
391:
392: private class UseQTHandler implements ActionListener {
393: public void actionPerformed(ActionEvent event) {
394: updatePreferenceState();
395: }
396: }
397:
398: }
|