summaryrefslogtreecommitdiff
path: root/smartdashboard/src/edu/wpi/first/smartdashboard/gui/elements/Scheduler.java
blob: d61e05264b7106e0f50526a57bb3fbb09d555081 (plain)
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
package edu.wpi.first.smartdashboard.gui.elements;

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.util.List;

import javax.swing.*;

import edu.wpi.first.smartdashboard.gui.*;
import edu.wpi.first.smartdashboard.properties.*;
import edu.wpi.first.smartdashboard.types.*;
import edu.wpi.first.smartdashboard.types.named.*;
import edu.wpi.first.wpilibj.networktables2.type.NumberArray;
import edu.wpi.first.wpilibj.networktables2.type.StringArray;
import edu.wpi.first.wpilibj.tables.*;

/**
 *
 * @author Jeff Copeland
 */
public class Scheduler extends Widget {

    public static final DataType[] TYPES = {SchedulerType.get()};
    private static final String NO_COMMAND_CARD = "No Command";
    private static final String COMMAND_CARD = "Commands";
    private int count = 0;
    private JLabel noCommands;
    private JPanel commandLabels;
    private JPanel cancelButtons;
    private JPanel commandPanel;
    private List<JLabel> labels;
    private List<JButton> buttons;
    private ITable table;
    private GridLayout commandLayout;
    private GridLayout cancelLayout;
    private CardLayout cardLayout;
    
    private StringArray commands = new StringArray();
    private NumberArray ids = new NumberArray();
    private NumberArray toCancel = new NumberArray();
    
    private ITableListener listener = new ITableListener() {

        boolean running = false;

        @Override
        public void valueChanged(ITable source, String key, Object value, boolean isNew) {
            if (running) {
                return;
            }
            running = true;
            SwingUtilities.invokeLater(new Runnable() {

                public void run() {
                    synchronized (table) {
                        table.retrieveValue("Names", commands);
                        table.retrieveValue("Ids", ids);
                        assert commands.size() == ids.size();
                        
                        // Update displayed commands
                        for (int i = 0; i < commands.size(); i++) {
                            if (i >= labels.size()) {
                                labels.add(new JLabel());
                            }
                            JLabel label = labels.get(i);
                            label.setText(commands.get(i));

                            if (i >= buttons.size()) {
                                JButton button = new JButton("cancel");
                                final int index = i;
                                button.addActionListener(new ActionListener() {
                                    public void actionPerformed(ActionEvent e) {
                                        // Cancel commands
                                        table.retrieveValue("Cancel", toCancel);
                                        toCancel.add(ids.get(index));
                                        table.putValue("Cancel", toCancel);
                                    }
                                });
                                buttons.add(button);
                            }
                            JButton button = buttons.get(i);

                            if (i > count - 1) {
                                commandLabels.add(label);
                                cancelButtons.add(button);
                            }
                        }
                    }

                    // Remove leftover widgets
                    if (count > commands.size()) {
                        for (int i = commands.size(); i < count; i++) {
                            commandLabels.remove(labels.get(i));
                            cancelButtons.remove(buttons.get(i));
                        }
                    }

                    count = commands.size();

                    cardLayout.show(Scheduler.this, count == 0 ? NO_COMMAND_CARD : COMMAND_CARD);

                    running = false;
                }
            });
        }
    };

    @Override
    public void setValue(Object value) {
        if (table != null) {
            table.removeTableListener(listener);
        }
        table = (ITable) value;
        table.addTableListener(listener, true);

        revalidate();
        repaint();
    }

    @Override
    public void init() {
        setLayout(cardLayout = new CardLayout());

        labels = new ArrayList<JLabel>();
        buttons = new ArrayList<JButton>();

        commandPanel = new JPanel();
        commandPanel.setLayout(new GridLayout(0, 2));

        commandLabels = new JPanel();
        cancelButtons = new JPanel();

        commandPanel.add(commandLabels, BorderLayout.WEST);
        commandPanel.add(cancelButtons, BorderLayout.CENTER);

        commandLayout = new GridLayout(0, 1);
        cancelLayout = new GridLayout(0, 1);

        commandLabels.setLayout(commandLayout);
        cancelButtons.setLayout(cancelLayout);

        add(commandPanel, COMMAND_CARD);

        noCommands = new JLabel("No commands running.");
        noCommands.setHorizontalAlignment(JLabel.CENTER);
        add(noCommands, NO_COMMAND_CARD);

        cardLayout.show(this, NO_COMMAND_CARD);

        repaint();
    }

    @Override
    public void propertyChanged(Property property) {
    }
}