001: /*
002: * PanelContentSender.java
003: *
004: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
005: *
006: * Copyright 2002-2008, Thomas Kellerer
007: * No part of this code maybe reused without the permission of the author
008: *
009: * To contact the author please send an email to: support@sql-workbench.net
010: *
011: */
012: package workbench.gui.sql;
013:
014: import java.awt.EventQueue;
015: import workbench.gui.MainWindow;
016: import workbench.interfaces.ResultReceiver;
017: import workbench.util.WbThread;
018:
019: /**
020: * This class sends a SQL statement to one of the
021: * panels in the MainWindow
022: *
023: * @author support@sql-workbench.net
024: */
025: public class PanelContentSender {
026: public static final int NEW_PANEL = -1;
027:
028: protected MainWindow target;
029:
030: public PanelContentSender(MainWindow window) {
031: this .target = window;
032: }
033:
034: public void showResult(final String sql, final String comment,
035: final int panelIndex, final boolean logText) {
036: if (sql == null)
037: return;
038:
039: // When adding a new panel, a new connection
040: // might be initiated automatically. As that is done in a separate
041: // thread, the call to showResult() might occur
042: // before the connection is actually established.
043: // So we need to wait until the new panel is connected
044: // that's what waitForConnection() is for.
045: // As this code might be execute on the EDT we have to make sure
046: // we are not blocking the current thread, so a new thread
047: // is created that will wait for the connection to succeed.
048: // then the actual showing of the data can be executed (on the EDT)
049: WbThread t = new WbThread("ShowThread") {
050: public void run() {
051: final boolean isCurrent = (target
052: .getCurrentPanelIndex() == panelIndex);
053: final SqlPanel panel = selectPanel(panelIndex);
054: target.waitForConnection();
055:
056: EventQueue.invokeLater(new Runnable() {
057: public void run() {
058: if (panel != null) {
059: target.requestFocus();
060: panel.selectEditor();
061: ResultReceiver.ShowType type;
062: if (panelIndex == NEW_PANEL) {
063: type = ResultReceiver.ShowType.replaceText;
064: } else if (panel.hasFileLoaded()) {
065: type = ResultReceiver.ShowType.logText;
066: } else {
067: type = (logText ? ResultReceiver.ShowType.logText
068: : ResultReceiver.ShowType.appendText);
069: }
070: panel.showResult(sql, comment, type);
071: }
072: }
073: });
074: }
075: };
076: t.start();
077: }
078:
079: public void sendContent(final String text, final int panelIndex,
080: final boolean appendText) {
081: if (text == null)
082: return;
083:
084: final SqlPanel panel = selectPanel(panelIndex);
085: if (panel == null)
086: return;
087:
088: EventQueue.invokeLater(new Runnable() {
089: public void run() {
090: if (appendText) {
091: panel.appendStatementText(text);
092: } else {
093: panel.setStatementText(text);
094: }
095: target.requestFocus();
096: panel.selectEditor();
097: }
098: });
099: }
100:
101: private SqlPanel selectPanel(int index) {
102: SqlPanel panel;
103:
104: if (index == NEW_PANEL) {
105: panel = (SqlPanel) this .target.addTab(true, true);
106: } else {
107: panel = (SqlPanel) this.target.getSqlPanel(index);
108: target.selectTab(index);
109: }
110: return panel;
111: }
112: }
|