Metodos que creaban dialogos a clases

Se separaron estos metodos para mayor claridad en el codigo y mas facil
reutilizacion de los dialogos ya que ahora me vi en el caso de que me
encontraba utilizando estos metodos  en 2 vistas separadas y me vi
copiando los metodos de una a otra, pero eso no es bonito! asi que las
separe en clases con un solo metodo publico llamado execute el cual
devuelve el objeto que se espera recibir o un null en caso de que el
usuario cancele el dialogo o ocurra algun error.
This commit is contained in:
Daniel Cortes
2019-02-27 02:48:54 -03:00
parent f5b44f32fe
commit 57d6d62b10
12 changed files with 668 additions and 194 deletions

View File

@@ -30,15 +30,12 @@ import danielcortes.xyz.informes.InformeLibroDeVentas;
import danielcortes.xyz.models.tipo_egreso.TipoEgreso;
import danielcortes.xyz.utils.StringUtils;
import danielcortes.xyz.views.InformesSideBar;
import danielcortes.xyz.views.dialogs.InformeGeneratedConfirmation;
import danielcortes.xyz.views.dialogs.MonthSelectDialog;
import danielcortes.xyz.views.dialogs.TipoEgresoSelectDialog;
import danielcortes.xyz.views.dialogs.XLSFileChooser;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.nio.file.*;
import java.nio.file.Path;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
@@ -60,7 +57,7 @@ public class InformesSideBarController {
}
private void generarInformeLibroDeVentasListener() {
LocalDate month = askForMonth();
LocalDate month = new MonthSelectDialog().execute();
if (month == null) {
return;
}
@@ -69,24 +66,25 @@ public class InformesSideBarController {
String formatedName = month.format(formatter);
String capitalized = StringUtils.capitalize(formatedName);
Path saveFile = askForFile(Configuration.get("base_save_directory") + "Libro " + capitalized);
Path saveFile = new XLSFileChooser(Configuration.get("base_save_directory") + "Libro " + capitalized).execute();
if (saveFile == null) {
return;
}
InformeLibroDeVentas informe = new InformeLibroDeVentas(month, saveFile);
Path generatedFile = informe.generarInforme();
informe.generarInforme();
this.showConfirmation(generatedFile);
new InformeGeneratedConfirmation(saveFile).execute();
}
private void generarInformeEgresosListener() {
TipoEgreso tipoEgreso = askForTipoEgreso();
TipoEgreso tipoEgreso = new TipoEgresoSelectDialog().execute();
if (tipoEgreso == null) {
return;
}
LocalDate month = askForMonth();
LocalDate month = new MonthSelectDialog().execute();
if (month == null) {
return;
}
@@ -94,7 +92,7 @@ public class InformesSideBarController {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMMM YYYY");
String formatedMonth = month.format(formatter);
Path saveFile = askForFile("Informe Egresos - " + tipoEgreso.getNombre() + " - " + StringUtils.capitalize(formatedMonth));
Path saveFile = new XLSFileChooser("Informe Egresos - " + tipoEgreso.getNombre() + " - " + StringUtils.capitalize(formatedMonth)).execute();
if (saveFile == null) {
return;
}
@@ -102,108 +100,6 @@ public class InformesSideBarController {
InformeEgresos informe = new InformeEgresos(tipoEgreso.getId(), month, saveFile);
Path generatedFile = informe.generarInforme();
this.showConfirmation(generatedFile);
}
private LocalDate askForMonth() {
MonthSelectDialog monthSelectDialog = new MonthSelectDialog(null);
if (monthSelectDialog.isAcepted()) {
return monthSelectDialog.getMonth();
} else {
return null;
}
}
private TipoEgreso askForTipoEgreso() {
TipoEgresoSelectDialog tipoEgresoSelectDialog = new TipoEgresoSelectDialog(null);
if (tipoEgresoSelectDialog.isAcepted()) {
return tipoEgresoSelectDialog.getTipoEgreso();
} else {
return null;
}
}
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) {
return processFilePath(chooser.getSelectedFile().getPath());
} else {
return null;
}
}
private void showConfirmation(Path path) {
int result = JOptionPane.showConfirmDialog(
null,
"El informes se a generado\n" +
"¿Desea abrirlo?",
"Confirmacion",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE
);
if (result == 0) {
try {
Desktop.getDesktop().open(path.toFile());
} catch (IOException e) {
e.printStackTrace();
}
}
}
private Path processFilePath(String pathString) {
Path path;
if (!pathString.endsWith(".xls")) {
pathString = pathString + ".xls";
}
try {
path = Paths.get(pathString);
} catch (InvalidPathException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(
this.view.getContentPanel(),
"El nombre de archivo entregado es invalido",
"Error!",
JOptionPane.ERROR_MESSAGE
);
return null;
}
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;
new InformeGeneratedConfirmation(saveFile).execute();
}
}