Initial checkin of unified hierarchy of WPILib 2015

This commit is contained in:
Brad Miller
2013-12-15 18:30:16 -05:00
commit 3178911eef
1560 changed files with 410007 additions and 0 deletions

View File

@@ -0,0 +1,27 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package edu.wpi.first.tableviewer;
import edu.wpi.first.tableviewer.popup.PreferencesPopup;
import edu.wpi.first.tableviewer.treeview.TableTreeView;
import javafx.application.Application;
import javafx.stage.Stage;
/**
*
* @author Sam
*/
public class NetworkTableViewer extends Application {
@Override
public void start(Stage stage) {
PreferencesPopup preferencesPopup = new PreferencesPopup();
preferencesPopup.showPopup();
TableTreeView view = new TableTreeView(preferencesPopup.getNode());
view.show();
}
}

View File

@@ -0,0 +1,159 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package edu.wpi.first.tableviewer.popup;
import edu.wpi.first.wpilibj.tables.ITable;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Control;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.stage.Modality;
/**
*
* @author Sam
*/
class AddValuePopup extends Popup {
public enum InputType {
BOOLEAN, NUMBER, STRING, SUBTABLE
}
private final InputType type;
private final ITable table;
public AddValuePopup(ITable table, InputType type) {
super();
this.table = table;
this.type = type;
initModality(Modality.WINDOW_MODAL);
}
@Override
public void showPopup() {
initComponents();
showAndWait();
}
@Override
public void initComponents() {
initComponents(type);
}
private void initComponents(final InputType type) {
final Scene scene = new Scene(new Group());
scene.setFill(Color.WHITESMOKE);
scene.getStylesheets().add(
"edu/wpi/first/tableviewer/stylesheets/ViewerStyleSheet.css");
final Label messageLabel = new Label();
final Label keyLabel = new Label("Key:");
final Label valueLabel = new Label("Value:");
final TextField keyField = new TextField();
final Button confirm = new Button("Add");
keyField.setPromptText("Key");
keyField.setPrefWidth(100);
final Control valueControl;
ObservableList<String> booleanOptions = FXCollections.observableArrayList("True", "False");
switch (type) {
case BOOLEAN:
messageLabel.setText("Add boolean");
valueControl = new ComboBox(booleanOptions);
((ComboBox) valueControl).getSelectionModel().select(0);
confirm.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent t) {
if (table.containsKey(keyField.getText())) {
PopupFactory.showErrorDialog("Key already in use!");
} else {
table.putBoolean(keyField.getText(), ((ComboBox) valueControl).getSelectionModel().isSelected(0));
close();
}
}
});
break;
case NUMBER:
messageLabel.setText("Add number");
valueControl = new TextField();
((TextField) valueControl).setPromptText("Number");
confirm.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent t) {
try {
if (table.containsKey(keyField.getText())) {
PopupFactory.showErrorDialog("Key already in use!");
} else {
table.putNumber(keyField.getText(), Double.parseDouble(((TextField) valueControl).getText()));
close();
}
} catch (NumberFormatException ex) {
PopupFactory.showErrorDialog("Invalid number!");
}
}
});
break;
case STRING:
messageLabel.setText("Add string");
valueControl = new TextField();
((TextField) valueControl).setPromptText("Text");
confirm.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent t) {
if (table.containsKey(keyField.getText())) {
PopupFactory.showErrorDialog("Key already in use!");
} else {
table.putString(keyField.getText(), ((TextField) valueControl).getText());
close();
}
}
});
break;
case SUBTABLE:
keyLabel.setText("Table name");
keyField.setPromptText("Name");
valueControl = null;
confirm.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent t) {
//TODO make a subtable show up without putting a value
ITable i = table.getSubTable(keyField.getText());
close();
}
});
break;
default:
return;
}
final GridPane grid = new GridPane();
grid.setHgap(10);
grid.setVgap(5);
grid.add(keyLabel, 1, 1);
grid.add(keyField, 2, 1);
if (type != InputType.SUBTABLE) {
grid.add(valueLabel, 1, 2);
grid.add(valueControl, 2, 2);
valueControl.setPrefWidth(100);
}
grid.add(confirm, 2, 3);
((Group) scene.getRoot()).getChildren().add(grid);
setScene(scene);
}
}

