Go to most recent revision | Details | Last modification | View Log | SVN | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 15 | arune | 1 | /* |
| 2 | * File created by Jimmy |
||
| 3 | * at 2003-aug-30 14:31:00 |
||
| 4 | */ |
||
| 5 | |||
| 6 | package Macbeth.Modules.modPacketSender; |
||
| 7 | |||
| 8 | import javax.swing.*; |
||
| 9 | import java.awt.*; |
||
| 10 | import java.awt.event.*; |
||
| 11 | import Macbeth.System.*; |
||
| 12 | import Macbeth.Utilities.MySystem; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * A Macbeth module that allows the user to write and send |
||
| 16 | * custom packets from a graphical user interface. |
||
| 17 | * @author Jimmy |
||
| 18 | */ |
||
| 19 | public class modPacketSender extends MbModule implements MbModuleGUI { |
||
| 20 | private GUI gui; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Creates a new instance of modPacketSender. |
||
| 24 | */ |
||
| 25 | public modPacketSender() { |
||
| 26 | super(); |
||
| 27 | gui = new GUI(); |
||
| 28 | } |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Gets the name of the component. |
||
| 32 | * @return The name of the component. |
||
| 33 | */ |
||
| 34 | public String name() { |
||
| 35 | return "PacketSender"; |
||
| 36 | } |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Gets the description of the component. |
||
| 40 | * @return The description of the component. |
||
| 41 | */ |
||
| 42 | public String description() { |
||
| 43 | return "Allows you to send custom packets"; |
||
| 44 | } |
||
| 45 | |||
| 46 | /** |
||
| 47 | * This method sets default values on all _required_ data |
||
| 48 | * fields in the data repository. |
||
| 49 | */ |
||
| 50 | protected void initDataFields() { |
||
| 51 | //this module doesn't require any data fields |
||
| 52 | } |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Starts up this module. |
||
| 56 | */ |
||
| 57 | public void startup() throws MbStartupException { |
||
| 58 | super.startup(); |
||
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Shuts down this module. |
||
| 63 | */ |
||
| 64 | public void shutdown() { |
||
| 65 | super.shutdown(); |
||
| 66 | } |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Will be called when a packet needs to be handled. |
||
| 70 | * @param p The packet that needs to be handled. |
||
| 71 | */ |
||
| 72 | public void handlePacket(MbPacket p) { |
||
| 73 | //we don't care about incoming packets |
||
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Will be called when our GUI should be shown. |
||
| 78 | */ |
||
| 79 | public void renderGUI(Container drawArea) { |
||
| 80 | gui.render(drawArea); |
||
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * A class for our GUI. |
||
| 85 | */ |
||
| 86 | private class GUI implements ActionListener { |
||
| 87 | private JLabel lblDestinationKernel; |
||
| 88 | private JLabel lblDestinationModule; |
||
| 89 | private JLabel lblContents; |
||
| 90 | private JTextField txtDestinationKernel; |
||
| 91 | private JTextField txtDestinationModule; |
||
| 92 | private JTextArea txtContents; |
||
| 93 | private JScrollPane txtContentsScrolled; |
||
| 94 | private JButton btnSend; |
||
| 95 | |||
| 96 | public void render(Container drawArea) { |
||
| 97 | lblDestinationKernel = new JLabel("Destination kernel:"); |
||
| 98 | lblDestinationModule = new JLabel("Destination module:"); |
||
| 99 | lblContents = new JLabel("Packet contents (XML):"); |
||
| 100 | txtDestinationKernel = new JTextField(15); |
||
| 101 | txtDestinationKernel.setText("self"); |
||
| 102 | txtDestinationModule = new JTextField(15); |
||
| 103 | txtDestinationModule.setText("PacketReceiver"); |
||
| 104 | txtContents = new JTextArea(5,15); |
||
| 105 | txtContents.setText("<test>" + MySystem.lineBreak + "XML-data only!" + MySystem.lineBreak + "</test>" + MySystem.lineBreak + "<wee attribute=\"value\"/>"); |
||
| 106 | txtContentsScrolled = new JScrollPane(txtContents); |
||
| 107 | btnSend = new JButton("Send"); |
||
| 108 | btnSend.setActionCommand("send"); |
||
| 109 | btnSend.addActionListener(this); |
||
| 110 | |||
| 111 | GridBagConstraints c = new GridBagConstraints(); |
||
| 112 | drawArea.setLayout(new GridBagLayout()); |
||
| 113 | c.ipadx = 2; |
||
| 114 | c.ipady = 2; |
||
| 115 | c.weightx = 1; |
||
| 116 | c.weighty = 0; |
||
| 117 | c.fill = GridBagConstraints.HORIZONTAL; |
||
| 118 | c.gridy = 0; |
||
| 119 | drawArea.add(lblDestinationKernel,c); |
||
| 120 | c.gridy = 1; |
||
| 121 | drawArea.add(txtDestinationKernel,c); |
||
| 122 | c.gridy = 2; |
||
| 123 | drawArea.add(lblDestinationModule,c); |
||
| 124 | c.gridy = 3; |
||
| 125 | drawArea.add(txtDestinationModule,c); |
||
| 126 | c.gridy = 4; |
||
| 127 | drawArea.add(lblContents,c); |
||
| 128 | c.fill = GridBagConstraints.BOTH; |
||
| 129 | c.gridy = 5; |
||
| 130 | c.weighty = 1; |
||
| 131 | drawArea.add(txtContentsScrolled,c); |
||
| 132 | c.fill = GridBagConstraints.HORIZONTAL; |
||
| 133 | c.gridy = 6; |
||
| 134 | c.weighty = 0; |
||
| 135 | drawArea.add(btnSend,c); |
||
| 136 | } |
||
| 137 | |||
| 138 | public void actionPerformed(ActionEvent e) { |
||
| 139 | if (e.getActionCommand().equals("send")) { |
||
| 140 | if (!txtDestinationKernel.getText().equals("") && !txtDestinationModule.getText().equals("") && !txtContents.getText().equals("")) { |
||
| 141 | MbPacket p = new MbPacket(); |
||
| 142 | p.setDestination(new MbLocation(txtDestinationKernel.getText(), txtDestinationModule.getText())); |
||
| 143 | p.setContents(txtContents.getText()); |
||
| 144 | sendPacket(p); |
||
| 145 | } |
||
| 146 | } |
||
| 147 | } |
||
| 148 | } |
||
| 149 | } |