I using EventBus in my app, but sometimes the event are duplicated,
I have one view with one
@Autowired
private EventBus.UIEventBus eventBus;
in this view I subs:
eventBus.subscribe(this);
Then when I click in one button I open another component, I use this code:
public static void abrirJanela( String titulo, Integer altura, Integer largura, Component tela, Window.CloseListener closeListener, boolean icCloseable){
Window window = new Window(titulo);
window.setCaption(titulo);
window.setWidth(largura, Sizeable.Unit.PERCENTAGE);
window.setHeight(altura, Sizeable.Unit.PERCENTAGE);
window.setModal(true);
window.setClosable(icCloseable);
if(closeListener != null){
window.addCloseListener(closeListener);
}
Class<?> clazz = tela.getClass();
Field field;
try {
field = clazz.getDeclaredField("window");
field.setAccessible(true);
field.set(tela, window);
} catch (Exception e) {
logger.log(Level.WARNING,"View sem uma propriedade com o nome window");
}
window.setContent(tela);
UI.getCurrent().addWindow(window);
try{
Method foco = clazz.getMethod("focus");
foco.invoke(tela);
} catch (NoSuchMethodException e) {
} catch (InvocationTargetException e) {
} catch (IllegalAccessException e) {
}
}
and this is my comp:
@SpringComponent
@UIScope
public class QualificarApresentanteImpl extends QualificarApresentante{
private Apresentante apresentante = new Apresentante();
private Window window;
@Autowired
private EventBus.UIEventBus eventBusApresentante;
public QualificarApresentanteImpl() {
btnSalvarApresentante.addClickListener(evt -> fecharJanela());
txtDocumento.addShortcutListener(new ShortcutListener("Shortcut Name", ShortcutAction.KeyCode.ENTER, null) {
@Override
public void handleAction(Object sender, Object target) {
fecharJanela();
}
});
txtApresentante.addShortcutListener(new ShortcutListener("Shortcut Name", ShortcutAction.KeyCode.ENTER, null) {
@Override
public void handleAction(Object sender, Object target) {
fecharJanela();
}
});
}
private void fecharJanela() {
CPFValidator cpfValidator =new CPFValidator();
try {
cpfValidator.assertValid(txtDocumento.getValue());
this.window.close();
// this.eventBusApresentante.publish(this, new CartorioInteligenteEvent.FinalizarMovimentacao());
}catch (InvalidStateException v){
Notification.show("Número de CPF Inválido", Notification.Type.ERROR_MESSAGE);
}
}
public void focus(){
txtApresentante.focus();
}
public String getNomeApresentante(){
return txtApresentante.getValue();
}
public String getNrDocumentoApresentante(){
return this.txtDocumento.getValue();
}
public Apresentante getApresentante(){
apresentante.setNome(txtApresentante.getValue());
return apresentante;
}
public void setNome(String nmApresentante){
this.txtApresentante.setValue(nmApresentante);
}
public void setNrDocumentoApresentante(String documento){
this.txtDocumento.setValue(documento);
}
public boolean isPreenchido() {
boolean retorno = true;
if(Strings.isNullOrEmpty(txtApresentante.getValue())){
retorno = false;
}
if(Strings.isNullOrEmpty(txtDocumento.getValue())){
retorno = false;
}
return retorno;
}
}
and this is the method that receive the event
@EventBusListenerMethod(scope = EventScope.UI)
private void finalizarMovimentacaoListener(CartorioInteligenteEvent.FinalizarMovimentacao finalizarMovimentacao){
finalizarPedidoClickEvent();
}
so sometimes that method are execute 2 times. anyone know why?
tks