View File

@@ -0,0 +1,128 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package edu.wpi.first.tableviewer.popup;
import edu.wpi.first.tableviewer.treeview.TableLeaf;
import javafx.collections.FXCollections;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
/**
*
* @author Sam
*/
public class EditorPopup extends Popup {
private final Label inputLabel = new Label();
private final TextField inputField = new TextField();
private final ComboBox booleanBox = new ComboBox(FXCollections.observableArrayList("True", "False"));
private final Button okayButton = new Button("OK"),
cancelButton = new Button("Cancel");
private final String itemType;
private final TableLeaf leaf;
public EditorPopup(TableLeaf leaf) {
super("Edit Value", null, null);
this.leaf = leaf;
String tableValue = leaf.getTableValue().toString();
String tableKey = leaf.getTableKey();
inputLabel.setText(tableKey+":");
inputField.setText(tableValue);
inputField.setPromptText("New value");
inputField.setPrefWidth(100);
booleanBox.getSelectionModel().selectFirst();
itemType = leaf.getType().toString();
cancelButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent t) {
close();
}
});
}
@Override
public void initComponents() {
initComponents(itemType);
}
private void initComponents(final String type) {
final Scene scene = new Scene(new Group());
scene.setFill(Color.WHITESMOKE);
scene.getStylesheets().add(
"edu/wpi/first/tableviewer/stylesheets/ViewerStyleSheet.css");
final GridPane grid = new GridPane();
grid.setHgap(10);
grid.setVgap(15);
grid.setPadding(new Insets(5, 5, 5, 5));
grid.setAlignment(Pos.CENTER);
grid.add(inputLabel, 0, 1);
((Group) scene.getRoot()).getChildren().add(grid);
setScene(scene);
switch (type.toLowerCase()) {
case "boolean":
grid.add(booleanBox, 1, 1);
okayButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent t) {
leaf.updateValue(booleanBox.getSelectionModel().isSelected(0));
leaf.setExpanded(false); // refresh the leaf to updateValue the shown value
leaf.setExpanded(true);
close();
}
});
break;
case "number":
grid.add(inputField, 1, 1);
okayButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent t) {
try {
leaf.updateValue(Double.parseDouble(inputField.getText()));
leaf.setExpanded(false); // refresh the leaf to updateValue the shown value
leaf.setExpanded(true);
close();
} catch (NumberFormatException ex) {
PopupFactory.showErrorDialog("Invalid number value!");
}
}
});
break;
case "string":
grid.add(inputField, 1, 1);
okayButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent t) {
leaf.updateValue(inputField.getText());
leaf.setExpanded(false); // refresh the leaf to updateValue the shown value
leaf.setExpanded(true);
close();
}
});
break;
}
grid.add(okayButton, 0, 2);
grid.add(cancelButton, 1, 2);
}
}

View File

@@ -0,0 +1,35 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package edu.wpi.first.tableviewer.popup;
/**
* An interface for defining popups.
* @author Sam
* @see Popup
* @see InputPopup
*/
public interface IPopup {
/**
* An enumeration representing the type of popup this is.
*/
public enum Type {
MESSAGE,
WARNING,
ERROR,
INPUT
}
/**
* A method used to show this popup.
*/
public void showPopup();
/**
* Initialize all components in this popup.
*/
public void initComponents();
}

View File

