Skip to content

Commit aa634cc

Browse files
committed
Group update when member change
1 parent f4f7f2e commit aa634cc

File tree

8 files changed

+138
-90
lines changed

8 files changed

+138
-90
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# habfx-ui
1+
# HABFX-UI
22
openHAB2 javaFX User Interface
33

44
HABFX-UI is an OpenHAB2 client.<br />

src/main/java/com/ben12/openhab/controller/impl/GroupController.java

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,32 @@
1717

1818
package com.ben12.openhab.controller.impl;
1919

20+
import java.util.HashMap;
21+
import java.util.Map;
22+
import java.util.logging.Level;
23+
import java.util.logging.Logger;
24+
25+
import javax.swing.event.ChangeListener;
26+
import javax.ws.rs.client.InvocationCallback;
27+
2028
import com.ben12.openhab.controller.MainViewController;
29+
import com.ben12.openhab.model.Item;
2130
import com.ben12.openhab.model.Page;
2231
import com.ben12.openhab.model.Widget;
32+
import com.ben12.openhab.model.util.BeanCopy;
33+
import com.ben12.openhab.rest.OpenHabRestClient;
2334

35+
import javafx.application.Platform;
36+
import javafx.collections.FXCollections;
37+
import javafx.collections.ListChangeListener;
38+
import javafx.collections.ObservableList;
2439
import javafx.scene.layout.Region;
2540

2641
public class GroupController extends WidgetController
2742
{
28-
private PageController pageController;
43+
private PageController pageController;
44+
45+
private ObservableList<Item> members;
2946

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

4057
pageController = new PageController();
4158
pageController.init(getWidget().getLinkedPage(), getMainViewController());
59+
60+
final ChangeListener memberChangeHandler = e -> reload();
61+
62+
final OpenHabRestClient restClient = getMainViewController().getRestClient();
63+
final Map<String, ItemChangeHandler> itemChangeHandlers = new HashMap<>();
64+
65+
members = FXCollections.observableArrayList();
66+
members.addListener((ListChangeListener<Item>) c -> {
67+
while (c.next())
68+
{
69+
if (c.wasRemoved())
70+
{
71+
for (final Item item : c.getRemoved())
72+
{
73+
final ItemChangeHandler handler = itemChangeHandlers.remove(item.getName());
74+
if (handler != null)
75+
{
76+
handler.release();
77+
}
78+
}
79+
}
80+
if (c.wasAdded())
81+
{
82+
for (final Item item : c.getAddedSubList())
83+
{
84+
itemChangeHandlers.computeIfAbsent(item.getName(),
85+
itemName -> new ItemChangeHandler(restClient, itemName, memberChangeHandler));
86+
}
87+
}
88+
}
89+
});
90+
91+
loadMembers();
92+
}
93+
94+
public void loadMembers()
95+
{
96+
final OpenHabRestClient restClient = getMainViewController().getRestClient();
97+
restClient.item(getWidget().getItem().getName(), new InvocationCallback<Item>()
98+
{
99+
@Override
100+
public void failed(final Throwable throwable)
101+
{
102+
Logger.getLogger(getClass().getName()).log(Level.SEVERE, "Cannot load group members", throwable);
103+
}
104+
105+
@Override
106+
public void completed(final Item response)
107+
{
108+
Platform.runLater(() -> BeanCopy.copy(response.getMembers(), members, Item::getName));
109+
}
110+
});
111+
}
112+
113+
@Override
114+
public void reload()
115+
{
116+
super.reload();
117+
loadMembers();
42118
}
43119

44120
@Override

src/main/java/com/ben12/openhab/controller/impl/PageController.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
import com.ben12.openhab.model.Widget;
2828
import com.ben12.openhab.ui.FullWidthTilePane;
2929

30+
import javafx.beans.binding.Bindings;
31+
import javafx.beans.binding.StringBinding;
3032
import javafx.beans.value.ObservableValue;
3133
import javafx.collections.ListChangeListener.Change;
3234
import javafx.geometry.Bounds;
@@ -42,6 +44,8 @@ public class PageController implements ContentController<Page>
4244

4345
private Label title;
4446

47+
private StringBinding titleProperty;
48+
4549
private Page page;
4650

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

5559
title = new Label();
5660
title.getStyleClass().add("title");
57-
title.textProperty().bind(page.titleProperty());
61+
title.textProperty().bind(titleProperty());
5862

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

130+
protected StringBinding titleProperty()
131+
{
132+
if (titleProperty == null)
133+
{
134+
titleProperty = Bindings.createStringBinding(() -> {
135+
String label = page.getTitle();
136+
label = label.replaceFirst("\\[(.*?)\\]$", "$1");
137+
return label;
138+
}, page.titleProperty());
139+
}
140+
return titleProperty;
141+
}
142+
126143
@Override
127144
public void reload()
128145
{

src/main/java/com/ben12/openhab/controller/impl/WidgetController.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -290,14 +290,14 @@ protected StringBinding labelProperty()
290290
{
291291
labelProperty = Bindings.createStringBinding(() -> {
292292
String label = widget.getLabel();
293-
label = label.replaceFirst("\\s*\\[(.*?)\\]$", "");
293+
label = label.replaceFirst("\\s*\\[.*?\\]$", "");
294294
return label;
295295
}, widget.labelProperty());
296296
}
297297
return labelProperty;
298298
}
299299

