Skip to content

Commit

Permalink
Switch Button & Rollershutter
Browse files Browse the repository at this point in the history
  • Loading branch information
ben12 committed Oct 17, 2017
1 parent 06c4b88 commit f4f7f2e
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 8 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ Supported sitemap widgets :
* Frame
* Group
* Text
* Switch
* Switch (On/Off, Up/Stop/Down and mappings supported)
* Setpoint
* Selection
* Slider
* Colorpicker
* Webview
* Chart (for 'Number' item)

## Build HABFX-UI :
## Build HABFX-UI with Maven :

Execute the command line :<br />
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

package com.ben12.openhab.controller.impl;

import java.util.Objects;

import org.controlsfx.control.ToggleSwitch;

import com.ben12.openhab.controller.MainViewController;
Expand All @@ -35,6 +37,8 @@
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.control.SelectionMode;
Expand All @@ -48,6 +52,8 @@ public class SwitchController extends WidgetController

private boolean isOnOff;

private boolean isButton;

public SwitchController(final Page parent)
{
super(parent);
Expand All @@ -56,12 +62,17 @@ public SwitchController(final Page parent)
@Override
public void init(final Widget pWidget, final MainViewController pMainViewController)
{
// ON/OFF Switch
isOnOff = pWidget.getMappings().isEmpty();
final boolean isRollershutter = "Rollershutter".equals(pWidget.getItem().getType());
isOnOff = !isRollershutter && pWidget.getMappings().isEmpty();
isButton = !isRollershutter && pWidget.getMappings().size() == 1;

super.init(pWidget, pMainViewController);

if (!isOnOff)
if (isRollershutter)
{
initRollershutter();
}
else if (!isOnOff && !isButton)
{
initSwitchWithMapping(pWidget.mappingsProperty());
}
Expand All @@ -74,15 +85,31 @@ protected Node createValueNode()
{
final ToggleSwitch stateButton = new ToggleSwitch();
stateButton.getStyleClass().add("value-label");
stateButton.textProperty().bind(itemStateProperty());
stateButton.selectedProperty().bind(Bindings
.createBooleanBinding(() -> "ON".equalsIgnoreCase(itemStateProperty().get()), itemStateProperty()));
stateButton.textProperty()
.bind(Bindings.createStringBinding(
() -> getWidget().getMappings()
.stream()
.filter(m -> Objects.equals(itemStateProperty().get(), m.getCommand()))
.map(Mapping::getLabel)
.findFirst()
.orElse(itemStateProperty().get()),
itemStateProperty()));
stateButton.selectedProperty().bind(itemStateProperty().isEqualToIgnoreCase("ON"));
stateButton.setMinSize(0, 0);
stateButton.setMaxSize(Region.USE_PREF_SIZE, Region.USE_PREF_SIZE);
stateButton.setMouseTransparent(true);

return stateButton;
}
else if (isButton)
{
final Label label = new Label();
label.textProperty().bind(Bindings.concat(getWidget().getMappings().get(0).getLabel(),
Bindings.when(valueProperty().isEmpty()).then("").otherwise(" "), valueProperty()));
label.styleProperty().bind(valueStyleProperty());
label.getStyleClass().add("value-label");
return label;
}
else
{
return super.createValueNode();
Expand All @@ -99,12 +126,68 @@ protected void display()
getWidget().getItem(),
"ON".equalsIgnoreCase(getWidget().getItem().getState()) ? "OFF" : "ON");
}
else if (isButton)
{
getMainViewController() //
.getRestClient().submit( //
getWidget().getItem(), //
getWidget().getMappings().get(0).getCommand());
}
else
{
super.display();
}
}

private void initRollershutter()
{
final Node iconImage = createIconNode();

final Button upCommand = new Button(getWidget().getMappings()
.stream()
.filter(m -> "UP".equals(m.getCommand()))
.map(Mapping::getLabel)
.findFirst()
.orElse("UP"));
upCommand.setOnAction(e -> getMainViewController().getRestClient() //
.submit(getWidget().getItem(), "UP"));
upCommand.setMinSize(50, 40);
upCommand.setMaxSize(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY);

final Button stopCommand = new Button(getWidget().getMappings()
.stream()
.filter(m -> "STOP".equals(m.getCommand()))
.map(Mapping::getLabel)
.findFirst()
.orElse("STOP"));
stopCommand.setOnAction(e -> getMainViewController().getRestClient() //
.submit(getWidget().getItem(), "STOP"));
stopCommand.setMinSize(50, 40);
stopCommand.setMaxSize(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY);

final Button downCommand = new Button(getWidget().getMappings()
.stream()
.filter(m -> "DOWN".equals(m.getCommand()))
.map(Mapping::getLabel)
.findFirst()
.orElse("DOWN"));
downCommand.setOnAction(e -> getMainViewController().getRestClient() //
.submit(getWidget().getItem(), "DOWN"));
downCommand.setMinSize(50, 40);
downCommand.setMaxSize(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY);

final VBox commands = new VBox(2, upCommand, stopCommand, downCommand);
commands.setFillWidth(true);

content = new VBox(iconImage, commands);
content.setFillWidth(false);
content.setAlignment(Pos.CENTER);
content.setMinSize(50, 50);
content.setMaxSize(Double.MAX_VALUE, Region.USE_PREF_SIZE);
content.prefWidth(0);
content.setPadding(new Insets(0, 5, 0, 5));
}

/**
* @param mappings
*/
Expand Down

0 comments on commit f4f7f2e

Please sign in to comment.