hemanth.balaji 0 Newbie Poster

Hi Guys,
I was doing a Mail Program and Got stuck in the following questions. I don't know How to approach.

1. Right Click pop Up Menu for Attachments List and adding pop up listener for this. Some kind of a right click context menu for each item in the list box having attachment file names to "add" or "remove" from the list
2. Adding a new attachment subsequent time will remove the previous Attachment. How do I prevent that add on to the already listed attachments.

/*
 1. Right Click pop Up Menu for Attachments List and adding pop up listener for this.
 2. Cancel Button Needs to be clicked twice to Close the dialog
 3. Close Button for dialog not working 
 4. Adding a new attachment subsequent time will remove the previous Attachment
 */
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.HashMap;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;

public class TestClient extends JDialog {

	private JPanel mailPanel = null;
	private JTextArea jtaMessage = null;
	private JButton jbtCancel = null;
	private JButton jbtAttachment = null;
	private JList jlstAttachedFiles = null;
	private JScrollPane jlstAttachedFilesPane = null;
	private JFileChooser jfcOpenChooser = null;
	private JPopupMenu jpopupAttachment = null;
	private JMenuItem jmenuitemAddAttachment = null;
	private JMenuItem jmenuitemRemoveAttachment = null;
	private HashMap attachedFiles = null;
	private static final String windows = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
	public TestClient(Frame frame, String title, boolean modal) {
		super(frame, title, modal);
		init();
	}
	public TestClient() {
		this((Frame) null, "", false);
	}
	private void init() {
		try {
			UIManager.setLookAndFeel(windows);
		} catch (Exception e1) {
			;
		}
		JFrame.setDefaultLookAndFeelDecorated(true);
		this.setSize(400, 400);
		this.setLayout(new GridBagLayout());
		GridBagConstraints toolbarCons = new GridBagConstraints();
		toolbarCons = new GridBagConstraints(0, 0, 1, 1, 0.1, 0,
				GridBagConstraints.WEST, GridBagConstraints.BOTH,
				new Insets(5, 5, 0, 5), 0, 0);
		jlstAttachedFiles = new JList();
		jlstAttachedFilesPane = new JScrollPane(jlstAttachedFiles);
		jlstAttachedFilesPane.setPreferredSize(new Dimension(150, 75));
		jbtAttachment = new JButton("Attachments");
		jbtCancel = new JButton("Cancel");
		jtaMessage = new JTextArea();
		jtaMessage.setPreferredSize(new java.awt.Dimension(300, 300));
		JScrollPane mailMessage = new JScrollPane(jtaMessage);
		jtaMessage.setLineWrap(true);
		jfcOpenChooser = new JFileChooser();
		jpopupAttachment = new JPopupMenu();
		jpopupAttachment.add(new JMenuItem("Add Attachment"));
		jpopupAttachment.add(new JMenuItem("Remove Attachment"));
		jmenuitemAddAttachment = new JMenuItem("Add Attachment");
		jmenuitemRemoveAttachment = new JMenuItem("Remove Attachment");
		mailPanel = new JPanel();
		mailPanel.setLayout(new GridBagLayout());
		GridBagConstraints cons = new GridBagConstraints();
		cons = new GridBagConstraints(0, 4, 1, 1, 0.2, 0.1,
				GridBagConstraints.WEST, GridBagConstraints.NONE,
				new Insets(5, 5, 0, 5), 0, 0);
		mailPanel.add(jbtAttachment, cons);
		cons = new GridBagConstraints(1, 4, 4, 1, 0.8, 0.1,
				GridBagConstraints.WEST, GridBagConstraints.NONE,
				new Insets(5, 5, 0, 5), 0, 0);
		mailPanel.add(jlstAttachedFilesPane, cons);
		cons = new GridBagConstraints(1, 5, 4, 4, 0.8, 0.4,
				GridBagConstraints.WEST, GridBagConstraints.BOTH,
				new Insets(5, 5, 0, 5), 0, 0);
		mailPanel.add(mailMessage, cons);
		cons = new GridBagConstraints(1, 9, 1, 1, 0.2, 0.1,
				GridBagConstraints.WEST, GridBagConstraints.NONE,
				new Insets(5, 5, 0, 5), 0, 0);
		mailPanel.add(jbtCancel, cons);
		jbtCancel.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				TestClient.this.dispose();
			}
		});
		jbtAttachment.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				jbtAttachment_actionPerformed(e);
			}
		});
		this.getContentPane().add(mailPanel, toolbarCons);
		this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		GridBagConstraints mainPanelCons = new GridBagConstraints();
		mainPanelCons = new GridBagConstraints(0, 1, 1, 1, 0.1, 0,
				GridBagConstraints.WEST, GridBagConstraints.BOTH,
				new Insets(5, 5, 0, 5), 0, 0);
		this.getContentPane().add(mailPanel, mainPanelCons);
		this.pack();
		this.setVisible(true);
	}
	private void jbtOK_actionPerformed(ActionEvent e) {
	}
	private void jbtAttachment_actionPerformed(ActionEvent e) {
		jfcOpenChooser.setMultiSelectionEnabled(true);
		jfcOpenChooser.showOpenDialog(null);
		File[] fileArray = jfcOpenChooser.getSelectedFiles();
		String[] fileNames = new String[fileArray.length];
		attachedFiles = new HashMap();
		for (int i = 0; i < fileArray.length; i++) {
			File file = fileArray[i];
			attachedFiles
					.put(file.getPath().trim(), file.getName().trim()); // (Path,
																		// Name)
			fileNames[i] = file.getName().trim();
		}
		jlstAttachedFiles.setListData(fileNames);
	}
	public static void main(String args[]) {
		try {
			UIManager.setLookAndFeel(windows);
		} catch (Exception e1) {
			;
		}
		TestClient mc = new TestClient(null, "Mail Client", true);
	}
}