@@ -0,0 +1,115 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package edu.wpi.first.tableviewer.popup;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.TextAlignment;
import javafx.stage.Modality;
import javafx.stage.Stage;
/**
* A generic popup. Call
* {@link PopupFactory Popupfactory}.{@code showXXXXDialog()} to show a
* predefined popup of a specific type.
*
* @see IPopup
* @see InputPopup
* @see PopupFactory
* @author Sam
*/
class Popup extends Stage implements IPopup {
protected final String message;
protected final Type type;
Popup() {
this(null, null, Type.INPUT);
}
Popup(String title, String message, Type type) {
super();
this.message = message;
this.type = type;
setTitle(title);
setWidth(200);
setHeight(150);
setResizable(false);
initModality(Modality.WINDOW_MODAL);
}
private void initComponents(Type messageType) {
final Scene scene = new Scene(new Group());
scene.setFill(Color.WHITESMOKE);
final Label messageText = new Label(message);
final Button okayButton = new Button("Okay");
final VBox box = new VBox();
messageText.setAlignment(Pos.CENTER);
messageText.setTextAlignment(TextAlignment.CENTER);
messageText.setWrapText(true);
okayButton.setAlignment(Pos.CENTER);
okayButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent t) {
close();
}
});
String messageClass = "";
switch (messageType) {
case MESSAGE:
messageClass = "";
box.setPadding(new Insets(10, 0, 0, 0));
break;
case WARNING:
messageClass = "warning-";
box.setPadding(new Insets(10, 0, 0, 0));
break;
case ERROR:
messageClass = "error-";
box.setPadding(new Insets(30, 0, 0, 0));
break;
default:
break;
}
scene.getStylesheets().add(
"edu/wpi/first/tableviewer/stylesheets/PopupCSS.css");
messageText.getStyleClass().add(messageClass + "message");
box.setPrefWidth(200);
box.setSpacing(15);
box.setAlignment(Pos.CENTER);
box.getChildren().addAll(messageText, okayButton);
((Group) scene.getRoot()).getChildren().addAll(box);
setScene(scene);
}
@Override
public void initComponents() {
initComponents(type);
}
@Override
public void showPopup() {
initComponents();
showAndWait();
}
}

View File

@@ -0,0 +1,65 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package edu.wpi.first.tableviewer.popup;
import edu.wpi.first.tableviewer.popup.IPopup.Type;
import edu.wpi.first.tableviewer.popup.AddValuePopup.InputType;
import edu.wpi.first.tableviewer.treeview.TableLeaf;
import edu.wpi.first.wpilibj.tables.ITable;
/**
* A factory for creating and showing {@link Popup Popups} of a defined type.
* @author Sam
*/
public final class PopupFactory {
public static void showMessageDialog(String title, String message) {
IPopup popup = new Popup(title, message, Type.MESSAGE);
popup.showPopup();
}
public static void showMessageDialog(String message) {
showMessageDialog("", message);
}
public static void showWarningDialog(String title, String message) {
IPopup popup = new Popup(title, message, Type.WARNING);
popup.showPopup();
}
public static void showWarningDialog(String message) {
showWarningDialog("", message);
}
public static void showErrorDialog(String title, String message) {
IPopup popup = new Popup(title, message, Type.ERROR);
popup.showPopup();
}
public static void showErrorDialog(String message) {
showErrorDialog("", message);
}
public static void showAddBooleanDialog(ITable table) {
IPopup popup = new AddValuePopup(table, InputType.BOOLEAN);
popup.showPopup();
}
public static void showAddNumberDialog(ITable table) {
IPopup popup = new AddValuePopup(table, InputType.NUMBER);
popup.showPopup();
}
public static void showAddStringDialog(ITable table) {
IPopup popup = new AddValuePopup(table, InputType.STRING);
popup.showPopup();
}
public static void showEditorDialog(TableLeaf leaf) {
IPopup popup = new EditorPopup(leaf);
popup.showPopup();
}
}

View File

