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

import java.awt.*;

import javax.swing.*;

import edu.wpi.first.smartdashboard.gui.*;
import edu.wpi.first.smartdashboard.properties.*;
import edu.wpi.first.smartdashboard.robot.Robot;
import edu.wpi.first.wpilibj.tables.*;

/**
 *
 * @author Joe Grinstead
 */
public class ConnectionIndicator extends StaticWidget implements IRemoteConnectionListener {

    public static final String NAME = "Connection Indicator";
    private static final int DRAW_EMBOSSED = 0;
    private static final int DRAW_ENGRAVED = 1;
    private static final int DRAW_ROUNDED = 2;
    private static final int DRAW_PLAIN = 3;
    public final ColorProperty positive = new ColorProperty(this, "Connection Color", Color.GREEN);
    public final ColorProperty negative = new ColorProperty(this, "No Connection Color", Color.RED);
    public final MultiProperty display = new MultiProperty(this, "Graphics");
    private boolean connected = false;
    private Runnable repainter = new Runnable() {

        public void run() {
            repaint();
        }
    };

    public ConnectionIndicator() {
        setPreferredSize(new Dimension(32, 32));

        display.add("Embossed", DRAW_EMBOSSED);
        display.add("Engraved", DRAW_ENGRAVED);
        display.add("Rounded", DRAW_ROUNDED);
        display.add("Simple", DRAW_PLAIN);
        display.setDefault("Embossed");
    }

    @Override
    public void init() {
        Robot.addConnectionListener(this, true);
    }

    @Override
    public void disconnect() {
        Robot.removeConnectionListener(this);
    }

    @Override
    public void propertyChanged(Property property) {
        if (property == positive && connected) {
            repaint();
        } else if (property == negative && !connected) {
            repaint();
        } else if (property == display) {
            repaint();
        }
    }

    @Override
    protected void paintComponent(Graphics g) {
        Dimension size = getSize();
        
        switch ((Integer) display.getValue()) {
            case DRAW_EMBOSSED:
                g.setColor(connected ? positive.getValue() : negative.getValue());
                g.fill3DRect(0, 0, size.width, size.height, true);
                break;
            case DRAW_ENGRAVED:
                g.setColor(connected ? positive.getValue() : negative.getValue());
                g.fill3DRect(0, 0, size.width, size.height, false);
                break;
            case DRAW_ROUNDED:
                g.setColor(getBackground());
                g.fillRect(0, 0, size.width, size.height);
                g.setColor(connected ? positive.getValue() : negative.getValue());
                g.fillRoundRect(0, 0, size.width, size.height, 8, 8);
                break;
            case DRAW_PLAIN:
            default:
                g.setColor(connected ? positive.getValue() : negative.getValue());
                g.fillRect(0, 0, size.width, size.height);
        }
    }

	@Override
	public void connected(IRemote remote) {
	    System.out.println("ConnectionIndicator CONNECTED");
        if (!connected) {
            connected = true;
            SwingUtilities.invokeLater(repainter);
        }
	}

	@Override
	public void disconnected(IRemote remote) {
	    System.out.println("ConnectionIndicator DISCONNECTED");
        if (connected) {
            connected = false;
            SwingUtilities.invokeLater(repainter);
        }
	}
}