Skip to content

Commit

Permalink
Group update when member change
Browse files Browse the repository at this point in the history
  • Loading branch information
ben12 committed Oct 21, 2017
1 parent f4f7f2e commit aa634cc
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 90 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# habfx-ui
# HABFX-UI
openHAB2 javaFX User Interface

HABFX-UI is an OpenHAB2 client.<br />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,32 @@

package com.ben12.openhab.controller.impl;

import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.event.ChangeListener;
import javax.ws.rs.client.InvocationCallback;

import com.ben12.openhab.controller.MainViewController;
import com.ben12.openhab.model.Item;
import com.ben12.openhab.model.Page;
import com.ben12.openhab.model.Widget;
import com.ben12.openhab.model.util.BeanCopy;
import com.ben12.openhab.rest.OpenHabRestClient;

import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.scene.layout.Region;

public class GroupController extends WidgetController
{
private PageController pageController;
private PageController pageController;

private ObservableList<Item> members;

public GroupController(final Page parent)
{
Expand All @@ -39,6 +56,65 @@ public void init(final Widget widget, final MainViewController pMainViewControll

pageController = new PageController();
pageController.init(getWidget().getLinkedPage(), getMainViewController());

final ChangeListener memberChangeHandler = e -> reload();

final OpenHabRestClient restClient = getMainViewController().getRestClient();
final Map<String, ItemChangeHandler> itemChangeHandlers = new HashMap<>();

members = FXCollections.observableArrayList();
members.addListener((ListChangeListener<Item>) c -> {
while (c.next())
{
if (c.wasRemoved())
{
for (final Item item : c.getRemoved())
{
final ItemChangeHandler handler = itemChangeHandlers.remove(item.getName());
if (handler != null)
{
handler.release();
}
}
}
if (c.wasAdded())
{
for (final Item item : c.getAddedSubList())
{
itemChangeHandlers.computeIfAbsent(item.getName(),
itemName -> new ItemChangeHandler(restClient, itemName, memberChangeHandler));
}
}
}
});

loadMembers();
}

public void loadMembers()
{
final OpenHabRestClient restClient = getMainViewController().getRestClient();
restClient.item(getWidget().getItem().getName(), new InvocationCallback<Item>()
{
@Override
public void failed(final Throwable throwable)
{
Logger.getLogger(getClass().getName()).log(Level.SEVERE, "Cannot load group members", throwable);
}

@Override
public void completed(final Item response)
{
Platform.runLater(() -> BeanCopy.copy(response.getMembers(), members, Item::getName));
}
});
}

@Override
public void reload()
{
super.reload();
loadMembers();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import com.ben12.openhab.model.Widget;
import com.ben12.openhab.ui.FullWidthTilePane;

import javafx.beans.binding.Bindings;
import javafx.beans.binding.StringBinding;
import javafx.beans.value.ObservableValue;
import javafx.collections.ListChangeListener.Change;
import javafx.geometry.Bounds;
Expand All @@ -42,6 +44,8 @@ public class PageController implements ContentController<Page>

private Label title;

private StringBinding titleProperty;

private Page page;

private Pane pane;
Expand All @@ -54,7 +58,7 @@ public void init(final Page data, final MainViewController pMainViewController)

title = new Label();
title.getStyleClass().add("title");
title.textProperty().bind(page.titleProperty());
title.textProperty().bind(titleProperty());

title.heightProperty().addListener((e, o, n) -> {
final Text textUtil = new Text(title.getText());
Expand Down Expand Up @@ -123,6 +127,19 @@ public void changed(final ObservableValue<? extends Bounds> observable, final Bo
.collect(Collectors.toList()));
}

protected StringBinding titleProperty()
{
if (titleProperty == null)
{
titleProperty = Bindings.createStringBinding(() -> {
String label = page.getTitle();
label = label.replaceFirst("\\[(.*?)\\]$", "$1");
return label;
}, page.titleProperty());
}
return titleProperty;
}

@Override
public void reload()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,14 +290,14 @@ protected StringBinding labelProperty()
{
labelProperty = Bindings.createStringBinding(() -> {
String label = widget.getLabel();
label = label.replaceFirst("\\s*\\[(.*?)\\]$", "");
label = label.replaceFirst("\\s*\\[.*?\\]$", "");
return label;
}, widget.labelProperty());
}
return labelProperty;
}

public ObjectExpression<String> labelStyleProperty()
protected ObjectExpression<String> labelStyleProperty()
{
if (labelStyleProperty == null)
{
Expand All @@ -314,7 +314,7 @@ public ObjectExpression<String> labelStyleProperty()
return labelStyleProperty;
}

public StringBinding valueProperty()
protected StringBinding valueProperty()
{
if (valueProperty == null)
{
Expand All @@ -327,7 +327,7 @@ public StringBinding valueProperty()
return valueProperty;
}

public ObjectExpression<String> valueStyleProperty()
protected ObjectExpression<String> valueStyleProperty()
{
if (valueStyleProperty == null)
{
Expand All @@ -344,7 +344,7 @@ public ObjectExpression<String> valueStyleProperty()
return valueStyleProperty;
}

private static class ItemChangeHandler extends WeakReference<ChangeListener> implements ChangeListener
protected static class ItemChangeHandler extends WeakReference<ChangeListener> implements ChangeListener
{
private final String itemName;

Expand Down
63 changes: 0 additions & 63 deletions src/main/java/com/ben12/openhab/model/GroupItem.java

This file was deleted.

27 changes: 27 additions & 0 deletions src/main/java/com/ben12/openhab/model/Item.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,21 @@

package com.ben12.openhab.model;

import java.util.List;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

import com.ben12.openhab.model.util.BeanCopy;

import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;

/**
* This is a java bean that is used with JAXB to serialize items
Expand All @@ -48,6 +54,8 @@ public class Item implements Linked

private final StringProperty link = new SimpleStringProperty();

public final ObservableList<Item> members = FXCollections.observableArrayList();

public final StringProperty typeProperty()
{
return type;
Expand Down Expand Up @@ -129,4 +137,23 @@ public final void setLink(final String link)
{
linkProperty().set(link);
}

public ObservableList<Item> membersProperty()
{
return members;
}

@XmlElement
public List<Item> getMembers()
{
return members;
}

public void setMembers(final List<Item> pMembers)
{
if (members != pMembers)
{
BeanCopy.copy(pMembers, members, Item::getName);
}
}
}
27 changes: 9 additions & 18 deletions src/main/java/com/ben12/openhab/model/util/BeanCopy.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.CountDownLatch;
import java.util.function.Function;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand All @@ -30,7 +29,7 @@
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.WrapDynaBean;

import javafx.application.Platform;
import jfxtras.util.PlatformUtil;

public class BeanCopy extends WrapDynaBean
{
Expand Down Expand Up @@ -80,24 +79,16 @@ public static <T> void copy(final List<T> source, final List<T> destination, fin

public static void copy(final Object source, final Object destination)
{
try
{
if (!Platform.isFxApplicationThread())
PlatformUtil.runAndWait(() -> {
try
{
final CountDownLatch done = new CountDownLatch(1);
Platform.runLater(() -> {
copy(source, destination);
done.countDown();
});
done.await();
return;
BeanUtils.copyProperties(wrap(destination), source);
}
BeanUtils.copyProperties(wrap(destination), source);
}
catch (final Exception e)
{
LOGGER.log(Level.SEVERE, "Cannot copy bean", e);
}
catch (final Exception e)
{
LOGGER.log(Level.SEVERE, "Cannot copy bean", e);
}
});
}

private static BeanCopy wrap(final Object value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ public void completed(final Response response)
public void addItemStateChangeListener(final String itemName, final ChangeListener l)
{
final String key = String.format(EVENT_KEY, itemName);
listeners.computeIfAbsent(key, k -> new ArrayList<>()).add(l);
listeners.computeIfAbsent(key, k -> new ArrayList<>(1)).add(l);
}

public void removeItemStateChangeListener(final String itemName, final ChangeListener l)
Expand Down

0 comments on commit aa634cc

Please sign in to comment.