KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > activation > viewers > TextViewer


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21
22 /*
23  * @(#)TextViewer.java 1.9 05/11/16
24  *
25  * Copyright 1997-2005 Sun Microsystems, Inc. All Rights Reserved.
26  */

27
28 package com.sun.activation.viewers;
29
30 import java.awt.*;
31 import java.io.*;
32 import java.beans.*;
33 import javax.activation.*;
34
35 public class TextViewer extends Panel implements CommandObject {
36     // UI Vars...
37
private TextArea text_area = null;
38     
39     // File Vars
40
private File text_file = null;
41     private String JavaDoc text_buffer = null;
42     
43     private DataHandler _dh = null;
44     private boolean DEBUG = false;
45     /**
46      * Constructor
47      */

48     public TextViewer() {
49     setLayout( new GridLayout(1,1));
50     // create the text area
51
text_area = new TextArea("", 24, 80,
52                  TextArea.SCROLLBARS_VERTICAL_ONLY );
53     text_area.setEditable( false );
54     
55     add(text_area);
56     }
57     
58     //--------------------------------------------------------------------
59
public void setCommandContext(String JavaDoc verb, DataHandler dh) throws IOException {
60     _dh = dh;
61     this.setInputStream( _dh.getInputStream() );
62     }
63   //--------------------------------------------------------------------
64

65   /**
66    * set the data stream, component to assume it is ready to
67    * be read.
68    */

69   public void setInputStream(InputStream ins) throws IOException {
70       
71       int bytes_read = 0;
72       // check that we can actually read
73
ByteArrayOutputStream baos = new ByteArrayOutputStream();
74       byte data[] = new byte[1024];
75       
76       while((bytes_read = ins.read(data)) >0)
77       baos.write(data, 0, bytes_read);
78       
79       ins.close();
80
81       // convert the buffer into a string
82
// popuplate the buffer
83
text_buffer = baos.toString();
84
85       // place in the text area
86
text_area.setText(text_buffer);
87
88     }
89   //--------------------------------------------------------------------
90
public void addNotify() {
91     super.addNotify();
92     invalidate();
93     }
94   //--------------------------------------------------------------------
95
public Dimension getPreferredSize() {
96     return text_area.getMinimumSize(24, 80);
97     }
98
99 }
100
101
102
103
104
105
106
Popular Tags