|
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 |
| |
|
26 |
| |
|
27 |
| |
|
28 |
| |
|
29 |
| |
|
30 |
| |
|
31 |
| |
|
32 |
| |
|
33 |
| |
|
34 |
| |
|
35 |
| |
|
36 |
| |
|
37 |
| package edu.rice.cs.drjava.ui; |
|
38 |
| |
|
39 |
| import java.io.*; |
|
40 |
| |
|
41 |
| import javax.swing.*; |
|
42 |
| import java.awt.event.*; |
|
43 |
| import java.awt.*; |
|
44 |
| |
|
45 |
| import edu.rice.cs.util.swing.Utilities; |
|
46 |
| |
|
47 |
| |
|
48 |
| |
|
49 |
| |
|
50 |
| |
|
51 |
| |
|
52 |
| public class LessPanel extends AbortablePanel { |
|
53 |
| |
|
54 |
| public final int BUFFER_SIZE = 10240; |
|
55 |
| |
|
56 |
| public final int BUFFER_READS_PER_TIMER = 5; |
|
57 |
| protected JTextArea _textArea; |
|
58 |
| protected File _f = null; |
|
59 |
| protected FileReader _fr = null; |
|
60 |
| protected JButton _updateNowButton; |
|
61 |
| protected JButton _restartButton; |
|
62 |
| protected Thread _updateThread; |
|
63 |
| private char[] _buf = new char[BUFFER_SIZE]; |
|
64 |
| private int _red = -1; |
|
65 |
| private long _totalRead = 0; |
|
66 |
| |
|
67 |
| |
|
68 |
| |
|
69 |
| |
|
70 |
| |
|
71 |
| |
|
72 |
| |
|
73 |
0
| public LessPanel(MainFrame frame, String title, File f) {
|
|
74 |
0
| super(frame, title);
|
|
75 |
0
| initThread(f);
|
|
76 |
0
| updateText();
|
|
77 |
| |
|
78 |
| } |
|
79 |
| |
|
80 |
0
| protected void initThread(File f) {
|
|
81 |
0
| try {
|
|
82 |
| |
|
83 |
| |
|
84 |
| |
|
85 |
| |
|
86 |
0
| if (f.exists() && f.canRead() && f.isFile()) {
|
|
87 |
| |
|
88 |
| |
|
89 |
0
| _f = f;
|
|
90 |
0
| _fr = new FileReader(_f);
|
|
91 |
0
| _red = -1;
|
|
92 |
0
| _totalRead = 0;
|
|
93 |
0
| _updateThread = new Thread(new Runnable() {
|
|
94 |
0
| public void run() {
|
|
95 |
0
| while(_fr != null) {
|
|
96 |
0
| try {
|
|
97 |
0
| Thread.sleep(edu.rice.cs.drjava.DrJava.getConfig().
|
|
98 |
| getSetting(edu.rice.cs.drjava.config.OptionConstants.FOLLOW_FILE_DELAY)); |
|
99 |
| } |
|
100 |
| catch(InterruptedException ie) { } |
|
101 |
0
| updateText();
|
|
102 |
| } |
|
103 |
| } |
|
104 |
| }); |
|
105 |
0
| _updateThread.start();
|
|
106 |
0
| _updateNowButton.setEnabled(true);
|
|
107 |
| |
|
108 |
| } |
|
109 |
| } |
|
110 |
| catch(Exception e) { |
|
111 |
0
| _fr = null;
|
|
112 |
| } |
|
113 |
| } |
|
114 |
| |
|
115 |
| |
|
116 |
0
| protected Component makeLeftPanel() {
|
|
117 |
0
| _textArea = new JTextArea();
|
|
118 |
0
| _textArea.setEditable(false);
|
|
119 |
0
| return _textArea;
|
|
120 |
| } |
|
121 |
| |
|
122 |
| |
|
123 |
0
| protected void abortActionPerformed(ActionEvent e) {
|
|
124 |
0
| if (_fr != null) {
|
|
125 |
0
| try {
|
|
126 |
0
| _fr.close();
|
|
127 |
| } |
|
128 |
| catch(IOException ioe) { } |
|
129 |
0
| _fr = null;
|
|
130 |
0
| updateButtons();
|
|
131 |
| } |
|
132 |
| } |
|
133 |
| |
|
134 |
| |
|
135 |
0
| protected void updateButtons() {
|
|
136 |
0
| _abortButton.setEnabled(_fr != null);
|
|
137 |
0
| _updateNowButton.setEnabled(_fr != null);
|
|
138 |
0
| _restartButton.setEnabled(_fr == null);
|
|
139 |
| } |
|
140 |
| |
|
141 |
| |
|
142 |
0
| protected JComponent[] makeButtons() {
|
|
143 |
0
| _updateNowButton = new JButton("Update");
|
|
144 |
0
| _updateNowButton.addActionListener(new ActionListener() {
|
|
145 |
0
| public void actionPerformed(ActionEvent e) { updateText(); }
|
|
146 |
| }); |
|
147 |
0
| _restartButton = new JButton("Restart");
|
|
148 |
0
| _restartButton.addActionListener(new ActionListener() {
|
|
149 |
0
| public void actionPerformed(ActionEvent e) { restartFollowing(); }
|
|
150 |
| }); |
|
151 |
0
| _restartButton.setEnabled(false);
|
|
152 |
0
| return new JComponent[] { _updateNowButton, _restartButton };
|
|
153 |
| } |
|
154 |
| |
|
155 |
| |
|
156 |
0
| protected void restartFollowing() {
|
|
157 |
0
| _textArea.setText("");
|
|
158 |
0
| initThread(_f);
|
|
159 |
0
| updateText();
|
|
160 |
| } |
|
161 |
| |
|
162 |
| |
|
163 |
| |
|
164 |
| |
|
165 |
0
| protected void updateText() {
|
|
166 |
0
| Utilities.invokeLater(new Runnable() {
|
|
167 |
0
| public void run() {
|
|
168 |
| |
|
169 |
0
| if ((_fr != null) &&
|
|
170 |
| (_updateNowButton.isEnabled())) { |
|
171 |
0
| _updateNowButton.setEnabled(false);
|
|
172 |
0
| int changeCount = 0;
|
|
173 |
0
| long newSize = _f.length();
|
|
174 |
0
| if (_totalRead!=newSize) {
|
|
175 |
0
| if (_totalRead>newSize) {
|
|
176 |
| |
|
177 |
0
| _textArea.setText("");
|
|
178 |
0
| _totalRead = 0;
|
|
179 |
| } |
|
180 |
0
| StringBuilder sb = new StringBuilder(_textArea.getText());
|
|
181 |
| |
|
182 |
0
| try {
|
|
183 |
0
| _fr.close();
|
|
184 |
0
| _fr = new FileReader(_f);
|
|
185 |
0
| _fr.skip(_totalRead);
|
|
186 |
| |
|
187 |
| |
|
188 |
| |
|
189 |
0
| while((changeCount<=BUFFER_READS_PER_TIMER) && ((_red = _fr.read(_buf)) >= 0)) {
|
|
190 |
| |
|
191 |
0
| _totalRead += _red;
|
|
192 |
0
| sb.append(new String(_buf, 0, _red));
|
|
193 |
0
| ++changeCount;
|
|
194 |
| } |
|
195 |
0
| if ((_red > 0) && (changeCount<BUFFER_READS_PER_TIMER)) {
|
|
196 |
0
| _totalRead += _red;
|
|
197 |
0
| sb.append(new String(_buf, 0, _red));
|
|
198 |
0
| ++changeCount;
|
|
199 |
| } |
|
200 |
| } |
|
201 |
| catch(IOException ioe) { |
|
202 |
| |
|
203 |
| |
|
204 |
0
| sb.append("\n\nI/O Exception reading file " + _f + "\n");
|
|
205 |
0
| ++changeCount;
|
|
206 |
0
| abortActionPerformed(null);
|
|
207 |
| } |
|
208 |
| finally { |
|
209 |
0
| if (changeCount > 0) {
|
|
210 |
| |
|
211 |
0
| _textArea.setText(sb.toString());
|
|
212 |
0
| int maxLines = edu.rice.cs.drjava.DrJava.getConfig().
|
|
213 |
| getSetting(edu.rice.cs.drjava.config.OptionConstants.FOLLOW_FILE_LINES); |
|
214 |
0
| if (maxLines > 0) {
|
|
215 |
0
| try {
|
|
216 |
0
| int start = 0;
|
|
217 |
0
| int len = _textArea.getText().length();
|
|
218 |
0
| int curLines = _textArea.getLineCount();
|
|
219 |
0
| if (curLines>maxLines) {
|
|
220 |
0
| start = _textArea.getLineStartOffset(curLines-maxLines);
|
|
221 |
0
| len -= start;
|
|
222 |
0
| sb = new StringBuilder(_textArea.getText(start,len));
|
|
223 |
0
| _textArea.setText(sb.toString());
|
|
224 |
| } |
|
225 |
| } |
|
226 |
| catch(javax.swing.text.BadLocationException e) { } |
|
227 |
| } |
|
228 |
| |
|
229 |
| } |
|
230 |
| } |
|
231 |
| } |
|
232 |
| } |
|
233 |
| |
|
234 |
0
| updateButtons();
|
|
235 |
| } |
|
236 |
| }); |
|
237 |
| } |
|
238 |
| } |