1 package de.matthias_burbach.mosaique.swing;
2
3 import java.awt.Font;
4 import java.io.File;
5 import java.io.FileReader;
6 import java.io.IOException;
7
8 import javax.swing.JPanel;
9 import javax.swing.JTextArea;
10
11 /***
12 * @author Matthias Burbach
13 */
14 public class MosaiqueFilePanel extends JPanel {
15 /***
16 * The text area to actually display the text files in.
17 */
18 private JTextArea textArea;
19
20 /***
21 * Creates a panel to display a text files in.
22 */
23 public MosaiqueFilePanel() {
24
25 textArea = new JTextArea("");
26 final int fontSize = 12;
27 textArea.setFont(new Font("MonoSpaced", Font.PLAIN, fontSize));
28 textArea.setEditable(false);
29 add("Center", textArea);
30 }
31
32 /***
33 * Loads and displays the specified file (if any) from the specified
34 * directory.
35 *
36 * @param directory The directory of the file to display.
37 * @param filename The name of the file to display.
38 * @param primarySearchText The text to search for in the file to display in
39 * order to scroll to the text found.
40 * Will be ignored if beginSelection is greater -1
41 * and endSelection is greater -1.
42 * @param secondarySearchText Can be <code>null</code>.
43 * If not <code>null</code> a second search
44 * relative to the found primarySearchText will
45 * be performed to determine the text to scroll
46 * to. Will be ignored if primarySearchText will
47 * ignored.
48 * @param beginSelection Marks the character position to begin text
49 * selection at. Will be ignored if equal to -1.
50 * @param endSelection Marks the character position to end text
51 * selection at.
52 * Will be ignored if beginSelection will
53 * be ignored.
54 */
55 public void setFile(
56 final String directory,
57 final String filename,
58 final String primarySearchText,
59 final String secondarySearchText,
60 final int beginSelection,
61 final int endSelection) {
62 if ((filename == null) || (filename.length() == 0)) {
63 return;
64 }
65 File f;
66 FileReader in = null;
67
68
69 try {
70 f = new File(directory, filename);
71 in = new FileReader(f);
72 int size = (int) f.length();
73 char[] data = new char[size];
74 int charsRead = 0;
75 while (charsRead < size) {
76
77 charsRead += in.read(data, charsRead, size - charsRead);
78 }
79 String text = new String(data);
80 textArea.setText(text);
81
82
83
84
85 int begin = -1;
86 int end = -1;
87 if (beginSelection > -1) {
88 begin = beginSelection;
89 end = Math.max(beginSelection, endSelection);
90 } else if (primarySearchText != null) {
91 begin = text.indexOf(primarySearchText);
92 end = begin + primarySearchText.length();
93 if (secondarySearchText != null) {
94 begin = text.indexOf(secondarySearchText, begin);
95 end = begin + secondarySearchText.length();
96 }
97 }
98
99
100
101 if (begin != -1) {
102 textArea.setCaretPosition(begin);
103 textArea.setSelectionStart(begin);
104 textArea.setSelectionEnd(end);
105 textArea.getCaret().setSelectionVisible(true);
106 }
107 } catch (IOException e) {
108 textArea.setText(e.getClass().getName() + ": " + e.getMessage());
109 } finally {
110 try {
111 if (in != null) {
112 in.close();
113 }
114 } catch (IOException e) {
115 e.printStackTrace();
116 }
117 }
118 }
119 }