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

@@ -0,0 +1,33 @@
package danielcortes.xyz.views.dialogs;
import javax.swing.*;
import java.awt.*;
import java.io.IOException;
import java.nio.file.Path;
public class InformeGeneratedConfirmation {
private Path path;
public InformeGeneratedConfirmation(Path path){
this.path = path;
}
public void execute(){
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(this.path.toFile());
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

View File

@@ -28,12 +28,11 @@ import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;
import com.intellij.uiDesigner.core.Spacer;
import danielcortes.xyz.views.components.YearSpinnerModel;
import danielcortes.xyz.views.listeners.WindowClosingListener;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.time.LocalDate;
import java.util.ArrayList;
@@ -48,29 +47,35 @@ public class MonthSelectDialog extends JDialog {
private boolean acepted;
public MonthSelectDialog(JComponent parent) {
public MonthSelectDialog() {
$$$setupUI$$$();
setContentPane(contentPane);
setModalityType(ModalityType.APPLICATION_MODAL);
getRootPane().setDefaultButton(buttonOK);
setup();
}
private void setup() {
this.setContentPane(contentPane);
this.setModalityType(ModalityType.APPLICATION_MODAL);
this.getRootPane().setDefaultButton(buttonOK);
buttonOK.addActionListener(e -> onOK());
this.buttonOK.addActionListener(e -> onOK());
buttonCancel.addActionListener(e -> onCancel());
this.buttonCancel.addActionListener(e -> onCancel());
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
onCancel();
}
});
contentPane.registerKeyboardAction(e -> onCancel(), KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
this.addWindowListener((WindowClosingListener) e -> onCancel());
this.contentPane.registerKeyboardAction(e -> onCancel(), KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
this.setLocationRelativeTo(null);
pack();
setLocationRelativeTo(parent);
}
public LocalDate execute() {
setVisible(true);
if (this.isAcepted()) {
return this.getMonth();
} else {
return null;
}
}
private void onOK() {
@@ -83,11 +88,11 @@ public class MonthSelectDialog extends JDialog {
dispose();
}
public boolean isAcepted() {
private boolean isAcepted() {
return this.acepted;
}
public LocalDate getMonth() {
private LocalDate getMonth() {
int year = Integer.valueOf((String) yearSpinner.getValue());
int month = this.months.indexOf(this.monthCombo.getSelectedItem()) + 1;

View File

@@ -29,12 +29,11 @@ import com.intellij.uiDesigner.core.GridLayoutManager;
import danielcortes.xyz.data.DAOManager;
import danielcortes.xyz.models.tipo_egreso.TipoEgreso;
import danielcortes.xyz.models.tipo_egreso.TipoEgresoToStringWrapper;
import danielcortes.xyz.views.listeners.WindowClosingListener;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TipoEgresoSelectDialog extends JDialog {
private JPanel contentPane;
@@ -44,27 +43,35 @@ public class TipoEgresoSelectDialog extends JDialog {
private boolean acepted;
public TipoEgresoSelectDialog(JComponent parent) {
public TipoEgresoSelectDialog() {
$$$setupUI$$$();
setContentPane(contentPane);
setModalityType(ModalityType.APPLICATION_MODAL);
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
getRootPane().setDefaultButton(buttonOK);
this.setup();
}
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
buttonCancel.doClick();
}
});
private void setup() {
this.setContentPane(contentPane);
this.setModalityType(ModalityType.APPLICATION_MODAL);
this.getRootPane().setDefaultButton(buttonOK);
contentPane.registerKeyboardAction(e -> onCancel(), KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
this.buttonOK.addActionListener(e -> onOK());
this.buttonCancel.addActionListener(e -> onCancel());
buttonOK.addActionListener(e -> onOK());
buttonCancel.addActionListener(e -> onCancel());
this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
this.addWindowListener((WindowClosingListener) e -> onCancel());
this.contentPane.registerKeyboardAction(e -> onCancel(), KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
pack();
setLocationRelativeTo(parent);
setVisible(true);
this.setLocationRelativeTo(null);
this.pack();
}
public TipoEgreso execute() {
this.setVisible(true);
if (this.isAcepted()) {
return this.getTipoEgreso();
} else {
return null;
}
}
private void onOK() {
@@ -134,4 +141,5 @@ public class TipoEgresoSelectDialog extends JDialog {
public JComponent $$$getRootComponent$$$() {
return contentPane;
}
}

View File

@@ -0,0 +1,79 @@
package danielcortes.xyz.views.dialogs;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.io.File;
import java.io.IOException;
import java.nio.file.*;
public class XLSFileChooser {
private JFileChooser chooser;
public XLSFileChooser(String suggestedName){
this.chooser = new JFileChooser();
this.chooser.setDialogType(JFileChooser.SAVE_DIALOG);
this.chooser.setSelectedFile(new File(suggestedName + ".xls"));
this.chooser.setFileFilter(new FileNameExtensionFilter("Excel 2007", "xls"));
}
public Path execute(){
boolean accepted = this.chooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION;
if(accepted){
Path path = processFilePath(this.chooser.getSelectedFile().getPath());
return path;
}else{
return null;
}
}
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(
null,
"El nombre de archivo entregado es invalido",
"Error!",
JOptionPane.ERROR_MESSAGE
);
return null;
}
try {
Files.createFile(path);
} catch (FileAlreadyExistsException e) {
int response = JOptionPane.showConfirmDialog(
null,
"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(
null,
"No a sido posible crear el archivo",
"Error!",
JOptionPane.ERROR_MESSAGE
);
return null;
}
return path;
}
}