001: /*
002: * Enhydra Java Application Server Project
003: *
004: * The contents of this file are subject to the Enhydra Public License
005: * Version 1.1 (the "License"); you may not use this file except in
006: * compliance with the License. You may obtain a copy of the License on
007: * the Enhydra web site ( http://www.enhydra.org/ ).
008: *
009: * Software distributed under the License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
011: * the License for the specific terms governing rights and limitations
012: * under the License.
013: *
014: * The Initial Developer of the Enhydra Application Server is Lutris
015: * Technologies, Inc. The Enhydra Application Server and portions created
016: * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
017: * All Rights Reserved.
018: *
019: * Contributor(s):
020: * Paul Mahar
021: *
022: */
023: package org.enhydra.kelp.common.swing;
024:
025: // Kelp imports
026: import org.enhydra.kelp.common.Constants;
027: import org.enhydra.kelp.common.event.WriteEvent;
028: import org.enhydra.kelp.common.event.WriteListener;
029: import org.enhydra.kelp.common.node.OtterProject;
030:
031: // ToolBox imports
032: import org.enhydra.tool.common.PathHandle;
033:
034: // Standard imports
035: import javax.swing.*;
036: import javax.swing.text.BadLocationException;
037: import java.awt.*;
038: import java.awt.event.*;
039: import java.beans.Beans;
040: import java.io.File;
041: import java.io.FileWriter;
042: import java.lang.ref.WeakReference;
043: import java.util.ResourceBundle;
044:
045: //
046: public class OutputPanel extends JPanel implements WriteListener {
047:
048: //
049: public static ResourceBundle res = ResourceBundle
050: .getBundle("org.enhydra.kelp.common.Res"); // nores
051: private JScrollPane scrollOutput;
052: private JTextArea textAreaOutput;
053: private BorderLayout layoutMain;
054: private WeakReference projectRef;
055: private int waitCount = 0;
056:
057: /**
058: * Constructor declaration
059: *
060: */
061: public OutputPanel() {
062: try {
063: jbInit();
064: } catch (Exception e) {
065: e.printStackTrace();
066: }
067: }
068:
069: public void clearAll() {
070: clearOutput();
071: if (projectRef != null) {
072: projectRef.clear();
073: projectRef = null;
074: }
075: removeAll();
076: }
077:
078: /**
079: * Method declaration
080: *
081: *
082: * @param event
083: */
084: public void onWrite(WriteEvent event) {
085: String out = event.getString();
086:
087: if (event.getType() == WriteEvent.CLEAR) {
088: clearOutput();
089: } else {
090: if (File.separatorChar == '\\') {
091:
092: // string not to be resourced
093: if (out.indexOf(":\\") > 0) {
094: out = out.replace('\\', '/');
095: }
096: }
097: addOutput(out);
098: }
099: }
100:
101: public void clearOutput() {
102: OutputClear clearRun = null;
103:
104: clearRun = new OutputClear(textAreaOutput, scrollOutput);
105: try {
106: if (SwingUtilities.isEventDispatchThread()) {
107: clearRun.run();
108: } else {
109: SwingUtilities.invokeAndWait(clearRun);
110: }
111: } catch (Exception e) {
112: e.printStackTrace();
113: }
114: }
115:
116: public void scrollToBottom() {
117: OutputScroll runSwing = null;
118:
119: runSwing = new OutputScroll(scrollOutput);
120: try {
121: if (SwingUtilities.isEventDispatchThread()) {
122: runSwing.run();
123: } else {
124: SwingUtilities.invokeAndWait(runSwing);
125: }
126: } catch (Exception e) {
127: e.printStackTrace();
128: }
129: try {
130: Thread.sleep(500);
131: } catch (InterruptedException e) {
132:
133: //
134: }
135: }
136:
137: public OtterProject getProject() {
138: OtterProject project = null;
139:
140: if (projectRef != null) {
141: project = (OtterProject) projectRef.get();
142: }
143: return project;
144: }
145:
146: public void setProject(OtterProject project) {
147: projectRef = new WeakReference(project);
148: }
149:
150: public int getRows() {
151: return textAreaOutput.getRows();
152: }
153:
154: public void setRows(int i) {
155: textAreaOutput.setRows(i);
156: }
157:
158: //
159: // PRIVATE
160: //
161: private void addOutput(final String output) {
162: OutputAdd adder = null;
163:
164: adder = new OutputAdd(textAreaOutput, scrollOutput, output);
165: try {
166: if (SwingUtilities.isEventDispatchThread()) {
167: adder.run();
168: } else {
169: waitCount++;
170: if (waitCount == 10) {
171: waitCount = 0;
172: SwingUtilities.invokeAndWait(adder);
173: } else {
174: SwingUtilities.invokeLater(adder);
175: }
176: }
177: } catch (Exception e) {
178: e.printStackTrace(System.err);
179: }
180: projectLog(output);
181: }
182:
183: private void projectLog(final String output) {
184: PathHandle log = null;
185:
186: if (getProject() != null) {
187: log = PathHandle.createPathHandle(getProject()
188: .getOutputFilename());
189: if (log.hasExtension("txt") && log.getFile().canWrite()) {
190: try {
191: FileWriter writer = null;
192:
193: writer = new FileWriter(log.getPath(), true);
194: writer.write(output); // line + new Character((char)13) + new Character((char)10));
195: writer.close();
196: } catch (java.io.IOException e) {
197: e.printStackTrace(System.err);
198: }
199: }
200: }
201: }
202:
203: /**
204: * Method declaration
205: *
206: *
207: * @throws Exception
208: */
209: private void jbInit() throws Exception {
210: textAreaOutput = (JTextArea) Beans.instantiate(getClass()
211: .getClassLoader(), JTextArea.class.getName());
212: textAreaOutput.setEditable(false);
213: textAreaOutput.setRows(15);
214: scrollOutput = new JScrollPane(textAreaOutput);
215: layoutMain = (BorderLayout) Beans.instantiate(getClass()
216: .getClassLoader(), BorderLayout.class.getName());
217: this .setLayout(layoutMain);
218: this .add(scrollOutput, BorderLayout.CENTER);
219: }
220:
221: //
222: private class OutputAdd implements Runnable {
223: private JTextArea textArea = null;
224: private JScrollPane scroll = null;
225: private String out = null;
226:
227: protected OutputAdd(JTextArea ta, JScrollPane sc, String o) {
228: textArea = ta;
229: scroll = sc;
230: out = o;
231: }
232:
233: synchronized public void run() {
234: int start = 0;
235: int end = 0;
236:
237: if (textArea.getLineCount() > 2000) {
238: try {
239: start = textArea.getLineStartOffset(0);
240: end = textArea.getLineEndOffset(0);
241: } catch (BadLocationException e) {
242: start = 0;
243: end = 0;
244: }
245: textArea.replaceRange(null, start, end);
246: }
247: textArea.append(out);
248: scroll.getVerticalScrollBar().setValue(
249: scroll.getVerticalScrollBar().getMaximum());
250: textArea.repaint();
251: repaint();
252: }
253:
254: }
255:
256: //
257: private class OutputClear implements Runnable {
258: private JTextArea textArea = null;
259: private JScrollPane scroll = null;
260:
261: protected OutputClear(JTextArea ta, JScrollPane sc) {
262: textArea = ta;
263: scroll = sc;
264: }
265:
266: synchronized public void run() {
267: textArea.setText(new String());
268: scroll.getVerticalScrollBar().setValue(
269: scroll.getVerticalScrollBar().getMaximum());
270: textArea.repaint();
271: repaint();
272: }
273:
274: }
275:
276: //
277: private class OutputScroll implements Runnable {
278: private JScrollPane scroll = null;
279:
280: protected OutputScroll(JScrollPane sc) {
281: scroll = sc;
282: }
283:
284: synchronized public void run() {
285: scroll.getVerticalScrollBar().setValue(
286: scroll.getVerticalScrollBar().getMaximum());
287: }
288:
289: }
290: }
|