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  import java.awt.event.ActionEvent;
30  import java.awt.event.ActionListener;
31  import java.awt.event.WindowAdapter;
32  import java.awt.event.WindowEvent;
33  import java.io.*;
34  import java.util.*;
35  
36  import javax.swing.*;
37  
38  
39  /***
40   * A simple viewer for text files.  It displays the file contents and
41   * can be directed to show a particular line.
42   * 
43   * @author  Murali Krishnan
44   *
45   */
46  public class FileViewer extends JFrame {
47  
48      //------------------------------------------------------------
49      //- Class Variables
50  
51      private static FileViewer sInstance = null;
52  
53  
54      //------------------------------------------------------------
55      //- Class Functions
56  
57      public static FileViewer getInstance()
58      {
59          if (sInstance == null) {
60              sInstance = new FileViewer();
61          }
62  
63          return sInstance;
64      }
65  
66  
67      //------------------------------------------------------------
68      //- Instance Variables
69  
70      private JTextArea _textArea = new JTextArea("", 30, 60);
71      private JButton _buttonOk = new JButton("Ok");
72      private JTextField _fieldFilename = new JTextField("");
73      private String _lastFilename = null;
74      private SelectionLocation _selectionLocation = new SelectionLocation();
75  
76  
77      //------------------------------------------------------------
78      //- Constructors
79  
80      public FileViewer() 
81      {
82          super("FileViewer");
83  
84          defineActions();
85          makeFrame();
86      }
87  
88  
89  
90      //------------------------------------------------------------
91      //- Accessors
92  
93      protected JTextArea getTextArea() {return _textArea;}
94      protected JButton getButtonOk() {return _buttonOk;}
95      protected JTextField getFieldFilename() {return _fieldFilename;}
96      protected String getLastFilename() {return _lastFilename;}
97      protected SelectionLocation getSelectionLocation() {return _selectionLocation;}
98  
99  
100     //------------------------------------------------------------
101     //- Settors
102 
103     protected void setLastFilename(String val) {_lastFilename=val;}
104 
105 
106     //------------------------------------------------------------
107     //- Private/Protected Utility Functions
108 
109     protected void defineActions() {
110         getFieldFilename().setEditable(false);
111 
112         getTextArea().setEditable(false);
113         // getTextArea().getCaret().setVisible(true);
114 
115         getButtonOk().addActionListener(new ActionListener() {
116                 public void actionPerformed (ActionEvent aEvent) {
117                     setVisible(false);
118                 }
119             });
120 
121         addWindowListener(new WindowAdapter() {
122                 public void windowActivated(WindowEvent e) {
123                     getButtonOk().grabFocus();
124                 }
125             });
126     }
127 
128 
129     protected void makeFrame() {
130         getContentPane().setLayout(new BorderLayout());
131 
132         JScrollPane scrollPane = new JScrollPane();
133         scrollPane.setViewportView(getTextArea());
134 
135         getContentPane().add(scrollPane, BorderLayout.CENTER);
136         getContentPane().add(makeBottomPanel(), BorderLayout.SOUTH);
137 
138         pack();
139     }
140 
141 
142     protected JPanel makeBottomPanel() {
143         JPanel result = new JPanel(new BorderLayout());
144 
145         result.add(getFieldFilename(), BorderLayout.CENTER);
146         result.add(getButtonOk(), BorderLayout.EAST);
147 
148         return result;
149     }
150 
151 
152     protected void readFile(String filename) {
153         StringBuffer buffer = new StringBuffer();
154 
155         getTextArea().setText("");
156         getSelectionLocation().clear();
157 
158         try {
159             FileReader fr = new FileReader(filename);
160             LineNumberReader lnr = new LineNumberReader(fr);
161 
162             String line=lnr.readLine();
163             while (line != null) {
164                 int position = buffer.length();
165                 getSelectionLocation().set(lnr.getLineNumber(), position, (position+line.length()));
166                 buffer.append(line).append('\n');
167                 line=lnr.readLine();
168             }
169 
170             lnr.close();
171             fr.close();
172         } catch (IOException ioe) {
173             System.err.println("Problem reading [" + filename + "].");
174             ioe.printStackTrace();
175         }
176 
177         getTextArea().setText(buffer.toString());
178         setLastFilename(filename);
179     }
180 
181 
182     protected void show(String pLinenum) {
183         int linenum = Integer.parseInt(pLinenum);
184 
185         Points points = getSelectionLocation().get(linenum);
186         getTextArea().getCaret().setDot(points.getEnd());
187         getTextArea().getCaret().moveDot(points.getStart());
188         getTextArea().getCaret().setSelectionVisible(true);
189 
190         // System.out.println("Selection [" + getTextArea().getSelectedText() + "]");
191     }
192 
193 
194     //------------------------------------------------------------
195     //- Public Interface Functions
196 
197     public void display(String filename, String linenum) {
198         // getTextArea().setText("display(" + filename + "," + linenum + ")");
199         if (!filename.equals(getLastFilename())) {
200             readFile(filename);
201         }
202 
203         show(linenum);
204 
205         getFieldFilename().setText(filename + "; line " + linenum);
206 
207         setVisible(true);
208     }
209 
210 
211 
212     //------------------------------------------------------------
213     //- Class Interface Functions
214 
215 
216 
217     //------------------------------------------------------------
218     //- Inner Classes
219 
220     protected class Points {
221         private int _start = 0;
222         private int _end = 0;
223 
224         protected Points(int pStart, int pEnd) {
225             _start = pStart;
226             _end = pEnd;
227         }
228 
229         public int getStart() {return _start;}
230         public int getEnd() {return _end;}
231     }
232 
233     protected class SelectionLocation extends HashMap<Integer,Points> {
234         protected void set(int linenum, int start, int end) {
235             put(linenum, new Points(start, end));
236         }
237     }
238 
239 
240 
241     //------------------------------------------------------------
242     //- Main
243 
244 
245 
246 }