Subversion Repositories HomeAutomation

Rev

Details | Last modification | View Log | SVN | RSS feed

Rev Author Line No. Line
15 arune 1
/*
2
 * modPacketReceiver.java
3
 *
4
 * Created on den 30 augusti 2003, 22:00
5
 */
6
package Macbeth.Modules.modPacketReceiver;
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 shows all incoming packets
16
 * in a textbox inside its graphical user interface.
17
 * @author Jimmy
18
 */
19
public class modPacketReceiver extends MbModule implements MbModuleGUI {
20
    //A GUI for our module.
21
    private GUI gui;
22
    //All packet contents delivered to this module.
23
    private String receivedData = "";
24
 
25
    /**
26
     * Creates a new instance of modPacketReceiver
27
     */
28
    public modPacketReceiver() {
29
        super();
30
        gui = new GUI();
31
    }
32
 
33
    /**
34
     * Gets the name of the component.
35
     * @return The name of the component.
36
     */
37
    public String name() {
38
        return "PacketReceiver";
39
    }
40
 
41
    /**
42
     * Gets the description of the component.
43
     * @return The description of the component.
44
     */
45
    public String description() {
46
        return "Shows all incoming packets in a textbox";
47
    }
48
 
49
    /**
50
     * This method sets default values on all _required_ data
51
     * fields in the data repository.
52
     */
53
    protected void initDataFields() {
54
        //this module doesn't require any data fields
55
    }
56
 
57
    /**
58
     * Starts up this module.
59
     */
60
    public void startup() throws MbStartupException {
61
        //start up MbModule
62
        super.startup();
63
    }
64
 
65
    /**
66
     * Shuts down this module.
67
     */
68
    public void shutdown() {
69
        //shut down MbModule
70
        super.shutdown();
71
    }
72
 
73
    /**
74
     * Will be called when a packet needs to be handled.
75
     * @param p The packet that needs to be handled.
76
     */
77
    public void handlePacket(MbPacket p) {
78
        receivedData += p.serializeToString() + MySystem.lineBreak + MySystem.lineBreak;
79
        gui.updateText();
80
    }
81
 
82
    /**
83
     * Will be called when our GUI should be shown.
84
     * @param drawArea The Container object onto which the
85
     * GUI objects should be added.
86
     */
87
    public void renderGUI(Container drawArea) {
88
        gui.render(drawArea);
89
    }
90
 
91
    /**
92
     * A class for our GUI.
93
     */
94
    private class GUI implements ActionListener {
95
        private JLabel      lblIncomingData;
96
        private JTextArea   txtIncomingData;
97
        private JScrollPane txtIncomingDataScrolled;
98
        private JButton     btnClear;
99
 
100
        public void render(Container renderArea) {
101
            lblIncomingData = new JLabel("Received data:");
102
            txtIncomingData = new JTextArea(6,15);
103
            txtIncomingData.setText(receivedData);
104
            txtIncomingDataScrolled = new JScrollPane(txtIncomingData);
105
            btnClear = new JButton("Clear history");
106
            btnClear.setActionCommand("clear");
107
            btnClear.addActionListener(this);
108
            renderArea.setLayout(new BorderLayout());
109
            renderArea.add(lblIncomingData,BorderLayout.NORTH);
110
            renderArea.add(txtIncomingDataScrolled,BorderLayout.CENTER);
111
            renderArea.add(btnClear,BorderLayout.SOUTH);
112
        }
113
 
114
        public void actionPerformed(ActionEvent e) {
115
            if (e.getActionCommand().equals("clear")) {
116
                receivedData = "";
117
                updateText();
118
            }
119
        }
120
 
121
        protected void updateText() {
122
            if (txtIncomingData!=null) {
123
                txtIncomingData.setText(receivedData);
124
                txtIncomingData.setCaretPosition(txtIncomingData.getDocument().getLength());
125
            }
126
        }
127
    }
128
}