-
Notifications
You must be signed in to change notification settings - Fork 28
/
DashboardPrefs.java
130 lines (115 loc) · 4.73 KB
/
DashboardPrefs.java
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
package edu.wpi.first.smartdashboard.gui;
import edu.wpi.first.smartdashboard.SmartDashboard;
import edu.wpi.first.smartdashboard.properties.BooleanProperty;
import edu.wpi.first.smartdashboard.properties.FileProperty;
import edu.wpi.first.smartdashboard.properties.IntegerListProperty;
import edu.wpi.first.smartdashboard.properties.IntegerProperty;
import edu.wpi.first.smartdashboard.properties.Property;
import edu.wpi.first.smartdashboard.properties.PropertyHolder;
import edu.wpi.first.smartdashboard.robot.Robot;
import java.io.File;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.prefs.Preferences;
import javax.swing.JOptionPane;
/**
* @author brad
*/
public class DashboardPrefs implements PropertyHolder {
private static final File USER_HOME = new File(System.getProperty("user.home"));
private static final File USER_SMARTDASHBOARD_HOME = new File(USER_HOME, "SmartDashboard");
static {
if (!USER_SMARTDASHBOARD_HOME.exists()) {
USER_SMARTDASHBOARD_HOME.mkdirs();
}
}
private Map<String, Property> properties = new LinkedHashMap<String, Property>();
public final IntegerProperty team = new IntegerProperty(this, "Team Number", 0);
public final BooleanProperty hideMenu = new BooleanProperty(this, "Hide Menu", false);
public final BooleanProperty autoShowWidgets
= new BooleanProperty(this, "Automatically Show Widgets", true);
public final IntegerListProperty grid_widths
= new IntegerListProperty(this, "Grid Cell Width(s)", new int[]{16});
public final IntegerListProperty grid_heights
= new IntegerListProperty(this, "Grid Cell Height(s)", new int[]{16});
public final IntegerProperty x = new IntegerProperty(this, "Window X Position", 0);
public final IntegerProperty y = new IntegerProperty(this, "Window Y Position", 0);
public final IntegerProperty width = new IntegerProperty(this, "Window Width", 640);
public final IntegerProperty height = new IntegerProperty(this, "Window Height", 480);
public final FileProperty saveFile
= new FileProperty(this, "Save File", new File(USER_SMARTDASHBOARD_HOME, "save.xml")
.getAbsolutePath());
public final BooleanProperty logToCSV = new BooleanProperty(this, "Log to CSV", false);
public final FileProperty csvFile
= new FileProperty(this, "CSV File", new File(USER_SMARTDASHBOARD_HOME, "csv.txt")
.getAbsolutePath());
private Preferences node;
public static DashboardPrefs getInstance() {
return DashboardFrame.getInstance().getPrefs();
}
private final DashboardFrame frame;
public DashboardPrefs(DashboardFrame frame) {
this.frame = frame;
node = Preferences.userNodeForPackage(SmartDashboard.class);
for (Property property : properties.values()) {
if (property == logToCSV) { //always set logtoCSV to default on load
continue;
}
load(property);
}
}
private void load(Property property) {
property.setSaveValue(node.get(property.getName(), property.getSaveValue()));
}
public Map<String, Property> getProperties() {
return properties;
}
public boolean validatePropertyChange(Property property, Object value) {
if (property == team || property == width || property == height) {
return (Integer) value > 0;
} else if (property == grid_widths || property == grid_heights) {
int[] values = (int[]) value;
if (values.length == 0) {
return false;
} else {
for (int i : values) {
if (i <= 0) {
return false;
}
}
return true;
}
} else if (property == logToCSV) {
if ((Boolean) value) {
int result
= JOptionPane.showOptionDialog(null, "Should SmartDashboard start logging to the CSV "
+ "file? (This will override the existing file)",
"Warning", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE, null, null, false);
return result == JOptionPane.YES_OPTION;
}
}
return true;
}
public void propertyChanged(Property property) {
node.put(property.getName(), property.getSaveValue());
if (property == x) {
frame.setLocation(x.getValue(), frame.getY());
} else if (property == y) {
frame.setLocation(frame.getX(), y.getValue());
} else if (property == width) {
frame.setSize(width.getValue(), frame.getHeight());
} else if (property == height) {
frame.setSize(frame.getWidth(), height.getValue());
} else if (property == team) {
Robot.setTeam(team.getValue());
} else if (property == hideMenu) {
frame.setShouldHideMenu(hideMenu.getValue());
} else if (property == logToCSV) {
if (logToCSV.getValue()) {
frame.getLogger().start(csvFile.getValue());
} else {
frame.getLogger().stop();
}
}
}
}