1 |
package uk.org.iscream.mnote; |
2 |
|
3 |
import java.net.UnknownHostException; |
4 |
import java.io.IOException; |
5 |
import javax.swing.JFrame; |
6 |
import javax.swing.JButton; |
7 |
import javax.swing.JOptionPane; |
8 |
import javax.swing.JPanel; |
9 |
import java.awt.event.ActionListener; |
10 |
import java.awt.event.ActionEvent; |
11 |
import java.awt.event.WindowAdapter; |
12 |
import java.awt.event.WindowEvent; |
13 |
|
14 |
class MNote extends JFrame implements NotificationListener { |
15 |
|
16 |
private static final String MULTICAST_GROUP = "228.5.6.7"; |
17 |
private static final int PORT = 6789; |
18 |
|
19 |
public static void main(String[] args) { |
20 |
MNoteProtocolHandler protocolHandler = new MNoteProtocolHandler(); |
21 |
MNoteSocketHandler socketHandler = null; |
22 |
try { |
23 |
socketHandler = new MNoteSocketHandler(PORT, MULTICAST_GROUP, protocolHandler); |
24 |
} catch (UnknownHostException e) { |
25 |
System.err.println("UNKNOWN HOST: " + e); |
26 |
} catch (IOException e2) { |
27 |
System.err.println("IO ERROR: " + e2); |
28 |
} |
29 |
MNote mNote = new MNote("MNote - Multicast Notification System", protocolHandler); |
30 |
} |
31 |
|
32 |
public MNote(String title, MNoteProtocolHandler protocolHandler) { |
33 |
super(title); |
34 |
_protocolHandler = protocolHandler; |
35 |
_protocolHandler.addNotificationListener(this); |
36 |
|
37 |
addWindowListener(new WindowAdapter() { |
38 |
public void windowClosing(WindowEvent e) {System.exit(0);} |
39 |
}); |
40 |
|
41 |
JButton newNote = new JButton("Send MNote message"); |
42 |
newNote.addActionListener(new ActionListener() { |
43 |
public void actionPerformed(ActionEvent e) { |
44 |
String message = JOptionPane.showInputDialog(null, "Please enter your MNote message:", "Compose MNote!", JOptionPane.INFORMATION_MESSAGE); |
45 |
if (message != null) { |
46 |
_protocolHandler.sendNotification(message); |
47 |
} |
48 |
} |
49 |
}); |
50 |
|
51 |
JPanel pane = new JPanel(); |
52 |
|
53 |
pane.add(newNote); |
54 |
|
55 |
this.getContentPane().add(pane); |
56 |
this.pack(); |
57 |
this.show(); |
58 |
} |
59 |
|
60 |
public void acceptNotificationEvent(NotificationEvent ne) { |
61 |
JOptionPane.showMessageDialog(this, "You have received a new MNote!\nMessage:\n" + ne.getMessage(), "New MNote!", JOptionPane.INFORMATION_MESSAGE); |
62 |
} |
63 |
|
64 |
private MNoteProtocolHandler _protocolHandler = null; |
65 |
} |