package fileopentestbean;

import java.awt.*;
import java.io.File;
import java.io.FileInputStream;
import javax.swing.*;
import org.vrjuggler.tweek.services.ExtensionFileFilter;
import org.vrjuggler.tweek.beans.FileLoader;


/**
 */
public class FileOpenTestBean extends JPanel
                              implements java.io.Serializable, FileLoader
{
   public FileOpenTestBean ()
   {
      try
      {
         jbInit();
      }
      catch(Exception e)
      {
         e.printStackTrace();
      }
   }

   public String getFileType ()
   {
      return "Text";
   }

   public boolean canOpenMultiple ()
   {
      return true;
   }

   public boolean openRequested ()
   {
      // Initialize this to false since a lot of things can go wrong in the
      // process of opening files.  Once the file is opened and read
      // successfully, this can be changed to true.
      boolean opened = false;

      JFileChooser chooser = new JFileChooser();
      chooser.setMultiSelectionEnabled(false);
      chooser.setDialogTitle("Text File Loader");
      chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);

      // Only load .txt files.
      ExtensionFileFilter filter = new ExtensionFileFilter("Text Files");
      filter.addExtension("txt");
      chooser.addChoosableFileFilter(filter);

      int status = chooser.showOpenDialog(this);

      if ( status == JFileChooser.APPROVE_OPTION )
      {
         File file = chooser.getSelectedFile();

         if ( file.canRead() )
         {
            try
            {
               // Read the contents of the file into a byte[] object.
               FileInputStream input_file = new FileInputStream(file);
               byte[] file_data = new byte[(int) file.length()];
               input_file.read(file_data);

               // Create a text area to hold the contents of the file.
               JTextArea text_area = new JTextArea();
               text_area.setEditable(false);
               text_area.insert(new String(file_data), 0);

               // Create a scroll pane to hold the text area; add it to the
               // tabbed pane with all the other previously loaded scroll
               // panes; and make the new scroll pane the selected component.
               JScrollPane text_comp = new JScrollPane(text_area);
               mTextContainer.add(text_comp, file.getName());
               mTextContainer.setSelectedComponent(text_comp);

               // Our work is done.
               openFileCount++;
               opened = true;
            }
            catch (java.io.FileNotFoundException ex)
            {
               JOptionPane.showMessageDialog(null, "Cannot find '" +
                                             file.getAbsolutePath() + "'",
                                             "Read Error",
                                             JOptionPane.ERROR_MESSAGE);
            }
            catch (java.io.IOException ex)
            {
               JOptionPane.showMessageDialog(null, "Error reading from '" +
                                             file.getAbsolutePath() + "':" +
                                             ex.getMessage(), "Read Error",
                                             JOptionPane.ERROR_MESSAGE);
            }
         }
         else
         {
            JOptionPane.showMessageDialog(null, "Cannot read from file '" +
                                          file.getAbsolutePath() + "'",
                                          "Read Error",
                                          JOptionPane.ERROR_MESSAGE);
         }
      }

      return opened;
   }

   public boolean canSave ()
   {
      return false;
   }

   public boolean saveRequested ()
   {
      return false;
   }

   public boolean closeRequested ()
   {
      mTextContainer.remove(mTextContainer.getSelectedComponent());
      openFileCount--;
      return true;
   }

   public int getOpenFileCount ()
   {
      return openFileCount;
   }

   private void jbInit() throws Exception
   {
      this.setLayout(mMainLayout);
      mBeanTitle.setHorizontalAlignment(SwingConstants.CENTER);
      mBeanTitle.setText("Text File Loader");
      this.add(mBeanTitle, BorderLayout.NORTH);
      this.add(mTextContainer, BorderLayout.CENTER);
   }

   private int openFileCount = 0;

   private BorderLayout mMainLayout    = new BorderLayout();
   private JLabel       mBeanTitle     = new JLabel();
   private JTabbedPane  mTextContainer = new JTabbedPane();
}

@@ -3745,32 +3745,99 @@
 rn false;%0A   %7D%0A%0A
+   public boolean saveAsRequested()%0A   %7B%0A      return false;%0A   %7D%0A%0A
    public boolea

Implement saveAsRequested().