Skip to content

Commit 72dbfdf

Browse files
authored
Bug fixes including choosers that don't appear and livewindow widgets that don't appear (#185)
1 parent aeb4f9c commit 72dbfdf

File tree

8 files changed

+90
-54
lines changed

8 files changed

+90
-54
lines changed

src/main/java/edu/wpi/first/smartdashboard/SmartDashboard.java

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package edu.wpi.first.smartdashboard;
22

3+
import edu.wpi.first.wpilibj.networktables.NetworkTable;
34
import edu.wpi.first.networktables.NetworkTablesJNI;
45
import edu.wpi.first.smartdashboard.extensions.FileSniffer;
56
import edu.wpi.first.smartdashboard.gui.DashboardFrame;
@@ -101,32 +102,6 @@ public void run() {
101102
System.exit(2);
102103
}
103104

104-
if (argParser.hasValue("ip")) {
105-
monitor.setProgress(650);
106-
monitor.setNote("Connecting to robot at: " + argParser.getValue("ip"));
107-
Robot.setHost(argParser.getValue("ip"));
108-
System.out.println("IP: " + argParser.getValue("ip"));
109-
} else {
110-
monitor.setProgress(600);
111-
monitor.setNote("Getting Team Number");
112-
StringProperty teamProp = frame.getPrefs().team;
113-
String teamNumber = teamProp.getValue();
114-
115-
teamNumberLoop:
116-
while (teamNumber.equals("0")) {
117-
String input = JOptionPane.showInputDialog("Input Team Number\\Host");
118-
if (input == null) {
119-
break teamNumberLoop;
120-
}
121-
teamNumber = input;
122-
}
123-
124-
monitor.setProgress(650);
125-
monitor.setNote("Connecting to robot: " + teamNumber);
126-
Robot.setHost(teamNumber);
127-
teamProp.setValue(teamNumber);
128-
}
129-
130105
try {
131106
SwingUtilities.invokeAndWait(new Runnable() {
132107

@@ -144,7 +119,7 @@ public void run() {
144119
frame.load(file.getPath());
145120
}
146121

147-
monitor.setProgress(1000);
122+
monitor.setProgress(750);
148123

149124
} catch (Exception e) {
150125
e.printStackTrace();
@@ -157,5 +132,39 @@ public void run() {
157132
ex.printStackTrace();
158133
System.exit(2);
159134
}
135+
136+
137+
if (argParser.hasValue("ip")) {
138+
monitor.setProgress(1000);
139+
monitor.setNote("Connecting to robot at: " + argParser.getValue("ip"));
140+
Robot.setHost(argParser.getValue("ip"));
141+
System.out.println("IP: " + argParser.getValue("ip"));
142+
} else {
143+
NetworkTable.setDSClientEnabled(true);
144+
try {
145+
Thread.sleep(500);
146+
} catch (InterruptedException e) {
147+
// No harm if the sleep is interrupted
148+
}
149+
monitor.setProgress(800);
150+
monitor.setNote("Getting Team Number");
151+
StringProperty teamProp = frame.getPrefs().team;
152+
String teamNumber = teamProp.getValue();
153+
154+
teamNumberLoop:
155+
while (teamNumber.equals("0")) {
156+
String input = JOptionPane.showInputDialog("Input Team Number\\Host");
157+
if (input == null) {
158+
break teamNumberLoop;
159+
}
160+
teamNumber = input;
161+
}
162+
163+
monitor.setProgress(850);
164+
monitor.setNote("Connecting to robot: " + teamNumber);
165+
Robot.setHost(teamNumber);
166+
teamProp.setValue(teamNumber);
167+
monitor.setProgress(1000);
168+
}
160169
}
161170
}

src/main/java/edu/wpi/first/smartdashboard/gui/DashboardFrame.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,7 @@ public void load(String path) {
389389
.getFieldName());
390390
DataType type = DataType.getType(value2);
391391
mostRecentParent.addWidget(w);
392+
mostRecentParent.add(w);
392393
w.setField(w.getFieldName(), w, type, value2, mostRecentParent, w.getSavedLocation());
393394
mostRecentParent.setSize(mostRecentParent.getPreferredSize());
394395
}

src/main/java/edu/wpi/first/smartdashboard/gui/elements/Chooser.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,10 @@ public void valueChanged(ITable source, String key, Object value, boolean isNew)
6565
}
6666

6767
if (!source.containsKey(SELECTED)) {
68-
source.putString(SELECTED, source.getString(DEFAULT, choices.get(0)));
68+
source.putString(SELECTED,
69+
source.getString(DEFAULT,
70+
!choices.isEmpty() ? choices.get(0)
71+
: "No Selection"));
6972
}
7073
}
7174

