Coverage Report - de.glossmaker.gui.gloss.bars.MenuBar
 
Classes in this File Line Coverage Branch Coverage Complexity
MenuBar
0%
0/248
0%
0/44
2,917
MenuBar$1
0%
0/10
0%
0/2
2,917
 
 1  
 package de.glossmaker.gui.gloss.bars;
 2  
 
 3  
 import java.awt.event.ActionListener;
 4  
 import java.awt.event.InputEvent;
 5  
 import java.awt.event.KeyEvent;
 6  
 import java.beans.PropertyChangeEvent;
 7  
 import java.beans.PropertyChangeListener;
 8  
 import java.util.Locale;
 9  
 
 10  
 import javax.help.CSH;
 11  
 import javax.swing.ButtonGroup;
 12  
 import javax.swing.JCheckBoxMenuItem;
 13  
 import javax.swing.JMenu;
 14  
 import javax.swing.JMenuBar;
 15  
 import javax.swing.JMenuItem;
 16  
 import javax.swing.JRadioButtonMenuItem;
 17  
 import javax.swing.JSeparator;
 18  
 import javax.swing.KeyStroke;
 19  
 import javax.swing.event.ChangeEvent;
 20  
 import javax.swing.event.ChangeListener;
 21  
 
 22  
 import de.glossmaker.gloss.datastructure.GlossItems;
 23  
 import de.glossmaker.gloss.datastructure.gui.Config;
 24  
 import de.glossmaker.gloss.language.Translation;
 25  
 import de.glossmaker.gloss.language.TranslationFinder;
 26  
 import de.glossmaker.gloss.listener.CustomActionListener;
 27  
 import de.glossmaker.gloss.listener.EActionCommands;
 28  
 import de.glossmaker.gloss.observer.GlossItemsChangePublisher;
 29  
 import de.glossmaker.gloss.undo.UndoGlossStack;
 30  
 import de.glossmaker.gui.gloss.help.HelpSystem;
 31  
 
 32  
 /**
 33  
  * This class implements the main menu bar for the application. 
 34  
  * 
 35  
  * @author Markus Flingelli
 36  
  */
 37  0
 public class MenuBar extends JMenuBar implements PropertyChangeListener {
 38  
         private static final long serialVersionUID = -4167448705066476424L;
 39  0
         private JMenu mFile = null;
 40  0
         private JMenuItem mFileOpen = null;
 41  0
         private JMenu mFileLastOpened = null;
 42  0
         private JMenuItem mFileNew = null;
 43  0
         private JMenuItem mFileSave = null;
 44  0
         private JMenuItem mFileSaveAs = null;
 45  0
         private JMenuItem mImportFile = null;
 46  0
         private JMenuItem mFileExit = null;
 47  
         
 48  0
         private JMenu mEdit = null;
 49  0
         private JMenuItem mUndo = null;
 50  0
         private JMenuItem mRedo = null;
 51  
         
 52  0
         private JMenu mView = null;
 53  0
         private JCheckBoxMenuItem mSpecialChars = null;
 54  
         
 55  0
         private JMenu mRefactor = null;
 56  0
         private JMenuItem mCheckGlossConsistency = null;
 57  
 
 58  0
         private JMenu mOptions = null;
 59  0
         private JMenu mLanguage = null;
 60  0
         private JMenuItem mBibTeX = null;
 61  
         
 62  0
         private JMenu mHelp = null;
 63  0
         private JMenuItem mHelpDialog = null;
 64  0
         private JMenuItem mInfo = null;
 65  
         
 66  0
         private ActionListener mActionListener = null;
 67  0
         private GlossItemsChangePublisher mGlossItemsChangePublisher = null;
 68  0
         private Translation mTranslation = null;
 69  
         
 70  0
         public MenuBar(GlossItems items) {
 71  0
                 mActionListener = CustomActionListener.getInstance(items);
 72  0
                 mGlossItemsChangePublisher = GlossItemsChangePublisher.getInstance(items);
 73  0
                 mGlossItemsChangePublisher.addPropertyChangeListener(this);
 74  0
                 mTranslation = Translation.getInstance();
 75  0
                 initialize();
 76  0
         }
 77  
 
 78  
         private void initialize() {
 79  0
                 this.add(getFileMenu());
 80  0
                 this.add(getEditMenu());
 81  0
                 this.add(getViewMenu());
 82  0
                 this.add(getRefactorMenu());
 83  0
                 this.add(getOptionsMenu());
 84  0
                 this.add(getCustomHelpMenu());
 85  0
                 changeLanguage();
 86  0
         }
 87  
         
 88  
         private JMenu getFileMenu() {
 89  0
                 if (mFile == null) {
 90  0
                         mFile = new JMenu(mTranslation.getValue("menu.file.text"));
 91  0
                         mFile.addChangeListener(new HelpItemChangeListener("section.file"));
 92  0
                         mFile.setName("File");
 93  0
                         mFileNew = new JMenuItem(mTranslation.getValue("menu.file.new.text"));
 94  0
                         mFileNew.setActionCommand(EActionCommands.CREATE_NEW_FILE.toString());
 95  0
                         mFileNew.addActionListener(mActionListener);
 96  0
                         mFileNew.setAccelerator(KeyStroke.getKeyStroke('N', InputEvent.CTRL_MASK));
 97  0
                         mFileNew.addChangeListener(new HelpItemChangeListener("section.new.file"));
 98  0
                         mFile.add(mFileNew);
 99  
                         
 100  0
                         mFileOpen = new JMenuItem(mTranslation.getValue("menu.file.open.text"));
 101  0
                         mFileOpen.setActionCommand(EActionCommands.OPEN_FILE.toString());
 102  0
                         mFileOpen.addActionListener(mActionListener);
 103  0
                         mFileOpen.setAccelerator(KeyStroke.getKeyStroke('O', InputEvent.CTRL_MASK));
 104  0
                         mFileOpen.addChangeListener(new HelpItemChangeListener("section.open.file"));
 105  0
                         mFileOpen.setName("Open");
 106  0
                         mFile.add(mFileOpen);
 107  
                         
 108  0
                         mFileLastOpened = new JMenu();
 109  0
                         mFileLastOpened.addChangeListener(new HelpItemChangeListener("section.reopen.file"));
 110  0
                         mFileLastOpened.addChangeListener(new ChangeListener() {
 111  
                                 
 112  
                                 @Override
 113  
                                 public void stateChanged(ChangeEvent event) {
 114  0
                                         mFileLastOpened.removeAll();
 115  0
                                         for(int i = 0; i < Config.getInstance().getLastOpenedFilesCount(); i++) {
 116  0
                                                 String fileName = Config.getInstance().getLastOpenedFile(i);
 117  0
                                                 JMenuItem item = new JMenuItem(fileName);
 118  0
                                                 item.setActionCommand(EActionCommands.LAST_OPENED_FILE.toString() + fileName);
 119  0
                                                 item.addActionListener(mActionListener);
 120  0
                                                 item.addChangeListener(new HelpItemChangeListener("section.reopen.file"));
 121  0
                                                 mFileLastOpened.add(item);
 122  
                                         }
 123  0
                                 }
 124  
                         });
 125  0
                         if (Config.getInstance().getLastOpenedFilesCount() < 1) {
 126  0
                                 mFileLastOpened.setEnabled(false);
 127  
                         } else {
 128  0
                                 mFileLastOpened.setEnabled(true);
 129  
                         }
 130  0
                         mFileLastOpened.setName("Reopen");
 131  0
                         mFile.add(mFileLastOpened);
 132  
                         
 133  
                         
 134  0
                         mFileSave = new JMenuItem(mTranslation.getValue("menu.file.save.text"));
 135  0
                         mFileSave.setActionCommand(EActionCommands.SAVE_FILE.toString());
 136  0
                         mFileSave.addActionListener(mActionListener);
 137  0
                         mFileSave.setAccelerator(KeyStroke.getKeyStroke('S', InputEvent.CTRL_MASK));
 138  0
                         mFileSave.setEnabled(false);
 139  0
                         mFileSave.addChangeListener(new HelpItemChangeListener("section.save.file"));
 140  0
                         mFileSave.setName("Save");
 141  0
                         mFile.add(mFileSave);
 142  
                         
 143  0
                         mFileSaveAs = new JMenuItem(mTranslation.getValue("menu.file.save_as.text"));
 144  0
                         mFileSaveAs.setActionCommand(EActionCommands.SAVE_FILE_AS.toString());
 145  0
                         mFileSaveAs.addActionListener(mActionListener);
 146  0
                         mFileSaveAs.setEnabled(false);
 147  0
                         mFileSaveAs.addChangeListener(new HelpItemChangeListener("section.save_as.file"));
 148  0
                         mFileSaveAs.setName("Save as");
 149  0
                         mFile.add(mFileSaveAs);
 150  
                         
 151  0
                         JSeparator separator = new JSeparator();
 152  0
                         mFile.add(separator);
 153  
                         
 154  0
                         mImportFile = new JMenuItem(mTranslation.getValue("menu.file.import.text"));
 155  0
                         mImportFile.setActionCommand(EActionCommands.IMPORT_FILE.toString());
 156  0
                         mImportFile.setEnabled(false);
 157  0
                         mImportFile.addActionListener(mActionListener);
 158  0
                         mImportFile.addChangeListener(new HelpItemChangeListener("section.import.glossary_file"));
 159  0
                         mImportFile.setName("Import file");
 160  0
                         mFile.add(mImportFile);
 161  
                         
 162  0
                         mFileExit = new JMenuItem(mTranslation.getValue("menu.file.exit.text"));
 163  0
                         mFileExit.setActionCommand(EActionCommands.EXIT.toString());
 164  0
                         mFileExit.addActionListener(mActionListener);
 165  0
                         mFileExit.addChangeListener(new HelpItemChangeListener("section.exit"));
 166  0
                         mFileExit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F4, InputEvent.ALT_DOWN_MASK));
 167  0
                         mFileExit.setName("Exit");
 168  0
                         mFile.add(new JSeparator());
 169  0
                         mFile.add(mFileExit);
 170  
                 }
 171  
                 
 172  0
                 return mFile;
 173  
         }
 174  
         
 175  
         private JMenu getEditMenu() {
 176  0
                 if (mEdit == null) {
 177  0
                         mEdit = new JMenu(mTranslation.getValue("menu.edit.text"));
 178  0
                         mEdit.addChangeListener(new HelpItemChangeListener("section.edit"));
 179  
                         
 180  0
                         mUndo = new JMenuItem(mTranslation.getValue("menu.edit.undo.text"));
 181  0
                         mUndo.setActionCommand(EActionCommands.UNDO.toString());
 182  0
                         mUndo.addActionListener(mActionListener);
 183  0
                         mUndo.setAccelerator(KeyStroke.getKeyStroke('Z', InputEvent.CTRL_MASK));
 184  0
                         mUndo.setEnabled(false);
 185  0
                         mUndo.addChangeListener(new HelpItemChangeListener("section.undo"));
 186  0
                         mEdit.add(mUndo);
 187  
                         
 188  0
                         mRedo = new JMenuItem(mTranslation.getValue("menu.edit.redo.text"));
 189  0
                         mRedo.setActionCommand(EActionCommands.REDO.toString());
 190  0
                         mRedo.addActionListener(mActionListener);
 191  0
                         mRedo.setAccelerator(KeyStroke.getKeyStroke('Y', InputEvent.CTRL_MASK));
 192  0
                         mRedo.setEnabled(false);
 193  0
                         mRedo.addChangeListener(new HelpItemChangeListener("section.redo"));
 194  0
                         mEdit.add(mRedo);
 195  
                 }
 196  
                 
 197  0
                 return mEdit;
 198  
         }
 199  
         
 200  
         private JMenu getViewMenu() {
 201  0
                 if (mView == null) {
 202  0
                         mView = new JMenu(mTranslation.getValue("menu.view.text"));
 203  0
                         mView.addChangeListener(new HelpItemChangeListener("section.view"));
 204  
                         
 205  0
                         mSpecialChars = new JCheckBoxMenuItem(mTranslation.getValue("menu.view.special_chars.text"));
 206  0
                         mSpecialChars.setSelected(Config.getInstance().isPrintBlankAndTabs());
 207  0
                         mSpecialChars.setActionCommand(EActionCommands.SPECIAL_CHARS.toString());
 208  0
                         mSpecialChars.addActionListener(mActionListener);
 209  0
                         mSpecialChars.addChangeListener(new HelpItemChangeListener("section.view"));
 210  0
                         mView.add(mSpecialChars);
 211  
                 }
 212  
                 
 213  0
                 return mView;
 214  
         }
 215  
         
 216  
         private JMenu getRefactorMenu() {
 217  0
                 if (mRefactor == null) {
 218  0
                         mRefactor = new JMenu(mTranslation.getValue("menu.refactor.text"));
 219  0
                         mRefactor.addChangeListener(new HelpItemChangeListener("section.refactor"));
 220  
                         
 221  0
                         mCheckGlossConsistency = new JMenuItem(mTranslation.getValue("menu.refactor.check_gloss_consistency.text"));
 222  0
                         mCheckGlossConsistency.setActionCommand(EActionCommands.GLOSS_CHECK_CONSISTENCY.toString());
 223  0
                         mCheckGlossConsistency.addActionListener(mActionListener);
 224  0
                         mCheckGlossConsistency.setEnabled(false);
 225  0
                         mCheckGlossConsistency.addChangeListener(new HelpItemChangeListener("section.check_consistency"));
 226  0
                         mRefactor.add(mCheckGlossConsistency);
 227  
                 }
 228  
                 
 229  0
                 return mRefactor;
 230  
         }
 231  
         
 232  
         private JMenu getOptionsMenu() {
 233  0
                 if (mOptions == null) {
 234  0
                         mOptions = new JMenu(mTranslation.getValue("menu.options.text"));
 235  0
                         mOptions.addChangeListener(new HelpItemChangeListener("section.options"));
 236  
                         
 237  0
                         mLanguage = new JMenu(mTranslation.getValue("menu.options.language.text"));
 238  0
                         mLanguage.addChangeListener(new HelpItemChangeListener("section.language"));
 239  0
                         mOptions.add(mLanguage);                        
 240  0
                         createLanguageMenuItems();
 241  
                         
 242  0
                         mBibTeX = new JMenuItem(mTranslation.getValue(""));
 243  0
                         mBibTeX.addChangeListener(new HelpItemChangeListener("section.bibtex"));
 244  0
                         mBibTeX.setActionCommand(EActionCommands.CHOOSE_BIBTEXFILE.toString());
 245  0
                         mBibTeX.addActionListener(mActionListener);
 246  0
                         mOptions.add(mBibTeX);
 247  
                 }
 248  
                 
 249  0
                 return mOptions;
 250  
         }
 251  
         
 252  
         private void createLanguageMenuItems() {
 253  0
                 mLanguage.removeAll();
 254  0
                 String[] languages = TranslationFinder.getLanguages();
 255  0
                 ButtonGroup group = new ButtonGroup();
 256  0
                 for(int i = 0; i < languages.length; i++) {
 257  0
                         JRadioButtonMenuItem language = new JRadioButtonMenuItem(languages[i]);
 258  0
                         if (Locale.getDefault().getDisplayLanguage().equals(languages[i])) {
 259  0
                                 language.setSelected(true);
 260  
                         }
 261  0
                         language.setActionCommand("CHANGE_LANGUAGE_" + TranslationFinder.getLanguageAbbrev(languages[i]));
 262  0
                         language.addActionListener(mActionListener);
 263  0
                         language.addChangeListener(new HelpItemChangeListener("section.language"));
 264  0
                         group.add(language);
 265  0
                         mLanguage.add(language);
 266  
                 }
 267  0
         }
 268  
         
 269  
         private JMenu getCustomHelpMenu() {
 270  0
                 if (mHelp == null) {
 271  0
                         mHelp = new JMenu(mTranslation.getValue("menu.help.text"));
 272  0
                         mHelp.addChangeListener(new HelpItemChangeListener("section.help"));
 273  
                         
 274  0
                         mHelpDialog = new JMenuItem(mTranslation.getValue("menu.help.help.text"));
 275  0
                         mHelpDialog.addActionListener(new CSH.DisplayHelpFromSource(HelpSystem.getInstance().getHelpBroker()));
 276  0
                         HelpSystem.getInstance().enableHelpOnButton(mHelpDialog, "chapter.introduction", null);
 277  0
                         mHelpDialog.addChangeListener(new HelpItemChangeListener("section.help_dialog"));
 278  0
                         mHelp.add(mHelpDialog);
 279  
                         
 280  0
                         mInfo = new JMenuItem(mTranslation.getValue("menu.help.info.text"));
 281  0
                         mInfo.setActionCommand(EActionCommands.INFO_DIALOG.toString());
 282  0
                         mInfo.addActionListener(mActionListener);
 283  0
                         mInfo.addChangeListener(new HelpItemChangeListener("section.info"));
 284  0
                         mHelp.add(mInfo);
 285  
                 }
 286  
                 
 287  0
                 return mHelp;
 288  
         }
 289  
         
 290  
         private void changeLanguage() {
 291  
                 // File menu
 292  0
                 mFile.setText(mTranslation.getValue("menu.file.text"));
 293  0
                 mFile.setMnemonic(mTranslation.getChar("menu.file.shortcut"));
 294  0
                 mFileNew.setText(mTranslation.getValue("menu.file.new.text"));
 295  0
                 mFileNew.setToolTipText(mTranslation.getValue("menu.file.new.tooltip"));
 296  0
                 mFileOpen.setText(mTranslation.getValue("menu.file.open.text"));
 297  0
                 mFileOpen.setToolTipText(mTranslation.getValue("menu.file.open.tooltip"));        
 298  0
                 mFileLastOpened.setText(mTranslation.getValue("menu.file.last_opened.text"));
 299  0
                 mFileLastOpened.setToolTipText(mTranslation.getValue("menu.file.last_opened.tooltip"));
 300  0
                 mFileSave.setText(mTranslation.getValue("menu.file.save.text"));
 301  0
                 mFileSave.setToolTipText(mTranslation.getValue("menu.file.save.tooltip"));                
 302  0
                 mFileSaveAs.setText(mTranslation.getValue("menu.file.save_as.text"));
 303  0
                 mFileSaveAs.setToolTipText(mTranslation.getValue("menu.file.save_as.tooltip"));
 304  0
                 mImportFile.setText(mTranslation.getValue("menu.file.import.text"));
 305  0
                 mImportFile.setToolTipText(mTranslation.getValue("menu.file.import.tooltip"));                
 306  0
                 mFileExit.setText(mTranslation.getValue("menu.file.exit.text"));
 307  0
                 mFileExit.setToolTipText(mTranslation.getValue("menu.file.exit.tooltip"));
 308  
                 
 309  
                 // Edit menu
 310  0
                 mEdit.setText(mTranslation.getValue("menu.edit.text"));                
 311  0
                 mEdit.setMnemonic(mTranslation.getChar("menu.edit.shortcut"));
 312  0
                 mUndo.setText(mTranslation.getValue("menu.edit.undo.text"));
 313  0
                 mUndo.setToolTipText(mTranslation.getValue("menu.edit.undo.tooltip"));                
 314  0
                 mRedo.setText(mTranslation.getValue("menu.edit.redo.text"));
 315  0
                 mRedo.setToolTipText(mTranslation.getValue("menu.edit.redo.tooltip"));
 316  
                 
 317  
                 // View menu
 318  0
                 mView.setText(mTranslation.getValue("menu.view.text"));
 319  0
                 mSpecialChars.setText(mTranslation.getValue("menu.view.special_chars.text"));
 320  0
                 mSpecialChars.setToolTipText(mTranslation.getValue("menu.view.special_chars.tooltip"));
 321  
                 
 322  
                 // Refactor menu
 323  0
                 mRefactor.setText(mTranslation.getValue("menu.refactor.text"));
 324  0
                 mRefactor.setMnemonic(mTranslation.getChar("menu.refactor.shortcut"));
 325  0
                 mRefactor.addChangeListener(new HelpItemChangeListener("section.refactor"));
 326  0
                 mCheckGlossConsistency.setText(mTranslation.getValue("menu.refactor.check_gloss_consistency.text"));
 327  0
                 mCheckGlossConsistency.setToolTipText(mTranslation.getValue("menu.refactor.check_gloss_consistency.tooltip"));
 328  
                 
 329  
                 // Options menu
 330  0
                 mOptions.setText(mTranslation.getValue("menu.options.text"));
 331  0
                 mOptions.setMnemonic(mTranslation.getChar("menu.options.shortcut"));
 332  0
                 mOptions.setToolTipText(mTranslation.getValue("menu.options.tooltip"));                
 333  0
                 mLanguage.setText(mTranslation.getValue("menu.options.language.text"));
 334  0
                 mLanguage.setToolTipText(mTranslation.getValue("menu.options.language.tooltip"));
 335  0
                 createLanguageMenuItems();
 336  0
                 mBibTeX.setText(mTranslation.getValue("menu.options.bibtex.text"));
 337  0
                 mBibTeX.setToolTipText(mTranslation.getValue("menu.options.bibtex.tooltip"));
 338  
                 
 339  
                 // Help menu
 340  0
                 mHelp.setText(mTranslation.getValue("menu.help.text"));
 341  0
                 mHelp.setMnemonic(mTranslation.getChar("menu.help.shortcut"));
 342  0
                 mHelpDialog.setText(mTranslation.getValue("menu.help.help.text"));
 343  0
                 mHelpDialog.setToolTipText(mTranslation.getValue("menu.help.help.tooltip"));
 344  0
                 mHelpDialog.addActionListener(new CSH.DisplayHelpFromSource(HelpSystem.getInstance().getHelpBroker()));                
 345  0
                 mInfo.setText(mTranslation.getValue("menu.help.info.text"));
 346  0
                 mInfo.setToolTipText(mTranslation.getValue("menu.help.info.tooltip"));
 347  0
         }
 348  
         
 349  
         @Override
 350  
         public void propertyChange(PropertyChangeEvent event) {
 351  0
                 if (mGlossItemsChangePublisher.isFileChanged() && mGlossItemsChangePublisher.isFileOpen()) {
 352  0
                         mFileSave.setEnabled(true);
 353  
                 }
 354  
                 
 355  0
                 if (mGlossItemsChangePublisher.isFileOpen()) {
 356  0
                         mImportFile.setEnabled(true);
 357  0
                         mFileSaveAs.setEnabled(true);
 358  0
                         mCheckGlossConsistency.setEnabled(true);
 359  
                 }
 360  
                 
 361  0
                 if (UndoGlossStack.getInstance().undoSize() > 0) {
 362  0
                         mUndo.setEnabled(true);
 363  
                 } else {
 364  0
                         mUndo.setEnabled(false);
 365  
                 }
 366  
                 
 367  0
                 if (UndoGlossStack.getInstance().redoSize() > 0) {
 368  0
                         mRedo.setEnabled(true);
 369  
                 } else {
 370  0
                         mRedo.setEnabled(false);
 371  
                 }
 372  0
                 if (event.getPropertyName().equals(EActionCommands.CREATE_NEW_FILE.toString())) {
 373  0
                         mFileSave.setEnabled(false);
 374  0
                         mFileSaveAs.setEnabled(true);
 375  0
                         mImportFile.setEnabled(true);
 376  
                 }
 377  
                                 
 378  0
                 if (event.getPropertyName().equals(EActionCommands.CREATE_NEW_FILE.toString()) ||
 379  
                                 event.getPropertyName().equals(EActionCommands.OPEN_FILE.toString()) ||
 380  
                                 event.getPropertyName().equals(EActionCommands.SAVE_FILE.toString()) ||
 381  
                                 event.getPropertyName().equals(EActionCommands.SAVE_FILE_AS.toString()) ) {
 382  0
                                 mUndo.setEnabled(false);
 383  0
                                 mRedo.setEnabled(false);
 384  
                 }
 385  
                 
 386  0
                 if (event.getPropertyName().equals(EActionCommands.OPEN_FILE.toString())) {
 387  0
                         mFileLastOpened.setEnabled(true);
 388  
                 }
 389  
                 
 390  0
                 if (event.getPropertyName().equals(EActionCommands.LANGUAGE_CHANGED.toString())) {
 391  0
                         changeLanguage();
 392  
                 }
 393  
                 
 394  0
                 if (event.getPropertyName().equals(EActionCommands.GLOSS_ITEM_ADDED.toString())) {
 395  0
                         mCheckGlossConsistency.setEnabled(true);
 396  
                 }
 397  0
         }
 398  
         
 399  
         
 400  
 }