Enhanced Window Opener

I using, vaadin with springboot, so I create one layout that I put inside one window to open just like a modal,

in this view I have one button with Enhanced Window Opener to open a jasperreport pdf, this works fine, but after I open a window, I close the window with pdf, and close the modal, if I open the modal again the tab with pdf re-open

can I fix this?
tks

Hi Fabio,
can you post the source code so I can make some check?

Sure
this is my main:

public class CartorioUI extends UI implements ViewDisplay{ 
  @Autowired ModalProv45DepositoPrevioImpl modalProv45DepositoPrevio;

    @Override
    protected void init(VaadinRequest vaadinRequest) {
     //here I create my view this one menu:

     menuDash.addItem("Prov.45 - Livro de Depósito Previo", FontAwesome.FILE_PDF_O, event -> GerenciarJanela.abrirJanela(null,50, 50, modalProv45DepositoPrevio));
    }
}

this is my GerenciarJanela
GerenciarJanela.java

public class GerenciarJanela {
public static void abrirPopup(String caption, Resource icon, String message, Button focus, Button... buttons){

        MyMessageBox window = new MyMessageBox(caption,icon, message,focus, buttons);
        window.setWidth(40, Sizeable.Unit.PERCENTAGE);
        window.setHeight(170, Sizeable.Unit.PIXELS);

        window.setClosable(false);
        window.setModal(true);
        window.setDraggable(false);
        window.setResizable(false);
        window.center();
        UI.getCurrent().addWindow(window);
        try{
            Class<?> clazz = window.getClass();
            Method foco = clazz.getMethod("focusBotao");
            foco.invoke(window);
        } catch (NoSuchMethodException e) {
            e.getLocalizedMessage();
        } catch (InvocationTargetException e) {
            e.getLocalizedMessage();
        } catch (IllegalAccessException e) {
            e.getLocalizedMessage();
        }
    }

    public static void abrirJanela(String titulo, Integer altura, Integer largura, Component tela) {
        abrirJanela(titulo, altura, largura, tela, null, true);
    }
}

this is my
ModalProv45DepositoPrevioImple (the class tha open a window)

@SpringComponent
@UIScope
public class ModalProv45DepositoPrevioImpl extends ModalProv45DepositoPrevio {
    @Autowired
    protected DataSource localDataSource;
    @Autowired
    ResourceLoader resourceLoader;
    @Autowired
    private ConfiguracaoService configuracaoService;
    private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm");

    private Window window;
    java.sql.Connection con = null;

    public ModalProv45DepositoPrevioImpl() {
        LocalDate dt = LocalDate.now();
        dtInicial.setValue(DateUtils.toDate(dt.withDayOfMonth(1)));
        dtFinal.setValue(DateUtils.toDate(dt.withDayOfMonth(dt.lengthOfMonth())));
        btnFechar.addClickListener(evt -> {this.window.close();});
        btnGerarRelatorio.addClickListener(evt -> EnhancedBrowserWindowOpener.extendOnce(btnGerarRelatorio).open(gerarRelatorio()));
    }

