File: Window.java

package info (click to toggle)
tinyos 2.1.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, jessie, jessie-kfreebsd, stretch
  • size: 47,476 kB
  • ctags: 36,607
  • sloc: ansic: 63,646; cpp: 14,974; java: 10,358; python: 5,215; makefile: 1,724; sh: 902; asm: 597; xml: 392; perl: 74; awk: 46
file content (299 lines) | stat: -rw-r--r-- 9,362 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*
 * Copyright (c) 2006 Intel Corporation
 * All rights reserved.
 *
 * This file is distributed under the terms in the attached INTEL-LICENSE     
 * file. If you do not find these files, copies can be found by writing to
 * Intel Research Berkeley, 2150 Shattuck Avenue, Suite 1300, Berkeley, CA, 
 * 94704.  Attention:  Intel License Inquiry.
 */

import javax.swing.*;
import javax.swing.table.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

/* The main GUI object. Build the GUI and coordinate all user activities */
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);
    MoteTableModel moteListModel; // GUI view of mote list
    JLabel xLabel; // Label displaying X axis range
    JTextField sampleText, yText; // inputs for sample period and Y axis range
    JFrame frame;

    Window(Oscilloscope parent) {
    this.parent = parent;
    }

    /* 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();

    /* Initial mote colors cycle through this list. Add more colors if
       you want. */
    private Color[] cycle = {
        Color.RED, Color.WHITE, Color.GREEN, Color.MAGENTA,
        Color.YELLOW, Color.GRAY, Color.YELLOW
    };
    int cycleIndex;

    /* TableModel methods for achieving our table appearance */
    public String getColumnName(int col) {
        if (col == 0) {
        return "Mote";
        }
        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) {
        return motes.get(row);
        }
        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);
            fireTableCellUpdated(row, col);
        graph.repaint();
        }

    /* Return mote id of i'th mote */
    int get(int i) { return ((Integer)motes.get(i)).intValue(); }
    
    /* Return color of i'th mote */
    Color getColor(int i)  { return (Color)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++)
        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();
        fireTableDataChanged();
    }
    }

    /* 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) {
        setBackground((Color)color);
        return this;
    }
    }

    /* Convenience methods for making buttons, labels and textfields.
       Simplifies code and ensures a consistent style. */

    JButton makeButton(String label, ActionListener action) {
    JButton button = new JButton();
        button.setText(label);
        button.setFont(boldFont);
    button.addActionListener(action);
    return button;
    }

    JLabel makeLabel(String txt, int alignment) {
    JLabel label = new JLabel(txt, alignment);
    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);
    tf.setMaximumSize(tf.getPreferredSize());
    tf.addActionListener(action);
    return tf;
    }

    /* Build the GUI */
    void setup() {
    JPanel main = new JPanel(new BorderLayout());

    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.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)
        xTable.put(new Integer(i),
               makeSmallLabel("" + (Graph.MIN_WIDTH << i),
                      JLabel.CENTER));
    xSlider.setLabelTable(xTable);
    xSlider.setPaintLabels(true);
    graph.updateXLabel();
    graph.setScale(graph.scale);
    xSlider.addChangeListener(new ChangeListener() {
        public void stateChanged(ChangeEvent e) {
            //if (!xSlider.getValueIsAdjusting())
            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());
    controls.add(Box.createRigidArea(new Dimension(20, 0)));
    controls.add(sampleLabel);
    controls.add(sampleText);
    controls.add(Box.createHorizontalGlue());
    controls.add(Box.createRigidArea(new Dimension(20, 0)));
    controls.add(xControl);
    controls.add(yLabel);
    controls.add(yText);
    main.add(controls, BorderLayout.SOUTH);

    // The frame part
    frame = new JFrame("Oscilloscope");
    frame.setSize(main.getPreferredSize());
    frame.getContentPane().add(main);
    frame.setVisible(true);
    frame.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) { System.exit(0); }
        });
    }

    /* User operation: clear data */
    void clearData() {
    synchronized (parent) {
        moteListModel.clear();
        parent.clear();
        graph.newData();
    }
    }

    /* User operation: set Y-axis range. */
    void setYAxis() {
    String val = yText.getText();

    try {
        int dash = val.indexOf('-');
        if (dash >= 0) {
        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)");
        return;
        }
    }
    catch (NumberFormatException e) { }
    error("Invalid range " + val + " (expected NN-MM)");
    }

    /* User operation: set sample period. */
    void setSamplePeriod() {
    String periodS = sampleText.getText().trim();
    try {
        int newPeriod = Integer.parseInt(periodS);
        if (parent.setInterval(newPeriod)) {
        return;
        }
    }
    catch (NumberFormatException e) { }
    error("Invalid sample period " + periodS);
    }

    /* Notification: sample period changed. */
    void updateSamplePeriod() {
    sampleText.setText("" + parent.interval);
    }

    /* Notification: new node. */
    void newNode(int nodeId) {
    moteListModel.newNode(nodeId);
    }

    /* Notification: new data. */
    void newData() {
    graph.newData();
    }

    void error(String msg) {
    JOptionPane.showMessageDialog(frame, msg, "Error",
                      JOptionPane.ERROR_MESSAGE);
    }
}