KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > kelp > common > swing > OutputPanel


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  *
19  * Contributor(s):
20  * Paul Mahar
21  *
22  */

23 package org.enhydra.kelp.common.swing;
24
25 // Kelp imports
26
import org.enhydra.kelp.common.Constants;
27 import org.enhydra.kelp.common.event.WriteEvent;
28 import org.enhydra.kelp.common.event.WriteListener;
29 import org.enhydra.kelp.common.node.OtterProject;
30
31 // ToolBox imports
32
import org.enhydra.tool.common.PathHandle;
33
34 // Standard imports
35
import javax.swing.*;
36 import javax.swing.text.BadLocationException JavaDoc;
37 import java.awt.*;
38 import java.awt.event.*;
39 import java.beans.Beans JavaDoc;
40 import java.io.File JavaDoc;
41 import java.io.FileWriter JavaDoc;
42 import java.lang.ref.WeakReference JavaDoc;
43 import java.util.ResourceBundle JavaDoc;
44
45 //
46
public class OutputPanel extends JPanel implements WriteListener {
47
48     //
49
public static ResourceBundle JavaDoc res =
50         ResourceBundle.getBundle("org.enhydra.kelp.common.Res"); // nores
51
private JScrollPane scrollOutput;
52     private JTextArea textAreaOutput;
53     private BorderLayout layoutMain;
54     private WeakReference JavaDoc projectRef;
55     private int waitCount = 0;
56
57     /**
58      * Constructor declaration
59      *
60      */

61     public OutputPanel() {
62         try {
63             jbInit();
64         } catch (Exception JavaDoc e) {
65             e.printStackTrace();
66         }
67     }
68
69     public void clearAll() {
70         clearOutput();
71         if (projectRef != null) {
72             projectRef.clear();
73             projectRef = null;
74         }
75         removeAll();
76     }
77
78     /**
79      * Method declaration
80      *
81      *
82      * @param event
83      */

84     public void onWrite(WriteEvent event) {
85         String JavaDoc out = event.getString();
86
87         if (event.getType() == WriteEvent.CLEAR) {
88             clearOutput();
89         } else {
90             if (File.separatorChar == '\\') {
91
92                 // string not to be resourced
93
if (out.indexOf(":\\") > 0) {
94                     out = out.replace('\\', '/');
95                 }
96             }
97             addOutput(out);
98         }
99     }
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 JavaDoc 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 JavaDoc e) {
127             e.printStackTrace();
128         }
129         try {
130             Thread.sleep(500);
131         } catch (InterruptedException JavaDoc 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 JavaDoc(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 JavaDoc 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 JavaDoc e) {
178             e.printStackTrace(System.err);
179         }
180         projectLog(output);
181     }
182
183     private void projectLog(final String JavaDoc output) {
184         PathHandle log = null;
185
186         if (getProject() != null) {
187             log =
188                 PathHandle.createPathHandle(getProject().getOutputFilename());
189             if (log.hasExtension("txt") && log.getFile().canWrite()) {
190                 try {
191                     FileWriter JavaDoc writer = null;
192
193                     writer = new FileWriter JavaDoc(log.getPath(), true);
194                     writer.write(output); // line + new Character((char)13) + new Character((char)10));
195
writer.close();
196                 } catch (java.io.IOException JavaDoc 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 JavaDoc {
210         textAreaOutput =
211             (JTextArea) Beans.instantiate(getClass().getClassLoader(),
212                                           JTextArea.class.getName());
213         textAreaOutput.setEditable(false);
214         textAreaOutput.setRows(15);
215         scrollOutput = new JScrollPane(textAreaOutput);
216         layoutMain =
217             (BorderLayout) Beans.instantiate(getClass().getClassLoader(),
218                                              BorderLayout.class.getName());
219         this.setLayout(layoutMain);
220         this.add(scrollOutput, BorderLayout.CENTER);
221     }
222
223     //
224
private class OutputAdd implements Runnable JavaDoc {
225         private JTextArea textArea = null;
226         private JScrollPane scroll = null;
227         private String JavaDoc out = null;
228
229         protected OutputAdd(JTextArea ta, JScrollPane sc, String JavaDoc o) {
230             textArea = ta;
231             scroll = sc;
232             out = o;
233         }
234
235         synchronized public void run() {
236             int start = 0;
237             int end = 0;
238
239             if (textArea.getLineCount() > 2000) {
240                 try {
241                     start = textArea.getLineStartOffset(0);
242                     end = textArea.getLineEndOffset(0);
243                 } catch (BadLocationException JavaDoc e) {
244                     start = 0;
245                     end = 0;
246                 }
247                 textArea.replaceRange(null, start, end);
248             }
249             textArea.append(out);
250             scroll.getVerticalScrollBar().setValue(scroll.getVerticalScrollBar().getMaximum());
251             textArea.repaint();
252             repaint();
253         }
254
255     }
256
257     //
258
private class OutputClear implements Runnable JavaDoc {
259         private JTextArea textArea = null;
260         private JScrollPane scroll = null;
261
262         protected OutputClear(JTextArea ta, JScrollPane sc) {
263             textArea = ta;
264             scroll = sc;
265         }
266
267         synchronized public void run() {
268             textArea.setText(new String JavaDoc());
269             scroll.getVerticalScrollBar().setValue(scroll.getVerticalScrollBar().getMaximum());
270             textArea.repaint();
271             repaint();
272         }
273
274     }
275
276     //
277
private class OutputScroll implements Runnable JavaDoc {
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(scroll.getVerticalScrollBar().getMaximum());
286         }
287
288     }
289 }
290
Popular Tags