View Javadoc

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          // Create a TextArea to display the contents of the file in
25          textArea = new JTextArea(""); //, 24, 80);
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          // Read and display the file contents. Since we're reading text, we
68          // use a FileReader instead of a FileInputStream.
69          try {
70              f = new File(directory, filename); // Create a file object
71              in = new FileReader(f); // Create a char stream to read it
72              int size = (int) f.length(); // Check file size
73              char[] data = new char[size]; // Allocate an array big enough for it
74              int charsRead = 0; // How many chars read so far?
75              while (charsRead < size) {
76                  // Loop until we've read it all
77                  charsRead += in.read(data, charsRead, size - charsRead);
78              }
79              String text = new String(data);
80              textArea.setText(text); // Display chars in TextArea
81  
82              /*
83               * Find begin and end position of characters to scroll to and select
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               * Scroll to and select the text to highlight
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 { // Always be sure to close the input stream!
110             try {
111                 if (in != null) {
112                     in.close();
113                 }
114             } catch (IOException e) {
115                 e.printStackTrace();
116             }
117         }
118     }
119 }