client
Clone or download
Modified Files
package com.stream_pi.client.window.dashboard.actiongridpane;
package com.stream_pi.client.window.dashboard.actiongridpane;
import com.stream_pi.action_api.action.Action;
import com.stream_pi.action_api.action.Action;
import com.stream_pi.action_api.action.ActionType;
import com.stream_pi.action_api.action.ActionType;
import com.stream_pi.action_api.action.DisplayTextAlignment;
import com.stream_pi.action_api.action.DisplayTextAlignment;
import com.stream_pi.client.controller.ClientListener;
import com.stream_pi.client.controller.ClientListener;
import com.stream_pi.client.io.Config;
import com.stream_pi.client.io.Config;
import com.stream_pi.client.window.ExceptionAndAlertHandler;
import com.stream_pi.client.window.ExceptionAndAlertHandler;
import com.stream_pi.util.alert.StreamPiAlertType;
import com.stream_pi.util.alert.StreamPiAlertType;
import com.stream_pi.util.exception.SevereException;
import com.stream_pi.util.exception.SevereException;
import javafx.animation.Interpolator;
import javafx.animation.Interpolator;
import javafx.animation.KeyFrame;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.animation.Timeline;
import javafx.geometry.Pos;
import javafx.geometry.Pos;
import javafx.scene.CacheHint;
import javafx.scene.CacheHint;
import javafx.scene.control.Label;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.Image;
import javafx.scene.layout.Background;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundImage;
import javafx.scene.layout.BackgroundImage;
import javafx.scene.layout.BackgroundPosition;
import javafx.scene.layout.BackgroundPosition;
import javafx.scene.layout.BackgroundRepeat;
import javafx.scene.layout.BackgroundRepeat;
import javafx.scene.layout.BackgroundSize;
import javafx.scene.layout.BackgroundSize;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.StackPane;
import javafx.scene.text.TextAlignment;
import javafx.scene.text.TextAlignment;
import javafx.util.Duration;
import javafx.util.Duration;
import org.kordamp.ikonli.javafx.FontIcon;
import org.kordamp.ikonli.javafx.FontIcon;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayInputStream;
import java.io.ObjectInputStream;
import java.io.ObjectInputStream;
import java.nio.ByteBuffer;
import java.nio.ByteBuffer;
import java.util.logging.Logger;
import java.util.logging.Logger;
public class ActionBox extends StackPane
public class ActionBox extends StackPane
{
{
private Label displayTextLabel;
private Label displayTextLabel;
private int row;
private int row;
private int col;
private int col;
public int getRow() {
public int getRow() {
return row;
return row;
}
}
public int getCol() {
public int getCol() {
return col;
return col;
}
}
private Logger logger;
private Logger logger;
public void clear()
public void clear()
{
{
setStyle(null);
setStyle(null);
setAction(null);
setAction(null);
setCurrentToggleStatus(false);
setCurrentToggleStatus(false);
getStyleClass().clear();
getStyleClass().clear();
setBackground(Background.EMPTY);
setBackground(Background.EMPTY);
removeFontIcon();
removeFontIcon();
getChildren().clear();
getChildren().clear();
baseInit();
baseInit();
}
}
private FontIcon statusIcon;
private FontIcon statusIcon;
public void baseInit()
public void baseInit()
{
{
displayTextLabel = new Label();
displayTextLabel = new Label();
displayTextLabel.setWrapText(true);
displayTextLabel.setWrapText(true);
displayTextLabel.setTextAlignment(TextAlignment.CENTER);
displayTextLabel.setTextAlignment(TextAlignment.CENTER);
displayTextLabel.getStyleClass().add("action_box_display_text_label");
displayTextLabel.getStyleClass().add("action_box_display_text_label");
displayTextLabel.prefHeightProperty().bind(heightProperty());
displayTextLabel.prefHeightProperty().bind(heightProperty());
displayTextLabel.prefWidthProperty().bind(widthProperty());
displayTextLabel.prefWidthProperty().bind(widthProperty());
statusIcon = new FontIcon("fas-exclamation-triangle");
statusIcon = new FontIcon("fas-exclamation-triangle");
statusIcon.getStyleClass().add("action_box_error_icon");
statusIcon.getStyleClass().add("action_box_error_icon");
statusIcon.setOpacity(0);
statusIcon.setOpacity(0);
statusIcon.setCache(true);
statusIcon.setCache(true);
statusIcon.setCacheHint(CacheHint.SPEED);
statusIcon.setCacheHint(CacheHint.SPEED);
statusIcon.setIconSize(size - 30);
statusIcon.setIconSize(size - 30);
getChildren().addAll(statusIcon, displayTextLabel);
getChildren().addAll(statusIcon, displayTextLabel);
setMinSize(size, size);
setMinSize(size, size);
setMaxSize(size, size);
setMaxSize(size, size);
getStyleClass().clear();
getStyleClass().clear();
getStyleClass().add("action_box");
getStyleClass().add("action_box");
getStyleClass().add("action_box_"+row+"_"+col);
getStyleClass().add("action_box_"+row+"_"+col);
setIcon(null);
setIcon(null);
setOnMouseClicked(touchEvent -> actionClicked());
setOnMouseClicked(touchEvent -> actionClicked());
setOnMousePressed(TouchEvent -> {
setOnMousePressed(TouchEvent -> {
if(action != null)
if(action != null)
{
{
getStyleClass().add("action_box_onclick");
getStyleClass().add("action_box_onclick");
}
}
});
});
setOnMouseReleased(TouchEvent ->{
setOnMouseReleased(TouchEvent ->{
if(action != null)
if(action != null)
{
{
getStyleClass().remove("action_box_onclick");
getStyleClass().remove("action_box_onclick");
}
}
});
});
statusIconAnimation = new Timeline(
statusIconAnimation = new Timeline(
new KeyFrame(
new KeyFrame(
Duration.millis(0.0D),
Duration.millis(0.0D),
new KeyValue(statusIcon.opacityProperty(), 0.0D, Interpolator.EASE_IN)),
new KeyValue(statusIcon.opacityProperty(), 0.0D, Interpolator.EASE_IN)),
new KeyFrame(
new KeyFrame(
Duration.millis(100.0D),
Duration.millis(100.0D),
new KeyValue(statusIcon.opacityProperty(), 1.0D, Interpolator.EASE_IN)),
new KeyValue(statusIcon.opacityProperty(), 1.0D, Interpolator.EASE_IN)),
new KeyFrame(
new KeyFrame(
Duration.millis(600.0D),
Duration.millis(600.0D),
new KeyValue(statusIcon.opacityProperty(), 1.0D, Interpolator.EASE_OUT)),
new KeyValue(statusIcon.opacityProperty(), 1.0D, Interpolator.EASE_OUT)),
new KeyFrame(
new KeyFrame(
Duration.millis(700.0D),
Duration.millis(700.0D),
new KeyValue(statusIcon.opacityProperty(), 0.0D, Interpolator.EASE_OUT))
new KeyValue(statusIcon.opacityProperty(), 0.0D, Interpolator.EASE_OUT))
);
);
statusIconAnimation.setOnFinished(event -> {
statusIconAnimation.setOnFinished(event -> {
statusIcon.toBack();
statusIcon.toBack();
});
});
setCache(true);
setCache(true);
setCacheHint(CacheHint.QUALITY);
setCacheHint(CacheHint.QUALITY);
}
}
public void actionClicked()
public void actionClicked()
{
{
if(action!=null)
if(action!=null)
{
{
if(!getActionGridPaneListener().isConnected())
if(!getActionGridPaneListener().isConnected())
{
{
try
try
{
{
if(Config.getInstance().isTryConnectingWhenActionClicked())
if(Config.getInstance().isTryConnectingWhenActionClicked())
{
{
clientListener.setupClientConnection(this::actionClicked);
clientListener.setupClientConnection(this::actionClicked);
}
}
else
else
{
{
exceptionAndAlertHandler.onAlert("Not Connected", "Not Connected to any Server", StreamPiAlertType.ERROR);
exceptionAndAlertHandler.onAlert("Not Connected", "Not Connected to any Server", StreamPiAlertType.ERROR);
}
}
return;
return;
}
}
catch (SevereException e)
catch (SevereException e)
{
{
exceptionAndAlertHandler.handleSevereException(e);
exceptionAndAlertHandler.handleSevereException(e);
}
}
}
}
if(action.getActionType() == ActionType.FOLDER)
if(action.getActionType() == ActionType.FOLDER)
{
{
getActionGridPaneListener().renderFolder(action.getID());
getActionGridPaneListener().renderFolder(action.getID());
}
}
else
else
{
{
if(action.getActionType() == ActionType.COMBINE)
if(action.getActionType() == ActionType.COMBINE)
{
{
getActionGridPaneListener().combineActionClicked(action.getID());
getActionGridPaneListener().combineActionClicked(action.getID());
}
}
else if(action.getActionType() == ActionType.NORMAL)
else if(action.getActionType() == ActionType.NORMAL)
{
{
getActionGridPaneListener().normalActionClicked(action.getID());
getActionGridPaneListener().normalActionClicked(action.getID());
}
}
else if(action.getActionType() == ActionType.TOGGLE)
else if(action.getActionType() == ActionType.TOGGLE)
{
{
toggle();
toggle();
getActionGridPaneListener().toggleActionClicked(action.getID(), getCurrentToggleStatus());
getActionGridPaneListener().toggleActionClicked(action.getID(), getCurrentToggleStatus());
}
}
}
}
}
}
}
}
private Timeline statusIconAnimation;
private Timeline statusIconAnimation;
public Timeline getStatusIconAnimation() {
public Timeline getStatusIconAnimation() {
return statusIconAnimation;
return statusIconAnimation;
}
}
public ActionGridPaneListener getActionGridPaneListener() {
public ActionGridPaneListener getActionGridPaneListener() {
return actionGridPaneListener;
return actionGridPaneListener;
}
}
private int size;
private int size;
private ActionGridPaneListener actionGridPaneListener;
private ActionGridPaneListener actionGridPaneListener;
private ClientListener clientListener;
private ClientListener clientListener;
public ActionBox(int size, ExceptionAndAlertHandler exceptionAndAlertHandler,
public ActionBox(int size, ExceptionAndAlertHandler exceptionAndAlertHandler,
ClientListener clientListener, ActionGridPaneListener actionGridPaneListener, int row, int col)
ClientListener clientListener, ActionGridPaneListener actionGridPaneListener, int row, int col)
{
{
this.actionGridPaneListener = actionGridPaneListener;
this.actionGridPaneListener = actionGridPaneListener;
this.exceptionAndAlertHandler = exceptionAndAlertHandler;
this.exceptionAndAlertHandler = exceptionAndAlertHandler;
this.size = size;
this.size = size;
this.row = row;
this.row = row;
this.col = col;
this.col = col;
this.clientListener = clientListener;
this.clientListener = clientListener;
this.logger = Logger.getLogger("");
this.logger = Logger.getLogger("");
baseInit();
baseInit();
}
}
public Logger getLogger()
public Logger getLogger()
{
{
return logger;
return logger;
}
}
public static Action deserialize(ByteBuffer buffer) {
try {
ByteArrayInputStream is = new ByteArrayInputStream(buffer.array());
ObjectInputStream ois = new ObjectInputStream(is);
return (Action) ois.readObject();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
public void setIcon(byte[] iconByteArray)
public void setIcon(byte[] iconByteArray)
{
{
removeFontIcon();
removeFontIcon();
if(iconByteArray == null)
if(iconByteArray == null)
{
{
getStyleClass().remove("action_box_icon_present");
getStyleClass().remove("action_box_icon_present");
getStyleClass().add("action_box_icon_not_present");
getStyleClass().add("action_box_icon_not_present");
setBackground(null);
setBackground(null);
}
}
else
else
{
{
getStyleClass().add("action_box_icon_present");
getStyleClass().add("action_box_icon_present");
getStyleClass().remove("action_box_icon_not_present");
getStyleClass().remove("action_box_icon_not_present");
setBackground(
setBackground(
new Background(
new Background(
new BackgroundImage(new Image(
new BackgroundImage(new Image(
new ByteArrayInputStream(iconByteArray), size, size, false, true
new ByteArrayInputStream(iconByteArray), size, size, false, true
), BackgroundRepeat.NO_REPEAT, BackgroundRepeat.NO_REPEAT, BackgroundPosition.CENTER,
), BackgroundRepeat.NO_REPEAT, BackgroundRepeat.NO_REPEAT, BackgroundPosition.CENTER,
new BackgroundSize(100, 100, true, true, true, false))
new BackgroundSize(100, 100, true, true, true, false))
)
)
);
);
}
}
}
}
private Action action = null;
private Action action = null;
public Action getAction() {
public Action getAction() {
return action;
return action;
}
}
private ExceptionAndAlertHandler exceptionAndAlertHandler;
private ExceptionAndAlertHandler exceptionAndAlertHandler;
private String parent;
private String parent;
public String getStreamPiParent() {
public String getStreamPiParent() {
return parent;
return parent;
}
}
public void setStreamPiParent(String parent) {
public void setStreamPiParent(String parent) {
this.parent = parent;
this.parent = parent;
}
}
public void setAction(Action action)
public void setAction(Action action)
{
{
this.action = action;
this.action = action;
}
}
public void init()
public void init()
{
{
setBackground(null);
setBackground(null);
setStyle(null);
setStyle(null);
setDisplayTextFontColour(action.getDisplayTextFontColourHex());
setDisplayTextFontColour(action.getDisplayTextFontColourHex());
if(action.isShowDisplayText())
if(action.isShowDisplayText())
setDisplayTextLabel(action.getDisplayText());
setDisplayTextLabel(action.getDisplayText());
else
else
setDisplayTextLabel("");
setDisplayTextLabel("");
setDisplayTextAlignment(action.getDisplayTextAlignment());
setDisplayTextAlignment(action.getDisplayTextAlignment());
setBackgroundColour(action.getBgColourHex());
setBackgroundColour(action.getBgColourHex());
try
try
{
{
if(action.getActionType() == ActionType.TOGGLE)
if(action.getActionType() == ActionType.TOGGLE)
{
{
toggle(getCurrentToggleStatus());
toggle(getCurrentToggleStatus());
}
}
else
else
{
{
if(action.isHasIcon())
if(action.isHasIcon())
{
{
if(!action.getCurrentIconState().isBlank())
if(!action.getCurrentIconState().isBlank())
{
{
setIcon(action.getCurrentIcon());
setIcon(action.getCurrentIcon());
}
}
}
}
else
else
{
{
setIcon(null);
setIcon(null);
}
}
}
}
}
}
catch (Exception e)
catch (Exception e)
{
{
e.printStackTrace();
e.printStackTrace();
}
}
}
}
public void setCurrentToggleStatus(boolean currentToggleStatus)
public void setCurrentToggleStatus(boolean currentToggleStatus)
{
{
if(getAction() != null)
if(getAction() != null)
getAction().setCurrentToggleStatus(currentToggleStatus);
getAction().setCurrentToggleStatus(currentToggleStatus);
}
}
public boolean getCurrentToggleStatus()
public boolean getCurrentToggleStatus()
{
{
if(getAction() == null)
if(getAction() == null)
return false;
return false;
return getAction().getCurrentToggleStatus();
return getAction().getCurrentToggleStatus();
}
}
public void toggle()
public void toggle()
{
{
setCurrentToggleStatus(!getCurrentToggleStatus());
setCurrentToggleStatus(!getCurrentToggleStatus());
toggle(getCurrentToggleStatus());
toggle(getCurrentToggleStatus());
}
}
public void toggle(boolean isON)
public void toggle(boolean isON)
{
{
String[] toggleStatesHiddenStatus = action.getCurrentIconState().split("__");
String[] toggleStatesHiddenStatus = action.getCurrentIconState().split("__");
boolean isToggleOffHidden = toggleStatesHiddenStatus[0].equals("true");
boolean isToggleOffHidden = toggleStatesHiddenStatus[0].equals("true");
boolean isToggleOnHidden = toggleStatesHiddenStatus[1].equals("true");
boolean isToggleOnHidden = toggleStatesHiddenStatus[1].equals("true");
if(isON) // ON
if(isON) // ON
{
{
if(action.isHasIcon())
if(action.isHasIcon())
{
{
boolean isToggleOnPresent = action.getIcons().containsKey("toggle_on");
boolean isToggleOnPresent = action.getIcons().containsKey("toggle_on");
if(isToggleOnPresent)
if(isToggleOnPresent)
{
{
if(isToggleOnHidden)
if(isToggleOnHidden)
{
{
setDefaultToggleIcon(true);
setDefaultToggleIcon(true);
}
}
else
else
{
{
setIcon(action.getIcons().get("toggle_on"));
setIcon(action.getIcons().get("toggle_on"));
}
}
}
}
else
else
{
{
setDefaultToggleIcon(true);
setDefaultToggleIcon(true);
}
}
}
}
else
else
{
{
setDefaultToggleIcon(true);
setDefaultToggleIcon(true);
}
}
}
}
else // OFF
else // OFF
{
{
if(action.isHasIcon())
if(action.isHasIcon())
{
{
boolean isToggleOffPresent = action.getIcons().containsKey("toggle_off");
boolean isToggleOffPresent = action.getIcons().containsKey("toggle_off");
if(isToggleOffPresent)
if(isToggleOffPresent)
{
{
if(isToggleOffHidden)
if(isToggleOffHidden)
{
{
setDefaultToggleIcon(false);
setDefaultToggleIcon(false);
}
}
else
else
{
{
setIcon(action.getIcons().get("toggle_off"));
setIcon(action.getIcons().get("toggle_off"));
}
}
}
}
else
else
{
{
setDefaultToggleIcon(false);
setDefaultToggleIcon(false);
}
}
}
}
else
else
{
{
setDefaultToggleIcon(false);
setDefaultToggleIcon(false);
}
}
}
}
}
}
public void setDefaultToggleIcon(boolean isToggleOn)
public void setDefaultToggleIcon(boolean isToggleOn)
{
{
String styleClass;
String styleClass;
if(isToggleOn)
if(isToggleOn)
{
{
styleClass = "action_box_toggle_on";
styleClass = "action_box_toggle_on";
}
}
else
else
{
{
styleClass = "action_box_toggle_off";
styleClass = "action_box_toggle_off";
}
}
setBackground(null);
setBackground(null);
if(fontIcon!=null)
if(fontIcon!=null)
{
{
fontIcon.getStyleClass().clear();
fontIcon.getStyleClass().clear();
}
}
else
else
{
{
fontIcon = new FontIcon();
fontIcon = new FontIcon();
fontIcon.setIconSize((int) (size * 0.8));
fontIcon.setIconSize((int) (size * 0.8));
getChildren().add(fontIcon);
getChildren().add(fontIcon);
}
}
fontIcon.getStyleClass().add(styleClass);
fontIcon.getStyleClass().add(styleClass);
fontIcon.toBack();
fontIcon.toBack();
}
}
public void removeFontIcon()
public void removeFontIcon()
{
{
if(fontIcon!=null)
if(fontIcon!=null)
{
{
getChildren().remove(fontIcon);
getChildren().remove(fontIcon);
fontIcon = null;
fontIcon = null;
}
}
}
}
FontIcon fontIcon = null;
FontIcon fontIcon = null;
public void animateStatus()
public void animateStatus()
{
{
statusIcon.toFront();
statusIcon.toFront();
statusIconAnimation.play();
statusIconAnimation.play();
}
}
public void setDisplayTextLabel(String text)
public void setDisplayTextLabel(String text)
{
{
displayTextLabel.setText(text);
displayTextLabel.setText(text);
}
}
public void setDisplayTextAlignment(DisplayTextAlignment displayTextAlignment)
public void setDisplayTextAlignment(DisplayTextAlignment displayTextAlignment)
{
{
if(displayTextAlignment == DisplayTextAlignment.CENTER)
if(displayTextAlignment == DisplayTextAlignment.CENTER)
displayTextLabel.setAlignment(Pos.CENTER);
displayTextLabel.setAlignment(Pos.CENTER);
else if (displayTextAlignment == DisplayTextAlignment.BOTTOM)
else if (displayTextAlignment == DisplayTextAlignment.BOTTOM)
displayTextLabel.setAlignment(Pos.BOTTOM_CENTER);
displayTextLabel.setAlignment(Pos.BOTTOM_CENTER);
else if (displayTextAlignment == DisplayTextAlignment.TOP)
else if (displayTextAlignment == DisplayTextAlignment.TOP)
displayTextLabel.setAlignment(Pos.TOP_CENTER);
displayTextLabel.setAlignment(Pos.TOP_CENTER);
}
}
public void setDisplayTextFontColour(String colour)
public void setDisplayTextFontColour(String colour)
{
{
if(!colour.isEmpty())
if(!colour.isEmpty())
{
{
displayTextLabel.setStyle("-fx-text-fill : "+colour+";");
displayTextLabel.setStyle("-fx-text-fill : "+colour+";");
}
}
}
}
public void setBackgroundColour(String colour)
public void setBackgroundColour(String colour)
{
{
if(!colour.isEmpty())
if(!colour.isEmpty())
setStyle("-fx-background-color : "+colour);
setStyle("-fx-background-color : "+colour);
}
}
}
}