import java.awt.Button; import java.awt.Color; import java.awt.FlowLayout; import java.awt.Frame; import java.awt.GridLayout; import java.awt.Label; import java.awt.Panel; import java.awt.ScrollPane; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.WindowAdapter; public class MailList1 extends Frame { // +-----------+----------------------------------------------- // | Constants | // +-----------+ /** The width of one message. */ private static final int MESSAGE_WIDTH = 400; // +--------+-------------------------------------------------- // | Fields | // +--------+ /** A scrolling area set up to show the list of messages. */ private ScrollPane holder; /** The panel that actually holds the messages. */ private Panel messages; /** The number of messages. */ int numMessages; /** The panel that contains the currently-active message. */ Panel currentPanel; /** The number of the currently-active message. */ int currentMessage; Label compose; // +--------------+-------------------------------------------- // | Constructors | // +--------------+ /** Set up an empty list of messages. */ public MailList1(String listName) { // Set up the frame. super(listName); // The number of messages numMessages = 0; // Prepare a scrolling area to hold the message list. holder = new ScrollPane(ScrollPane.SCROLLBARS_AS_NEEDED); holder.setSize(MESSAGE_WIDTH+20,50); // out1 = new Label("Reply"); // out2 = new Label("Send"); // Prepare a panel to hold the actual messages. messages = new Panel(new GridLayout(0,1)); // holder.add(out1); // holder.add(out2); holder.add(messages); this.add(holder); this.setSize(MESSAGE_WIDTH+400,700); this.show(); } // MailList(String listName) // +---------+------------------------------------------------- // | Methods | // +---------+ /** * Add a from/subject pair. */ public void addMessage(String name, String subject, String date, String time) { // Increment the number of messages ++numMessages; // Set up a button Button clickMe = new Button("" + numMessages); // Prepare labels for the two parts of the message. Label namePart = new Label(name); Label subjectPart = new Label(subject); Label datePart = new Label (date); Label timePart = new Label (time); // Prepare a panel to hold the whole message. Panel message = new Panel(); message.setLayout(new GridLayout(1,3)); // Set up the two parts. namePart.setSize(MESSAGE_WIDTH/4, 10); namePart.setAlignment(Label.LEFT); subjectPart.setSize(MESSAGE_WIDTH/4, 10); subjectPart.setAlignment(Label.LEFT); datePart.setSize(MESSAGE_WIDTH/4, 10); datePart.setAlignment(Label.LEFT); timePart.setSize(MESSAGE_WIDTH/4, 10); timePart.setAlignment(Label.LEFT); // Add them to the panel in order, and then set up the panel. message.add(clickMe); message.add(namePart); message.add(subjectPart); message.add(datePart); message.add(timePart); message.setSize(MESSAGE_WIDTH,10); clickMe.addActionListener(new MessageListener(this, numMessages)); // Add the mesage to the message list. messages.add(message); this.show(); } // addMessage public void addLabel(String mail, String from, String sub, String dat, String tim){ Label comp = new Label(mail); Label fr = new Label(from); Label subj = new Label (sub); Label data = new Label(dat); Label tima = new Label(tim); Panel message = new Panel(); message.setLayout(new GridLayout(1,3)); comp.setSize(MESSAGE_WIDTH/5, 10); comp.setAlignment(Label.LEFT); fr.setSize(MESSAGE_WIDTH/5, 10); fr.setAlignment(Label.LEFT); subj.setSize(MESSAGE_WIDTH/5, 10); subj.setAlignment(Label.LEFT); data.setSize(MESSAGE_WIDTH/5, 10); data.setAlignment(Label.LEFT); tima.setSize(MESSAGE_WIDTH/5, 10); tima.setAlignment(Label.LEFT); message.add(comp); message.add(fr); message.add(subj); message.add(data); message.add(tima); message.setSize(MESSAGE_WIDTH,10); messages.add(message); this.show(); } public void addButton (String delete, String folders, String compose, String reload, String preferences, String Help) { Button button1 = new Button(delete); Button button2 = new Button(folders); Button button3 = new Button(compose); Button button4 = new Button(reload); Button button5 = new Button(preferences); Button button6 = new Button(Help); Panel message = new Panel(); message.setLayout(new GridLayout (1,3)); message.add(button1); message.add(button2); message.add(button3); message.add(button4); message.add(button5); message.add(button6); message.setSize(MESSAGE_WIDTH,10); button1.addActionListener(new MessageListener(this, numMessages)); button2.addActionListener(new MessageListener(this, numMessages)); button3.addActionListener(new MessageListener(this, numMessages)); button4.addActionListener(new MessageListener(this, numMessages)); button5.addActionListener(new MessageListener(this, numMessages)); button6.addActionListener(new MessageListener(this, numMessages)); messages.add(message); this.show(); } /** * "Activate" one of the messages (changing its background color). */ public void activate(int messageNumber) { System.err.println("Activating " + messageNumber); } // activate(Panel,int) // +------+---------------------------------------------------- // | Main | // +------+ /** A test. Create a list with a few messages. */ public static void main(String[] args) { MailList1 example = new MailList1("Inbox"); example.addButton("Delete", "Folders", "Compose", "Reload" , "Preferences", "Help"); example.addLabel("Message", "From","Subject", "Date", "Time"); example.addMessage("Sam Rebelsky", "A Test", "12/08/99", "13.43"); example.addMessage("Bill Gates", "Another Test", "14/08/99", "21.34" ); example.addMessage("Ashfaqur Rahman", "Help!","14/08/99", "21.09" ); example.addMessage("Mike Yohay", "More Help!", "14/08/99", "21.09" ); example.addMessage("Jared Baszler", "Re: Help!", "09/08/99", "14.45"); } // main(String[]) } // class MailList /** * A helper class, intended (right now) to set the color of a panel to *show that * it's been clicked. */ class MessageListener extends MouseAdapter implements ActionListener { MailList1 list; int messageNumber; public MessageListener(MailList1 list, int messageNumber) { this.list = list; this.messageNumber = messageNumber; } public void actionPerformed(ActionEvent e) { list.activate(messageNumber); String command = e.getActionCommand(); if (command.equals("Compose")) { new Compose1(); } else if (command.equals("1")) { new ComposeV(); } else if (command.equals("2")) { new ComposeV(); } else if (command.equals("3")) { new ComposeV(); } else if (command.equals("4")) { new ComposeV(); } else if (command.equals("5")) { new ComposeV(); } else if (command.equals("Reload")) { MailList1 example = new MailList1("Inbox"); example.addButton("Delete", "Folders", "Compose", "Reload" , "Preferences", "Help"); example.addLabel("Message", "From","Subject", "Date", "Time"); example.addMessage("Sam Rebelsky", "A Test", "12/08/99", "13.43"); example.addMessage("Bill Gates", "Another Test", "14/08/99", "21.34" ); example.addMessage("Ashfaqur Rahman", "Help!","14/08/99", "21.09" ); example.addMessage("Mike Yohay", "More Help!", "14/08/99", "21.09" ); example.addMessage("Jared Baszler", "Re: Help!", "09/08/99", "14.45"); } else if(command.equals("Folders")) { new Folders(); } else if(command.equals("Preferences")) { new Preferences(); } else if(command.equals("Help")) { new Help(); } } //actionPerformed(e) } // class mailList1