FileDocCategorySizeDatePackage
TextFilePreview.javaAPI DocExample2382Tue Feb 14 13:49:34 GMT 2006None

TextFilePreview

public class TextFilePreview extends JTextArea implements PropertyChangeListener

Fields Summary
private File
selectedFile
private String
preview
private int
previewLength
Constructors Summary
public TextFilePreview(JFileChooser fc)

  
     
    super(10, 20);
    this.setEditable(false);
    this.setPreferredSize(new Dimension(150, 150));
    this.setLineWrap(true);
    fc.addPropertyChangeListener(this);
  
Methods Summary
private voidloadText()


    if (selectedFile != null) {
      try {
        FileInputStream fin = new FileInputStream(selectedFile);
        byte[] data = new byte[previewLength];
        int bytesRead = 0;
        for (int i = 0; i < previewLength; i++) {
          int b = fin.read();
          if (b == -1) break;
          bytesRead++;
          data[i] = (byte) b;
        }
        preview = new String(data, 0, bytesRead);
        fin.close();
      }
      catch (IOException ex) {
        // File preview is not an essential operation so 
        // we'll simply ignore the exception and return.
      }
    }
  
public static voidmain(java.lang.String[] args)

    
    SwingUtilities.invokeAndWait(
      new Runnable() {
        public void run() {
          JFileChooser fc = new JFileChooser();
          fc.setAccessory(new TextFilePreview(fc));
          int result = fc.showOpenDialog(new JFrame());
          if (result == JFileChooser.APPROVE_OPTION) {
            try {
              File f = fc.getSelectedFile();
              if (f != null) {
                InputStream in = new FileInputStream(f);
                for (int c = in.read(); c != -1; c = in.read()) {
                   System.out.write(c);
                }
                in.close();
              }
            }
            catch (IOException ex) {System.err.println(ex);}
          }
          System.exit(0);
        }  // end run
      } // end Runnable
    ); // end invokeAndWait
  
public voidpropertyChange(java.beans.PropertyChangeEvent evt)

  
    if (evt.getPropertyName().equals(
     JFileChooser.SELECTED_FILE_CHANGED_PROPERTY)) {
      selectedFile = (File) evt.getNewValue();
      if(isShowing()) {
        loadText();
        this.setText(preview);
      }
    }