[英]Switch add-on with push, vaadin
I have a simple example of the add-on switch with vaadin, what I want is to keep the state of the switch even when I update the UI, that is, I support multiple tabs, but I can not do it, this push example is very similar to What I want to do but with a textField . 我有一个使用vaadin的附加开关的简单示例,我想要的是即使在更新UI时也保持开关的状态,也就是说,我支持多个选项卡,但我不能这样做,此push示例与我要执行的操作非常相似,但具有textField 。
https://github.com/vaadin-marcus/push-example/blob/master/src/main/java/com/vaadin/training/ScrumBoardLayout.java https://github.com/vaadin-marcus/push-example/blob/master/src/main/java/com/vaadin/training/ScrumBoardLayout.java
https://github.com/rucko24/MVP/blob/testingSwitchPushTemu/src/main/java/com/Core/vaadin/pushServer/ejemploPushMarkus/ScrumBoard.java https://github.com/rucko24/MVP/blob/testingSwitchPushTemu/src/main/java/com/Core/vaadin/pushServer/ejemploPushMarkus/ScrumBoard.java
To my example I add a bulb so that when another accesses the application can see the current state of the bulb. 在我的示例中,我添加了一个灯泡,以便当其他人访问该应用程序时可以看到灯泡的当前状态。 My example in github is this with only 3 classes
我在github中的示例是只有3个类的示例
https://github.com/rucko24/MVP/tree/testingSwitchPushTemu/src/main/java/com/Core/vaadin/pushServer/ejemploPushMarkus https://github.com/rucko24/MVP/tree/testingSwitchPushTemu/src/main/java/com/Core/vaadin/pushServer/ejemploPushMarkus
This is the swithc listener that changes my bulb, but when I get the boolean value (true, or false), I still do not understand the right way to push the other switch 这是swithc监听器,它改变了我的灯泡,但是当我获得布尔值(true或false)时,我仍然不明白按另一个开关的正确方法
switchTemu.addValueChangeListener(new Property.ValueChangeListener() {
private static final long serialVersionUID = 1L;
@Override
public void valueChange(Property.ValueChangeEvent event) {
boolean estado = (boolean) event.getProperty().getValue();
ScrumBoard.addSwitch(estado);
switchTemu.removeValueChangeListener(this);
if(estado == Boolean.TRUE) {
bombilla.setIcon(bombillaON);
}else {
bombilla.setIcon(bombillaOFF);
}
switchTemu.addValueChangeListener(this);
}
});
Update 更新资料
In my example github achievement, change the state of all switches to all UI, but I still do not know how to get the state of the switches 在我的示例github成就中,将所有开关的状态更改为所有UI,但是我仍然不知道如何获取开关的状态
I made a couple of changes to your sources (still basic, but it gets you started): 我对您的来源做了几处更改(仍然很基本,但可以帮助您入门):
switch value change listeners
now just trigger a state changed event
switch value change listeners
现在仅触发state changed event
state changed listeners
now update the UI elements when triggered state changed listeners
现在在触发时更新UI元素 state changed listeners
is informed (triggered) about the current state state changed listeners
器当前状态 The main idea is to have just a single shared state and any change is communicated to all the listeners (including the one where the change originated). 主要思想是只有一个共享状态,并且任何更改都将传达给所有侦听器(包括更改发生的源)。
Below you can find the code: (PS I did not recompile my widgetset so the nice switch icon falls back to the default check box style) 在下面,您可以找到代码:(PS我没有重新编译我的窗口小部件集,因此漂亮的开关图标会退回到默认的复选框样式)
1) SwitchState
- represents the state of the switch shared between all the app instances 1)
SwitchState
表示所有应用程序实例之间共享的开关状态
public enum SwitchState {
ON(true, new ThemeResource("img/on.png")), OFF(false, new ThemeResource("img/off.png"));
private final boolean value;
private final ThemeResource icon;
SwitchState(boolean value, ThemeResource icon) {
this.value = value;
this.icon = icon;
}
public boolean getValue() {
return value;
}
public ThemeResource getIcon() {
return icon;
}
public static SwitchState from(boolean value) {
return value ? ON : OFF;
}
}
2) ScrumBoard
common state and listeners manager 2)
ScrumBoard
公共状态和监听器管理器
public class ScrumBoard {
// list of listeners
private static List<SwitchChangeListener> LISTENERS = new ArrayList<>();
// initial state
private static SwitchState STATE = SwitchState.OFF;
// state change listener contract
public interface SwitchChangeListener {
void handleStateChange(SwitchState state);
}
// handle a a state change request
public static synchronized void updateState(boolean value) {
STATE = SwitchState.from(value);
fireChangeEvent(STATE);
}
// register a new state listener
public static synchronized void addSwitchChangeListener(SwitchChangeListener listener) {
System.out.println("Added listener for " + listener);
LISTENERS.add(listener);
// when a new listener is registered, also inform it of the current state
listener.handleStateChange(STATE);
}
// remove a state listener
public static synchronized void removeSwitchListener(SwitchChangeListener listener) {
LISTENERS.remove(listener);
}
// fire a change event to all registered listeners
private static void fireChangeEvent(SwitchState state) {
for (SwitchChangeListener listener : LISTENERS) {
listener.handleStateChange(state);
}
}
}
3) ScrumBoardLayout
- UI layout and components 3)
ScrumBoardLayout
-UI布局和组件
public class ScrumBoardLayout extends VerticalLayout implements ScrumBoard.SwitchChangeListener {
private Label icon = new Label();
private Switch mySwitch = new Switch();
public ScrumBoardLayout() {
setMargin(true);
setSpacing(true);
addHeader();
// listen for state changes
ScrumBoard.addSwitchChangeListener(this);
}
private void addHeader() {
mySwitch.setImmediate(true);
icon.setSizeUndefined();
// notify of state change
mySwitch.addValueChangeListener((Property.ValueChangeListener) event -> ScrumBoard.updateState((Boolean) event.getProperty().getValue()));
VerticalLayout layout = new VerticalLayout();
layout.setHeight("78%");
layout.addComponents(icon, mySwitch);
layout.setComponentAlignment(icon, Alignment.BOTTOM_CENTER);
layout.setComponentAlignment(mySwitch, Alignment.BOTTOM_CENTER);
layout.setExpandRatio(mySwitch, 1);
addComponents(layout);
}
@Override
public void handleStateChange(SwitchState state) {
// update UI on state change
UI.getCurrent().access(() -> {
mySwitch.setValue(state.getValue());
icon.setIcon(state.getIcon());
Notification.show(state.name(), Type.ASSISTIVE_NOTIFICATION);
});
}
@Override
public void detach() {
super.detach();
ScrumBoard.removeSwitchListener(this);
}
}
I could see that with the ThemeResource () class, changing the bulb to its ON / OFF effect is strange, but I solve it as follows 我可以看到,通过ThemeResource()类,将灯泡更改为其ON / OFF效果很奇怪,但是我可以按以下方式解决
.bombillo-on {
@include valo-animate-in-fade($duration: 1s);
width: 181px;
height: 216px;
background: url(img/on.png) no-repeat;
}
.bombillo-off {
@include valo-animate-in-fade($duration: 1s);
width: 181px;
height: 216px;
background: url(img/off.png) no-repeat;
}
public enum Sstate {
ON(true,"bombillo-on"),
OFF(false,"bombillo-off");
private boolean value;
private String style;
Sstate(boolean value, String style) {
this.value = value;
this.style = style;
}
public boolean getValue() { return value;}
public String getStyle() { return style;}
public static Sstate from(boolean value) { return value ? ON:OFF;}
}
And the handleChangeEvent It stays like this 和handleChangeEvent它保持像这样
@Override
public void handleChangeEvent(Sstate state) {
ui.access(() -> {
bombilla.setStyleName(state.getStyle());
s.setValue(state.getValue());
System.out.println(state+" values "+s);
});
}
UPDATE: 更新:
I notice an issue, that when I add a new view, or change using the buttonMenuToggle, it loses the synchronization, and update the bulb quite strange, clear with the themeResource does not happen that. 我注意到一个问题,当我添加一个新视图或使用buttonMenuToggle进行更改时,它将失去同步,并更新灯泡,这很奇怪,使用themeResource清除不会发生这种情况。
Solution: 解:
to avoid UiDetachedException when using the Navigator try this, It works very well 避免在使用Navigator时避免UiDetachedException尝试此操作,它工作得很好
@Override
public void handleChangeEvent(Sstate state) {
if(!ui.isAttached()) {
BroadcastesSwitch.removeListener(this);
return;
}
ui.access(() -> {
bombilla.setStyleName(state.getStyle());
s.setValue(state.getValue());
System.out.println(state+" values "+s);
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.