[Bprocessor-commit] gui/src/net/sourceforge/bprocessor/gui/actions FileActionListener.java, NONE, 1
Status: Pre-Alpha
Brought to you by:
henryml
|
From: rimestad <rim...@us...> - 2006-11-21 17:29:51
|
Update of /cvsroot/bprocessor/gui/src/net/sourceforge/bprocessor/gui/actions In directory sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv3787/src/net/sourceforge/bprocessor/gui/actions Modified Files: FileNewActionListener.java FileExitActionListener.java FileLoadActionListener.java FileImportActionListener.java FileCloseActionListener.java FileSaveAsActionListener.java Added Files: FileActionListener.java Log Message: the 6 repitition of the same save code have bee refactored into FileActionListener which is abstract and the 6 old files now extends instead implementing additionalAction which does the individual job, gives lesser coupling and heigher cohesion Index: FileLoadActionListener.java =================================================================== RCS file: /cvsroot/bprocessor/gui/src/net/sourceforge/bprocessor/gui/actions/FileLoadActionListener.java,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** FileLoadActionListener.java 21 Mar 2006 12:24:13 -0000 1.6 --- FileLoadActionListener.java 21 Nov 2006 17:29:34 -0000 1.7 *************** *** 10,18 **** import java.awt.event.ActionEvent; - import java.awt.event.ActionListener; import java.io.File; import javax.swing.JFileChooser; - import javax.swing.JOptionPane; import org.apache.log4j.Logger; --- 10,16 ---- *************** *** 21,25 **** * The file->load action listener */ ! public class FileLoadActionListener implements ActionListener { /** The logger */ private static Logger log = Logger.getLogger(FileLoadActionListener.class); --- 19,23 ---- * The file->load action listener */ ! public class FileLoadActionListener extends FileActionListener { /** The logger */ private static Logger log = Logger.getLogger(FileLoadActionListener.class); *************** *** 30,117 **** public FileLoadActionListener() { } ! /** ! * Action performed ! * @param e The action event */ ! public void actionPerformed(ActionEvent e) { ! ! if (Project.getInstance().isDirty()) { ! int n = JOptionPane.showConfirmDialog( ! null, ! "Do you want to save the changes?", ! "Projekt has been changed", ! JOptionPane.YES_NO_CANCEL_OPTION); ! if (n == 0) { ! JFileChooser chooser = new JFileChooser(Project.getInstance().getDefaultPath()); ! chooser.addChoosableFileFilter(new MyFileFilter()); ! ! int state = chooser.showSaveDialog(null); ! File file = chooser.getSelectedFile(); ! ! if (file != null && ! state == JFileChooser.APPROVE_OPTION) { ! try { ! Project.getInstance().setDefaultPath(file.getAbsolutePath()); ! if (file.getName().lastIndexOf('.') != -1) { ! Project.getInstance().saveAs(file); ! } else { ! Project.getInstance().saveAs(new File(file.getCanonicalFile() + ".bp")); ! } ! ! JFileChooser loadChooser = new JFileChooser(Project.getInstance().getDefaultPath()); ! loadChooser.addChoosableFileFilter(new MyFileFilter()); ! int lstate = loadChooser.showOpenDialog(null); ! File lfile = loadChooser.getSelectedFile(); ! if (lfile != null && ! lstate == JFileChooser.APPROVE_OPTION) { ! try { ! Project.getInstance().load(lfile); ! } catch (Exception ex) { ! log.error("Could not open file: " + lfile, ex); ! } ! } ! } catch (Exception ex) { ! log.error("Could not save to file: " + file, ex); ! } ! } ! } ! ! if (n == 1) { ! JFileChooser loadChooser = new JFileChooser(Project.getInstance().getDefaultPath()); ! loadChooser.addChoosableFileFilter(new MyFileFilter()); ! int lstate = loadChooser.showOpenDialog(null); ! File lfile = loadChooser.getSelectedFile(); ! if (lfile != null && ! lstate == JFileChooser.APPROVE_OPTION) { ! try { ! Project.getInstance().load(lfile); ! } catch (Exception ex) { ! log.error("Could not open file: " + lfile, ex); ! } ! } ! } ! ! ! } else { ! ! JFileChooser loadChooser = new JFileChooser(Project.getInstance().getDefaultPath()); ! loadChooser.addChoosableFileFilter(new MyFileFilter()); ! int lstate = loadChooser.showOpenDialog(null); ! File lfile = loadChooser.getSelectedFile(); ! if (lfile != null && ! lstate == JFileChooser.APPROVE_OPTION) { ! try { ! Project.getInstance().load(lfile); ! } catch (Exception ex) { ! log.error("Could not open file: " + lfile, ex); ! } } } - - - - - } } --- 28,49 ---- public FileLoadActionListener() { } ! /** ! * The action to perform after the standard file action ! * @param e the original event */ ! public void additionalAction(ActionEvent e) { ! JFileChooser loadChooser = new JFileChooser(Project.getInstance().getDefaultPath()); ! loadChooser.addChoosableFileFilter(new MyFileFilter()); ! int lstate = loadChooser.showOpenDialog(null); ! File lfile = loadChooser.getSelectedFile(); ! if (lfile != null && ! lstate == JFileChooser.APPROVE_OPTION) { ! try { ! Project.getInstance().load(lfile); ! } catch (Exception ex) { ! log.error("Could not open file: " + lfile, ex); } } } } Index: FileExitActionListener.java =================================================================== RCS file: /cvsroot/bprocessor/gui/src/net/sourceforge/bprocessor/gui/actions/FileExitActionListener.java,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** FileExitActionListener.java 21 Mar 2006 12:24:13 -0000 1.6 --- FileExitActionListener.java 21 Nov 2006 17:29:34 -0000 1.7 *************** *** 8,28 **** import java.awt.event.ActionEvent; - import java.awt.event.ActionListener; - import java.io.File; - - import javax.swing.JFileChooser; - import javax.swing.JOptionPane; - - import net.sourceforge.bprocessor.model.Project; - - import org.apache.log4j.Logger; /** * The file->exit action listener */ ! public class FileExitActionListener implements ActionListener { ! /** The logger */ ! private static Logger log = Logger.getLogger(FileExitActionListener.class); ! /** * FileExitActionListener --- 8,16 ---- import java.awt.event.ActionEvent; /** * The file->exit action listener */ ! public class FileExitActionListener extends FileActionListener { /** * FileExitActionListener *************** *** 32,75 **** /** ! * Action performed ! * @param e The action event */ ! public void actionPerformed(ActionEvent e) { ! if (Project.getInstance().isDirty()) { ! int n = JOptionPane.showConfirmDialog( ! null, ! "Do you want to save the changes?", ! "Project has been changed", ! JOptionPane.YES_NO_CANCEL_OPTION); ! if (n == 0) { ! if (Project.getInstance().getSavePath() == null) { ! JFileChooser chooser = new JFileChooser(Project.getInstance().getDefaultPath()); ! chooser.addChoosableFileFilter(new MyFileFilter()); ! int state = chooser.showSaveDialog(null); ! File file = chooser.getSelectedFile(); ! if (file != null && ! state == JFileChooser.APPROVE_OPTION) { ! try { ! Project.getInstance().saveAs(file); ! System.exit(0); ! } catch (Exception ex) { ! log.error("Could not save to file: " + file, ex); ! } ! } ! } else { ! try { ! Project.getInstance().save(); ! System.exit(0); ! } catch (Exception ex) { ! log.error("Could not save to file: " + ! new File(Project.getInstance().getSavePath()), ex); ! } ! } ! } else if (n == 1) { ! System.exit(0); ! } ! } else { ! System.exit(0); ! } } } --- 20,28 ---- /** ! * The action to perform after the standard file action ! * @param e the original event */ ! public void additionalAction(ActionEvent e) { ! System.exit(0); } } Index: FileCloseActionListener.java =================================================================== RCS file: /cvsroot/bprocessor/gui/src/net/sourceforge/bprocessor/gui/actions/FileCloseActionListener.java,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** FileCloseActionListener.java 21 Mar 2006 12:24:13 -0000 1.6 --- FileCloseActionListener.java 21 Nov 2006 17:29:34 -0000 1.7 *************** *** 9,19 **** import java.awt.event.ActionEvent; - import java.awt.event.ActionListener; - import java.io.File; - - import javax.swing.JFileChooser; - import javax.swing.JOptionPane; - - import org.apache.log4j.Logger; import net.sourceforge.bprocessor.model.Project; --- 9,12 ---- *************** *** 23,31 **** */ ! public class FileCloseActionListener implements ActionListener { ! ! /** The logger */ ! private static Logger log = Logger.getLogger(FileCloseActionListener.class); ! /** * Constructs the FileCloseActionListner --- 16,20 ---- */ ! public class FileCloseActionListener extends FileActionListener { /** * Constructs the FileCloseActionListner *************** *** 36,87 **** /** ! * Clears the project ! * @param event The event */ ! public void actionPerformed(ActionEvent event) { ! if (Project.getInstance().isDirty()) { ! int n = JOptionPane.showConfirmDialog( ! null, ! "Do you want to save the changes?", ! "Project has been changed", ! JOptionPane.YES_NO_CANCEL_OPTION); ! if (n == 0) { ! if (Project.getInstance().getSavePath() == null) { ! String path = Project.getInstance().getDefaultPath(); ! JFileChooser chooser = new JFileChooser(path); ! chooser.addChoosableFileFilter(new MyFileFilter()); ! int state = chooser.showSaveDialog(null); ! File file = chooser.getSelectedFile(); ! if (file != null && ! state == JFileChooser.APPROVE_OPTION) { ! try { ! ! if (file.getName().lastIndexOf('.') != -1) { ! Project.getInstance().saveAs(file); ! } else { ! Project.getInstance().saveAs(new File(file.getCanonicalFile() + ".bp")); ! } ! Project.getInstance().close(); ! } catch (Exception ex) { ! log.error("Could not save to file: " + file, ex); ! } ! } ! } else { ! try { ! Project.getInstance().save(); ! Project.getInstance().close(); ! } catch (Exception ex) { ! log.error("Could not export to file: " + ! new File(Project.getInstance().getSavePath()), ex); ! } ! } ! } ! ! if (n == 1) { ! Project.getInstance().close(); ! } ! } else { ! Project.getInstance().close(); ! } } } --- 25,33 ---- /** ! * The action to perform after the standard file action ! * @param e the original event */ ! public void additionalAction(ActionEvent e) { ! Project.getInstance().close(); } } Index: FileNewActionListener.java =================================================================== RCS file: /cvsroot/bprocessor/gui/src/net/sourceforge/bprocessor/gui/actions/FileNewActionListener.java,v retrieving revision 1.8 retrieving revision 1.9 diff -C2 -d -r1.8 -r1.9 *** FileNewActionListener.java 13 Oct 2006 15:47:34 -0000 1.8 --- FileNewActionListener.java 21 Nov 2006 17:29:34 -0000 1.9 *************** *** 9,19 **** import java.awt.event.ActionEvent; - import java.awt.event.ActionListener; - import java.io.File; - - import javax.swing.JFileChooser; - import javax.swing.JOptionPane; - - import org.apache.log4j.Logger; import net.sourceforge.bprocessor.model.Project; --- 9,12 ---- *************** *** 23,30 **** */ ! public class FileNewActionListener implements ActionListener { ! ! /** The logger */ ! private static Logger log = Logger.getLogger(FileCloseActionListener.class); /** --- 16,20 ---- */ ! public class FileNewActionListener extends FileActionListener { /** *************** *** 36,86 **** /** ! * Clears the project ! * @param event The event */ ! public void actionPerformed(ActionEvent event) { ! ! if (Project.getInstance().isDirty()) { ! int n = JOptionPane.showConfirmDialog( ! null, ! "Do you want to save the changes?", ! "Projekt has been changed", ! JOptionPane.YES_NO_CANCEL_OPTION); ! if (n == JOptionPane.YES_OPTION) { ! if (Project.getInstance().getSavePath() == null) { ! JFileChooser chooser = new JFileChooser(Project.getInstance().getDefaultPath()); ! chooser.addChoosableFileFilter(new MyFileFilter()); ! int state = chooser.showSaveDialog(null); ! File file = chooser.getSelectedFile(); ! if (file != null && ! state == JFileChooser.APPROVE_OPTION) { ! try { ! if (file.getName().lastIndexOf('.') != -1) { ! Project.getInstance().saveAs(file); ! } else { ! Project.getInstance().saveAs(new File(file.getCanonicalFile() + ".bp")); ! } ! Project.getInstance().close(); ! } catch (Exception ex) { ! log.error("Could not export to file: " + file, ex); ! } ! } ! } else { ! try { ! Project.getInstance().save(); ! Project.getInstance().close(); ! } catch (Exception ex) { ! log.error("Could not export to file: " + ! new File(Project.getInstance().getSavePath()), ex); ! } ! } ! } ! if (n == JOptionPane.NO_OPTION) { ! Project.getInstance().close(); ! } ! } else { ! Project.getInstance().close(); ! } } - } --- 26,35 ---- /** ! * The action to perform after the standard file action ! * @param e the original event */ ! public void additionalAction(ActionEvent e) { ! Project.getInstance().close(); ! Project.getInstance().changed(Project.getInstance()); } } Index: FileSaveAsActionListener.java =================================================================== RCS file: /cvsroot/bprocessor/gui/src/net/sourceforge/bprocessor/gui/actions/FileSaveAsActionListener.java,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** FileSaveAsActionListener.java 21 Mar 2006 12:24:13 -0000 1.4 --- FileSaveAsActionListener.java 21 Nov 2006 17:29:34 -0000 1.5 *************** *** 49,53 **** } } catch (Exception ex) { ! log.error("Could not export to file: " + file, ex); } } --- 49,53 ---- } } catch (Exception ex) { ! log.error("Could not save to file: " + file, ex); } } Index: FileImportActionListener.java =================================================================== RCS file: /cvsroot/bprocessor/gui/src/net/sourceforge/bprocessor/gui/actions/FileImportActionListener.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** FileImportActionListener.java 5 Apr 2006 09:35:50 -0000 1.2 --- FileImportActionListener.java 21 Nov 2006 17:29:34 -0000 1.3 *************** *** 6,16 **** //--------------------------------------------------------------------------------- package net.sourceforge.bprocessor.gui.actions; - - import net.sourceforge.bprocessor.model.Persistence; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; - import java.io.File; - - import javax.swing.JFileChooser; import org.apache.log4j.Logger; --- 6,11 ---- *************** *** 34,49 **** */ public void actionPerformed(ActionEvent e) { ! JFileChooser chooser = new JFileChooser(); ! int state = chooser.showOpenDialog(null); ! File file = chooser.getSelectedFile(); ! ! if (file != null && ! state == JFileChooser.APPROVE_OPTION) { ! try { ! Persistence.load(file); ! } catch (Exception ex) { ! log.error("Could not import file: " + file, ex); ! } ! } } } --- 29,33 ---- */ public void actionPerformed(ActionEvent e) { ! //TODO implement me } } --- NEW FILE: FileActionListener.java --- //--------------------------------------------------------------------------------- // $Id: FileActionListener.java,v 1.1 2006/11/21 17:29:34 rimestad Exp $ // // Copyright (c) 2005 The BProcessor Team (http://bprocessor.sourceforge.net) // Released under the Lesser GNU Public License v2.1 //--------------------------------------------------------------------------------- package net.sourceforge.bprocessor.gui.actions; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import javax.swing.JFileChooser; import javax.swing.JOptionPane; import org.apache.log4j.Logger; import net.sourceforge.bprocessor.model.Project; /** * The FileCloseActionListener */ public abstract class FileActionListener implements ActionListener { /** The logger */ private static Logger log = Logger.getLogger(FileActionListener.class); /** * Constructs the FileCloseActionListner */ public FileActionListener() { super(); } /** * Clears the project * @param event The event */ public void actionPerformed(ActionEvent event) { if (Project.getInstance().isDirty()) { int n = JOptionPane.showConfirmDialog( null, "Do you want to save the changes?", "Project has been changed", JOptionPane.YES_NO_CANCEL_OPTION); if (n == 0) { if (Project.getInstance().getSavePath() == null) { String path = Project.getInstance().getDefaultPath(); JFileChooser chooser = new JFileChooser(path); chooser.addChoosableFileFilter(new MyFileFilter()); int state = chooser.showSaveDialog(null); File file = chooser.getSelectedFile(); if (file != null && state == JFileChooser.APPROVE_OPTION) { try { if (file.getName().lastIndexOf('.') != -1) { Project.getInstance().saveAs(file); } else { Project.getInstance().saveAs(new File(file.getCanonicalFile() + ".bp")); } Project.getInstance().close(); } catch (Exception ex) { log.error("Could not save to file: " + file, ex); } } } else { try { Project.getInstance().save(); Project.getInstance().close(); } catch (Exception ex) { log.error("Could not save to file: " + new File(Project.getInstance().getSavePath()), ex); } } } if (n == 1) { additionalAction(event); } } else { additionalAction(event); } } /** * The action to perform when the file is saved or save is cancled * @param e The action event */ public abstract void additionalAction(ActionEvent e); } |