import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.filechooser.FileFilter;

/** Simple demo of a file chooser.<p>
*
* Screen snap...<br>
* <center><img src="DemoFileChooser-1.gif"></center><p>
*
* @see <a href="DemoFileChooser.java">source code</a>
* @author Scott MacKenzie, 2002
*/
public class DemoFileChooser
{
   public static void main(String[] args)
   {
      // use look and feel for my system (Win32)
      try {
         UIManager.setLookAndFeel(
            UIManager.getSystemLookAndFeelClassName());
      } catch (Exception e) {}
 
      DemoFileChooserFrame frame = new DemoFileChooserFrame();
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setTitle("DemoFileChooser");
      frame.pack();
      frame.show();
   }
}

class DemoFileChooserFrame extends JFrame implements ActionListener
{
   JButton open;
   JTextField current;
   JTextArea viewFile;
   JFileChooser fc;

   JLabel test;

   // constructor

   public DemoFileChooserFrame()
   {
      // -------------------------------
      // create and configure components
      // -------------------------------

      viewFile = new JTextArea(25, 80);
      viewFile.setMargin(new Insets(5, 5, 5, 5));
      viewFile.setEditable(false);

      JScrollPane scrollPane = new JScrollPane(viewFile);

      current = new JTextField(15);
      current.setEditable(false);
      current.setBackground(new Color(255, 222, 222));
      current.setForeground(new Color(140,0, 0));

      open = new JButton("Open");
      open.setMnemonic(KeyEvent.VK_O); // ALT-O brings up file chooser

      fc = new JFileChooser(new File("."));

      // initialize an array of file extensions to display (.java, .txt)
      final String[] EXTENSIONS = { ".java", ".txt" }; 

      // give the file chooser a FileFilter object to use to
      // decide which files to display
      fc.addChoosableFileFilter(new MyFileFilter(EXTENSIONS));

      // -------------
      // add listeners
      // -------------

      open.addActionListener(this);

      // ------------------
      // arrange components
      // ------------------

      // add components to panel
      
      JPanel topPanel = new JPanel();
      // for in-class demo...
      //topPanel.add(new SimpleClock(10, new Font("monospaced", Font.PLAIN, 12)));
      topPanel.add(open);
      topPanel.add(new JLabel("   Viewing"));
      topPanel.add(current);

      JPanel bottomPanel = new JPanel();

      bottomPanel.add(scrollPane);

      // add panels to content pane

      JPanel contentPane = new JPanel();
      contentPane.setLayout(new BorderLayout());
      contentPane.add(topPanel, "North");
      contentPane.add(bottomPanel, "South");
      this.setContentPane(contentPane);
   }

   // ------------------------
   // implement ActionListener
   // ------------------------

   public void actionPerformed(ActionEvent ac) 
   {
      // show the file chooser dialog box
      int i = fc.showOpenDialog(this);

      if (i == JFileChooser.APPROVE_OPTION)
      {
         File f = fc.getSelectedFile();

         String s = getFileContents(f);
         if (s == null)
            JOptionPane.showMessageDialog(this,
               "Error reading file!",
               "Error",
               JOptionPane.ERROR_MESSAGE);
         else
         {
            viewFile.setText(s);
            viewFile.setCaretPosition(0);
            viewFile.requestFocus();
            current.setText(" " + f.getName());
         }
      }
      else
      {
         // 'Cancel' option (do nothing)
      }  
   }

   // -------------
   // other methods
   // -------------

   /** Put the dirty work here.  Just return a string
   * containing the entire contents of the file.
   */
   private String getFileContents(File f)
   {
      BufferedReader br = null;
      try
      {
         br = new BufferedReader(new FileReader(f));
      } catch (FileNotFoundException e)
      {
         return null;
      }

      // use a StringBuffer object to accumulate the file contents
      // (Note: more efficient than using String objects

      StringBuffer sb = new StringBuffer(); 
      String line = "";
      char[] c;
      viewFile.setText("");
      while(true)
      {
         try
         {
            line = br.readLine();
         } catch (IOException e)
         {
            return null;
         }

         if (line == null) break;

         sb.append(line);
         sb.append('\n');
      }
      return sb.toString();
   }

   // -----------
   // inner class
   // -----------

   /** Define an inner class to extend the FileFilter class
   * (which is abstract) and implement the 'accept' and
   * 'getDescription' methods.
   */
   class MyFileFilter extends FileFilter
   {
      private String[] s;

      MyFileFilter(String[] ss)
      {
         s = ss;
      }

      // determine which files to display in the chooser
      public boolean accept(File f)
      {
         // if it's a directory, show it
         if (f.isDirectory())
            return true;
   
         // if the filename contains the extension, show it
         for (int i = 0; i < s.length; ++i)
            if (f.getName().toLowerCase().indexOf(s[i].toLowerCase()) > 0)
               return true;

         // filter out everything else
         return false;
      }   
       
      public String getDescription()
      {
         String tmp = "";
         for (int i = 0; i < s.length; ++i)
            tmp += "*" + s[i] + " ";

         return tmp;
      }
   }
}

