view java/src/uk/ac/qmul/eecs/ccmi/gui/ResourceFactory.java @ 1:e3935c01cde2 tip

moved license of PdPersistenceManager to the beginning of the file
author Fiore Martin <f.martin@qmul.ac.uk>
date Tue, 08 Jul 2014 19:52:03 +0100
parents 78b7fc5391a2
children
line wrap: on
line source
/*  
 CCmI Editor - A Collaborative Cross-Modal Diagram Editing Tool
	  
 Copyright (C) 2002 Cay S. Horstmann (http://horstmann.com)
 Copyright (C) 2011  Queen Mary University of London (http://ccmi.eecs.qmul.ac.uk/)

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

package uk.ac.qmul.eecs.ccmi.gui;

import java.awt.event.ActionListener;
import java.beans.EventHandler;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;

/**
 * A factory class for swing components creation support. 
 * 
 * Components that are created via this class are, in turn created by {@code SpeechMenuFactory} methods.
 * This class handles the labelling and other menu properties (such as accelerators) 
 * using the {@code ResourceBundle} passed as argument to the constructor.
 */
class ResourceFactory{
   public ResourceFactory(ResourceBundle bundle){
      this.bundle = bundle;
   }

   public JMenu createMenu(String prefix){
	      String text = bundle.getString(prefix + ".text");
	      JMenu menu = SpeechMenuFactory.getMenu(text);
	      try{
	         String mnemonic = bundle.getString(prefix + ".mnemonic");
	         menu.setMnemonic(mnemonic.charAt(0));
	      }catch (MissingResourceException exception){
	         // ok not to set mnemonic
	      }

	      try{
	         String tooltip = bundle.getString(prefix + ".tooltip");
	         menu.setToolTipText(tooltip);         
	      }
	      catch (MissingResourceException exception){
	         // ok not to set tooltip
	      }
	      return menu;
   }
   
   public JMenuItem createMenuItem(String prefix, 
      Object target, String methodName){
      return createMenuItem(prefix,
         (ActionListener) EventHandler.create(
            ActionListener.class, target, methodName));
   }

   public JMenuItem createMenuItem(String prefix, 
      ActionListener listener){
      String text = bundle.getString(prefix + ".text");
      JMenuItem menuItem = SpeechMenuFactory.getMenuItem(text);
      return configure(menuItem, prefix, listener);
   }
   
   public JMenuItem createCheckBoxMenuItem(String prefix, 
      ActionListener listener){
      String text = bundle.getString(prefix + ".text");
      JMenuItem menuItem = SpeechMenuFactory.getJCheckBoxMenuItem(text);
      return configure(menuItem, prefix, listener);
   }

   private JMenuItem configure(JMenuItem menuItem, 
      String prefix, ActionListener listener){      
      menuItem.addActionListener(listener);
      try{
         String mnemonic = bundle.getString(prefix + ".mnemonic");
         menuItem.setMnemonic(mnemonic.charAt(0));
      }catch (MissingResourceException exception){
         // ok not to set mnemonic
      }

      try{
         String accelerator = bundle.getString(prefix + ".accelerator");
         menuItem.setAccelerator(KeyStroke.getKeyStroke(accelerator));
      }catch (MissingResourceException exception){
         // ok not to set accelerator
      }

      try{
         String tooltip = bundle.getString(prefix + ".tooltip");
         menuItem.setToolTipText(tooltip);         
      }catch (MissingResourceException exception){
         // ok not to set tooltip
      }
      return menuItem;
   }
   
   private ResourceBundle bundle;
}