@@ -0,0 +1,136 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package edu.wpi.first.tableviewer.popup;
import edu.wpi.first.tableviewer.NetworkTableViewer;
import edu.wpi.first.tableviewer.treeview.TableTreeView;
import edu.wpi.first.wpilibj.networktables2.NetworkTableNode;
import edu.wpi.first.wpilibj.networktables2.client.NetworkTableClient;
import edu.wpi.first.wpilibj.networktables2.server.NetworkTableServer;
import edu.wpi.first.wpilibj.networktables2.stream.SocketStreams;
import java.io.IOException;
import java.util.prefs.Preferences;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.stage.WindowEvent;
/**
*
* @author Sam
*/
public class PreferencesPopup extends Popup {
public static final int DEFAULT_PORT = 1735;
private final Preferences prefs = Preferences.userNodeForPackage(NetworkTableViewer.class);
private String host = "localhost";
private NetworkTableNode node;
public PreferencesPopup() {
super("Preferences", null, null);
setWidth(215);
setHeight(160);
}
public NetworkTableNode getNode() {
return node;
}
@Override
public void initComponents() {
try {
host = prefs.get("host", "localhost");
} catch (NullPointerException e) {
host = "localhost";
}
final Scene scene = new Scene(new Group());
scene.getStylesheets().add("edu/wpi/first/tableviewer/stylesheets/ViewerStyleSheet.css");
final Label hostLabel = new Label("Host:");
final CheckBox clientBox = new CheckBox("Client");
clientBox.setSelected(true); // default to client
final TextField hostField = new TextField("localhost");
final Button okayButton = new Button("OK");
final Button cancelButton = new Button("Cancel");
hostField.setMaxWidth(100);
hostField.setPromptText("Host");
hostField.setText(prefs.get("host", "localhost"));
okayButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent t) {
try {
host = hostField.getText();
if (clientBox.isSelected()) { // client
setClient(host, DEFAULT_PORT);
System.out.println("Connecting client to " + host + ":" + DEFAULT_PORT);
} else { // server
setServer(DEFAULT_PORT);
System.out.println("Starting server on port " + DEFAULT_PORT);
}
TableTreeView.setRootNode(node);
prefs.put("host", host);
close();
} catch (NumberFormatException ex) { // invalid team number
PopupFactory.showErrorDialog("Invalid team number!");
} catch (IOException ex) {
System.err.println("Error creating table node: "
+ ex.getClass().toString().substring(ex.getClass().toString().lastIndexOf(".") + 1) + ": "
+ ex.getMessage());
}
}
});
cancelButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent t) {
System.exit(0);
}
});
setOnCloseRequest(new EventHandler<WindowEvent>() {
@Override
public void handle(WindowEvent t) {
System.exit(0);
}
});
GridPane grid = new GridPane();
grid.setHgap(10);
grid.setVgap(5);
grid.add(clientBox, 1, 1);
grid.add(hostLabel, 1, 2);
grid.add(hostField, 2, 2);
grid.add(okayButton, 1, 3);
grid.add(cancelButton, 2, 3);
((Group) scene.getRoot()).getChildren().addAll(grid);
setScene(scene);
}
private void setServer(int port) throws IOException {
this.node = new NetworkTableServer(SocketStreams.newStreamProvider(port));
}
private void setClient(String host, int port) throws IOException {
this.node = new NetworkTableClient(SocketStreams.newStreamFactory(host, port));
((NetworkTableClient)node).reconnect();
}
}

View File

@@ -0,0 +1,32 @@
/*
Document : ErrorMessageCSS
Created on : Apr 23, 2013, 4:23:41 PM
Author : Sam
Description:
Purpose of the stylesheet follows.
*/
root {
display: block;
-fx-font-family: "Trebuchet MS";
-fx-background: #000000;
}
.message {
-fx-text-fill: #000000; /* black */
-fx-font-size: 14pt;
-fx-font-weight: bold;
}
.warning-message {
-fx-text-fill: #FF5500; /* yellow */
-fx-font-size: 14pt;
-fx-font-weight: bold;
}
/* Might need to change this*/
.error-message {
-fx-text-fill: #CC0000; /* red */
-fx-font-size: 14pt;
-fx-font-weight: bold;
}

View File

