/** *Class PrivateDialog * *Internetprogrammering 2 - Course * *@author Martin Carlsson * **/ import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.text.*; /** *Class PrivateDialog is a graphical class that when created shows a dialog box *containing a message to for a user. *It is used in the Magnificent Chat application for sending private messages. */ public class PrivateDialog extends JDialog implements ActionListener { private JButton cancel = new JButton("Cancel"); private JButton send = new JButton("Send"); private JTextArea messageArea; private JScrollPane messageAreaPane = new JScrollPane(messageArea); private JTextArea inputArea = new JTextArea(); private JScrollPane inputAreaPane = new JScrollPane(inputArea); private JPanel centralPanel; private JPanel southPanel; private JPanel northPanel; public String message, from; Container c; boolean modal; Frame f; MagnificentChatApplet gui; /** *Constructs a dialog box *@param f the frame the dialog box should open on *@param message message that is shown in the dialog box *@param from the name of the client that sent the message *@param modal should current thread stop when a dialog box is created? *@param gui reference to the GUI class */ public PrivateDialog(Frame f, String message, String from, boolean modal, MagnificentChatApplet gui) { super(f, "Private message from: " + from, modal); this.f = f; this.message = message; this.from = from; this.modal = modal; this.gui = gui; setPreferredSize(new Dimension (270,320)); setResizable(false); c = getContentPane(); centralPanel = new JPanel(); southPanel = new JPanel(); northPanel = new JPanel(); send.addActionListener(this); cancel.addActionListener(this); messageArea = new JTextArea(message); messageArea.setLineWrap(true); messageArea.setToolTipText("Private message received"); messageArea.setBorder(BorderFactory.createEmptyBorder(4,4,4,4)); // Invisible border around the message area messageArea.setEditable(false); messageArea.setBackground(Color.black); messageArea.setForeground(Color.white); DefaultCaret msgCaret = (DefaultCaret) messageArea.getCaret(); msgCaret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE); messageAreaPane = new JScrollPane(messageArea); messageAreaPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); messageAreaPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); messageAreaPane.setPreferredSize(new Dimension(200,100)); messageArea.setWrapStyleWord(true); centralPanel.add(inputAreaPane); southPanel.add(send); southPanel.add(cancel); southPanel.setPreferredSize(new Dimension(200,40)); inputArea.setLineWrap(true); inputArea.setWrapStyleWord(true); inputArea.setBorder(BorderFactory.createEmptyBorder(4,4,4,4)); // Invisible border around the input area inputAreaPane.setPreferredSize(new Dimension(200,100)); inputAreaPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); inputAreaPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); northPanel.add(messageAreaPane); c.add(southPanel,BorderLayout.SOUTH); c.add(centralPanel, BorderLayout.CENTER); c.add(northPanel,BorderLayout.NORTH); pack(); setVisible(true); setLocation(f.getLocation().x+f.getWidth()/2-getWidth()/2, f.getLocation().y+f.getHeight()/2-getHeight()/2); } /** *Listens to the buttons in the dialog box *@param event event generated by a user */ public void actionPerformed(ActionEvent event) { if(event.getSource() == send) { String myMessage = inputArea.getText().replace("\n"," "); if(myMessage != null && !myMessage.equals("")) { if(gui.isUserOnline(from)) { gui.sendToServer(from + ","+myMessage,gui.PRIVATE); setVisible(false); } else { inputArea.append("\n\nNO MESSAGE SENT!\n\""+ from + "\" is not online!"); from = gui.user; } } else Toolkit.getDefaultToolkit().beep(); } else if(event.getSource() == cancel) setVisible(false); } } // End class PrivateDialog