Cambiada la libreria utilizada para manejar los archivos del informe, se usa NIO ahora y deberia estar solucionado los errores con windows.
This commit is contained in:
@@ -38,6 +38,10 @@ import javax.swing.filechooser.FileNameExtensionFilter;
|
||||
import java.awt.*;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.FileAlreadyExistsException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.time.LocalDate;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
|
||||
@@ -64,13 +68,13 @@ public class InformesController {
|
||||
String formatedName = month.format(formatter);
|
||||
String capitalized = StringUtils.toUpperCase(formatedName);
|
||||
|
||||
File saveFile = askForFile("Libro " + capitalized);
|
||||
Path saveFile = askForFile("Libro " + capitalized);
|
||||
if (saveFile == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
InformeLibroDeVentas informe = new InformeLibroDeVentas(month, saveFile);
|
||||
File generatedFile = informe.generarInforme();
|
||||
Path generatedFile = informe.generarInforme();
|
||||
|
||||
this.showConfirmation(generatedFile);
|
||||
}
|
||||
@@ -89,13 +93,13 @@ public class InformesController {
|
||||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMMM YYYY");
|
||||
String formatedMonth = month.format(formatter);
|
||||
|
||||
File saveFile = askForFile("Informe Egresos - " + tipoEgreso.getNombre() + " - " + StringUtils.toUpperCase(formatedMonth));
|
||||
Path saveFile = askForFile("Informe Egresos - " + tipoEgreso.getNombre() + " - " + StringUtils.toUpperCase(formatedMonth));
|
||||
if (saveFile == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
InformeEgresos informe = new InformeEgresos(tipoEgreso.getId(), month, saveFile);
|
||||
File generatedFile = informe.generarInforme();
|
||||
Path generatedFile = informe.generarInforme();
|
||||
|
||||
this.showConfirmation(generatedFile);
|
||||
}
|
||||
@@ -118,28 +122,58 @@ public class InformesController {
|
||||
}
|
||||
}
|
||||
|
||||
private File askForFile(String suggestedName) {
|
||||
private Path askForFile(String suggestedName) {
|
||||
JFileChooser chooser = new JFileChooser();
|
||||
chooser.setDialogType(JFileChooser.SAVE_DIALOG);
|
||||
chooser.setSelectedFile(new File(suggestedName + ".xls"));
|
||||
chooser.setFileFilter(new FileNameExtensionFilter("Excel 2007", "xls"));
|
||||
|
||||
if (chooser.showSaveDialog(this.view.getContentPanel()) == JFileChooser.APPROVE_OPTION) {
|
||||
String filename = chooser.getSelectedFile().getPath();
|
||||
if (!FileUtils.isValidPath(filename)) {
|
||||
JOptionPane.showMessageDialog(this.view.getContentPanel(),"El archivo seleccionado no es valido","Archivo no valido", JOptionPane.ERROR_MESSAGE);
|
||||
return null;
|
||||
} else if (!filename.endsWith(".xls")) {
|
||||
filename += ".xls";
|
||||
String pathString = chooser.getSelectedFile().getPath();
|
||||
|
||||
if (!pathString.endsWith(".xls")) {
|
||||
pathString = pathString + ".xls";
|
||||
}
|
||||
|
||||
return new File(filename);
|
||||
|
||||
Path path = Paths.get(pathString);
|
||||
|
||||
|
||||
try {
|
||||
Files.createFile(path);
|
||||
} catch (FileAlreadyExistsException e) {
|
||||
int response = JOptionPane.showConfirmDialog(
|
||||
this.view.getContentPanel(),
|
||||
"El archivo ya existe\n¿Desea sobreescribirlo?",
|
||||
"Confirmacion",
|
||||
JOptionPane.YES_NO_OPTION,
|
||||
JOptionPane.WARNING_MESSAGE
|
||||
);
|
||||
|
||||
if (response != 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
JOptionPane.showMessageDialog(
|
||||
this.view.getContentPanel(),
|
||||
"No a sido posible crear el archivo",
|
||||
"Error!",
|
||||
JOptionPane.ERROR_MESSAGE
|
||||
);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
return path;
|
||||
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private void showConfirmation(File file) {
|
||||
private void showConfirmation(Path path) {
|
||||
int result = JOptionPane.showConfirmDialog(
|
||||
this.view.getContentPanel(),
|
||||
"El informes se a generado\n" +
|
||||
@@ -148,9 +182,10 @@ public class InformesController {
|
||||
JOptionPane.YES_NO_OPTION,
|
||||
JOptionPane.QUESTION_MESSAGE
|
||||
);
|
||||
|
||||
if (result == 0) {
|
||||
try {
|
||||
Desktop.getDesktop().open(file);
|
||||
Desktop.getDesktop().open(path.toFile());
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user