package fenetres.create_act; import java.awt.event.ActionEvent; import java.awt.event.KeyEvent; import java.io.File; import java.io.IOException; import java.util.regex.Matcher; import java.util.regex.Pattern; import javax.swing.AbstractAction; import javax.swing.Action; import javax.swing.ImageIcon; import javax.swing.JFileChooser; import MEPTL.commandes; import MEPTL.verificationFichierAnalyse; import cXML.Run; public class actSaveAs extends AbstractAction{ /** * */ private static final long serialVersionUID = 1L; { putValue( Action.NAME, "Enregistrer sous..." ); putValue( Action.SMALL_ICON, new ImageIcon(getClass().getResource("/save_as.png") )); putValue( Action.MNEMONIC_KEY, KeyEvent.VK_A ); putValue( Action.SHORT_DESCRIPTION, "Enregistrer sous..." ); } @Override public void actionPerformed(ActionEvent e) { JFileChooser fileChooser = new JFileChooser(); fileChooser.setDialogTitle("Enregistrer sous..."); File file = null; if(!commandes.nameSujet.isEmpty()) { file = new File(commandes.PathFilenameAnalysis ); //+ "\\" + utils.filename + ".xml" System.out.println("commandes.PathFilenameAnalysis="+commandes.PathFilenameAnalysis); } fileChooser.setCurrentDirectory(file); int userSelection = fileChooser.showSaveDialog(null); if (userSelection == JFileChooser.APPROVE_OPTION) { File fileToSave = fileChooser.getSelectedFile(); try { verificationFichierAnalyse.MiseAJourHashNomFichierAnalyse(fileToSave.getName()); if(commandes.os.contains("Win")) { Run.ecritureNodeEnXML(commandes.sujet, fileToSave.getName(), fileToSave.getPath().substring(0,fileToSave.getPath().lastIndexOf("\\")),Run.TypeFile.Sujet ); }else { Run.ecritureNodeEnXML(commandes.sujet, fileToSave.getName(), fileToSave.getPath().substring(0,fileToSave.getPath().lastIndexOf("/")),Run.TypeFile.Sujet ); } commandes.nameSujet = fileToSave.getName(); if(commandes.os.contains("Win")) { commandes.PathFilenameAnalysis = fileToSave.getPath().substring(0,fileToSave.getPath().lastIndexOf("\\")); }else{ commandes.PathFilenameAnalysis = fileToSave.getPath().substring(0,fileToSave.getPath().lastIndexOf("/")); } Pattern p = Pattern.compile("[.xml]\\b"); Matcher m = p.matcher(commandes.nameSujet); if(m.find()) { file = new File(commandes.PathFilenameAnalysis + "\\" + commandes.nameSujet); fenetres.create.getTextNodeSelect().setText("Le fichier \"" + commandes.nameSujet + " a été renregistré.\n\nIl se trouve dans le dossier " + commandes.PathFilenameAnalysis); }else { file = new File(commandes.PathFilenameAnalysis + "\\" + commandes.nameSujet + ".xml"); fenetres.create.getTextNodeSelect().setText("Le fichier \"" + commandes.nameSujet + ".xml\" a été renregistré.\n\nIl se trouve dans le dossier " + commandes.PathFilenameAnalysis); } } catch (IOException e1) { e1.printStackTrace(); } catch (CloneNotSupportedException e1) { e1.printStackTrace(); } System.out.println("Save as file: " + fileToSave.getAbsolutePath()); } System.out.println( "Save as" ); } }