001: package net.sourceforge.squirrel_sql.plugins.userscript.kernel;
002:
003: import net.sourceforge.squirrel_sql.client.session.ISQLPanelAPI;
004: import net.sourceforge.squirrel_sql.fw.gui.GUIUtils;
005: import net.sourceforge.squirrel_sql.fw.util.StringManager;
006: import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
007:
008: import javax.swing.*;
009: import java.io.PrintStream;
010: import java.io.ByteArrayOutputStream;
011: import java.util.Vector;
012: import java.awt.*;
013:
014: public class ScriptEnvironment {
015: private static final StringManager s_stringMgr = StringManagerFactory
016: .getStringManager(ScriptEnvironment.class);
017:
018: private ISQLPanelAPI m_sqlPanelApi;
019: private JFrame m_ownerFrame;
020:
021: private JDialog m_dlg;
022: private JTabbedPane m_tabbedPane;
023: private JLabel m_lblStatus;
024:
025: private Vector<PrintStream> m_printStreams = new Vector<PrintStream>();
026: private int createdPrintStreamsCount = 0;
027:
028: ScriptEnvironment(ISQLPanelAPI sqlPanelApi, JFrame ownerFrame) {
029: m_sqlPanelApi = sqlPanelApi;
030: m_ownerFrame = ownerFrame;
031:
032: // i18n[userscript.execOutput=Script execution output]
033: m_dlg = new JDialog(m_ownerFrame, s_stringMgr
034: .getString("userscript.execOutput"), false);
035: m_dlg.getContentPane().setLayout(new BorderLayout());
036: m_tabbedPane = new JTabbedPane();
037: // i18n[userscript.executing=Executing Script...]
038: m_lblStatus = new JLabel(s_stringMgr
039: .getString("userscript.executing"));
040:
041: m_dlg.getContentPane().add(m_tabbedPane, BorderLayout.CENTER);
042: m_dlg.getContentPane().add(m_lblStatus, BorderLayout.SOUTH);
043:
044: GUIUtils.centerWithinParent(m_dlg);
045:
046: m_dlg.setSize(400, 400);
047: }
048:
049: public PrintStream createPrintStream() {
050: return createPrintStream(null);
051: }
052:
053: public PrintStream createPrintStream(String tabTitle) {
054: final ByteArrayOutputStream bos = new ByteArrayOutputStream();
055: final JTextArea txtOut = new JTextArea();
056:
057: if (null == tabTitle) {
058: m_tabbedPane.addTab("<" + (++createdPrintStreamsCount)
059: + ">", txtOut);
060: } else {
061: m_tabbedPane.addTab(tabTitle, new JScrollPane(txtOut));
062: }
063:
064: PrintStream ret = new PrintStream(bos) {
065: public void flush() {
066: super .flush();
067: onFlush(bos, txtOut);
068: }
069: };
070:
071: // Dialog is shown only when it is written to.
072: m_dlg.setVisible(true);
073:
074: m_printStreams.add(ret);
075:
076: return ret;
077: }
078:
079: private void onFlush(ByteArrayOutputStream bos, JTextArea txtOut) {
080: txtOut.append(bos.toString());
081: bos.reset();
082: }
083:
084: public PrintStream getSQLAreaPrintStream() {
085: final ByteArrayOutputStream bos = new ByteArrayOutputStream();
086: PrintStream ret = new PrintStream(bos) {
087: public void flush() {
088: super .flush();
089: onFlushToSqlArea(bos);
090: }
091: };
092: m_printStreams.add(ret);
093: return ret;
094: }
095:
096: private void onFlushToSqlArea(ByteArrayOutputStream bos) {
097: m_sqlPanelApi.appendSQLScript(bos.toString());
098: bos.reset();
099: }
100:
101: void flushAll() {
102: for (int i = 0; i < m_printStreams.size(); i++) {
103: PrintStream printStream = m_printStreams.elementAt(i);
104: printStream.flush();
105: }
106: }
107:
108: void setExecutionFinished(boolean successful) {
109: if (successful) {
110: // i18n[userscript.scriptCompleted=Script completed]
111: m_lblStatus.setText(s_stringMgr
112: .getString("userscript.scriptCompleted"));
113: } else {
114: // i18n[userscript.scriptCompletedErr=Script completed with errors]
115: m_lblStatus.setText(s_stringMgr
116: .getString("userscript.scriptCompletedErr"));
117: }
118: }
119: }
|