import java.util.*;
/* The main GUI object. Build the GUI and coordinate all user activities */
-class Window
-{
+class Window {
Oscilloscope parent;
Graph graph;
-
+
Font smallFont = new Font("Dialog", Font.PLAIN, 8);
Font boldFont = new Font("Dialog", Font.BOLD, 12);
Font normalFont = new Font("Dialog", Font.PLAIN, 12);
/* A model for the mote table, and general utility operations on the mote
list */
class MoteTableModel extends AbstractTableModel {
- private ArrayList motes = new ArrayList();
- private ArrayList colors = new ArrayList();
+ private ArrayList<Integer> motes = new ArrayList<Integer>();
+ private ArrayList<Color> colors = new ArrayList<Color>();
/* Initial mote colors cycle through this list. Add more colors if
you want. */
Color.YELLOW, Color.GRAY, Color.YELLOW
};
int cycleIndex;
-
+
/* TableModel methods for achieving our table appearance */
public String getColumnName(int col) {
- if (col == 0)
+ if (col == 0) {
return "Mote";
- else
+ } else {
return "Color";
+ }
}
+
public int getColumnCount() { return 2; }
+
public synchronized int getRowCount() { return motes.size(); }
+
public synchronized Object getValueAt(int row, int col) {
- if (col == 0)
+ if (col == 0) {
return motes.get(row);
- else
+ } else {
return colors.get(row);
+ }
}
+
public Class getColumnClass(int col) {
return getValueAt(0, col).getClass();
}
- public boolean isCellEditable(int row, int col) { return col == 1; }
- public synchronized void setValueAt(Object value, int row, int col) {
- colors.set(row, value);
+
+ public boolean isCellEditable(int row, int col) {
+ return col == 1;
+ }
+
+ public synchronized void setValueAt(Object value, int row, int col) {
+ colors.set(row, (Color)value);
fireTableCellUpdated(row, col);
graph.repaint();
}
/* Return mote id of i'th mote */
- int get(int i) { return ((Integer)motes.get(i)).intValue(); }
+ int get(int i) { return (motes.get(i)).intValue(); }
/* Return color of i'th mote */
- Color getColor(int i) { return (Color)colors.get(i); }
-
+ Color getColor(int i) { return colors.get(i); }
+
/* Return number of motes */
int size() { return motes.size(); }
-
+
/* Add a new mote */
synchronized void newNode(int nodeId) {
/* Shock, horror. No binary search. */
int i, len = motes.size();
-
- for (i = 0; ; i++)
+
+ for (i = 0; ; i++) {
if (i == len || nodeId < get(i)) {
motes.add(i, new Integer(nodeId));
// Cycle through a set of initial colors
colors.add(i, cycle[cycleIndex++ % cycle.length]);
break;
}
+ }
fireTableRowsInserted(i, i);
}
-
+
/* Remove all motes */
void clear() {
- motes = new ArrayList();
- colors = new ArrayList();
+ motes = new ArrayList<Integer>();
+ colors = new ArrayList<Color>();
fireTableDataChanged();
}
- }
+ } /* End of MoteTableModel */
/* A simple full-color cell */
static class MoteColor extends JLabel implements TableCellRenderer {
public MoteColor() { setOpaque(true); }
public Component getTableCellRendererComponent
(JTable table, Object color,
- boolean isSelected, boolean hasFocus, int row, int column) {
+ boolean isSelected, boolean hasFocus,
+ int row, int column) {
setBackground((Color)color);
return this;
}
label.setFont(boldFont);
return label;
}
-
+
JLabel makeSmallLabel(String txt, int alignment) {
JLabel label = new JLabel(txt, alignment);
label.setFont(smallFont);
return label;
}
-
+
JTextField makeTextField(int columns, ActionListener action) {
JTextField tf = new JTextField(columns);
tf.setFont(normalFont);
main.setMinimumSize(new Dimension(500, 250));
main.setPreferredSize(new Dimension(800, 400));
-
+
// Three panels: mote list, graph, controls
moteListModel = new MoteTableModel();
JTable moteList = new JTable(moteListModel);
moteList.setDefaultRenderer(Color.class, new MoteColor());
- moteList.setDefaultEditor(Color.class, new ColorCellEditor("Pick Mote Color"));
+ moteList.setDefaultEditor(Color.class,
+ new ColorCellEditor("Pick Mote Color"));
moteList.setPreferredScrollableViewportSize(new Dimension(100, 400));
JScrollPane motePanel = new JScrollPane();
motePanel.getViewport().add(moteList, null);
main.add(motePanel, BorderLayout.WEST);
-
+
graph = new Graph(this);
main.add(graph, BorderLayout.CENTER);
-
+
// Controls. Organised using box layouts.
-
+
// Sample period.
JLabel sampleLabel = makeLabel("Sample period (ms):", JLabel.RIGHT);
sampleText = makeTextField(6, new ActionListener() {
public void actionPerformed(ActionEvent e) { setSamplePeriod(); }
} );
updateSamplePeriod();
-
+
// Clear data.
JButton clearButton = makeButton("Clear data", new ActionListener() {
public void actionPerformed(ActionEvent e) { clearData(); }
} );
-
+
// Adjust X-axis zoom.
Box xControl = new Box(BoxLayout.Y_AXIS);
xLabel = makeLabel("", JLabel.CENTER);
final JSlider xSlider = new JSlider(JSlider.HORIZONTAL, 0, 8, graph.scale);
- Hashtable xTable = new Hashtable();
- for (int i = 0; i <= 8; i += 2)
+ Hashtable<Integer, JLabel> xTable = new Hashtable<Integer, JLabel>();
+ for (int i = 0; i <= 8; i += 2) {
xTable.put(new Integer(i),
makeSmallLabel("" + (Graph.MIN_WIDTH << i),
JLabel.CENTER));
+ }
xSlider.setLabelTable(xTable);
xSlider.setPaintLabels(true);
graph.updateXLabel();
xSlider.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
//if (!xSlider.getValueIsAdjusting())
- graph.setScale((int)xSlider.getValue());
+ graph.setScale((int)xSlider.getValue());
}
});
xControl.add(xLabel);
xControl.add(xSlider);
-
+
// Adjust Y-axis range.
JLabel yLabel = makeLabel("Y:", JLabel.RIGHT);
yText = makeTextField(12, new ActionListener() {
public void actionPerformed(ActionEvent e) { setYAxis(); }
} );
yText.setText(graph.gy0 + " - " + graph.gy1);
-
+
Box controls = new Box(BoxLayout.X_AXIS);
controls.add(clearButton);
controls.add(Box.createHorizontalGlue());
String min = val.substring(0, dash).trim();
String max = val.substring(dash + 1).trim();
- if (!graph.setYAxis(Integer.parseInt(min), Integer.parseInt(max)))
- error("Invalid range " + min + " - " + max + " (expected values between 0 and 65535)");
+ if (!graph.setYAxis(Integer.parseInt(min), Integer.parseInt(max))) {
+ error("Invalid range "
+ + min
+ + " - "
+ + max
+ + " (expected values between 0 and 65535)");
+ }
return;
}
}
String periodS = sampleText.getText().trim();
try {
int newPeriod = Integer.parseInt(periodS);
- if (parent.setInterval(newPeriod))
+ if (parent.setInterval(newPeriod)) {
return;
+ }
}
catch (NumberFormatException e) { }
error("Invalid sample period " + periodS);