Skip to content

Package: TextViewer

TextViewer

nameinstructionbranchcomplexitylinemethod
TextViewer()
M: 51 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 12 C: 0
0%
M: 1 C: 0
0%
setCommandContext(String, DataHandler)
M: 11 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
setInputStream(InputStream)
M: 33 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 11 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * Copyright (c) 1997, 2021 Oracle and/or its affiliates. All rights reserved.
3: *
4: * This program and the accompanying materials are made available under the
5: * terms of the Eclipse Distribution License v. 1.0, which is available at
6: * http://www.eclipse.org/org/documents/edl-v10.php.
7: *
8: * SPDX-License-Identifier: BSD-3-Clause
9: */
10:
11: import java.awt.*;
12: import java.io.*;
13: import java.beans.*;
14: import jakarta.activation.*;
15: import javax.swing.JPanel;
16: import javax.swing.JTextArea;
17: import javax.swing.JScrollPane;
18:
19:
20: /**
21: * A very simple TextViewer Bean for the MIMEType "text/plain"
22: *
23: * @author        Christopher Cotton
24: */
25:
26: public class TextViewer extends JPanel implements CommandObject
27: {
28:
29: private JTextArea text_area = null;
30: private DataHandler dh = null;
31: private String        verb = null;
32:
33: /**
34: * Constructor
35: */
36: public TextViewer() {
37:         super(new GridLayout(1,1));
38:
39:         // create the text area
40:         text_area = new JTextArea();
41:         text_area.setEditable(false);
42:         text_area.setLineWrap(true);
43:
44:         // create a scroll pane for the JTextArea
45:         JScrollPane sp = new JScrollPane();
46:         sp.setPreferredSize(new Dimension(300, 300));
47:         sp.getViewport().add(text_area);
48:         
49:         add(sp);
50: }
51:
52:
53: public void setCommandContext(String verb, DataHandler dh)
54:         throws IOException {
55:
56:         this.verb = verb;
57:         this.dh = dh;
58:         
59:         this.setInputStream( dh.getInputStream() );
60: }
61:
62:
63: /**
64: * set the data stream, component to assume it is ready to
65: * be read.
66: */
67: public void setInputStream(InputStream ins) {
68:
69: int bytes_read = 0;
70: // check that we can actually read
71: ByteArrayOutputStream baos = new ByteArrayOutputStream();
72: byte data[] = new byte[1024];
73:
74: try {
75:•         while((bytes_read = ins.read(data)) >0)
76:                  baos.write(data, 0, bytes_read);
77:          ins.close();
78: } catch(Exception e) {
79:          e.printStackTrace();
80: }
81:
82: // convert the buffer into a string
83: // place in the text area
84: text_area.setText(baos.toString());
85:
86: }
87: }