package views;
import java.awt.BorderLayout;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.Enumeration;
import java.util.Hashtable;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import misc.CustomFileFilter;
import model.Model;
import application.Application;
public class ExportPanel extends JPanel implements PropertyChangeListener {
private Model model;
Hashtable exportLanguages = null;
private JPanel jPanel = null;
private JComboBox jComboBoxLanguage = null;
private JLabel jLabelLanguage = null;
private JLabel jLabelObject = null;
private JLabel jLabelObjectName = null;
private JLabel jLabelObjectPath = null;
private JTextField jTextFieldObjectName = null;
private JTextField jTextFieldObjectPath = null;
private JButton jButtonBrowse = null;
private JButton jButtonExport = null;
private JCheckBox jCheckBox = null;
private JLabel jLabel1 = null;
private JLabel jLabel2 = null;
/**
* This method initializes
*
*/
public ExportPanel(Model model) {
super();
this.model = model;
model.addPropertyChangeListener(this);
exportLanguages = Application.settings.getExportLanguages();
initialize();
}
/**
* This method initializes this
*
*/
private void initialize() {
this.setLayout(new BorderLayout());
this.setSize(new java.awt.Dimension(290,300));
this.setBorder(BorderFactory.createTitledBorder(Application.messages.getString("TITLE_EXPORT")));
this.add(getJPanel(), java.awt.BorderLayout.CENTER);
}
/**
* This method initializes jPanel
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel() {
if (jPanel == null) {
jLabel1 = new JLabel();
jLabel1.setBounds(new java.awt.Rectangle(15,10,50,27));
jLabel1.setText("_____");
jLabelLanguage = new JLabel();
jLabelLanguage.setBounds(new java.awt.Rectangle(50,14,62,27));
jLabelLanguage.setText(Application.messages.getString("EXPORT_LANGUAGE"));
jLabel2 = new JLabel();
jLabel2.setBounds(new java.awt.Rectangle(15,84,70,28));
jLabel2.setText("_____");
jLabelObject = new JLabel();
jLabelObject.setBounds(new java.awt.Rectangle(50,89,62,27));
jLabelObject.setText(Application.messages.getString("EXPORT_FILE"));
jLabelObjectName = new JLabel();
jLabelObjectName.setBounds(new java.awt.Rectangle(17,122,85,25));
jLabelObjectName.setText(Application.messages.getString("EXPORT_OBJECT")+" :");
jLabelObjectPath = new JLabel();
jLabelObjectPath.setBounds(new java.awt.Rectangle(17,163,85,25));
jLabelObjectPath.setText(Application.messages.getString("EXPORT_SAVE_AS")+" :");
jPanel = new JPanel();
jPanel.setLayout(null);
jPanel.add(getJComboBoxLanguage(), null);
jPanel.add(jLabelLanguage, null);
jPanel.add(jLabelObject, null);
jPanel.add(jLabelObjectName, null);
jPanel.add(jLabelObjectPath, null);
jPanel.add(getJTextFieldObjectName(), null);
jPanel.add(getJTextFieldObjectPath(), null);
jPanel.add(getJButtonBrowse(), null);
jPanel.add(getJButtonExport(), null);
jPanel.add(getJCheckBox(), null);
jPanel.add(jLabel1, null);
jPanel.add(jLabel2, null);
}
return jPanel;
}
/**
* This method initializes jComboBoxLanguage
*
* @return javax.swing.JComboBox
*/
private JComboBox getJComboBoxLanguage() {
if (jComboBoxLanguage == null) {
jComboBoxLanguage = new JComboBox();
jComboBoxLanguage.setBounds(new java.awt.Rectangle(15,45,266,25));
for (Enumeration e = exportLanguages.keys(); e.hasMoreElements() ;)
jComboBoxLanguage.addItem(e.nextElement());
}
return jComboBoxLanguage;
}
/**
* This method initializes jTextFieldObjectName
*
* @return javax.swing.JTextField
*/
private JTextField getJTextFieldObjectName() {
if (jTextFieldObjectName == null) {
jTextFieldObjectName = new JTextField();
jTextFieldObjectName.setBounds(new java.awt.Rectangle(94,120,185,25));
jTextFieldObjectName.setToolTipText(Application.messages.getString("EXPORT_OBJECT_TOOLTIP"));
}
return jTextFieldObjectName;
}
/**
* This method initializes getJTextFieldObjectPath
*
* @return javax.swing.JTextField
*/
private JTextField getJTextFieldObjectPath() {
if (jTextFieldObjectPath == null) {
jTextFieldObjectPath = new JTextField();
jTextFieldObjectPath.setBounds(new java.awt.Rectangle(80,160,170,25));
}
return jTextFieldObjectPath;
}
/**
* This method initializes jButtonBrowse
*
* @return javax.swing.JButton
*/
private JButton getJButtonBrowse() {
if (jButtonBrowse == null) {
jButtonBrowse = new JButton();
jButtonBrowse.setBounds(new java.awt.Rectangle(254,160,25,25));
jButtonBrowse.setText("...");
jButtonBrowse.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
chooseExportFile();
}
});
}
return jButtonBrowse;
}
/**
* This method initializes jButtonExport
*
* @return javax.swing.JButton
*/
private JButton getJButtonExport() {
if (jButtonExport == null) {
jButtonExport = new JButton();
jButtonExport.setBounds(new java.awt.Rectangle(189,200,90,25));
jButtonExport.setText(Application.messages.getString("TITLE_EXPORT"));
jButtonExport.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
export();
}
});
}
return jButtonExport;
}
/**
* This method initializes jCheckBox
*
* @return javax.swing.JCheckBox
*/
private JCheckBox getJCheckBox() {
if (jCheckBox == null) {
jCheckBox = new JCheckBox();
jCheckBox.setBounds(new java.awt.Rectangle(14,203,147,27));
jCheckBox.setText(Application.messages.getString("EXPORT_INCLUDE_TEXT"));
}
return jCheckBox;
}
/**
* chooseExportFile asks the user to choose his export file path
* The choice is limited by the dynamic extension filter
*/
private void chooseExportFile() {
JFileChooser fileChooser = new JFileChooser(Application.settings.getLastPath());
CustomFileFilter filter = new CustomFileFilter();
filter.setDescription("*."+(String)exportLanguages.get(jComboBoxLanguage.getSelectedItem()));
filter.addExtension((String)exportLanguages.get(jComboBoxLanguage.getSelectedItem()));
fileChooser.setFileFilter(filter);
if(fileChooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {
String filePath = fileChooser.getSelectedFile().getAbsolutePath();
// Check on extension
if (!filePath.toLowerCase().endsWith("."+(String)exportLanguages.get(jComboBoxLanguage.getSelectedItem())))
filePath += "."+(String)exportLanguages.get(jComboBoxLanguage.getSelectedItem());
jTextFieldObjectPath.setText(filePath);
}
}
/**
* export method calls the model export process
* to export the current regular expression to a selected object
*/
private void export() {
if (!jTextFieldObjectPath.getText().equals("") && !jTextFieldObjectName.getText().equals("")) {
this.jButtonExport.setText(Application.messages.getString("EXPORT_EXPORTING"));
this.jButtonExport.setEnabled(false);
model.export(jTextFieldObjectName.getText(), (String)jComboBoxLanguage.getSelectedItem(), jTextFieldObjectPath.getText(), jCheckBox.isValid());
}
}
/**
* When the export is done, enable the export button back
*/
public void propertyChange(PropertyChangeEvent arg0) {
if (arg0.getPropertyName() == "readyToExport") {
JOptionPane.showMessageDialog(null,Application.messages.getString("EXPORT_DONE_TEXT") + jTextFieldObjectPath.getText(),Application.messages.getString("EXPORT_DONE_TITLE"),JOptionPane.INFORMATION_MESSAGE);
this.jButtonExport.setText(Application.messages.getString("TITLE_EXPORT"));
this.jButtonExport.setEnabled(true);
}
}
}