View Javadoc

1   package es.inteco.eni.swing;
2   
3   import java.awt.event.ActionEvent;
4   import java.awt.event.ActionListener;
5   import java.io.File;
6   import java.io.IOException;
7   
8   import javax.swing.JFileChooser;
9   import javax.swing.JOptionPane;
10  
11  import org.apache.log4j.Logger;
12  
13  import es.inteco.eni.commons.MessagesHelper;
14  import es.inteco.eni.utils.ExceptionUtils;
15  import es.inteco.eni.utils.FileUtils;
16  
17  /**
18   * Action listener para gestionar el click en el botón de guardar XML
19   * 
20   * 
21   *
22   */
23  public class SaveXmlButtonActionListener implements ActionListener{
24  	/**
25  	 * Log
26  	 */
27  	private static Logger log = Logger.getLogger(SaveXmlButtonActionListener.class);
28  	/**
29  	 * Ventana del validador
30  	 */
31  	private ENIValidator eniValidator;
32  
33  	/**
34  	 * Constructor
35  	 * @param eniValidator
36  	 */
37  	public SaveXmlButtonActionListener(ENIValidator eniValidator){
38  		this.eniValidator = eniValidator;
39  	}
40  
41  	/**
42  	 * 
43  	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
44  	 */
45  	@Override
46  	public void actionPerformed(ActionEvent e) {
47  		try{
48  			//Si se intenta guardar modificaciones en un fichero ya abierto
49  			if(eniValidator.getFile()!=null){
50  				FileUtils.writeFile (eniValidator.getFile(), eniValidator.getXmlContentTextarea().getText());
51  			}else{
52  				SaveXmlButtonActionListener.showAndSaveFileChooser(eniValidator);
53  			}
54  		}catch (IOException ioe) {
55  			log.error(ExceptionUtils.stackTraceToString(ioe));
56  			JOptionPane.showMessageDialog(eniValidator, 
57  						MessagesHelper.getText("eni.save.xml.error.message"),
58  						MessagesHelper.getText("eni.save.xml.error.title"),
59  						JOptionPane.ERROR_MESSAGE); 
60  		}
61  	} 
62  	
63  	/**
64  	 * Muetra el dialogo para seleccionar el fichero y guarda el contenido del area de texto en el fichero
65  	 * @param eniValidator Ventana del validador
66  	 * @throws IOException
67  	 */
68  	public static void showAndSaveFileChooser(ENIValidator eniValidator) throws IOException{
69  		JFileChooser fc = new JFileChooser ();
70  		//Directorio actual
71  		fc.setCurrentDirectory(eniValidator.getParentFile());
72  		//Filtra los ficheos XML
73  		EniFilter xmlFilter = new EniFilter(FileUtils.XML_EXTENSION);
74  		fc.setFileFilter (xmlFilter);
75  
76  		
77  		//Estable el nombre por defecto
78  		File fFile = new File (MessagesHelper.getText("eni.default.xml.file.name"));
79  		fc.setSelectedFile (fFile);
80  
81  		//Abre el dialogo para seleccionar el fichero
82  		int result = fc.showSaveDialog(eniValidator);
83  
84  		if (result == JFileChooser.APPROVE_OPTION) {
85  			fFile = fc.getSelectedFile ();
86  			if (fFile.exists ()) {
87  				int response = JOptionPane.showConfirmDialog (null,
88  						MessagesHelper.getText("eni.overwrite.file.warning.message"),
89  						MessagesHelper.getText("eni.overwrite.file.warning.title"),
90  						JOptionPane.OK_CANCEL_OPTION,
91  						JOptionPane.QUESTION_MESSAGE);
92  				if (response == JOptionPane.OK_OPTION){
93  					FileUtils.writeFile (fFile, eniValidator.getXmlContentTextarea().getText());
94  				}
95  			}else{
96  				FileUtils.writeFile (fFile, eniValidator.getXmlContentTextarea().getText());
97  			}
98  			eniValidator.setFile(fFile);
99  			eniValidator.setTitle(MessagesHelper.getText("eni.windows.title") + " - " + fFile.getAbsolutePath());
100 			eniValidator.setParentFile(fFile.getParentFile());
101 		} 
102 	}
103 }