gui - added utility for loading fxml and cleaned up annoatuib toolbar

This commit is contained in:
Blake J. Harnden 2018-09-20 14:20:45 -07:00
parent 9830d63ff3
commit 1a8618ebde
5 changed files with 95 additions and 98 deletions

View file

@ -1,12 +1,13 @@
package com.core.ui;
import com.core.graph.NetworkGraph;
import com.core.utils.FxmlUtils;
import com.jfoenix.controls.JFXColorPicker;
import com.jfoenix.controls.JFXComboBox;
import com.jfoenix.controls.JFXToggleButton;
import edu.uci.ics.jung.visualization.annotations.Annotation;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.layout.GridPane;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
@ -15,10 +16,14 @@ import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.awt.geom.RectangularShape;
import java.awt.geom.RoundRectangle2D;
import java.io.IOException;
public class AnnotationToolbar extends GridPane {
private static final Logger logger = LogManager.getLogger();
private static final String RECTANGLE = "Rectangle";
private static final String ROUND_RECTANGLE = "RoundRectangle";
private static final String ELLIPSE = "Ellipse";
private static final String UPPER_LAYER = "Upper";
private static final String LOWER_LAYER = "Lower";
private NetworkGraph graph;
@FXML private JFXComboBox<String> shapeCombo;
@FXML private JFXColorPicker colorPicker;
@ -27,73 +32,73 @@ public class AnnotationToolbar extends GridPane {
public AnnotationToolbar(NetworkGraph graph) {
this.graph = graph;
FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/annotation_toolbar.fxml"));
loader.setRoot(this);
loader.setController(this);
FxmlUtils.loadRootController(this, "/fxml/annotation_toolbar.fxml");
try {
loader.load();
} catch (IOException ex) {
throw new RuntimeException(ex);
}
setup();
}
public void setup() {
// setup annotation shape combo
shapeCombo.getItems().addAll("Rectangle", "RoundRectangle", "Ellipse");
shapeCombo.getSelectionModel().select("Rectangle");
shapeCombo.setOnAction(event -> {
String selected = shapeCombo.getSelectionModel().getSelectedItem();
logger.info("annotation shape selected: {}", selected);
RectangularShape shape = new Rectangle();
switch (selected) {
case "Rectangle":
shape = new Rectangle();
break;
case "RoundRectangle":
shape = new RoundRectangle2D.Double(0, 0, 0, 0, 50.0, 50.0);
break;
case "Ellipse":
shape = new Ellipse2D.Double();
break;
}
graph.getGraphMouse().getAnnotatingPlugin().setRectangularShape(shape);
});
shapeCombo.getItems().addAll(RECTANGLE, ROUND_RECTANGLE, ELLIPSE);
shapeCombo.setOnAction(this::shapeChange);
shapeCombo.getSelectionModel().selectFirst();
// setup annotation layer combo
layerCombo.getItems().addAll("Lower", "Upper");
layerCombo.getSelectionModel().select("Lower");
layerCombo.setOnAction(event -> {
String selected = layerCombo.getSelectionModel().getSelectedItem();
logger.info("annotation layer selected: {}", selected);
Annotation.Layer layer;
if ("Lower".equals(selected)) {
layer = Annotation.Layer.LOWER;
} else {
layer = Annotation.Layer.UPPER;
}
graph.getGraphMouse().getAnnotatingPlugin().setLayer(layer);
});
layerCombo.getItems().addAll(LOWER_LAYER, UPPER_LAYER);
layerCombo.setOnAction(this::layerChange);
layerCombo.getSelectionModel().selectFirst();
// setup annotation color picker
colorPicker.setOnAction(event -> {
javafx.scene.paint.Color fxColor = colorPicker.getValue();
java.awt.Color color = new java.awt.Color(
(float) fxColor.getRed(),
(float) fxColor.getGreen(),
(float) fxColor.getBlue(),
(float) fxColor.getOpacity()
);
logger.info("color selected: {}", fxColor);
graph.getGraphMouse().getAnnotatingPlugin().setAnnotationColor(color);
});
colorPicker.setOnAction(this::colorChange);
colorPicker.setValue(javafx.scene.paint.Color.AQUA);
colorPicker.fireEvent(new ActionEvent());
// setup annotation toggle fill
fillToggle.setOnAction(event -> {
boolean selected = fillToggle.isSelected();
graph.getGraphMouse().getAnnotatingPlugin().setFill(selected);
});
fillToggle.setOnAction(this::fillChange);
}
private void fillChange(ActionEvent event) {
boolean selected = fillToggle.isSelected();
graph.getGraphMouse().getAnnotatingPlugin().setFill(selected);
}
private void colorChange(ActionEvent event) {
javafx.scene.paint.Color fxColor = colorPicker.getValue();
java.awt.Color color = new java.awt.Color(
(float) fxColor.getRed(),
(float) fxColor.getGreen(),
(float) fxColor.getBlue(),
(float) fxColor.getOpacity()
);
logger.info("color selected: {}", fxColor);
graph.getGraphMouse().getAnnotatingPlugin().setAnnotationColor(color);
}
private void layerChange(ActionEvent event) {
String selected = layerCombo.getSelectionModel().getSelectedItem();
logger.info("annotation layer selected: {}", selected);
Annotation.Layer layer;
if (LOWER_LAYER.equals(selected)) {
layer = Annotation.Layer.LOWER;
} else {
layer = Annotation.Layer.UPPER;
}
graph.getGraphMouse().getAnnotatingPlugin().setLayer(layer);
}
private void shapeChange(ActionEvent event) {
String selected = shapeCombo.getSelectionModel().getSelectedItem();
logger.info("annotation shape selected: {}", selected);
RectangularShape shape = new Rectangle();
switch (selected) {
case RECTANGLE:
shape = new Rectangle();
break;
case ROUND_RECTANGLE:
shape = new RoundRectangle2D.Double(0, 0, 0, 0, 50.0, 50.0);
break;
case ELLIPSE:
shape = new Ellipse2D.Double();
break;
default:
Toast.error("Unknown annotation shape " + selected);
}
graph.getGraphMouse().getAnnotatingPlugin().setRectangularShape(shape);
}
}

View file

@ -4,9 +4,9 @@ import com.core.data.CoreInterface;
import com.core.data.CoreLink;
import com.core.data.CoreNode;
import com.core.graph.NetworkGraph;
import com.core.utils.FxmlUtils;
import com.jfoenix.controls.JFXTextField;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Insets;
import javafx.geometry.Orientation;
import javafx.scene.control.Label;
@ -16,8 +16,6 @@ import javafx.scene.layout.GridPane;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.io.IOException;
public class LinkDetails extends ScrollPane {
private static final Logger logger = LogManager.getLogger();
private static final int START_INDEX = 1;
@ -27,15 +25,7 @@ public class LinkDetails extends ScrollPane {
public LinkDetails(NetworkGraph graph) {
this.graph = graph;
FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/link_details.fxml"));
loader.setRoot(this);
loader.setController(this);
try {
loader.load();
} catch (IOException ex) {
throw new RuntimeException(ex);
}
FxmlUtils.loadRootController(this, "/fxml/link_details.fxml");
}
public void setLink(CoreLink link) {

View file

@ -3,10 +3,10 @@ package com.core.ui;
import com.core.Controller;
import com.core.data.CoreNode;
import com.core.data.MobilityConfig;
import com.core.utils.FxmlUtils;
import com.core.utils.IconUtils;
import com.jfoenix.controls.JFXButton;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import org.apache.logging.log4j.LogManager;
@ -28,15 +28,7 @@ public class MobilityPlayer extends HBox {
public MobilityPlayer(Controller controller) {
this.controller = controller;
FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/mobility_player.fxml"));
loader.setRoot(this);
loader.setController(this);
try {
loader.load();
} catch (IOException ex) {
throw new RuntimeException(ex);
}
FxmlUtils.loadRootController(this, "/fxml/mobility_player.fxml");
playButton.setGraphic(IconUtils.get("play_arrow", ICON_SIZE, ICON_FILL));
playButton.setOnAction(event -> action("start"));

View file

@ -5,6 +5,7 @@ import com.core.data.CoreInterface;
import com.core.data.CoreLink;
import com.core.data.CoreNode;
import com.core.data.NodeType;
import com.core.utils.FxmlUtils;
import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXListView;
import com.jfoenix.controls.JFXScrollPane;
@ -12,7 +13,6 @@ import com.jfoenix.controls.JFXTextField;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Insets;
import javafx.geometry.Orientation;
import javafx.scene.control.Label;
@ -22,8 +22,6 @@ import javafx.scene.layout.GridPane;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.io.IOException;
public class NodeDetails extends ScrollPane {
private static final Logger logger = LogManager.getLogger();
private static final int START_INDEX = 1;
@ -34,22 +32,12 @@ public class NodeDetails extends ScrollPane {
public NodeDetails(Controller controller) {
this.controller = controller;
FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/node_details.fxml"));
loader.setRoot(this);
loader.setController(this);
try {
loader.load();
} catch (IOException ex) {
throw new RuntimeException(ex);
}
FxmlUtils.loadRootController(this, "/fxml/node_details.fxml");
setPrefWidth(500);
}
public void setNode(CoreNode node) {
clear();
addSeparator();
addRow("Name", node.getName());

View file

@ -0,0 +1,22 @@
package com.core.utils;
import javafx.fxml.FXMLLoader;
import java.io.IOException;
public final class FxmlUtils {
private FxmlUtils() {
}
public static void loadRootController(Object obj, String fxmlPath) {
FXMLLoader loader = new FXMLLoader(FxmlUtils.class.getResource(fxmlPath));
loader.setRoot(obj);
loader.setController(obj);
try {
loader.load();
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}
}