From: rnayabed Date: Wed, 17 Mar 2021 19:27:28 +0530 Subject: Implementing Toggle Action --- Implementing Toggle Action --- --- 'a/src/main/java/com/stream_pi/server/connection/ClientConnection.java' +++ b/src/main/java/com/stream_pi/server/connection/ClientConnection.java @@ -105,7 +105,7 @@ public class ClientConnection extends Th } - public void sendIcon(String profileID, String actionID, byte[] icon) throws SevereException + public void sendIcon(String profileID, String actionID, String state, byte[] icon) throws SevereException { try { @@ -117,7 +117,7 @@ public class ClientConnection extends Th } Message message = new Message("action_icon"); - message.setStringArrValue(profileID, actionID); + message.setStringArrValue(profileID, actionID, state); message.setByteArrValue(icon); sendMessage(message); } @@ -265,14 +265,18 @@ public class ClientConnection extends Th // commands - private void onActionIconReceived(Message message) + private void onActionIconReceived(Message message) throws MinorException { String[] s = message.getStringArrValue(); String profileID = s[0]; String actionID = s[1]; + String iconState = s[2]; - getClient().getProfileByID(profileID).getActionByID(actionID).setIcon(message.getByteArrValue()); + getClient() + .getProfileByID(profileID) + .getActionByID(actionID) + .addIcon(iconState,message.getByteArrValue()); } public void initAfterConnectionQuerySend() throws SevereException @@ -423,12 +427,6 @@ public class ClientConnection extends Th serverListener.clearTemp(); } - /*public void getActionIcon(String profileID, String actionID) throws StreamPiException - { - System.out.println("getting action icon from "+profileID+", "+actionID); - writeToStream("get_action_icon::"+profileID+"::"+actionID); - }*/ - public synchronized void registerActionToProfile(Message message) throws StreamPiException { String[] r = message.getStringArrValue(); @@ -445,8 +443,8 @@ public class ClientConnection extends Th String bgColorHex = r[5]; //icon - boolean isHasIcon = r[6].equals("true"); - boolean isShowIcon = r[7].equals("true"); + String[] allIconStateNames = r[6].split("::"); + String defaultIconState = r[7]; //text boolean isShowDisplayText = r[8].equals("true"); @@ -465,8 +463,7 @@ public class ClientConnection extends Th Action action = new Action(ID, actionType); action.setBgColourHex(bgColorHex); - action.setShowIcon(isShowIcon); - action.setHasIcon(isHasIcon); + action.setCurrentIconState(defaultIconState); action.setShowDisplayText(isShowDisplayText); action.setDisplayTextFontColourHex(displayFontColor); @@ -553,26 +550,7 @@ public class ClientConnection extends Th try { - for(Property prop : action.getClientProperties().get()) - { - logger.info("G@@@@@ : "+prop.getRawValue()); - } - - getClient().getProfileByID(profileID).addAction(action); - - - - for(String action1x : getClient().getProfileByID(profileID).getActionsKeySet()) - { - Action action1 = getClient().getProfileByID(profileID).getActionByID(action1x); - logger.info("231cc : "+action1.getID()); - for(Property prop : action1.getClientProperties().get()) - { - logger.info("G@VVVV@@@ : "+prop.getRawValue()); - } - } - } catch (CloneNotSupportedException e) { @@ -616,8 +594,16 @@ public class ClientConnection extends Th a.add(action.getBgColourHex()); //icon - a.add(action.isHasIcon()+""); - a.add(action.isShowIcon()+""); + + StringBuilder allIconStatesNames = new StringBuilder(); + for(String eachState : action.getIcons().keySet()) + { + allIconStatesNames.append(eachState).append("::"); + } + a.add(allIconStatesNames.toString()); + + + a.add(action.getCurrentIconState()); //text a.add(action.isShowDisplayText()+""); --- 'a/src/main/java/com/stream_pi/server/window/dashboard/actiondetailpane/ActionDetailsPane.java' +++ b/src/main/java/com/stream_pi/server/window/dashboard/actiondetailpane/ActionDetailsPane.java @@ -46,7 +46,8 @@ import java.nio.file.Files; import java.util.ArrayList; import java.util.logging.Logger; -public class ActionDetailsPane extends VBox implements ActionDetailsPaneListener { +public class ActionDetailsPane extends VBox implements ActionDetailsPaneListener +{ private ScrollPane scrollPane; @@ -69,7 +70,8 @@ public class ActionDetailsPane extends V private HostServices hostServices; - public ActionDetailsPane(ExceptionAndAlertHandler exceptionAndAlertHandler, HostServices hostServices) { + public ActionDetailsPane(ExceptionAndAlertHandler exceptionAndAlertHandler, HostServices hostServices) + { this.hostServices = hostServices; this.exceptionAndAlertHandler = exceptionAndAlertHandler; @@ -177,23 +179,57 @@ public class ActionDetailsPane extends V displayNameTextField = new TextField(); displayNameTextField.managedProperty().bind(displayNameTextField.visibleProperty()); - iconFileTextField = new TextField(); - iconFileTextField.managedProperty().bind(iconFileTextField.visibleProperty()); - iconFileTextField.textProperty().addListener((observableValue, s, t1) -> { + defaultIconFileTextField = new TextField(); + defaultIconFileTextField.textProperty().addListener((observableValue, s, t1) -> { try { if (!s.equals(t1) && t1.length() > 0) { byte[] iconFileByteArray = Files.readAllBytes(new File(t1).toPath()); - hideIconCheckBox.setDisable(false); - hideIconCheckBox.setSelected(false); + hideDefaultIconCheckBox.setDisable(false); + hideDefaultIconCheckBox.setSelected(false); clearIconButton.setDisable(false); - System.out.println("ABABABABABBABABBABABABCCCCCCCCCCCCCCCCCC"); + action.setDefaultIcon(iconFileByteArray); + setSendIcon(true); + } + } catch (Exception e) { + e.printStackTrace(); + exceptionAndAlertHandler.handleMinorException(new MinorException(e.getMessage())); + } + }); + + toggleOffIconFileTextField = new TextField(); + toggleOffIconFileTextField.textProperty().addListener((observableValue, s, t1) -> { + try { + if (!s.equals(t1) && t1.length() > 0) { + byte[] iconFileByteArray = Files.readAllBytes(new File(t1).toPath()); - action.setIcon(iconFileByteArray); + hideToggleOffIconCheckBox.setDisable(false); + hideToggleOffIconCheckBox.setSelected(false); + clearIconButton.setDisable(false); + + action.setToggleOffIcon(iconFileByteArray); setSendIcon(true); + } + } catch (Exception e) { + e.printStackTrace(); + exceptionAndAlertHandler.handleMinorException(new MinorException(e.getMessage())); + } + }); + - System.out.println(action.getIconAsByteArray().length); + toggleOnIconFileTextField = new TextField(); + toggleOnIconFileTextField.textProperty().addListener((observableValue, s, t1) -> { + try { + if (!s.equals(t1) && t1.length() > 0) { + byte[] iconFileByteArray = Files.readAllBytes(new File(t1).toPath()); + + hideToggleOnIconCheckBox.setDisable(false); + hideToggleOnIconCheckBox.setSelected(false); + clearIconButton.setDisable(false); + + action.setToggleOnIcon(iconFileByteArray); + setSendIcon(true); } } catch (Exception e) { e.printStackTrace(); @@ -201,23 +237,37 @@ public class ActionDetailsPane extends V } }); + clearIconButton = new Button("Clear Icon"); clearIconButton.managedProperty().bind(clearIconButton.visibleProperty()); clearIconButton.setOnAction(event -> { - hideIconCheckBox.setDisable(true); - hideIconCheckBox.setSelected(false); + hideDefaultIconCheckBox.setDisable(true); + hideDefaultIconCheckBox.setSelected(false); + + hideToggleOffIconCheckBox.setDisable(true); + hideToggleOffIconCheckBox.setSelected(false); + + hideToggleOnIconCheckBox.setDisable(true); + hideToggleOnIconCheckBox.setSelected(false); clearIconButton.setDisable(true); + setSendIcon(false); - iconFileTextField.clear(); + + defaultIconFileTextField.clear(); + toggleOffIconFileTextField.clear(); + toggleOnIconFileTextField.clear(); }); hideDisplayTextCheckBox = new CheckBox("Hide"); hideDisplayTextCheckBox.managedProperty().bind(hideDisplayTextCheckBox.visibleProperty()); - hideIconCheckBox = new CheckBox("Hide"); - hideIconCheckBox.managedProperty().bind(hideIconCheckBox.visibleProperty()); + hideDefaultIconCheckBox = new CheckBox("Hide"); + + hideToggleOnIconCheckBox = new CheckBox("Hide"); + + hideToggleOffIconCheckBox = new CheckBox("Hide"); actionBackgroundColourPicker = new ColorPicker(); actionBackgroundColourPicker.managedProperty().bind(actionBackgroundColourPicker.visibleProperty()); @@ -264,14 +314,29 @@ public class ActionDetailsPane extends V new HBox(new Label("Alignment"), SpaceFiller.horizontal(), displayTextAlignmentComboBox), - new HBoxInputBoxWithFileChooser("Icon", iconFileTextField, hideIconCheckBox, + new HBoxInputBoxWithFileChooser("Icon", defaultIconFileTextField, hideDefaultIconCheckBox, new FileChooser.ExtensionFilter("Images", "*.jpeg", "*.jpg", "*.png", "*.gif")), clearIconHBox, bgColourHBox); normalActionsPropsVBox.managedProperty().bind(normalActionsPropsVBox.visibleProperty()); normalActionsPropsVBox.setSpacing(10.0); - vbox.getChildren().addAll(displayTextFieldHBox, normalActionsPropsVBox, clientPropertiesVBox); + toggleActionsPropsVBox = new VBox(displayTextColourHBox, + + new HBox(new Label("Alignment"), SpaceFiller.horizontal(), + displayTextAlignmentComboBox), + + new HBoxInputBoxWithFileChooser("Toggle Off Icon", toggleOffIconFileTextField, hideToggleOffIconCheckBox, + new FileChooser.ExtensionFilter("Images", "*.jpeg", "*.jpg", "*.png", "*.gif")), + + new HBoxInputBoxWithFileChooser("Toggle On Icon", toggleOnIconFileTextField, hideToggleOnIconCheckBox, + new FileChooser.ExtensionFilter("Images", "*.jpeg", "*.jpg", "*.png", "*.gif")), + + clearIconHBox, bgColourHBox); + toggleActionsPropsVBox.managedProperty().bind(toggleActionsPropsVBox.visibleProperty()); + toggleActionsPropsVBox.setSpacing(10.0); + + vbox.getChildren().addAll(displayTextFieldHBox, normalActionsPropsVBox, toggleActionsPropsVBox, clientPropertiesVBox); vbox.setVisible(false); scrollPane.setVbarPolicy(ScrollPane.ScrollBarPolicy.NEVER); @@ -321,6 +386,7 @@ public class ActionDetailsPane extends V } private VBox normalActionsPropsVBox; + private VBox toggleActionsPropsVBox; private HBox displayTextFieldHBox; @@ -372,8 +438,16 @@ public class ActionDetailsPane extends V private TextField displayNameTextField; private CheckBox hideDisplayTextCheckBox; - private CheckBox hideIconCheckBox; - private TextField iconFileTextField; + + private CheckBox hideDefaultIconCheckBox; + private TextField defaultIconFileTextField; + + private CheckBox hideToggleOnIconCheckBox; + private TextField toggleOnIconFileTextField; + + private CheckBox hideToggleOffIconCheckBox; + private TextField toggleOffIconFileTextField; + private Button clearIconButton; private ColorPicker actionBackgroundColourPicker; private ColorPicker displayTextColourPicker; @@ -386,7 +460,13 @@ public class ActionDetailsPane extends V sendIcon = false; actionClientProperties.clear(); displayNameTextField.clear(); - iconFileTextField.clear(); + + + defaultIconFileTextField.clear(); + toggleOffIconFileTextField.clear(); + toggleOnIconFileTextField.clear(); + + clientPropertiesVBox.getChildren().clear(); vbox.setVisible(false); scrollPane.setVbarPolicy(ScrollPane.ScrollBarPolicy.NEVER); @@ -420,7 +500,41 @@ public class ActionDetailsPane extends V } else { - normalActionsPropsVBox.setVisible(true); + if(getAction().getActionType() == ActionType.NORMAL) + { + normalActionsPropsVBox.setVisible(true); + toggleActionsPropsVBox.setVisible(false); + + + boolean doesDefaultExist = action.getIcons().containsKey("default"); + boolean isDefaultHidden = !action.getCurrentIconState().equals("default"); + + hideDefaultIconCheckBox.setDisable(!doesDefaultExist); + hideDefaultIconCheckBox.setSelected(isDefaultHidden); + } + else + { + normalActionsPropsVBox.setVisible(false); + toggleActionsPropsVBox.setVisible(true); + + + boolean doesToggleOnExist = action.getIcons().containsKey("toggle_on"); + boolean isToggleOnHidden = action.getCurrentIconState().contains("toggle_on"); + + hideToggleOnIconCheckBox.setDisable(!doesToggleOnExist); + hideToggleOnIconCheckBox.setSelected(isToggleOnHidden); + + + + + boolean doesToggleOffExist = action.getIcons().containsKey("toggle_off"); + boolean isToggleOffHidden = action.getCurrentIconState().contains("toggle_off"); + + hideToggleOffIconCheckBox.setDisable(!doesToggleOffExist); + hideToggleOffIconCheckBox.setSelected(isToggleOffHidden); + } + + setReturnButtonForCombineActionChildVisible(false); hideDisplayTextCheckBox.setVisible(true); setFolderButtonVisible(action.getActionType().equals(ActionType.FOLDER)); @@ -442,16 +556,6 @@ public class ActionDetailsPane extends V hideDisplayTextCheckBox.setSelected(!action.isShowDisplayText()); - - hideIconCheckBox.setDisable(!action.isHasIcon()); - - hideIconCheckBox.setSelected(!action.isShowIcon()); - - if(!action.isHasIcon()) - { - hideIconCheckBox.setSelected(false); - } - clearIconButton.setDisable(!action.isHasIcon()); } @@ -461,7 +565,7 @@ public class ActionDetailsPane extends V vbox.setVisible(true); scrollPane.setVbarPolicy(ScrollPane.ScrollBarPolicy.AS_NEEDED); - if(action.getActionType() == ActionType.NORMAL) + if(action.getActionType() == ActionType.NORMAL || action.getActionType() == ActionType.TOGGLE) { if(action.isInvalid()) setActionHeadingLabelText("Invalid action ("+action.getModuleName()+")"); @@ -476,7 +580,7 @@ public class ActionDetailsPane extends V if(!action.isInvalid()) { - if(action.getActionType() == ActionType.NORMAL) + if(action.getActionType() == ActionType.NORMAL || action.getActionType() == ActionType.TOGGLE) renderClientProperties(); else if(action.getActionType() == ActionType.COMBINE) renderCombineActionProperties(); @@ -527,10 +631,8 @@ public class ActionDetailsPane extends V folderAction.setParent(action.getParent()); folderAction.getClientProperties().set(action.getClientProperties()); folderAction.setDisplayTextAlignment(action.getDisplayTextAlignment()); - folderAction.setShowIcon(action.isShowIcon()); - folderAction.setHasIcon(action.isHasIcon()); - if(folderAction.isHasIcon()) - folderAction.setIcon(action.getIconAsByteArray()); + folderAction.setIcons(action.getIcons()); + folderAction.setCurrentIconState(action.getCurrentIconState()); folderAction.setDisplayTextFontColourHex(action.getDisplayTextFontColourHex()); return folderAction; @@ -547,16 +649,10 @@ public class ActionDetailsPane extends V combineAction.setParent(action.getParent()); combineAction.getClientProperties().set(action.getClientProperties()); combineAction.setDisplayTextAlignment(action.getDisplayTextAlignment()); - combineAction.setShowIcon(action.isShowIcon()); - combineAction.setHasIcon(action.isHasIcon()); - if(combineAction.isHasIcon()) - combineAction.setIcon(action.getIconAsByteArray()); + combineAction.setIcons(action.getIcons()); + combineAction.setCurrentIconState(action.getCurrentIconState()); combineAction.setDisplayTextFontColourHex(action.getDisplayTextFontColourHex()); - for(Property prop : combineAction.getClientProperties().get()) - { - System.out.println("PROP : "+prop.getName()+","+prop.getRawValue()); - } return combineAction; } @@ -619,7 +715,6 @@ public class ActionDetailsPane extends V hBox.getChildren().add(SpaceFiller.horizontal()); - System.out.println("2222222222dddddddddddddd : "+eachProperty.getControlType()); if(eachProperty.getControlType() == ControlType.COMBO_BOX) { @@ -762,7 +857,9 @@ public class ActionDetailsPane extends V displayTextColourDefaultCheckBox.isSelected(), "#" + displayTextColourPicker.getValue().toString().substring(2), clearIconButton.isDisable(), - !hideIconCheckBox.isSelected(), + !hideDefaultIconCheckBox.isSelected(), + !hideToggleOffIconCheckBox.isSelected(), + !hideToggleOnIconCheckBox.isSelected(), displayTextAlignmentComboBox.getSelectionModel().getSelectedItem(), actionBackgroundColourTransparentCheckBox.isSelected(), "#" + actionBackgroundColourPicker.getValue().toString().substring(2), @@ -796,15 +893,20 @@ public class ActionDetailsPane extends V if(!isCombineChild()) { - if(action.isHasIcon()) - { - if(hideDisplayTextCheckBox.isSelected() && hideIconCheckBox.isSelected()) - finalErrors.append(" * Both Icon and display text check box cannot be hidden.\n"); - } - else + if(action.getActionType() == ActionType.NORMAL) { - if(hideDisplayTextCheckBox.isSelected()) - finalErrors.append(" * Display Text cannot be hidden, since there is also no icon.\n"); + if(action.isHasIcon()) + { + if(hideDisplayTextCheckBox.isSelected() && hideDefaultIconCheckBox.isSelected()) + { + finalErrors.append(" * Both Icon and display text check box cannot be hidden.\n"); + } + } + else + { + if(hideDisplayTextCheckBox.isSelected()) + finalErrors.append(" * Display Text cannot be hidden, since there is also no icon.\n"); + } } } --- 'a/src/main/java/com/stream_pi/server/window/dashboard/actiondetailpane/OnSaveActionTask.java' +++ b/src/main/java/com/stream_pi/server/window/dashboard/actiondetailpane/OnSaveActionTask.java @@ -29,7 +29,7 @@ public class OnSaveActionTask extends Ta public OnSaveActionTask(ClientConnection connection, Action action, String delayBeforeRunningString, String displayNameText, boolean isCombineChild, boolean isShowDisplayText, boolean isDefaultDisplayTextColour, String displayTextFontColour, boolean isClearIcon, - boolean isHideIcon, DisplayTextAlignment displayTextAlignment, boolean isTransparentBackground, String backgroundColour, + boolean isHideDefaultIcon, boolean isHideToggleOffIcon, boolean isHideToggleOnIcon, DisplayTextAlignment displayTextAlignment, boolean isTransparentBackground, String backgroundColour, CombineActionPropertiesPane combineActionPropertiesPane, ClientProfile clientProfile, boolean sendIcon, ActionBox actionBox, ArrayList actionClientProperties, ExceptionAndAlertHandler exceptionAndAlertHandler, Button saveButton, Button deleteButton, boolean runAsync) @@ -46,7 +46,9 @@ public class OnSaveActionTask extends Ta this.isDefaultDisplayTextColour = isDefaultDisplayTextColour; this.displayTextFontColour = displayTextFontColour; this.isClearIcon = isClearIcon; - this.isHideIcon = isHideIcon; + this.isHideDefaultIcon = isHideDefaultIcon; + this.isHideToggleOffIcon = isHideToggleOffIcon; + this.isHideToggleOnIcon = isHideToggleOnIcon; this.displayTextAlignment = displayTextAlignment; this.isTransparentBackground = isTransparentBackground; this.combineActionPropertiesPane = combineActionPropertiesPane; @@ -77,7 +79,9 @@ public class OnSaveActionTask extends Ta private ArrayList actionClientProperties; private String displayTextFontColour; private boolean isClearIcon; - private boolean isHideIcon; + private boolean isHideDefaultIcon; + private boolean isHideToggleOffIcon; + private boolean isHideToggleOnIcon; private DisplayTextAlignment displayTextAlignment; private boolean isTransparentBackground; private String backgroundColour; @@ -120,24 +124,34 @@ public class OnSaveActionTask extends Ta if(isClearIcon) { - action.setIcon(null); - action.setHasIcon(false); - action.setShowIcon(false); + action.setIcons(null); + action.setCurrentIconState(""); } - if(action.isHasIcon()) - action.setShowIcon(isHideIcon); + if(action.getActionType() == ActionType.NORMAL) + { + if(isHideDefaultIcon) + { + action.setCurrentIconState("default"); + } + else + { + action.setCurrentIconState(""); + } + } + else if (action.getActionType() == ActionType.TOGGLE) + { + action.setCurrentIconState(isHideToggleOffIcon+"__"+isHideToggleOnIcon); + } action.setDisplayTextAlignment(displayTextAlignment); - logger.info("BBBGGG : "+backgroundColour); if(isTransparentBackground) action.setBgColourHex(""); else { - //String bgColour = "#" + actionBackgroundColourPicker.getValue().toString().substring(2); action.setBgColourHex(backgroundColour); } } @@ -177,27 +191,11 @@ public class OnSaveActionTask extends Ta { logger.info("Saving action ... "+action.isHasIcon()+"+"+sendIcon); - /*if(action.isHasIcon()) - { - if(clientProfile.getActionByID(action.getID()).getIconAsByteArray() == null) - { - sendIcon = true; - } - else - { - if(!Arrays.equals(action.getIconAsByteArray(), clientProfile.getActionByID(action.getID()).getIconAsByteArray())) - { - logger.info("Sending ..."); - sendIcon = true; - } - } - }*/ - connection.saveActionDetails(clientProfile.getID(), action); if(sendIcon) - { - connection.sendIcon(clientProfile.getID(), action.getID(), action.getIconAsByteArray()); + { + sendAllIcons(clientProfile, action); } if(!isCombineChild) @@ -228,6 +226,14 @@ public class OnSaveActionTask extends Ta } + private void sendAllIcons(ClientProfile clientProfile, Action action) throws SevereException + { + for(String state : action.getIcons().keySet()) + { + connection.sendIcon(clientProfile.getID(), action.getID(), state, action.getIcon(state)); + } + } + @Override protected Void call() throws Exception {