package cz.cvut.fel.restauracefel.library.library_gui;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.File;
import java.util.Collection;
import java.util.Map;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import cz.cvut.fel.restauracefel.library.service.Printer;
/**
* Tiskovy GUI dialog. Trida je univerzalni, zalezi na nastavenych parametrech.
*
* @author Jarda
*/
public class PrintDialog extends javax.swing.JDialog {
private JFrame parent = null;
private JTable table = null;
private Collection collection = null;
private Map<String, Object> params = null;
private File printFile = null;
/**
* Konstruktor tridy PrintDialog.
*
* @param parent
* @param modal
* @param params
* @param printFile
*/
public PrintDialog(JFrame parent, boolean modal, Map<String, Object> params, File printFile) {
super(parent, modal);
super.setTitle("Tisk");
this.parent = parent;
this.params = params;
this.printFile = printFile;
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
int y = (int) ((dim.getHeight() - 116) / 2);
int x = (int) ((dim.getWidth() - 315) / 2);
setBounds(x, y, 315, 116);
initComponents();
jLabelFileName.setVisible(false);
jTextFieldFileName.setVisible(false);
jTextFieldFileName.setText(printFile.getName().replaceFirst(".jasper", ""));
jButtonDirectory.setVisible(false);
jLabelDir.setVisible(false);
jTextFieldDir.setVisible(false);
jTextFieldDir.setText(System.getProperty("user.dir"));
jComboBoxForm.setSelectedIndex(0);
jComboBoxForm.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
if (jComboBoxForm.getSelectedIndex() == 0) {
//tisk na tiskarnu
jLabelFileName.setVisible(false);
jTextFieldFileName.setVisible(false);
jButtonDirectory.setVisible(false);
jLabelDir.setVisible(false);
jTextFieldDir.setVisible(false);
}
if (jComboBoxForm.getSelectedIndex() == 1) {
//tisk do PDF souboru
jLabelFileName.setVisible(true);
jTextFieldFileName.setVisible(true);
jButtonDirectory.setVisible(true);
jLabelDir.setVisible(true);
jTextFieldDir.setVisible(true);
String name = jTextFieldFileName.getText();
if (!name.contains(".pdf") && !name.contains(".csv")) {
jTextFieldFileName.setText(jTextFieldFileName.getText() + ".pdf");
} else if (name.contains(".csv")) {
jTextFieldFileName.setText(jTextFieldFileName.getText().replaceFirst(".csv", ".pdf"));
}
}
if (jComboBoxForm.getSelectedIndex() == 2) {
//tisk do PDF souboru
jLabelFileName.setVisible(true);
jTextFieldFileName.setVisible(true);
jButtonDirectory.setVisible(true);
jLabelDir.setVisible(true);
jTextFieldDir.setVisible(true);
String name = jTextFieldFileName.getText();
if (!name.contains(".pdf") && !name.contains(".csv")) {
jTextFieldFileName.setText(jTextFieldFileName.getText() + ".csv");
} else if (name.contains(".pdf")) {
jTextFieldFileName.setText(jTextFieldFileName.getText().replaceFirst(".pdf", ".csv"));
}
}
}
});
}
/**
* Nastaveni tabulky JTable jako zdroje dat pro tisk.
*
* @param table tabulka JTable ze ktere se budou brat data pro tisk
*/
public void setTable(JTable table) {
this.table = table;
this.collection = null;
}
/**
* Nastavuje kolekci Collection jako zdroj dat pro tisk.
*
* @param collection kolekce typu Collection
*/
public void setCollection(Collection collection) {
this.collection = collection;
this.table = null;
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
jLabelForm = new javax.swing.JLabel();
jComboBoxForm = new javax.swing.JComboBox();
jLabelFileName = new javax.swing.JLabel();
jTextFieldFileName = new javax.swing.JTextField();
jButtonDirectory = new javax.swing.JButton();
jButtonPrint = new javax.swing.JButton();
jLabelDir = new javax.swing.JLabel();
jTextFieldDir = new javax.swing.JTextField();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
jLabelForm.setText("Forma tisku");
jComboBoxForm.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Tisknout na tiskárnu", "Tisknout do PDF", "Tisknout do CSV" }));
jLabelFileName.setText("Název PDF souboru");
jButtonDirectory.setText("Vybrat");
jButtonDirectory.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonDirectoryActionPerformed(evt);
}
});
jButtonPrint.setText("Tisknout");
jButtonPrint.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonPrintActionPerformed(evt);
}
});
jLabelDir.setText("Umístění souboru");
jTextFieldDir.setEditable(false);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabelFileName)
.addComponent(jLabelForm)
.addComponent(jLabelDir))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jButtonPrint, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 121, Short.MAX_VALUE)
.addComponent(jTextFieldFileName, javax.swing.GroupLayout.DEFAULT_SIZE, 121, Short.MAX_VALUE)
.addComponent(jComboBoxForm, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jTextFieldDir, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 121, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButtonDirectory, javax.swing.GroupLayout.PREFERRED_SIZE, 66, Short.MAX_VALUE)
.addGap(14, 14, 14))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(17, 17, 17)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabelForm)
.addComponent(jComboBoxForm, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabelFileName)
.addComponent(jTextFieldFileName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabelDir)
.addComponent(jTextFieldDir, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButtonDirectory))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 11, Short.MAX_VALUE)
.addComponent(jButtonPrint)
.addContainerGap())
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void jButtonDirectoryActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonDirectoryActionPerformed
JFileChooser fileChooser = new JFileChooser(new File(jTextFieldDir.getText()));
fileChooser.setDialogTitle("Tiskové PDF uložit do...");
fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int returnVal = fileChooser.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
jTextFieldDir.setText(fileChooser.getSelectedFile().getAbsolutePath());
} else {
return;
}
}//GEN-LAST:event_jButtonDirectoryActionPerformed
private void jButtonPrintActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonPrintActionPerformed
try {
if (jComboBoxForm.getSelectedIndex() == 0) {
if (table != null) {
Printer.printFromTableToPrinter(params, table.getModel(), printFile);
}
if (collection != null) {
Printer.printFromListToPrinter(params, collection, printFile);
}
}
if (jComboBoxForm.getSelectedIndex() == 1) {
String output = jTextFieldDir.getText() + System.getProperty("file.separator") + jTextFieldFileName.getText();
if (table != null) {
Printer.printFromTableToPDF(params, table.getModel(), printFile, output);
}
if (collection != null) {
Printer.printFromListToPDF(params, collection, printFile, output);
}
JOptionPane.showMessageDialog(this, "Tiskový výstup byl úspěšně uložen do souboru \""
+ jTextFieldFileName.getText() + "\".", "Tisk", JOptionPane.INFORMATION_MESSAGE);
}
if (jComboBoxForm.getSelectedIndex() == 2) {
String output = jTextFieldDir.getText() + System.getProperty("file.separator") + jTextFieldFileName.getText();
if (table != null) {
Printer.printFromTableToCSV(table.getModel(), output);
}
if (collection != null) {
//
}
}
} catch (Exception jre) {
JOptionPane.showMessageDialog(this, "Během přípravy tisku se "
+ "vyskytla chyba. V tisku není možno pokračovat.", "Chyba "
+ "tisku", JOptionPane.ERROR_MESSAGE);
jre.printStackTrace();
}
}//GEN-LAST:event_jButtonPrintActionPerformed
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton jButtonDirectory;
private javax.swing.JButton jButtonPrint;
private javax.swing.JComboBox jComboBoxForm;
private javax.swing.JLabel jLabelDir;
private javax.swing.JLabel jLabelFileName;
private javax.swing.JLabel jLabelForm;
private javax.swing.JTextField jTextFieldDir;
private javax.swing.JTextField jTextFieldFileName;
// End of variables declaration//GEN-END:variables
}