Skip to content

Commit

Permalink
ZonePanel is now processing key presses in a reactive way (#271)
Browse files Browse the repository at this point in the history
  • Loading branch information
climategadgets committed Sep 22, 2023
1 parent 8fbf021 commit 11a4bb3
Show file tree
Hide file tree
Showing 6 changed files with 183 additions and 163 deletions.
18 changes: 18 additions & 0 deletions dz3r-swing/src/main/java/net/sf/dz3r/view/swing/EntityPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

/**
Expand All @@ -22,6 +23,8 @@ public abstract class EntityPanel<T, P> extends SwingSink<T, P> implements KeyLi

protected static final String UNDEFINED = "--";

protected final KeyFlux keyFlux = new KeyFlux();

@SuppressWarnings("squid:S1199")
protected final void createLayout(String name, JPanel chart) {

Expand Down Expand Up @@ -111,4 +114,19 @@ private JPanel createControls() {
protected boolean isBackgroundTransparent() {
return false;
}

@Override
public final void keyTyped(KeyEvent e) {
keyFlux.keyTyped(e);
}

@Override
public final void keyPressed(KeyEvent e) {
keyFlux.keyPressed(e);
}

@Override
public final void keyReleased(KeyEvent e) {
keyFlux.keyReleased(e);
}
}
39 changes: 39 additions & 0 deletions dz3r-swing/src/main/java/net/sf/dz3r/view/swing/KeyFlux.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package net.sf.dz3r.view.swing;

import reactor.core.publisher.Flux;
import reactor.core.publisher.Sinks;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.time.Duration;

/**
* Reactive adapter for the {@link KeyListener} interface.
*
* @author Copyright &copy; <a href="mailto:vt@homeclimatecontrol.com">Vadim Tkachenko</a> 2001-2023
*/
public class KeyFlux implements KeyListener {

private final Sinks.Many<KeyEvent> sink;
public final Flux<KeyEvent> flux;

public KeyFlux() {
sink = Sinks.many().replay().limit(Duration.ofMinutes(1));
flux = sink.asFlux();
}

@Override
public void keyTyped(KeyEvent e) {
sink.tryEmitNext(e);
}

@Override
public void keyPressed(KeyEvent e) {
sink.tryEmitNext(e);
}

@Override
public void keyReleased(KeyEvent e) {
sink.tryEmitNext(e);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.KeyEvent;
import java.util.Map;
import java.util.Optional;
import java.util.TreeMap;
Expand Down Expand Up @@ -89,21 +88,6 @@ private void initGraphics() {
.subscribe();
}

@Override
public void keyTyped(KeyEvent e) {
// No special handling
}

@Override
public void keyPressed(KeyEvent e) {
// No special handling yet
}

@Override
public void keyReleased(KeyEvent e) {
// No special handling
}

@Override
protected void consumeSignalValue(SystemStatus status) {
// No special handling
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import javax.swing.SwingConstants;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.KeyEvent;
import java.text.DecimalFormat;
import java.time.Clock;

Expand Down Expand Up @@ -70,21 +69,6 @@ public void setFontSize(ScreenDescriptor screenDescriptor) {
currentSignalLabel.setFont(screenDescriptor.fontSetpoint);
}

@Override
public void keyTyped(KeyEvent e) {
// No special handling
}

@Override
public void keyPressed(KeyEvent e) {
// No special handling yet
}

@Override
public void keyReleased(KeyEvent e) {
// No special handling
}

@Override
protected void consumeSignalValue(Double value) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.KeyEvent;
import java.text.DecimalFormat;
import java.time.Clock;
import java.time.Duration;
Expand Down Expand Up @@ -134,21 +133,6 @@ public void setFontSize(ScreenDescriptor screenDescriptor) {
currentLeftLabel.setFont(screenDescriptor.fontSetpoint);
}

@Override
public void keyTyped(KeyEvent e) {
// No special handling
}

@Override
public void keyPressed(KeyEvent e) {
// No special handling yet
}

@Override
public void keyReleased(KeyEvent e) {
// No special handling
}

@Override
protected void consumeSignalValue(HvacDeviceStatus status) {
// No special handling
Expand Down
Loading

0 comments on commit 11a4bb3

Please sign in to comment.