View Javadoc

1   /*
2    * Fsgrep is a simple Java application which allows a user to
3    * search all files in a directory structure for lines matching
4    * a given pattern.  Its functionality is a combination of the
5    * Unix 'find' and 'grep' utilities.
6    * Visit [http://fsgrep.sourceforge.net/] for more information.
7    * 
8    * Copyright (C) 2003-2006 Murali Krishnan [murali_ca_us@users.sourceforge.net]
9    * 
10   * Fsgrep is free software; you can redistribute it and/or modify
11   * it under the terms of version 2 of the GNU General Public
12   * License as published by the Free Software Foundation.
13   * 
14   * Fsgrep is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Public License for more details.
18   * 
19   * You should have received a copy of the GNU General Public License
20   * along with Fsgrep (see the file named LICENSE.txt); if not, write
21   * to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
22   * Boston, MA  02111-1307  USA
23   */
24  
25  package mk.fsgrep.gui;
26  
27  
28  import java.awt.*;
29  
30  import javax.swing.*;
31  
32  import mk.fsgrep.util.Formatable;
33  import mk.fsgrep.util.output.OutputDestination;
34  
35  
36  /***
37   * 
38   * 
39   * @author  Murali Krishnan
40   *
41   */
42  public class HtmlTablePane extends JEditorPane {
43  
44      //------------------------------------------------------------
45      //- Class Variables
46  
47      protected static final String PAGE_START = "<html><body><table width=\"100%\">\n";
48      protected static final String PAGE_END = "</table></body></html>\n";
49  
50  
51      //------------------------------------------------------------
52      //- Class Functions
53  
54  
55  
56      //------------------------------------------------------------
57      //- Instance Variables
58  
59      private StringBuffer _buffer = new StringBuffer();
60  
61  
62  
63      //------------------------------------------------------------
64      //- Constructors
65  
66      public HtmlTablePane() {
67          super("text/html", "");
68          setEditable(false);
69      }
70  
71  
72      //------------------------------------------------------------
73      //- Accessors
74  
75      protected StringBuffer getBuffer() {return _buffer;}
76  
77  
78  
79      //------------------------------------------------------------
80      //- Settors
81  
82  
83  
84      //------------------------------------------------------------
85      //- Private/Protected Utility Functions
86  
87      protected void redraw() {
88          String content = PAGE_START + getBuffer() + PAGE_END;
89  
90          int caret = getCaretPosition();
91          setText(content);
92          setCaretPosition(caret);
93      }
94  
95  
96      //------------------------------------------------------------
97      //- Public Interface Functions
98  
99      public void clear() {
100         _buffer = new StringBuffer();
101         setCaretPosition(0);
102 
103         redraw();
104     }
105 
106 
107     public void append(String string) {
108         getBuffer().append(string);
109 
110         redraw();
111     }
112 
113 
114     public OutputDestination createOutput() {
115         OutputDestination result = new Output(this);
116 
117         return result;
118     }
119 
120 
121     //------------------------------------------------------------
122     //- Class Interface Functions
123 
124 
125 
126     //------------------------------------------------------------
127     //- Inner Classes
128 
129     class Output extends OutputDestination {
130 
131         private HtmlTablePane _pane = null;
132 
133         Output(HtmlTablePane pPane) {
134             _pane = pPane;
135         }
136 
137         public void print(Object object) {
138             String string = (object instanceof Formatable) ?
139                 ((Formatable)object).toHtml() :
140                 String.valueOf(object);
141 
142             print(string);
143         }
144 
145         public void print(String string) {
146             _pane.append(string);
147         }
148     }
149 
150 
151     //------------------------------------------------------------
152     //- Main
153 
154 
155 
156 }