Search code examples
javavaadinserver-side-renderingserver-sidevaadin8

Cannot close Java confirmation subwindow (Vaadin 8)


I have a button that should generate a Excel file with some data. I need to add a pop-up window that asks "Are you sure?" with 2 button : "YES" or "NO".

I have a button, i added a clicklistener, inside the clicklistener I have initialized an object from a class (YesNoConfirmation) that i have created.
The YesNoConfirmation creates a Window with a string and 2 buttons and adds it to the main window. I have added clicklisteners to the 2 buttons of my Confirmation window. My YesNoConfirmation class has a member named "isConfirmed", that should keep the state - if confirmed or no. The 2 clicklisteners from the YES/NO buttons update the isConfirmed member of my class. Then, based on the value of "isConfirmed", it should decide whether to execute the code for generating the Excel file or not. I have done some debugging ,and it seems that the "isConfirmed" member gets updated accordingly, but when i check if the user clicked YES or NO with this: if(panel1.getConfirmationStatus() == true), nothing happens.

Can you help me please?

The code from the main application is:

        getBtnGenereazaNOTAAUTORIZARE().addClickListener(new Button.ClickListener() {
            public void buttonClick(ClickEvent event) {
                if(log.isDebugEnabled())log.debug("getBtnGenereazaNOTAAUTORIZARE ");
                //ListDataProvider<LinkedHashMap<String, String>> rapoarte=(ListDataProvider<LinkedHashMap<String, String>>) getGrdRapoarteDetalii().getDataProvider();
                //ArrayList<LinkedHashMap<String, String>> rapoarteList=(ArrayList<LinkedHashMap<String, String>>) rapoarte.getItems();
                File xlsFile = new File("/home/misadmin/rapoarte/Nota_Autoriz_Ch_DL_Anexa9a.xlsx");
                SelectBeanStream result = new SelectBeanStream();//"ok";
                InputStream is;
                YesNoConfirm panel1 = new YesNoConfirm();
                panel1.openConfirmationPanel("Confirmare");
                if(panel1.getConfirmationStatus() == true) {
                    try {
                        //String fisierNume=prj_cod+"_"+TipRap+"_"+NR_Rap+"_"+(new Date());
                        String pattern = "yyyy-MM-dd HH:mm:ss";
                        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
                        String fisierNume=prj_cod+"_RF_"+NR_Rap+"-NotaAutorizare_"+(simpleDateFormat.format(new Date())).replace(" ", "_").replaceAll(":", "_");
                        if(log.isDebugEnabled())log.debug("getBtnGenereazaNOTAAUTORIZARE ctrlExportXls fisierNume "+fisierNume);
                        //fisierNume=fisierNume.replaceAll(" ", "_").replaceAll(":", "_");
                        result = GrantulLocalServiceUtil.dl_Cheltuieli_NA(xlsFile, fisierNume, prj_cod, String.valueOf(NR_Rap));
                        if(log.isDebugEnabled())log.debug("getBtnGenereazaNOTAAUTORIZARE ctrlExportXls result "+result);
                        is=result.getStream();
                        if(log.isDebugEnabled())log.debug("getBtnGenereazaNOTAAUTORIZARE ctrlExportXls is "+is);
                        if(log.isDebugEnabled())log.debug("getBtnGenereazaNOTAAUTORIZARE ctrlExportXls is.available() "+is.available());
                        byte[] targetByte = new byte[is.available()];
                        is.read(targetByte);
                        
                        showFile(getBtnGenereazaNOTAAUTORIZARE().getUI(), fisierNume, targetByte, "XLSX", getBtnGenereazaNOTAAUTORIZARE());
                        //PortletResponseUtil.sendFile(portletRequest, (MimeResponse) portletResponse, fisierNume+".xlsx", is, "xlsx");
                        is.close();
                        
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }   
                }
                
            }
            
            
        });

The code of my YesNoConfirmation class is:

public class YesNoConfirm extends Window{
    
    public boolean isConfirmed = false;
    public void openConfirmationPanel(String confirmationTitle) {
        // Create a sub-window and set the content
        Window subWindow = new Window(confirmationTitle);
        VerticalLayout subContent = new VerticalLayout();
        subContent.setMargin(true);
        subWindow.setContent(subContent);
        
        HorizontalLayout buttonStack = new HorizontalLayout();
        Button okButton = new Button("OK");
        Button cancelButton = new Button("Anuleaza");
        buttonStack.addComponent(okButton);
        buttonStack.addComponent(cancelButton);
        
        // Put some components in it
        subContent.addComponent(new Label("Confirmare"));
        subContent.addComponent(buttonStack);
        
        // Center it in the browser window
        subWindow.center();
        
        // Open it in the UI
        UI.getCurrent().addWindow(subWindow);
        
        okButton.addClickListener(ClickEvent -> {
            this.isConfirmed = true;
        });
        
        cancelButton.addClickListener(ClickEvent -> {
            this.isConfirmed = false;
        });
        
    }
    
    public boolean getConfirmationStatus() {
        return this.isConfirmed;
    }
}

Thanks in advance


Solution

  • The panel code is not blocking. Looking at the two rows below

    panel1.openConfirmationPanel("Confirmare");
    if(panel1.getConfirmationStatus() == true) {
    

    Once you've opened the panel, the next line is executed immediately. At this point, the status is still false, so it doesn't execute the code inside your if-statement.

    Even if the isConfirmed flag is updated later, that code is not executed again, so nothing will happen.

    What you can do is to use callbacks. Either you can pass button click listeners directly to the YesNoConfirm, or you can use some other class like Runnable or a custom class/interface.

    For example, something like this:

    public class YesNoConfirm extends Window {
    
        private final Runnable runOnYesAction;
    
        public YesNoConfirm(Runnable runOnYesAction) {
            this.runOnYesAction = runOnYesAction;
        }
        
        public void openConfirmationPanel(String confirmationTitle) {
            // Create the window etc
            ...
        
            // Open it in the UI
            UI.getCurrent().addWindow(subWindow);
            
            okButton.addClickListener(clickEvent -> runOnYesAction.run());
            
            cancelButton.addClickListener(clickEvent -> {
                // Maybe just close the window?
            });
            
        }
    }
    

    And you would use it something like this:

    YesNoConfirm confirmDialog = new YesNoConfirm(() -> {
        // Put code that should be run if Yes is clicked here
    });