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