001: package net.sourceforge.squirrel_sql.plugins.oracle.dboutput;
002:
003: /*
004: * Copyright (C) 2004 Jason Height
005: * jmheight@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:
022: import java.awt.BorderLayout;
023: import java.sql.SQLException;
024: import java.sql.CallableStatement;
025: import java.util.Timer;
026: import java.util.TimerTask;
027:
028: import javax.swing.JPanel;
029: import javax.swing.JScrollPane;
030: import javax.swing.JTextArea;
031: import javax.swing.SwingUtilities;
032: import javax.swing.text.BadLocationException;
033: import javax.swing.text.Document;
034:
035: import net.sourceforge.squirrel_sql.fw.util.log.ILogger;
036: import net.sourceforge.squirrel_sql.fw.util.log.LoggerController;
037:
038: import net.sourceforge.squirrel_sql.client.IApplication;
039: import net.sourceforge.squirrel_sql.client.session.ISession;
040:
041: public class DBOutputPanel extends JPanel {
042: /**
043: * Logger for this class.
044: */
045: private static final ILogger s_log = LoggerController
046: .createLogger(DBOutputPanel.class);
047:
048: /**
049: * Current session.
050: */
051: private ISession _session;
052:
053: private JTextArea _textArea;
054: private Timer _refreshTimer = new Timer(true);
055:
056: private boolean _autoRefresh = false;
057: private int _refreshPeriod = 10;
058:
059: public class RefreshTimerTask extends TimerTask {
060: public void run() {
061: populateDBOutput();
062: }
063: }
064:
065: /**
066: * Ctor.
067: *
068: * @param autoRefeshPeriod
069: * @param session Current session.
070: * @throws IllegalArgumentException Thrown if a <TT>null</TT> <TT>ISession</TT> passed.
071: */
072: public DBOutputPanel(ISession session, int autoRefeshPeriod) {
073: super ();
074: _session = session;
075: _refreshPeriod = autoRefeshPeriod;
076: createGUI();
077: initDBOutput();
078: }
079:
080: protected void initDBOutput() {
081: try {
082: CallableStatement c = _session.getSQLConnection()
083: .getConnection().prepareCall(
084: "{call dbms_output.enable()}");
085: c.execute();
086: } catch (SQLException ex) {
087: _session.showErrorMessage(ex);
088: }
089: }
090:
091: /**
092: * Current session.
093: */
094: public ISession getSession() {
095: return _session;
096: }
097:
098: private void resetTimer() {
099: if (_refreshTimer != null) {
100: _refreshTimer.cancel();
101: //Nil out the timer so that it can be gc'd
102: _refreshTimer = null;
103: }
104: if (_autoRefresh && (_refreshPeriod > 0)) {
105: _refreshTimer = new Timer(true);
106: _refreshTimer.scheduleAtFixedRate(new RefreshTimerTask(),
107: _refreshPeriod * 1000, _refreshPeriod * 1000);
108: }
109: }
110:
111: public void setAutoRefresh(boolean enable) {
112: if (enable != _autoRefresh) {
113: _autoRefresh = enable;
114: resetTimer();
115: }
116: }
117:
118: public boolean getAutoRefesh() {
119: return _autoRefresh;
120: }
121:
122: public void setAutoRefreshPeriod(int seconds) {
123: if (_refreshPeriod != seconds) {
124: _refreshPeriod = seconds;
125: resetTimer();
126: }
127: }
128:
129: public int getAutoRefreshPeriod() {
130: return _refreshPeriod;
131: }
132:
133: public void clearOutput() {
134: Document doc = _textArea.getDocument();
135: int length = doc.getLength();
136: try {
137: doc.remove(0, length);
138: } catch (BadLocationException ex) {
139: //Silently ignore, what could we do anyway?
140: }
141: }
142:
143: public synchronized void populateDBOutput() {
144: try {
145: final StringBuffer buf = new StringBuffer();
146: //In an effort to avoid non JDBC standard out parameter types ie
147: //oracle specific ones, the dbms_output.get_line is used rather than
148: //the dbms_output.getlines. The disadvantage is there are more trips
149: //to the server to return multiple lines.
150: CallableStatement c = _session.getSQLConnection()
151: .getConnection().prepareCall(
152: "{call dbms_output.get_line(?, ?)}");
153: c.registerOutParameter(1, java.sql.Types.VARCHAR);
154: c.registerOutParameter(2, java.sql.Types.INTEGER);
155: //Get 10 lines at a time.
156: int status = 0;
157: while (status == 0) {
158: c.execute();
159: status = c.getInt(2);
160: if (status == 0) {
161: String str = c.getString(1);
162: if (str != null)
163: buf.append(str);
164: buf.append("\n");
165: }
166: }
167: c.close();
168: if (buf.length() > 0) {
169: final JTextArea store = _textArea;
170: SwingUtilities.invokeLater(new Runnable() {
171: public void run() {
172: store.append(buf.toString());
173: }
174: });
175: }
176: } catch (SQLException ex) {
177: _session.showErrorMessage(ex);
178: }
179: }
180:
181: private void createGUI() {
182: final IApplication app = _session.getApplication();
183: setLayout(new BorderLayout());
184: _textArea = new JTextArea();
185: _textArea.setEditable(false);
186: add(new JScrollPane(_textArea,
187: JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
188: JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED));
189: }
190:
191: }
|