Newer
Older
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/* FileViewer.java
*
* created: Thu Nov 19 1998
*
* This file is part of Artemis
*
* Copyright(C) 1998,1999,2000,2001 Genome Research Limited
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or(at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* $Header: //tmp/pathsoft/artemis/uk/ac/sanger/artemis/components/FileViewer.java,v 1.1 2004-06-09 09:46:55 tjc Exp $
*/
package uk.ac.sanger.artemis.components;
import uk.ac.sanger.artemis.*;
import java.awt.*;
import java.awt.event.*;
import java.io.Reader;
import java.io.BufferedReader;
import java.io.IOException;
import javax.swing.*;
/**
* This class implements a simple file viewer. In fact any Reader object can
* be viewed.
*
* @author Kim Rutherford
* @version $Id: FileViewer.java,v 1.1 2004-06-09 09:46:55 tjc Exp $
*
**/
public class FileViewer
extends JFrame
{
/** Pressing this button will distroy the JFrame. */
private JButton close_button;
/** A JPanel to hold the close button. */
private JPanel button_panel;
/** The main component we use for displaying the file. */
private JTextArea text_area = null;
/**
* The size of the last FileViewer JFrame to be resized. When a new
* FileViewer component is created it is given the current value of this
* variable. This is updated when any FileViewer frame is resized.
**/
private static Dimension saved_size = null;
/**
* The position of the last FileViewer JFrame to be moved. When a new
* FileViewer component is created it is given the current value of this
* variable. This is updated when any FileViewer frame is moved.
**/
private static Point saved_position = null;
/**
* Create a new FileViewer component and make it visible.
* @param label The name to attach to the new JFrame.
**/
public FileViewer(final String label)
{
this(label, true);
}
/**
* Create a new FileViewer component.
* @param label The name to attach to the new JFrame.
* @param visible The new FileViewer will be made visible if and only if
* this argument is true.
**/
public FileViewer(final String label, final boolean visible)
{
super(label);
getContentPane().setLayout(new BorderLayout());
final Font font = Options.getOptions().getFont();
setFont(font);
text_area = new JTextArea(18, 90);
text_area.setEditable(false);
text_area.setFont(font);
text_area.setBackground(Color.white);
getContentPane().add(new JScrollPane(text_area), "Center");
button_panel = new JPanel();
getContentPane().add(button_panel, "South");
close_button = new JButton("Close");
close_button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
dispose();
}
});
button_panel.add(close_button);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent event)
{
dispose();
}
});
addComponentListener(new ComponentAdapter()
{
public void componentResized(ComponentEvent e)
{
saved_size = FileViewer.this.getSize();
saved_position = FileViewer.this.getLocation();
}
public void componentMoved(ComponentEvent e)
{
saved_size = FileViewer.this.getSize();
saved_position = FileViewer.this.getLocation();
}
});
pack();
final Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
if(saved_position == null)
{
int screen_height = screen.height;
int screen_width = screen.width;
if(screen_width <= 800 || screen_height <= 700)
setSize(screen_width * 9 / 10, screen_height * 9 / 10);
else
setSize(800, 700);
setLocation(new Point((screen.width - getSize().width) / 2,
(screen.height - getSize().height) / 2));
}
else
{
if(saved_position.x < 0 || saved_position.x + 20 > screen.width)
saved_position.x = 20;
if(saved_position.y < 0 || saved_position.y + 20 > screen.height)
saved_position.y = 20;
if(saved_size.width < 50)
saved_size.width = 50;
if(saved_size.height < 50)
saved_size.height = 50;
setLocation(saved_position);
setSize(saved_size);
}
setVisible(visible);
}
/**
* Clear the viewer window.
**/
public void clear()
{
text_area.setText("");
}
/**
* Read from the given Reader and append the text to this FileViewer.
* @param read_stream The stream to read the contents of the viewer from.
**/
public void appendFile(Reader read_stream)
throws IOException
{
final BufferedReader buffered_reader = new BufferedReader(read_stream);
String line;
while((line = buffered_reader.readLine()) != null)
{
appendString(line + "\n");
Thread.yield();
}
buffered_reader.close();
}
/**
* Clear the viewer window and display the lines from the given Reader.
* @param read_stream The stream to read the contents of the viewer from.
**/
public void readFile(Reader read_stream)
throws IOException
{
final BufferedReader buffered_reader = new BufferedReader(read_stream);
String line;
final StringBuffer line_buffer = new StringBuffer();
while((line = buffered_reader.readLine()) != null)
line_buffer.append(line).append('\n');
buffered_reader.close();
final String new_text = line_buffer.toString();
text_area.setText(new_text);
// text_area.getCaret().setDot(0);
text_area.setCaretPosition(0);
}
/**
* Clear the viewer window and display the lines from the given String.
* @param read_string The string to read the contents of the viewer from.
**/
public void setText(String read_string)
{
if(!read_string.equals(text_area.getText()))
{
text_area.setText(read_string);
// text_area.getCaret().setDot(0);
text_area.setCaretPosition(0);
}
}
/**
* Clear the viewer window and display the lines from the given String.
* @param read_string The string to read the contents of the viewer from.
**/
public void appendString(String read_string)
{
text_area.append(read_string);
text_area.getCaret().setDot(0);
}
/**
* Return a String containing the text that this component is displaying.
**/
public String getText()
{
return getTextArea().getText();
}
/**
* Destroy this component.
**/
public void dispose()
{
setVisible(false);
saved_size = getSize();
saved_position = getLocation();
super.dispose();
}
/**
* return the TextArea component from this FileViewer.
**/
public JTextArea getTextArea()
{
return text_area;
}
/**
* Return the JPanel containing the close button of this FileViewer.
**/
protected JPanel getButtonPanel()
{
return button_panel;
}
}