300-
public ObjectExpression<String> labelStyleProperty()
300+
protected ObjectExpression<String> labelStyleProperty()
301301
{
302302
if (labelStyleProperty == null)
303303
{
@@ -314,7 +314,7 @@ public ObjectExpression<String> labelStyleProperty()
314314
return labelStyleProperty;
315315
}
316316

317-
public StringBinding valueProperty()
317+
protected StringBinding valueProperty()
318318
{
319319
if (valueProperty == null)
320320
{
@@ -327,7 +327,7 @@ public StringBinding valueProperty()
327327
return valueProperty;
328328
}
329329

330-
public ObjectExpression<String> valueStyleProperty()
330+
protected ObjectExpression<String> valueStyleProperty()
331331
{
332332
if (valueStyleProperty == null)
333333
{
@@ -344,7 +344,7 @@ public ObjectExpression<String> valueStyleProperty()
344344
return valueStyleProperty;
345345
}
346346

347-
private static class ItemChangeHandler extends WeakReference<ChangeListener> implements ChangeListener
347+
protected static class ItemChangeHandler extends WeakReference<ChangeListener> implements ChangeListener
348348
{
349349
private final String itemName;
350350

src/main/java/com/ben12/openhab/model/GroupItem.java

Lines changed: 0 additions & 63 deletions
This file was deleted.

src/main/java/com/ben12/openhab/model/Item.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,21 @@
1717

1818
package com.ben12.openhab.model;
1919

20+
import java.util.List;
21+
2022
import javax.xml.bind.annotation.XmlAccessType;
2123
import javax.xml.bind.annotation.XmlAccessorType;
2224
import javax.xml.bind.annotation.XmlElement;
2325
import javax.xml.bind.annotation.XmlRootElement;
2426

27+
import com.ben12.openhab.model.util.BeanCopy;
28+
2529
import javafx.beans.property.ObjectProperty;
2630
import javafx.beans.property.SimpleObjectProperty;
2731
import javafx.beans.property.SimpleStringProperty;
2832
import javafx.beans.property.StringProperty;
33+
import javafx.collections.FXCollections;
34+
import javafx.collections.ObservableList;
2935

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

4955
private final StringProperty link = new SimpleStringProperty();
5056

57+
public final ObservableList<Item> members = FXCollections.observableArrayList();
58+
5159
public final StringProperty typeProperty()
5260
{
5361
return type;
@@ -129,4 +137,23 @@ public final void setLink(final String link)
129137
{
130138
linkProperty().set(link);
131139
}
140+
141+
public ObservableList<Item> membersProperty()
142+
{
143+
return members;
144+
}
145+
146+
@XmlElement
147+
public List<Item> getMembers()
148+
{
149+
return members;
150+
}
151+
152+
public void setMembers(final List<Item> pMembers)
153+
{
154+
if (members != pMembers)
155+
{
156+
BeanCopy.copy(pMembers, members, Item::getName);
157+
}
158+
}
132159
}

src/main/java/com/ben12/openhab/model/util/BeanCopy.java

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import java.lang.reflect.InvocationTargetException;
2121
import java.util.List;
2222
import java.util.Objects;
23-
import java.util.concurrent.CountDownLatch;
2423
import java.util.function.Function;
2524
import java.util.logging.Level;
2625
import java.util.logging.Logger;
@@ -30,7 +29,7 @@
3029
import org.apache.commons.beanutils.BeanUtils;
3130
import org.apache.commons.beanutils.WrapDynaBean;
3231

33-
import javafx.application.Platform;
32+
import jfxtras.util.PlatformUtil;
3433

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

8180
public static void copy(final Object source, final Object destination)
8281
{
83-
try
84-
{
85-
if (!Platform.isFxApplicationThread())
82+
PlatformUtil.runAndWait(() -> {
83+
try
8684
{
87-
final CountDownLatch done = new CountDownLatch(1);
88-
Platform.runLater(() -> {
89-
copy(source, destination);
90-
done.countDown();
91-
});
92-
done.await();
93-
return;
85+
BeanUtils.copyProperties(wrap(destination), source);
9486
}
95-
BeanUtils.copyProperties(wrap(destination), source);
96-
}
97-
catch (final Exception e)
98-
{
99-
LOGGER.log(Level.SEVERE, "Cannot copy bean", e);
100-
}
87+
catch (final Exception e)
88+
{
89+
LOGGER.log(Level.SEVERE, "Cannot copy bean", e);
90+
}
91+
});
10192
}
10293

10394
private static BeanCopy wrap(final Object value)

src/main/java/com/ben12/openhab/rest/OpenHabRestClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ public void completed(final Response response)
288288
public void addItemStateChangeListener(final String itemName, final ChangeListener l)
289289
{
290290
final String key = String.format(EVENT_KEY, itemName);
291-
listeners.computeIfAbsent(key, k -> new ArrayList<>()).add(l);
291+
listeners.computeIfAbsent(key, k -> new ArrayList<>(1)).add(l);
292292
}
293293

294294
public void removeItemStateChangeListener(final String itemName, final ChangeListener l)

0 commit comments

Comments
 (0)