User:Yusufsani264

/* * Created on Aug 27, 2005 */package ca.uwaterloo.plg.my2iu.cs01; import java.awt.BorderLayout;import java.awt.Dialog;import java.awt.Dimension;import java.awt.FlowLayout;import java.awt.Frame;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.io.IOException;import java.util.Iterator; import javax.swing.DefaultListModel;import javax.swing.JButton;import javax.swing.JDialog;import javax.swing.JLabel;import javax.swing.JList;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JScrollPane; import org.programmingbasics.my2iu.cs01.tar.RandomAccessInterface;import org.programmingbasics.my2iu.cs01.tar.TarFile; import ch.epfl.labos.iu.tar.TarRandomAccessFile; public class AttachmentsDialog extends JDialog{  static final int COPY_SIZE = 512;   AttachmentsManager attachManager;   public static final String ATTACHMENT_DIR = "attachments/"; //$NON-NLS-1$   public static final String LIBRARY_DIR = "lib/"; //$NON-NLS-1$   static final int UIGAP = 3;   static final int MINLISTSIZE = 200;   DefaultListModel listModel;   JList list;      void createDialog(boolean isAddText, String listName)   {      JPanel parent = new JPanel;      getContentPane.add(parent);      getContentPane.setLayout(new FlowLayout(FlowLayout.CENTER, UIGAP, UIGAP));      parent.setLayout(new BorderLayout(UIGAP, UIGAP));            JButton okButton = new JButton(Messages.getString("DoneButton")); //$NON-NLS-1$      okButton.addActionListener(new ActionListener {         public void actionPerformed(ActionEvent e)         {            setVisible(false);         }});            JPanel okPanel = new JPanel;      okPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0));      okPanel.add(okButton);            listModel = new DefaultListModel;      list = new JList(listModel);      list.setLayoutOrientation(JList.VERTICAL);      list.setVisibleRowCount(10);      JScrollPane listScroller = new JScrollPane(list);      listScroller.setPreferredSize(new Dimension(MINLISTSIZE, Math.max(MINLISTSIZE, list.getPreferredSize.height)));       JPanel leftPanel = new JPanel;      leftPanel.setLayout(new BorderLayout(UIGAP, UIGAP));      leftPanel.add(listScroller, BorderLayout.CENTER);      leftPanel.add(okPanel, BorderLayout.SOUTH);      leftPanel.add(new JLabel(listName), BorderLayout.NORTH);      parent.add(leftPanel, BorderLayout.CENTER);       JButton addButton = new JButton(Messages.getString("AddButton")); //$NON-NLS-1$      JButton addUtfButton = new JButton(Messages.getString("AddUTFButton")); //$NON-NLS-1$      JButton removeButton = new JButton(Messages.getString("RemoveButton")); //$NON-NLS-1$      JButton exportButton = new JButton(Messages.getString("ExportButton")); //$NON-NLS-1$      if (isAddText)         addButton.addActionListener(new ActionListener {public void actionPerformed(ActionEvent e) {addTextFile;}});      else         addButton.addActionListener(new ActionListener {public void actionPerformed(ActionEvent e) {addFile;}});      addUtfButton.addActionListener(new ActionListener {public void actionPerformed(ActionEvent e) {addFile;}});      removeButton.addActionListener(new ActionListener {public void actionPerformed(ActionEvent e) {removeFile;}});      exportButton.addActionListener(new ActionListener {public void actionPerformed(ActionEvent e) {exportFile;}});                        JPanel panel = new JPanel;      panel.setLayout(new GridLayout(0, 1, UIGAP, UIGAP));      panel.add(addButton);      if (isAddText)         panel.add(addUtfButton);      panel.add(removeButton);      panel.add(exportButton);       JPanel buttonSpacerPanel = new JPanel;      buttonSpacerPanel.setLayout(new BorderLayout);      buttonSpacerPanel.add(panel, BorderLayout.NORTH);      parent.add(buttonSpacerPanel, BorderLayout.EAST);   }      void addFile   {      try {         RandomAccessInterface []fs = TarRandomAccessFile.userOpenMultipleFile(this, null);         if (fs == null) return;         for (int n = 0; n < fs.length; n++)         {            RandomAccessInterface f = fs[n];            long length = f.length;            byte [] data = new byte[(int)length];            f.readFully(data);            f.close;                        attachManager.addOrUpdateFile(f.getName, data);         }         fillList;      } catch (IOException e)      {         JOptionPane.showMessageDialog(this, Messages.getString("AttachmentReadErrorDialogMessage"), Messages.getString("AttachmentReadErrorDialogTitle"), JOptionPane.ERROR_MESSAGE); //$NON-NLS-1$ //$NON-NLS-2$         e.printStackTrace;      }   }    void addTextFile   {      try      {         RandomAccessInterface []fs = TarRandomAccessFile.userOpenMultipleFile(this, null);         if (fs == null) return;         for (int n = 0; n < fs.length; n++)         {            RandomAccessInterface f = fs[n];            long length = f.length;            byte [] data = new byte[(int)length];            f.readFully(data);            f.close;                        String str = new String(data);  // Convert text to UTF-8            data = str.getBytes("UTF-8"); //$NON-NLS-1$                        attachManager.addOrUpdateFile(f.getName, data);         }         fillList;      } catch (IOException e)      {         JOptionPane.showMessageDialog(this, Messages.getString("AttachmentReadErrorDialogMessage"), Messages.getString("AttachmentReadErrorDialogTitle"), JOptionPane.ERROR_MESSAGE); //$NON-NLS-1$ //$NON-NLS-2$         e.printStackTrace;      }   }    void removeFile   {      try {         int [] idxs = list.getSelectedIndices;         for (int n = 0; n < idxs.length; n++)            attachManager.removeFile((String)listModel.get(idxs[n]));         fillList;      } catch (IOException e)      {         JOptionPane.showMessageDialog(this, Messages.getString("AttachmentRemoveErrorDialogMessage"), Messages.getString("AttachmentRemoveErrorDialogTitle"), JOptionPane.ERROR_MESSAGE); //$NON-NLS-1$ //$NON-NLS-2$         e.printStackTrace;      }   }      void exportFile   {      int [] idxs = list.getSelectedIndices;      if (idxs.length < 1)      {         JOptionPane.showMessageDialog(this, Messages.getString("AttachmentNoFileExportErrorDialogMessage"), Messages.getString("AttachmentNoFileExportErrorDialogTitle"), JOptionPane.ERROR_MESSAGE); //$NON-NLS-1$ //$NON-NLS-2$         return;      }      if (idxs.length > 1)      {         JOptionPane.showMessageDialog(this, Messages.getString("AttachmentMultiFileExportErrorDialogMessage"), Messages.getString("AttachmentMultiFileExportDialogTitle"), JOptionPane.ERROR_MESSAGE); //$NON-NLS-1$ //$NON-NLS-2$         return;      }            try {         byte [] data = attachManager.getAttachmentData((String)listModel.get(idxs[0]));         if (data == null) return;  // Should never happen                  RandomAccessInterface output = TarRandomAccessFile.userSaveFile(this, null);         if (output != null)         {            output.write(data);            output.close;         }      } catch (IOException e)      {         JOptionPane.showMessageDialog(this, Messages.getString("AttachmentExportErrorDialogMessage"), Messages.getString("AttachmentExportErrorDialogTitle"), JOptionPane.ERROR_MESSAGE); //$NON-NLS-1$ //$NON-NLS-2$         e.printStackTrace;      }   }      void fillList throws IOException   {      listModel.clear;      Iterator it = attachManager.getAttachmentList.iterator;      while (it.hasNext)         listModel.addElement((String)it.next);   }    public AttachmentsDialog(Dialog parent, String title, String listName, TarFile tf, boolean isAddText, String attachmentDir) throws IOException   {      super(parent, title, true);      attachManager = new AttachmentsManager(tf, attachmentDir);      createDialog(isAddText, listName);      fillList;   }      public AttachmentsDialog(Frame parent, String title, String listName, TarFile tf, boolean isAddText, String attachmentDir) throws IOException   {      super(parent, title, true);      attachManager = new AttachmentsManager(tf, attachmentDir);      createDialog(isAddText, listName);      fillList;   }       public static void OpenForAttachments(Frame parent, TarFile tf) throws IOException   {      AttachmentsDialog ad = null;      try {	      ad = new AttachmentsDialog(parent, Messages.getString("AttachmentsDialogTitle"), Messages.getString("AttachmentsDialogListTitle"), tf, false, ATTACHMENT_DIR); //$NON-NLS-1$ //$NON-NLS-2$		      ad.pack;	      ad.setLocationRelativeTo(parent);	      ad.setVisible(true);	      ad.dispose;	      ad = null;      }      finally       {         if (ad != null)            ad.dispose;      }   }   public static void OpenForLibraries(Frame parent, TarFile tf) throws IOException   {      AttachmentsDialog ad = null;      try {          ad = new AttachmentsDialog(parent, Messages.getString("LibrariesDialogTitle"), Messages.getString("LibrariesDialogListTitle"), tf, true, LIBRARY_DIR); //$NON-NLS-1$ //$NON-NLS-2$              ad.pack;          ad.setLocationRelativeTo(parent);          ad.setVisible(true);          ad.dispose;          ad = null;      }      finally       {         if (ad != null)            ad.dispose;      }   }