src/main/java/edu/wpi/first/smartdashboard/gui/elements/Scheduler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,9 @@ public void run() {
7575
button.addActionListener(new ActionListener() {
7676
public void actionPerformed(ActionEvent e) {
7777
// Cancel commands
78-
toCancel = Arrays.asList(table.getNumberArray("Cancel", new Double[0]));
78+
toCancel = new ArrayList<Double>(Arrays.asList(table.getNumberArray("Cancel", new Double[0])));
7979
toCancel.add(ids.get(index));
80-
table.putValue("Cancel", toCancel);
80+
table.putValue("Cancel", toCancel.toArray(new Double[toCancel.size()]));
8181
}
8282
});
8383
buttons.add(button);

src/main/java/edu/wpi/first/smartdashboard/livewindow/elements/LWSubsystem.java

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -87,24 +87,30 @@ private void addSubsystemElement(String key, ITable value) {
8787
"\nSubsystem \"" + getFieldName() + "\" does not contain widget \"" + key + "\"");
8888
System.out.println("Table: " + value);
8989
System.out.println("Type: " + value.getString(".type", null));
90-
System.out.println(
91-
"Trying to add a widget of type \"" + DataType.getType(value) + "\" and key " + key);
92-
Class<? extends Widget> widgetClass = DataType.getType(value).getDefault();
93-
Widget widget = widgetClass.newInstance();
94-
widget.setFieldName(key);
95-
widget.setType(DataType.getType(value));
96-
widget.init();
97-
widget.setValue(value);
98-
widgets.add(widget);
99-
add(widget);
100-
preferredSize = layout.preferredLayoutSize(this);
101-
setPreferredSize(preferredSize);
102-
setMinimumSize(preferredSize);
103-
setSavedSize(preferredSize);
104-
setSize(preferredSize);
105-
System.out.println("New size [" + preferredSize.width + ", " + preferredSize.height + "]");
106-
revalidate();
107-
repaint();
90+
DataType dataType = DataType.getType(value);
91+
if (dataType != null) {
92+
System.out.println(
93+
"Trying to add a widget of type \"" + dataType + "\" and key " + key);
94+
95+
Class<? extends Widget> widgetClass = DataType.getType(value).getDefault();
96+
Widget widget = widgetClass.newInstance();
97+
widget.setFieldName(key);
98+
widget.setType(DataType.getType(value));
99+
widget.init();
100+
widget.setValue(value);
101+
widgets.add(widget);
102+
add(widget);
103+
preferredSize = layout.preferredLayoutSize(this);
104+
setPreferredSize(preferredSize);
105+
setMinimumSize(preferredSize);
106+
setSavedSize(preferredSize);
107+
setSize(preferredSize);
108+
System.out.println("New size [" + preferredSize.width + ", " + preferredSize.height + "]");
109+
revalidate();
110+
repaint();
111+
} else {
112+
System.out.println("Skipping: No registered dataType for type " + value.getString(".type", null) );
113+
}
108114
System.out.println();
109115
} catch (InstantiationException | IllegalAccessException ex) {
110116
Logger.getLogger(LWSubsystem.class.getName()).log(Level.SEVERE, null, ex);

src/main/java/edu/wpi/first/smartdashboard/livewindow/elements/LiveWindowWidgetRegistrar.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package edu.wpi.first.smartdashboard.livewindow.elements;
22

3+
import edu.wpi.first.smartdashboard.gui.elements.Scheduler;
34
import edu.wpi.first.smartdashboard.types.DisplayElementRegistry;
45

56
/**
@@ -24,6 +25,7 @@ public static void init() {
2425
DisplayElementRegistry.registerWidget(PowerDistribution.class);
2526
DisplayElementRegistry.registerWidget(ThreeAxisAccelerometer.class);
2627
DisplayElementRegistry.registerWidget(CANSpeedController.class);
28+
DisplayElementRegistry.registerWidget(Scheduler.class);
2729
}
2830

2931
}

src/main/java/edu/wpi/first/smartdashboard/robot/Robot.java

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package edu.wpi.first.smartdashboard.robot;
22

3+
import java.util.concurrent.ConcurrentHashMap;
4+
import java.util.concurrent.ConcurrentMap;
5+
36
import edu.wpi.first.wpilibj.networktables.NetworkTable;
47
import edu.wpi.first.wpilibj.tables.IRemoteConnectionListener;
58
import edu.wpi.first.wpilibj.tables.ITable;
@@ -21,6 +24,7 @@ public class Robot {
2124
static {
2225
NetworkTable.setClientMode();
2326
NetworkTable.setNetworkIdentity(identity);
27+
NetworkTable.setDSClientEnabled(false);
2428
NetworkTable.initialize();
2529
}
2630

@@ -63,20 +67,30 @@ public static void setPort(int port) {
6367
NetworkTable.initialize();
6468
}
6569

66-
public static ITable getTable(String tableName) {
67-
return NetworkTable.getTable(tableName);
70+
// NetworkTable.getTable() returns a new table every time it's called.
71+
// We need to make sure there is only one table for each table name.
72+
private static final ConcurrentMap<String, ITable> tables = new ConcurrentHashMap<String, ITable>();
73+
74+
public static synchronized ITable getTable(String tableName) {
75+
if (tables.containsKey(tableName)) {
76+
return tables.get(tableName);
77+
} else {
78+
ITable newTable = NetworkTable.getTable(tableName);
79+
tables.put(tableName, newTable);
80+
return newTable;
81+
}
6882
}
6983

7084
public static ITable getTable() {
71-
return NetworkTable.getTable(TABLE_NAME);
85+
return getTable(TABLE_NAME);
7286
}
7387

7488
public static ITable getPreferences() {
75-
return NetworkTable.getTable(PREFERENCES_NAME);
89+
return getTable(PREFERENCES_NAME);
7690
}
7791

7892
public static ITable getLiveWindow() {
79-
return NetworkTable.getTable(LIVE_WINDOW_NAME);
93+
return getTable(LIVE_WINDOW_NAME);
8094
}
8195

8296
public static void addConnectionListener(IRemoteConnectionListener listener, boolean

src/main/java/edu/wpi/first/smartdashboard/types/named/SchedulerType.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package edu.wpi.first.smartdashboard.types.named;
22

33
import edu.wpi.first.smartdashboard.types.NamedDataType;
4+
import edu.wpi.first.smartdashboard.gui.elements.Scheduler;
45

56
/**
67
* @author Joe Grinstead
@@ -10,7 +11,7 @@ public class SchedulerType extends NamedDataType {
1011
public static final String LABEL = "Scheduler";
1112

1213
private SchedulerType() {
13-
super(LABEL);
14+
super(LABEL, Scheduler.class);
1415
}
1516

1617
public static NamedDataType get() {

0 commit comments

Comments
 (0)