KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > TextViewer


1 /*
2  * @(#)TextViewer.java 1.12 01/05/23
3  *
4  * Copyright 1997-2000 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * - Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * - Redistribution in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the distribution.
16  *
17  * Neither the name of Sun Microsystems, Inc. or the names of contributors
18  * may be used to endorse or promote products derived from this software
19  * without specific prior written permission.
20  *
21  * This software is provided "AS IS," without a warranty of any kind. ALL
22  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
23  * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
24  * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND
25  * ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES OR LIABILITIES
26  * SUFFERED BY LICENSEE AS A RESULT OF OR RELATING TO USE, MODIFICATION
27  * OR DISTRIBUTION OF THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL
28  * SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
29  * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE
30  * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
31  * ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS
32  * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
33  *
34  * You acknowledge that Software is not designed, licensed or intended
35  * for use in the design, construction, operation or maintenance of any
36  * nuclear facility.
37  */

38
39 import java.awt.*;
40 import java.io.*;
41 import java.beans.*;
42 import javax.activation.*;
43 import javax.swing.JPanel JavaDoc;
44 import javax.swing.JTextArea JavaDoc;
45 import javax.swing.JScrollPane JavaDoc;
46
47
48 /**
49  * A very simple TextViewer Bean for the MIMEType "text/plain"
50  *
51  * @version 1.12, 01/05/23
52  * @author Christopher Cotton
53  */

54
55 public class TextViewer extends JPanel JavaDoc implements CommandObject
56 {
57
58     private JTextArea JavaDoc text_area = null;
59     private DataHandler dh = null;
60     private String JavaDoc verb = null;
61
62     /**
63      * Constructor
64      */

65     public TextViewer() {
66     super(new GridLayout(1,1));
67
68     // create the text area
69
text_area = new JTextArea JavaDoc();
70     text_area.setEditable(false);
71     text_area.setLineWrap(true);
72
73     // create a scroll pane for the JTextArea
74
JScrollPane JavaDoc sp = new JScrollPane JavaDoc();
75     sp.setPreferredSize(new Dimension(300, 300));
76     sp.getViewport().add(text_area);
77     
78     add(sp);
79     }
80
81
82     public void setCommandContext(String JavaDoc verb, DataHandler dh)
83     throws IOException {
84
85     this.verb = verb;
86     this.dh = dh;
87     
88     this.setInputStream( dh.getInputStream() );
89     }
90
91
92   /**
93    * set the data stream, component to assume it is ready to
94    * be read.
95    */

96   public void setInputStream(InputStream ins) {
97       
98       int bytes_read = 0;
99       // check that we can actually read
100
ByteArrayOutputStream baos = new ByteArrayOutputStream();
101       byte data[] = new byte[1024];
102       
103       try {
104       while((bytes_read = ins.read(data)) >0)
105           baos.write(data, 0, bytes_read);
106       ins.close();
107       } catch(Exception JavaDoc e) {
108       e.printStackTrace();
109       }
110
111       // convert the buffer into a string
112
// place in the text area
113
text_area.setText(baos.toString());
114
115     }
116 }
117
Popular Tags