View Javadoc

1   /*
2    * Copyright (c) 2005 The University of Reading
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    * 1. Redistributions of source code must retain the above copyright
9    *    notice, this list of conditions and the following disclaimer.
10   * 2. Redistributions in binary form must reproduce the above copyright
11   *    notice, this list of conditions and the following disclaimer in the
12   *    documentation and/or other materials provided with the distribution.
13   * 3. Neither the name of the University of Reading, nor the names of the
14   *    authors or contributors may be used to endorse or promote products
15   *    derived from this software without specific prior written permission.
16   * 
17   * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18   * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20   * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21   * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23   * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24   * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26   * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27   */
28  
29  package uk.ac.rdg.resc.jstyx.client.browser;
30  
31  import java.io.InputStream;
32  import java.io.InputStreamReader;
33  import java.io.BufferedReader;
34  import java.io.Reader;
35  import java.io.IOException;
36  
37  import uk.ac.rdg.resc.jstyx.client.CStyxFileInputStream;
38  import uk.ac.rdg.resc.jstyx.client.CStyxFileInputStreamReader;
39  import uk.ac.rdg.resc.jstyx.StyxUtils;
40  
41  /***
42   * Displays the text from a file in a widget
43   * @todo handle the window sizing properly
44   *
45   * @author Jon Blower
46   * $Revision: 377 $
47   * $Date: 2005-08-31 18:03:19 +0100 (Wed, 31 Aug 2005) $
48   * $Log$
49   * Revision 1.5  2005/08/31 17:03:18  jonblower
50   * Renamed "StyxFile*putStream*" to "CStyxFile*putStream*" for consistency with CStyxFile class
51   *
52   * Revision 1.4  2005/03/19 21:47:02  jonblower
53   * Further fixes relating to releasing ByteBuffers
54   *
55   * Revision 1.3  2005/02/18 17:52:40  jonblower
56   * Added client.browser package
57   *
58   */
59  public class TextDisplayer extends javax.swing.JFrame
60  {
61      
62      /*** Creates new form TextDisplayer */
63      public TextDisplayer(String title)
64      {
65          super(title);
66          initComponents();
67      }
68      
69      /*** This method is called from within the constructor to
70       * initialize the form.
71       * WARNING: Do NOT modify this code. The content of this method is
72       * always regenerated by the Form Editor.
73       */
74      private void initComponents()//GEN-BEGIN:initComponents
75      {
76          jScrollPane1 = new javax.swing.JScrollPane();
77          jTextPane1 = new javax.swing.JTextPane();
78  
79          setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
80          jScrollPane1.setViewportView(jTextPane1);
81  
82          getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER);
83  
84          pack();
85      }//GEN-END:initComponents
86      
87      /***
88       * Reads all the data from the input stream and displays in the text area
89       */
90      public void displayData (InputStream is) throws IOException
91      {
92          Reader reader;
93          if (is instanceof CStyxFileInputStream)
94          {
95              reader = new CStyxFileInputStreamReader((CStyxFileInputStream)is);
96          }
97          else
98          {
99              // Used for reading local files
100             reader = new InputStreamReader(is);
101         }
102         BufferedReader bufReader = new BufferedReader(reader);
103         //int i;
104         StringBuffer sBuf = new StringBuffer(this.jTextPane1.getText());
105         String s;
106         do
107         {
108             s = bufReader.readLine();
109             if (s != null)
110             {
111                 sBuf.append(s + StyxUtils.SYSTEM_NEWLINE);
112             }
113         } while (s != null);
114         
115         is.close();
116         this.jTextPane1.setText(sBuf.toString());
117         this.repaint();
118     }
119     
120     // Variables declaration - do not modify//GEN-BEGIN:variables
121     private javax.swing.JScrollPane jScrollPane1;
122     private javax.swing.JTextPane jTextPane1;
123     // End of variables declaration//GEN-END:variables
124     
125 }