Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/eireneh/swing/TextViewPanel.java


1   
2   package com.eireneh.swing;
3   
4   import java.awt.*;
5   import java.awt.event.*;
6   import java.awt.datatransfer.*;
7   import java.beans.*;
8   import java.net.*;
9   import java.io.*;
10  import javax.swing.*;
11  
12  import com.eireneh.util.*;
13  
14  /**
15   * TextViewPanel allow viewing of some text in its own standalone frame.
16   * The text to be viewed can be grabbed from a String, a URL, or a file.
17   *
18   * <table border='1' cellPadding='3' cellSpacing='0' width="100%">
19   * <tr><td bgColor='white'class='TableRowColor'><font size='-7'>
20   * Distribution Licence:<br />
21   * Project B is free software; you can redistribute it
22   * and/or modify it under the terms of the GNU General Public License,
23   * version 2 as published by the Free Software Foundation.<br />
24   * This program is distributed in the hope that it will be useful,
25   * but WITHOUT ANY WARRANTY; without even the implied warranty of
26   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27   * General Public License for more details.<br />
28   * The License is available on the internet
29   * <a href='http://www.gnu.org/copyleft/gpl.html'>here</a>, by writing to
30   * <i>Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
31   * MA 02111-1307, USA</i>, Or locally at the Licence link below.<br />
32   * The copyright to this program is held by it's authors.
33   * </font></td></tr></table>
34   * @see <a href='http://www.eireneh.com/servlets/Web'>Project B Home</a>
35   * @see docs.Licence
36   * @author Joe Walker
37   */
38  public class TextViewPanel extends JPanel
39  {
40      /**
41       * Construct a TextViewPanel by calling jbInit()
42       */
43      public TextViewPanel()
44      {
45          jbInit();
46      }
47  
48      /**
49       * Construct a TextViewPanel with some string contents
50       * @param text The contents of the text area
51       */
52      public TextViewPanel(String text)
53      {
54          jbInit();
55          setText(text);
56      }
57  
58      /**
59       * Construct a TextViewPanel with a URL from which to read the text
60       * @param url A pointer to the contents of the text area
61       */
62      public TextViewPanel(URL url) throws IOException
63      {
64          jbInit();
65          setText(url);
66      }
67  
68      /**
69       * Construct a TextViewPanel with a File from which to read the text
70       * @param file A pointer to the contents of the text area
71       */
72      public TextViewPanel(File file) throws IOException
73      {
74          jbInit();
75          setText(file);
76      }
77  
78      /**
79       * Construct a TextViewPanel with some string contents
80       * @param text The contents of the text area
81       * @param header The string for the header area of the window
82       */
83      public TextViewPanel(String text, String header)
84      {
85          jbInit();
86          setText(text);
87          setHeader(header);
88      }
89  
90      /**
91       * Construct a TextViewPanel with a URL from which to read the text
92       * @param url A pointer to the contents of the text area
93       * @param header The string for the header area of the window
94       */
95      public TextViewPanel(URL url, String header) throws IOException
96      {
97          jbInit();
98          setText(url);
99          setHeader(header);
100     }
101 
102     /**
103      * Construct a TextViewPanel with a File from which to read the text
104      * @param file A pointer to the contents of the text area
105      * @param header The string for the header area of the window
106      */
107     public TextViewPanel(File file, String header) throws IOException
108     {
109         jbInit();
110         setText(file);
111         setHeader(header);
112     }
113 
114     /**
115      * Actually create the GUI
116      */
117     private void jbInit()
118     {
119         scr_text.getViewport().add(txt_text, null);
120         txt_text.setEditable(false);
121         txt_text.setColumns(80);
122         txt_text.setRows(24);
123 
124         btn_clipboard.addActionListener(new ActionListener() {
125             public void actionPerformed(ActionEvent ev)
126             {
127                 clipboard();
128             }
129         });
130         btn_clipboard.setMnemonic('C');
131         btn_clipboard.setText("Copy to Clipboard");
132 
133         lay_buttons.setAlignment(FlowLayout.RIGHT);
134         pnl_buttons.setLayout(lay_buttons);
135         pnl_buttons.add(btn_clipboard, null);
136 
137         this.setLayout(new BorderLayout());
138         this.add(scr_text, BorderLayout.CENTER);
139         this.add(pnl_buttons, BorderLayout.SOUTH);
140     }
141 
142     /**
143      * Display this Panel in a new JFrame
144      */
145     public void showInFrame(Frame parent)
146     {
147         frame = new JDialog(parent, "Text Viewer");
148 
149         btn_close = new JButton("Close");
150         btn_close.setMnemonic('L');
151         btn_close.addActionListener(new ActionListener() {
152             public void actionPerformed(ActionEvent ev)
153             {
154                 frame.setVisible(false);
155                 frame.dispose();
156             }
157         });
158         pnl_buttons.add(btn_close, null);
159 
160         this.setBorder(BorderFactory.createEmptyBorder(5, 5, 0, 5));
161 
162         frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
163         frame.getContentPane().setLayout(new BorderLayout());
164         frame.getContentPane().add(this, BorderLayout.CENTER);
165 
166         frame.pack();
167         GuiUtil.centerWindow(frame);
168         frame.setVisible(true);
169     }
170 
171     /**
172      * Copy the current text into the system clipboard
173      */
174     public void clipboard()
175     {
176         StringSelection ss = new StringSelection(getText());
177         Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);
178     }
179 
180     /**
181      * Setter for the text in the header area
182      * @param new_header The new header text
183      */
184     public void setHeader(String new_header)
185     {
186         String old_header = lbl_main.getText();
187         lbl_main.setText(new_header);
188 
189         if (new_header != null)
190             this.add(lbl_main, BorderLayout.NORTH);
191         else
192             this.remove(lbl_main);
193 
194         listeners.firePropertyChange("header", old_header, new_header);
195     }
196 
197     /**
198      * Getter for the text in the header area
199      * @return The current header
200      */
201     public String getHeader()
202     {
203         return lbl_main.getText();
204     }
205 
206     /**
207      * Setter for the main body of text.
208      * @param new_text The text to display
209      */
210     public void setText(String new_text)
211     {
212         String old_text = txt_text.getText();
213         txt_text.setText(new_text);
214         txt_text.setCaretPosition(0);
215 
216         if (frame != null)
217             GuiUtil.restrainedPack(frame);
218 
219         listeners.firePropertyChange("text", old_text, new_text);
220     }
221 
222     /**
223      * Setter for the main body of text
224      * @param url A pointer to the text to display
225      */
226     public void setText(URL url) throws IOException
227     {
228         setText(url.openStream());
229     }
230 
231     /**
232      * Setter for the main body of text
233      * @param file A pointer to the text to display
234      */
235     public void setText(File file) throws IOException
236     {
237         setText(new FileInputStream(file));
238     }
239 
240     /**
241      * Setter for the main body of text
242      * @param file A pointer to the text to display
243      */
244     public void setText(final InputStream in) throws IOException
245     {
246         // Yes this is twisted, however there is some kind of perverse
247         // pleasure in writing this kind of code.
248         // But for the setPriority() I might have dispensed with the
249         // "Thread work = " bit and just tacked ".start()" to the end
250         // This simply creates a thread to read the file, and then a
251         // Runnable to update the GUI (swing is single threaded)
252         Thread work = new Thread(new Runnable() {
253             public void run()
254             {
255                 try
256                 {
257                     InputStream pmin = new ProgressMonitorInputStream(TextViewPanel.this,
258                                                                       "Loading text ...",in);
259                     Reader rin = new InputStreamReader(pmin);
260                     final String data = StringUtil.read(rin);
261 
262                     SwingUtilities.invokeLater(new Runnable() {
263                         public void run() { setText(data); }
264                     });
265                 }
266                 catch (IOException ex)
267                 {
268                     Reporter.informUser(TextViewPanel.this, ex);
269                 }
270             }
271         });
272 
273         work.start();
274         work.setPriority(Thread.MIN_PRIORITY);
275     }
276 
277     /**
278      * Getter for the main body of text
279      * @return The string from the main text area
280      */
281     public String getText()
282     {
283         return txt_text.getText();
284     }
285 
286     /**
287      * Add a property change listener
288      * @param li The property change listener to add
289      */
290     public synchronized void removePropertyChangeListener(PropertyChangeListener li)
291     {
292         super.removePropertyChangeListener(li);
293         listeners.removePropertyChangeListener(li);
294     }
295 
296     /**
297      * Remove a property change listener
298      * @param li The property change listener to remove
299      */
300     public synchronized void addPropertyChangeListener(PropertyChangeListener li)
301     {
302         super.addPropertyChangeListener(li);
303         listeners.addPropertyChangeListener(li);
304     }
305 
306     /** Optional header label */
307     private JLabel lbl_main = new JLabel();
308 
309     /** Scroller for the text area */
310     private JScrollPane scr_text = new JScrollPane();
311 
312     /** The main text area */
313     private JTextArea txt_text = new JTextArea();
314 
315     /** The button bar */
316     private JPanel pnl_buttons = new JPanel();
317 
318     /** Button bar layout */
319     private FlowLayout lay_buttons = new FlowLayout();
320 
321     /** Copy text to clipboard button */
322     private JButton btn_clipboard = new JButton();
323 
324     /** Close button */
325     private JButton btn_close = null;
326 
327     /** The frame that we are displayed in */
328     private JDialog frame = null;
329 
330     /** Property change listener collection */
331     private transient PropertyChangeSupport listeners = new PropertyChangeSupport(this);
332 }