Coverage Report - de.glossmaker.gloss.listener.CustomActionListener
 
Classes in this File Line Coverage Branch Coverage Complexity
CustomActionListener
0%
0/146
0%
0/74
3,056
 
 1  
 package de.glossmaker.gloss.listener;
 2  
 
 3  
 
 4  
 import guihelper.statusbar.EStatusBarMessageLevel;
 5  
 import guihelper.statusbar.StatusBarMessagePublisher;
 6  
 
 7  
 import java.awt.event.ActionEvent;
 8  
 import java.awt.event.ActionListener;
 9  
 import java.io.File;
 10  
 import java.util.Locale;
 11  
 import java.util.StringTokenizer;
 12  
 
 13  
 import javax.swing.JFileChooser;
 14  
 import javax.swing.JOptionPane;
 15  
 
 16  
 import org.apache.log4j.Logger;
 17  
 
 18  
 import de.glossmaker.gloss.datastructure.GlossItems;
 19  
 import de.glossmaker.gloss.datastructure.gui.Config;
 20  
 import de.glossmaker.gloss.language.Translation;
 21  
 import de.glossmaker.gloss.observer.GlossItemsChangePublisher;
 22  
 import de.glossmaker.gloss.undo.UndoGlossStack;
 23  
 import de.glossmaker.gui.gloss.GlossFileChooser;
 24  
 import de.glossmaker.gui.gloss.bibOptions.BibTeXOptionFrame;
 25  
 import de.glossmaker.gui.gloss.frames.InfoFrame;
 26  
 import de.glossmaker.gui.gloss.frames.MessageFrame;
 27  
 import de.glossmaker.gui.gloss.importGloss.ImportGlossFrame;
 28  
 import de.glossmaker.gui.gloss.main.BibTeXItemsFrame;
 29  
 
 30  
 /**
 31  
  * 
 32  
  * 
 33  
  * @author Markus Flingelli
 34  
  */
 35  
 public class CustomActionListener implements ActionListener {
 36  0
         private final static Logger mLogger = Logger.getLogger(CustomActionListener.class);
 37  0
         private static CustomActionListener mActionListener = null;
 38  
         
 39  0
         private GlossItemsChangePublisher mGlossItemsChangePublisher = null;
 40  0
         private GlossItems mGlossItems = null;
 41  0
         private Translation mTranslation = null;
 42  
         
 43  0
         private CustomActionListener(GlossItems items) {
 44  0
                 mGlossItems = items;
 45  0
                 mGlossItemsChangePublisher = GlossItemsChangePublisher.getInstance(items);
 46  0
                 mTranslation = Translation.getInstance();
 47  0
         }
 48  
         
 49  
         public static CustomActionListener getInstance(GlossItems items) {
 50  0
                 if (mActionListener == null) {
 51  0
                         mActionListener =  new CustomActionListener(items);
 52  
                 }
 53  
                 
 54  0
                 return mActionListener;
 55  
         }
 56  
         
 57  
         private void checkFileChanged() {
 58  0
                 if (mGlossItemsChangePublisher.isFileChanged()) {
 59  0
                         int result = JOptionPane.showConfirmDialog(null,
 60  
                                         mTranslation.getValue("confirmation.save_changes.text"),
 61  
                                         mTranslation.getValue("confirmation.save_changes.title"),
 62  
                                         JOptionPane.YES_NO_OPTION);
 63  0
                         if (result == JOptionPane.YES_OPTION) {
 64  0
                                 mGlossItemsChangePublisher.saveFile();
 65  
                         }        
 66  
                 }
 67  0
         }
 68  
         
 69  
         private void createNewFile() {
 70  0
                 checkFileChanged();
 71  0
                 mGlossItemsChangePublisher.clearContent();
 72  0
                 UndoGlossStack.getInstance().reset();
 73  0
         }
 74  
         
 75  
         private void saveFile() {
 76  0
                 if (mGlossItemsChangePublisher.isFileOpen()) {
 77  0
                         mGlossItemsChangePublisher.saveFile();
 78  0
                         UndoGlossStack.getInstance().reset();
 79  
                 }
 80  0
         }
 81  
         
 82  
         private void saveFileAs() {
 83  0
                 int result = JOptionPane.YES_OPTION;
 84  0
                 GlossFileChooser fc = new GlossFileChooser(
 85  
                                 mTranslation.getValue("filechooser.save_as.text"),
 86  
                                 mTranslation.getValue("filechooser.gloss.description.text"),
 87  
                                 "bib");
 88  0
                 String path = Config.getInstance().getLastSavedPath();
 89  0
                 if (path != null) {
 90  0
                         File f = new File(path);
 91  0
                         fc.setCurrentDirectory(f);
 92  
                 }
 93  0
                 if (fc.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {
 94  0
                         String fileName = fc.getSelectedFile().getAbsolutePath();
 95  0
                         if (fileName.contains(".") == false) {
 96  0
                                 fileName += ".bib";
 97  
                         }
 98  0
                         File f = new File (fileName);
 99  0
                         if (f.exists()) {
 100  0
                                 result = JOptionPane.showConfirmDialog(null, mTranslation.getValue("filechooser.override_file.text", fileName));                                
 101  
                         }
 102  0
                         if (result == JOptionPane.YES_OPTION) {
 103  0
                                 mGlossItemsChangePublisher.saveFileAs(fileName);
 104  0
                                 UndoGlossStack.getInstance().reset();
 105  0
                                 Config.getInstance().setLastSavedPath(fc.getCurrentDirectory().getAbsolutePath());
 106  
                         }
 107  
                 }
 108  0
         }
 109  
         
 110  
         private void openFile(String fileName) {
 111  0
                 mGlossItemsChangePublisher.openFile(fileName);
 112  0
                 Config.getInstance().setLastOpenedPath(fileName);
 113  0
         }
 114  
         
 115  
         private void openFile() {
 116  0
                 checkFileChanged();
 117  0
                 GlossFileChooser fc = new GlossFileChooser(
 118  
                                 mTranslation.getValue("filechooser.open.text"),
 119  
                                 mTranslation.getValue("filechooser.gloss.description.text"),
 120  
                                 "bib");
 121  0
                 String path = Config.getInstance().getLastOpenedPath();
 122  0
                 if (path != null) {
 123  0
                         File f = new File(path);
 124  0
                         fc.setCurrentDirectory(f);
 125  
                 }
 126  0
                 if (fc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
 127  0
                         openFile(fc.getSelectedFile().getAbsolutePath());
 128  
                 }
 129  0
         }
 130  
         
 131  
         private boolean getAction(ActionEvent event, EActionCommands actionCommand) {
 132  0
                 return event.getActionCommand().equals(actionCommand.toString());
 133  
         }
 134  
         
 135  
         private void terminateApplication() {
 136  0
                 if (mGlossItemsChangePublisher.isFileChanged() == true) {
 137  0
                         int result = JOptionPane.showConfirmDialog(null,
 138  
                                         mTranslation.getValue("confirmation.save_changes.text"),
 139  
                                         mTranslation.getValue("confirmation.save_changes.title"),
 140  
                                         JOptionPane.YES_NO_OPTION);
 141  0
                         if (result == JOptionPane.YES_OPTION) {
 142  0
                                 if (mGlossItemsChangePublisher.getFileName() == null) {
 143  0
                                         saveFileAs();
 144  
                                 } else {
 145  0
                                         mGlossItemsChangePublisher.saveFile();
 146  
                                 }
 147  
                         }
 148  
                 }
 149  0
                 Config.getInstance().setLanguage(Locale.getDefault().getLanguage());
 150  0
                 Config.getInstance().saveFile(Config.CONFIG_FILE);
 151  0
                 System.exit(0);                
 152  
                 
 153  0
         }
 154  
         
 155  
         private void showInfoDialog() {
 156  0
                 InfoFrame frame = new InfoFrame();
 157  0
                 frame.pack();
 158  0
                 frame.setVisible(true);
 159  0
         }
 160  
         
 161  
         private void checkGlossaryConsistency() {
 162  0
                 if (mGlossItems != null) {
 163  0
                         if (mGlossItems.isGlossReferencesConsistent() == false) {
 164  0
                                 StringTokenizer st = new StringTokenizer(mGlossItems.getConsistencyLog(), ".");
 165  0
                                 String html = "<html><body><p>";
 166  0
                                 while(st.hasMoreElements()) {
 167  0
                                         html += st.nextToken() + ".<br/>";
 168  
                                 }
 169  0
                                 html += "</p></body></html>";
 170  0
                                 MessageFrame info = new MessageFrame(html);
 171  0
                                 info.setVisible(true);
 172  0
                         } else {
 173  0
                                 StatusBarMessagePublisher.getInstance().publish(EStatusBarMessageLevel.INFO, 
 174  
                                                 mTranslation.getValue("statusbar.info.references_valid"));
 175  
                         }
 176  
                 }
 177  0
         }
 178  
         
 179  
         private void importGlossaryFile() {
 180  0
                 GlossFileChooser fc = new GlossFileChooser(
 181  
                                 mTranslation.getValue("filechooser.import.text"),
 182  
                                 mTranslation.getValue("filechooser.gloss.description.text"),
 183  
                                 "bib");
 184  0
                 String path = Config.getInstance().getLastImporteddPath();
 185  0
                 if (path != null) {
 186  0
                         File f = new File(path);
 187  0
                         fc.setCurrentDirectory(f);
 188  
                 }
 189  0
                 if (fc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
 190  
                         try {
 191  0
                                 ImportGlossFrame frame = new ImportGlossFrame(fc.getSelectedFile().getAbsolutePath(), mGlossItems);
 192  0
                                 frame.pack();
 193  0
                                 frame.setVisible(true);
 194  0
                                 Config.getInstance().setLastImportedPath(fc.getCurrentDirectory().getAbsolutePath());
 195  0
                         } catch (Exception e) {
 196  0
                                 mLogger.error(e);
 197  0
                         }
 198  
                 }
 199  0
         }
 200  
         
 201  
         private void reopenFile(String command) {
 202  0
                 checkFileChanged();
 203  0
                 String fileName = command.substring(EActionCommands.LAST_OPENED_FILE.toString().length(), command.length());
 204  0
                 File f = new File(fileName);
 205  0
                 if (f.exists()) {
 206  0
                         openFile(fileName);
 207  
                 }
 208  0
         }
 209  
         
 210  
         private void changeLanguage(String command) {
 211  0
                 String lang = command.substring(16, command.length());
 212  0
                 Locale.setDefault(new Locale(lang.toUpperCase()));
 213  0
                 mGlossItemsChangePublisher.languageChanged();
 214  0
         }
 215  
         
 216  
         private void chooseBibTeXFile() {
 217  0
                 new BibTeXOptionFrame();
 218  0
         }
 219  
         
 220  
         private void showBibTeXEditor() {
 221  0
                 new BibTeXItemsFrame(Config.getInstance().getBibTeXFile());
 222  0
         }
 223  
         
 224  
         @Override
 225  
         public void actionPerformed(ActionEvent event) {
 226  0
                 if (getAction(event, EActionCommands.CREATE_NEW_FILE)) {
 227  0
                         createNewFile();
 228  0
                 } else if (getAction(event, EActionCommands.SAVE_FILE)) {
 229  0
                         saveFile();
 230  0
                 } else if (getAction(event, EActionCommands.SAVE_FILE_AS)) {
 231  0
                         saveFileAs();
 232  0
                 } else if (getAction(event, EActionCommands.OPEN_FILE)) {
 233  0
                         openFile();
 234  0
                 } else if (event.getActionCommand().startsWith(EActionCommands.LAST_OPENED_FILE.toString())) {
 235  0
                         reopenFile(event.getActionCommand());
 236  0
                 } else if (getAction(event, EActionCommands.IMPORT_FILE)) {
 237  0
                         importGlossaryFile();
 238  0
                 } else if (getAction(event, EActionCommands.EXIT)) {
 239  0
                         terminateApplication();
 240  0
                 } else if (getAction(event, EActionCommands.UNDO)) {
 241  0
                         UndoListener.getInstance(mGlossItems).undo();
 242  0
                 } else if (getAction(event, EActionCommands.REDO)) {
 243  0
                         UndoListener.getInstance(mGlossItems).redo();
 244  0
                 } else if (getAction(event, EActionCommands.INFO_DIALOG)) {
 245  0
                         showInfoDialog();
 246  0
                 } else if (getAction(event, EActionCommands.GLOSS_CHECK_CONSISTENCY)) {
 247  0
                         checkGlossaryConsistency();
 248  0
                 } else if (event.getActionCommand().startsWith("CHANGE_LANGUAGE_")) {
 249  0
                         changeLanguage(event.getActionCommand());
 250  0
                 } else if (getAction(event, EActionCommands.CHOOSE_BIBTEXFILE)) {
 251  0
                         chooseBibTeXFile();
 252  0
                 } else if (getAction(event, EActionCommands.SHOW_BIB_EDITOR)) {
 253  0
                         showBibTeXEditor();
 254  0
                 } else if (getAction(event, EActionCommands.SHOW_TABLE_VIEW)) {
 255  0
                         mGlossItemsChangePublisher.showTableView();
 256  0
                 } else if (getAction(event, EActionCommands.SPECIAL_CHARS)) {
 257  0
                         boolean value = !Config.getInstance().isPrintBlankAndTabs();
 258  0
                         Config.getInstance().setPrintBlanksAndTabs(value);
 259  0
                         mGlossItemsChangePublisher.showSpecialCharacters(value);
 260  
                 }
 261  0
         }
 262  
 }