@@ -0,0 +1,71 @@
root {
display: block;
-fx-font-family: "Trebuchet MS";
}
.button {
-fx-text-fill: #111111;
-fx-border-color: #000000;
-fx-background-color: linear-gradient(#DDDDDD, #AAAAAA);
-fx-border-radius: 5;
-fx-padding: 3 6 6 6;
}
.button:hover {
-fx-background-color: linear-gradient(#FFFFFF, #CCCCCC);
}
.button:focused {
-fx-background-color: linear-gradient(#FFFFFF, #CCCCCC);
}
.combo-box {
-fx-text-fill: #111111;
-fx-border-color: #000000;
-fx-background-color: linear-gradient(#DDDDDD, #AAAAAA);
-fx-border-radius: 5;
}
.combo-box:focused {
-fx-background-color: linear-gradient(#FFFFFF, #CCCCCC);
}
.text-field {
-fx-text-fill: #111111;
-fx-prompt-text-fill: #444444;
-fx-border-color: #000000;
-fx-background-color: #CCCCCC;
-fx-border-radius: 5;
}
.text-field:focused {
-fx-background-color: #EEEEEE;
}
.scroll-bar .track{
-fx-background-color: #EFEFEF;
}
.scroll-bar .thumb {
-fx-background-color: linear-gradient(to right, #AAAAAA, #CCCCCC);
-fx-border-radius: 5;
}
.tree-cell {
-fx-font: 12pt "Trebuchet MS";
-fx-text-fill: #111111;
-fx-border-color: #DDDDFF;
}
.tree-cell:selected:focused {
-fx-background-color: #CCCCCC;
}
.tree-cell:odd {
-fx-background-color: rgb(240,240,255);
}
.tree-cell:even {
-fx-background-color: rgb(255,255,255);
}

View File

@@ -0,0 +1,24 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package edu.wpi.first.tableviewer.treeview;
import javafx.scene.control.TreeItem;
/**
*
* @author Sam
*/
public abstract class ITableItem extends TreeItem<TreeItem> {
public ITableItem() {
super();
}
@Override
public abstract String toString();
@Override
public abstract boolean isLeaf();
}

View File

@@ -0,0 +1,119 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package edu.wpi.first.tableviewer.treeview;
import edu.wpi.first.wpilibj.networktables.NetworkTableProvider;
import edu.wpi.first.wpilibj.networktables2.NetworkTableNode;
import edu.wpi.first.wpilibj.tables.ITable;
import edu.wpi.first.wpilibj.tables.ITableListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.scene.control.TreeItem;
/**
* A {@link TreeItem} responsible for displaying all the data within a certain
* {@link ITable}. It can contain {@link TableLeaf TableLeaves} and other
* {@code TableBranches} if there are subtables in the given {@code ITable}.
*
* @see TableLeaf
* @author Sam
*/
public class TableBranch extends ITableItem implements ITableListener {
/**
* Keeps track of values in the table.
*/
private HashMap<String, ITableItem> entries = new HashMap<>();
private final ITable table;
/**
* Constructs a TableBranch that displays data from the given ITable.
*
* @param table The ITable to show the data of.
*/
public TableBranch(final ITable table) {
super();
this.table = table;
table.addTableListener(this, true);
table.addSubTableListener(this);
setExpanded(true);
setValue(this);
expandedProperty().bind(new SimpleBooleanProperty(true)); //REMOVEME
}
/**
* This should only be called once, when making the root TableBranch.
*
* @param node The root node to listen to. Sibling or parent nodes, if they
* exist, will be ignored.
* @see TableBranch(ITable)
*/
public TableBranch(NetworkTableNode node) {
this(new NetworkTableProvider(node).getRootTable());
}
@Override
public void valueChanged(ITable source, String key, Object value, boolean isNew) {
if (isNew && !entries.containsKey(key)) {
if (value.toString().startsWith("NetworkTable: /")) {
String tableName = value.toString().substring(source.toString().length() + 1);
TableBranch branch = new TableBranch(source.getSubTable(tableName));
this.getChildren().add(branch);
entries.put(key, branch);
} else {
TableLeaf leaf = new TableLeaf(source, key, value);
entries.put(key, leaf);
this.getChildren().add(leaf);
this.getChildren().removeAll(getLeaves());
this.getChildren().addAll(sortLeaves(getLeaves()));
}
} else if (!isNew && entries.get(key) instanceof TableLeaf) {
((TableLeaf) entries.get(key)).setTableValue(value);
}
}
/**
* Sorts
*
* @param leaves
* @return
*/
private List<TableLeaf> sortLeaves(List<TableLeaf> leaves) {
List<TableLeaf> list = leaves.subList(0, leaves.size());
System.out.print(leaves + "->" + list + "\t");
Collections.sort(list);
System.out.println(list);
return list;
}
private List<TableLeaf> getLeaves() {
List<TableLeaf> leaves = new ArrayList<>();
for (ITableItem item : entries.values()) {
if (item instanceof TableLeaf) {
leaves.add((TableLeaf) item);
}
}
return leaves;
}
public ITable getTable() {
return table;
}
@Override
public boolean isLeaf() {
return false;
}
@Override
public String toString() {
return table.toString().substring(table.toString().lastIndexOf("/") + 1);
}
}

View File

@@ -0,0 +1,102 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package edu.wpi.first.tableviewer.treeview;
import edu.wpi.first.wpilibj.tables.ITable;
/**
*
* @author Sam
*/
public class TableLeaf extends ITableItem implements Comparable {
private final ITable table;
private String entryKey;
private Object entryValue;
private EntryType entryType = EntryType.UNSUPPORTED;
@Override
public int compareTo(Object o) {
return this.getTableKey().toLowerCase().compareTo(((TableLeaf)o).getTableKey().toLowerCase());
}
public enum EntryType {
BOOLEAN, NUMBER, STRING, UNSUPPORTED
}
public TableLeaf(ITable table, String key, Object value) {
super();
this.table = table;
this.entryKey = key;
this.entryValue = value;
this.entryType = getTypeFromValue(value);
setValue(this);
}
public ITable getTable() {
return table;
}
public String getTableKey() {
return entryKey;
}
public Object getTableValue() {
return entryValue;
}
/**
* Sets the display value of this TableLeaf to a new value.
*
* @param newValue The new value of this leaf.
*/
public void setTableValue(Object newValue) {
entryValue = newValue;
entryType = getTypeFromValue(newValue);
setValue(null);
setValue(this);
}
private EntryType getTypeFromValue(Object value) {
String valueClassName = value.getClass().toString().substring(value.getClass().toString().lastIndexOf(".") + 1);
switch (valueClassName.toLowerCase()) {
case "boolean":
return EntryType.BOOLEAN;
case "double":
return EntryType.NUMBER;
case "string":
return EntryType.STRING;
default:
return EntryType.UNSUPPORTED;
}
}
/**
* Updates the value in this TableLeaf's ITable and updates the displayed
* value.
*
* @param newValue The new value of this entry.
* @see #setTableValue
*/
public void updateValue(Object newValue) {
setTableValue(newValue);
table.putValue(entryKey, entryValue);
}
public EntryType getType() {
return entryType;
}
@Override
public String toString() {
return entryKey + " = " + entryValue;
}
@Override
public boolean isLeaf() {
return true;
}
}

View File

@@ -0,0 +1,143 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package edu.wpi.first.tableviewer.treeview;
import edu.wpi.first.tableviewer.popup.PopupFactory;
import edu.wpi.first.tableviewer.treeview.TableLeaf.EntryType;
import java.util.ArrayList;
import java.util.List;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.Label;
import javafx.scene.control.MenuItem;
import javafx.scene.control.TreeCell;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.GridPane;
/**
*
* @author Sam
*/
public class TableTreeCell extends TreeCell<ITableItem> {
private GridPane grid = new GridPane();
private Label key = new Label(),
value = new Label(),
type = new Label();
// Very hackish but it almost works. Just don't collapse tables.
// I blame JavaFX/Patrick for making me do this.
private static List<TableTreeCell> cells = new ArrayList<>();
private static int currentCell = 0;
private static int maxCells = 200;
static {
for (int i = 0; i < maxCells; i++) {
cells.add(new TableTreeCell());
}
}
public static TableTreeCell getCell() {
if (currentCell >= maxCells) {
currentCell = 0;
}
return cells.get(currentCell++);
}
private TableTreeCell() {
super();
genGrid();
this.setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent t) {
ITableItem selected = getItem();
if (selected instanceof TableLeaf && t.getClickCount() == 2) {
TableLeaf leaf = (TableLeaf) selected;
PopupFactory.showEditorDialog(leaf);
}
}
});
}
private void genGrid() {
key.setPrefWidth(50);
value.setPrefWidth(125);
type.setPrefWidth(75);
grid.add(key, 0, 0);
grid.add(value, 1, 0);
grid.add(type, 2, 0);
grid.setHgap(15);
}
@Override
public void updateItem(ITableItem tableItem, boolean empty) {
super.updateItem(tableItem, empty);
if (tableItem != null && !empty) {
if (tableItem instanceof TableBranch) {
setContextMenu(getBranchMenu((TableBranch) tableItem));
setText(tableItem.toString());
setGraphic(null);
} else if (tableItem instanceof TableLeaf) {
TableLeaf leaf = (TableLeaf) tableItem;
key.setText(leaf.getTableKey());
value.setText(leaf.getTableValue().toString());
type.setText(leaf.getType().name());
setText(null);
setGraphic(grid);
setContextMenu(getLeafMenu(leaf));
}
}
}
private ContextMenu getBranchMenu(final TableBranch branch) {
MenuItem addBoolean = new MenuItem("Add boolean");
MenuItem addNumber = new MenuItem("Add number");
MenuItem addString = new MenuItem("Add string");
addBoolean.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent t) {
PopupFactory.showAddBooleanDialog(branch.getTable());
}
});
addNumber.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent t) {
PopupFactory.showAddNumberDialog(branch.getTable());
}
});
addString.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent t) {
PopupFactory.showAddStringDialog(branch.getTable());
}
});
ContextMenu cMenu = new ContextMenu();
cMenu.getItems().addAll(addBoolean, addNumber, addString);
return cMenu;
}
private ContextMenu getLeafMenu(final TableLeaf leaf) {
MenuItem edit = new MenuItem("Edit");
edit.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent t) {
PopupFactory.showEditorDialog(leaf);
}
});
ContextMenu cMenu = new ContextMenu(edit);
return cMenu;
}
}

