001: package net.sourceforge.squirrel_sql.plugins.sessionscript;
002:
003: /*
004: * Copyright (C) 2002 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.GridBagConstraints;
022: import java.awt.GridBagLayout;
023: import java.awt.Insets;
024: import java.awt.event.ActionEvent;
025: import java.awt.event.ActionListener;
026:
027: import javax.swing.BorderFactory;
028: import javax.swing.JButton;
029: import javax.swing.JPanel;
030: import javax.swing.JScrollPane;
031: import javax.swing.JTextArea;
032: import javax.swing.event.DocumentEvent;
033: import javax.swing.event.DocumentListener;
034:
035: import net.sourceforge.squirrel_sql.fw.sql.ISQLAlias;
036: import net.sourceforge.squirrel_sql.fw.util.StringManager;
037: import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
038:
039: import net.sourceforge.squirrel_sql.client.IApplication;
040:
041: public class ViewSessionScriptsPanel extends JPanel {
042: private static final StringManager s_stringMgr = StringManagerFactory
043: .getStringManager(ViewSessionScriptsPanel.class);
044:
045: private SessionScriptPlugin _plugin;
046: private IApplication _app;
047: private SQLALiasesCombo _aliasesCmb = new SQLALiasesCombo();
048: private JTextArea _sqlEntry = new JTextArea();
049: private JButton _saveBtn;
050:
051: ViewSessionScriptsPanel(SessionScriptPlugin plugin, IApplication app) {
052: super ();
053: if (plugin == null) {
054: throw new IllegalArgumentException(
055: "SessionScriptPlugin == null");
056: }
057: if (app == null) {
058: throw new IllegalArgumentException("IApplication == null");
059: }
060:
061: _plugin = plugin;
062: _app = app;
063:
064: createUserInterface();
065: refreshScript();
066: }
067:
068: private void refreshScript() {
069: boolean setText = false;
070: ISQLAlias alias = _aliasesCmb.getSelectedSQLAlias();
071: if (alias != null) {
072: AliasScript script = _plugin.getScriptsCache().get(alias);
073: String sql = script.getSQL();
074: if (sql != null && sql.length() > 0) {
075: _sqlEntry.setText(sql);
076: setText = true;
077: }
078: }
079: if (!setText) {
080: _sqlEntry.setText("");
081: }
082: _saveBtn.setEnabled(false);
083: }
084:
085: private void createUserInterface() {
086: setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
087:
088: _aliasesCmb.load(_app);
089: _aliasesCmb.addActionListener(new AliasesComboListener(this ));
090:
091: _sqlEntry
092: .setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
093: _sqlEntry.getDocument().addDocumentListener(
094: new DocumentListener() {
095: public void changedUpdate(DocumentEvent evt) {
096: _saveBtn.setEnabled(true);
097: }
098:
099: public void insertUpdate(DocumentEvent evt) {
100: _saveBtn.setEnabled(true);
101: }
102:
103: public void removeUpdate(DocumentEvent evt) {
104: _saveBtn.setEnabled(true);
105: }
106: });
107:
108: // i18n[sessionscript.Save=Save]
109: _saveBtn = new JButton(s_stringMgr
110: .getString("sessionscript.Save"));
111: _saveBtn.addActionListener(new ActionListener() {
112: public void actionPerformed(ActionEvent evt) {
113: synchronized (_sqlEntry) {
114: ISQLAlias alias = _aliasesCmb.getSelectedSQLAlias();
115: if (alias != null) {
116: AliasScript script = _plugin.getScriptsCache()
117: .get(alias);
118: script.setSQL(_sqlEntry.getText());
119: _saveBtn.setEnabled(false);
120: }
121: }
122: }
123: });
124:
125: setLayout(new GridBagLayout());
126: final GridBagConstraints gbc = new GridBagConstraints();
127: gbc.anchor = GridBagConstraints.NORTHWEST;
128: gbc.weightx = 1.0;
129: gbc.insets = new Insets(4, 4, 4, 4);
130:
131: gbc.gridx = 0;
132: gbc.gridy = 0;
133: gbc.fill = GridBagConstraints.HORIZONTAL;
134: add(_aliasesCmb, gbc);
135:
136: gbc.fill = GridBagConstraints.BOTH;
137: gbc.weighty = 1.0;
138: ++gbc.gridy;
139: add(new JScrollPane(_sqlEntry), gbc);
140:
141: gbc.fill = GridBagConstraints.HORIZONTAL;
142: gbc.weightx = 0;
143: gbc.weighty = 0;
144: ++gbc.gridx;
145: add(_saveBtn, gbc);
146: }
147:
148: /**
149: * THis listener keeps the script text area synched with the Aliases
150: * combobox. I.E. as a new alias is selected the script for that alias
151: * is displayed.
152: */
153: private static final class AliasesComboListener implements
154: ActionListener {
155: private ViewSessionScriptsPanel _pnl;
156:
157: AliasesComboListener(ViewSessionScriptsPanel pnl) {
158: super ();
159: _pnl = pnl;
160: }
161:
162: public void actionPerformed(ActionEvent evt) {
163: _pnl.refreshScript();
164: }
165:
166: }
167: }
|