    public void focus(){
        dtInicial.focus();
    }
    private StreamResource gerarRelatorio() {
        StreamResource resourcesReport = null;
        try {
            if(!validarForm()){
                Notification.show("Favor preencher todos os campos", Notification.Type.ERROR_MESSAGE);
                return null;
            }

            con = localDataSource.getConnection();

            Resource resource = resourceLoader.getResource(resourceLoader.CLASSPATH_URL_PREFIX+"/static/relatorios/LivroDepositoPrevio.jrxml");
            JasperDesign jasperDesign = JRXmlLoader.load(resource.getInputStream());

            JasperReport jasperReport  = JasperCompileManager.compileReport(jasperDesign);

            StreamResource.StreamSource source = new StreamResource.StreamSource() {
                public InputStream getStream() {
                    byte[] b = null;
                    try{
                        b = JasperRunManager.runReportToPdf(jasperReport, preencherParametros(resource.getFile().getParent()), con);

                    } catch (Exception e) {
                        e.printStackTrace();

                    }
                    return new ByteArrayInputStream(b);
                }
            };
            resourcesReport = new StreamResource(source, "LivroDepositoPrevio" + System.currentTimeMillis() + ".pdf");
            return resourcesReport;

        } catch (SQLException e) {
            e.printStackTrace();
        } catch (JRException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return resourcesReport;
    }

    private boolean validarForm() {

        if(!dtInicial.isValid()){
            return false;
        }
        if(!dtFinal.isValid()){
            return false;
        }
        if(!nmLivro.isValid()){
            return false;
        }
        if(!nrFolhaInicial.isValid()){
            return false;
        }
        return true;
    }

    private Map<String, Object> preencherParametros(String diretorio) {
        Map<String, Object> parametros = new HashMap<>();

        parametros.put("SUBREPORT_DIR",diretorio+ File.separator);
        parametros.put("cartorio", configuracaoService.getConfiguracao().getNmCartorio());
        parametros.put("endereco", configuracaoService.getConfiguracao().getNmEnderecoCompletoCartorio());
        parametros.put("cnpj", configuracaoService.getConfiguracao().getNrCnpjCartorio());
        parametros.put("tabeliao", configuracaoService.getConfiguracao().getNmTabeliao());
        parametros.put("nrFolhaInicial", Integer.parseInt(nrFolhaInicial.getValue()));
        parametros.put("nrLivro", Integer.parseInt(nmLivro.getValue()));
        parametros.put("dtInicial", this.dtInicial.getValue());
        parametros.put("dtFinal", this.dtFinal.getValue());
        parametros.put("oficial", configuracaoService.getConfiguracao().getNmTabeliao());
        return parametros;
    }
}

So if I click in menu

menuDash.addItem("Prov.45 - Livro de Depósito Previo", FontAwesome.FILE_PDF_O, event -> GerenciarJanela.abrirJanela(null,50, 50, modalProv45DepositoPrevio));

he open a window with modal, then I click to generate my report works fine, but after I close and click in menu again the closed tab with my report open again

Hi Fabio,
the problem here is that EnhancedWindowOpener uses a timestamp flag in State
to tell client side to open a new window after the content is generated;
this flag is set when you call open method.

In your case, when you open the popup the second time, the flag is already set
beacuse you are using a component (ModalProv45DepositoPrevioImpl) that is UI scoped;
thus another window is opened when the modal window content is attached .

If you cannot change the scope of ModalProv45DepositoPrevioImpl instance
I suggest you to extend the button in attach method and remove the extension in detach
(I’m assuming you are using Vaadin 8)

@SpringComponent
@UIScope
public class ModalProv45DepositoPrevioImpl extends ModalProv45DepositoPrevio {

    // your stuff
    // --

    Registration openerClickRegistration;

    public ModalProv45DepositoPrevioImpl() {
        // your stuff....

        // Don't extend the button in constructor
        // btnGerarRelatorio.addClickListener(evt -> EnhancedBrowserWindowOpener.extendOnce(btnGerarRelatorio).open(gerarRelatorio()));
    }

    // In attach extende the button and register the click listener
    @Override
    public void attach() {
        super.attach();
        EnhancedBrowserWindowOpener opener = EnhancedBrowserWindowOpener.extendOnce(btnGerarRelatorio);
        openerClickRegistration = btnGerarRelatorio.addClickListener(e2 -> opener.open(streamContent()));
    }

    // In detach remove the extension and the click listener
    @Override
    public void detach() {
        EnhancedBrowserWindowOpener.extendOnce(button).remove();
        openerClickRegistration.remove();
        super.detach();
    }

    // Other stuff....

}

Let me know if this solves your problem

Best Regards
Marco

For vaadin 7 take use removeClickListener method instead or Registration.remove() , the rest is the same

[code]
@SpringComponent
@UIScope
public class ModalProv45DepositoPrevioImpl extends ModalProv45DepositoPrevio {
// your stuff
// –
Button.ClickListener clickListener;

public ModalProv45DepositoPrevioImpl() {
    // your stuff....
}

// In attach extende the button and register the click listener
@Override
public void attach() {
    super.attach();
    EnhancedBrowserWindowOpener opener = EnhancedBrowserWindowOpener.extendOnce(btnGerarRelatorio);
    clickListener = e2 -> opener.open(streamContent());
}
// In detach remove the extension and the click listener
@Override
public void detach() {
    EnhancedBrowserWindowOpener.extendOnce(button).remove();
    button.removeClickListener(clickListener);
    super.detach();
}
// Other stuff....

}
[/code]HTH
Marco

I using vaadin 7(but will use in one project with 8) tu have the solution to 7?

tks