View File

@@ -0,0 +1,74 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package edu.wpi.first.tableviewer.treeview;
import edu.wpi.first.wpilibj.networktables2.NetworkTableNode;
import edu.wpi.first.wpilibj.networktables2.server.NetworkTableServer;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.TreeCell;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
import javafx.util.Callback;
/**
*
* @author Sam
*/
public class TableTreeView extends Stage {
private TreeView<ITableItem> tree;
private static NetworkTableNode rootNode;
public static void setRootNode(NetworkTableNode node) {
rootNode = node;
}
public TableTreeView(NetworkTableNode node) {
Scene scene = initTree();
setTitle("Network Table Viewer " + ((node instanceof NetworkTableServer) ? "Server" : "Client"));
setMinHeight(500);
setMinWidth(360);
setScene(scene);
setOnCloseRequest(new EventHandler<WindowEvent>() {
@Override
public void handle(WindowEvent t) {
// network tables keep the application running, so exit
System.exit(0);
}
});
}
private Scene initTree() {
Scene scene = new Scene(new Group());
scene.getStylesheets().add("edu/wpi/first/tableviewer/stylesheets/ViewerStyleSheet.css");
TableBranch rootBranch = new TableBranch(rootNode); // create a branch to show the entire contents of the node
TreeItem<TreeItem> root = new TreeItem<>();
root.getChildren().add(rootBranch);
tree = new TreeView(root);
tree.setShowRoot(false);
tree.prefWidthProperty().bind(scene.widthProperty());
tree.prefHeightProperty().bind(scene.heightProperty());
tree.setCellFactory(new Callback<TreeView<ITableItem>, TreeCell<ITableItem>>() {
int calls = 0;
@Override
public TreeCell<ITableItem> call(TreeView<ITableItem> p) {
// System.out.println(++calls);
return TableTreeCell.getCell();
}
});
((Group) scene.getRoot()).getChildren().add(tree);
return scene;
}
}