1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
50
51 private static FileViewer sInstance = null;
52
53
54
55
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
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
79
80 public FileViewer()
81 {
82 super("FileViewer");
83
84 defineActions();
85 makeFrame();
86 }
87
88
89
90
91
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
102
103 protected void setLastFilename(String val) {_lastFilename=val;}
104
105
106
107
108
109 protected void defineActions() {
110 getFieldFilename().setEditable(false);
111
112 getTextArea().setEditable(false);
113
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
191 }
192
193
194
195
196
197 public void display(String filename, String linenum) {
198
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
214
215
216
217
218
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
243
244
245
246 }