[Tinyos-2-commits] CVS: tinyos-2.x/apps/AntiTheft/java
AntiTheftGui.java, 1.2, 1.3 BagPanel.java, 1.1, 1.2
David Gay
idgay at users.sourceforge.net
Wed Apr 4 15:29:31 PDT 2007
Update of /cvsroot/tinyos/tinyos-2.x/apps/AntiTheft/java
In directory sc8-pr-cvs10.sourceforge.net:/tmp/cvs-serv17082/java
Modified Files:
AntiTheftGui.java BagPanel.java
Log Message:
comment updates
Index: AntiTheftGui.java
===================================================================
RCS file: /cvsroot/tinyos/tinyos-2.x/apps/AntiTheft/java/AntiTheftGui.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -d -r1.2 -r1.3
*** AntiTheftGui.java 4 Apr 2007 22:06:22 -0000 1.2
--- AntiTheftGui.java 4 Apr 2007 22:29:29 -0000 1.3
***************
*** 48,55 ****
public class AntiTheftGui implements MessageListener, Messenger {
! MoteIF mote;
! JFrame frame;
! JTextArea mssgArea;
! JTextField fieldInterval;
JCheckBox detDarkCb, detAccelCb, repLedCb, repSirenCb, repServerCb,
repNeighboursCb;
--- 48,59 ----
public class AntiTheftGui implements MessageListener, Messenger {
! MoteIF mote; // For talking to the antitheft root node
!
! /* Various swing components we need to use after initialisation */
! JFrame frame; // The whole frame
! JTextArea mssgArea; // The message area
! JTextField fieldInterval; // The requested check interval
!
! /* The checkboxes for the requested settings */
JCheckBox detDarkCb, detAccelCb, repLedCb, repSirenCb, repServerCb,
repNeighboursCb;
***************
*** 58,61 ****
--- 62,67 ----
try {
guiInit();
+ /* Setup communication with the mote and request a messageReceived
+ callback when an AlertMsg is received */
mote = new MoteIF(this);
mote.registerListener(new AlertMsg(), this);
***************
*** 67,70 ****
--- 73,78 ----
}
+ /* Build up the GUI using Swing magic. Nothing very exciting here - the
+ BagPanel class makes the code a bit cleaner/easier to read. */
private void guiInit() throws Exception {
JPanel mainPanel = new JPanel(new BorderLayout());
***************
*** 72,75 ****
--- 80,84 ----
mainPanel.setPreferredSize(new Dimension(500, 300));
+ /* The message area */
JScrollPane mssgPanel = new JScrollPane();
mssgPanel.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
***************
*** 80,83 ****
--- 89,93 ----
mssgPanel.getViewport().add(mssgArea, null);
+ /* The button area */
BagPanel buttonPanel = new BagPanel();
GridBagConstraints c = buttonPanel.c;
***************
*** 109,113 ****
fieldInterval.setText(Integer.toString(Constants.DEFAULT_CHECK_INTERVAL));
- // Send settings button
ActionListener settingsAction = new ActionListener() {
public void actionPerformed(ActionEvent e) {
--- 119,122 ----
***************
*** 119,123 ****
mainPanel.add(buttonPanel, BorderLayout.EAST);
! // The frame part
frame = new JFrame("AntiTheft");
frame.setSize(mainPanel.getPreferredSize());
--- 128,132 ----
mainPanel.add(buttonPanel, BorderLayout.EAST);
! /* The frame part */
frame = new JFrame("AntiTheft");
frame.setSize(mainPanel.getPreferredSize());
***************
*** 129,132 ****
--- 138,148 ----
}
+ /* Add a message to the message area, auto-scroll to end */
+ public synchronized void message(String s) {
+ mssgArea.append(s + "\n");
+ mssgArea.setCaretPosition(mssgArea.getDocument().getLength());
+ }
+
+ /* Popup an error message */
void error(String msg) {
JOptionPane.showMessageDialog(frame, msg, "Error",
***************
*** 134,137 ****
--- 150,157 ----
}
+ /* User pressed the "Update" button. Read the GUI fields and
+ send a SettingsMsg with the requested values. When the
+ requested settings are bad, we silently update them to sane
+ values. */
public void updateSettings() {
SettingsMsg smsg = new SettingsMsg();
***************
*** 140,144 ****
int checkInterval = Constants.DEFAULT_CHECK_INTERVAL;
! /* Extract current interval value, ignoring bad values */
String intervalS = fieldInterval.getText().trim();
try {
--- 160,164 ----
int checkInterval = Constants.DEFAULT_CHECK_INTERVAL;
! /* Extract current interval value, fixing bad values */
String intervalS = fieldInterval.getText().trim();
try {
***************
*** 152,155 ****
--- 172,176 ----
}
+ /* Extract alert settings */
if (repLedCb.isSelected())
alert |= Constants.ALERT_LEDS;
***************
*** 166,169 ****
--- 187,191 ----
}
+ /* Extract detection settings */
if (detDarkCb.isSelected())
detect |= Constants.DETECT_DARK;
***************
*** 176,183 ****
}
smsg.set_alert(alert);
smsg.set_detect(detect);
smsg.set_checkInterval(checkInterval);
-
try {
mote.send(MoteIF.TOS_BCAST_ADDR, smsg);
--- 198,205 ----
}
+ /* Build and send settings message */
smsg.set_alert(alert);
smsg.set_detect(detect);
smsg.set_checkInterval(checkInterval);
try {
mote.send(MoteIF.TOS_BCAST_ADDR, smsg);
***************
*** 188,207 ****
}
! public static void main(String[] args) {
! AntiTheftGui me = new AntiTheftGui();
! }
!
! synchronized public void messageReceived(int dest_addr, Message msg) {
if (msg instanceof AlertMsg) {
AlertMsg alertMsg = (AlertMsg)msg;
!
! String warning = "Theft of " + alertMsg.get_stolenId() + "\n";
! mssgArea.append(warning);
! mssgArea.setCaretPosition(mssgArea.getDocument().getLength());
}
}
! public void message(String s) {
! error(s);
}
}
--- 210,225 ----
}
! /* Message received from mote network. Update message area if it's
! a theft message. */
! public void messageReceived(int dest_addr, Message msg) {
if (msg instanceof AlertMsg) {
AlertMsg alertMsg = (AlertMsg)msg;
! message("Theft of " + alertMsg.get_stolenId());
}
}
! /* Just start the app... */
! public static void main(String[] args) {
! AntiTheftGui me = new AntiTheftGui();
}
}
Index: BagPanel.java
===================================================================
RCS file: /cvsroot/tinyos/tinyos-2.x/apps/AntiTheft/java/BagPanel.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -d -r1.1 -r1.2
*** BagPanel.java 4 Apr 2007 21:01:11 -0000 1.1
--- BagPanel.java 4 Apr 2007 22:29:29 -0000 1.2
***************
*** 30,33 ****
--- 30,36 ----
GridBagConstraints c;
+ /* Create a panel with a bag layout. Create some constraints are
+ users can modify prior to creating widgets - the current constraints
+ will be applied to all widgets created with makeXXX */
public BagPanel() {
bag = new GridBagLayout();
***************
*** 36,39 ****
--- 39,46 ----
}
+ /* The makeXXX methods create XXX widgets, apply the current constraints
+ to them, and add them to this panel. The widget is returned in case
+ the creator needs to hang on to it. */
+
public JButton makeButton(String label, ActionListener action) {
JButton button = new JButton();
***************
*** 72,79 ****
}
! public void addSeparator(int axis) {
JSeparator sep = new JSeparator(axis);
bag.setConstraints(sep, c);
add(sep);
}
--- 79,87 ----
}
! public JSeparator makeSeparator(int axis) {
JSeparator sep = new JSeparator(axis);
bag.setConstraints(sep, c);
add(sep);
+ return sep;
}
More information about the Tinyos-2-commits
mailing list