/*
 * This file is part of the Poliqarp suite.
 * 
 * Copyright (C) 2004-2009 by Instytut Podstaw Informatyki Polskiej
 * Akademii Nauk (IPI PAN; Institute of Computer Science, Polish
 * Academy of Sciences; cf. www.ipipan.waw.pl).  All rights reserved.
 * 
 * This file may be distributed and/or modified under the terms of the
 * GNU General Public License version 2 as published by the Free Software
 * Foundation and appearing in the file gpl.txt included in the packaging
 * of this file.  (See http://www.gnu.org/licenses/translations.html for
 * unofficial translations.)
 * 
 * A commercial license is available from IPI PAN (contact
 * Michal.Ciesiolka@ipipan.waw.pl or ipi@ipipan.waw.pl for more
 * information).  Licensees holding a valid commercial license from IPI
 * PAN may use this file in accordance with that license.
 * 
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING
 * THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE.
 */

package ipipan.poliqarp.gui;

import java.util.Map;
import java.util.Set;
import java.util.HashMap;
import java.util.HashSet;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

import ipipan.poliqarp.util.HotString;
import ipipan.poliqarp.util.PoliqarpUtilities;

/** 
 * This is a wrapper around the standard MenuBar, providing useful means of 
 * adding menu components to the menu as well as renaming them at need 
 * (for instance, when a language change was requested). 
 */
public class MultilingualMenuBar implements MultilingualEntity
{
   class MenuElement 
   {
      int number;
      JMenuItem item;

      MenuElement(int number, JMenuItem item)
      {
         this.number = number;
         this.item = item;
      }
   }
      
   private JMenuBar menuBar; 
   private Set<MenuElement> set;
   private Map<Integer, JMenuItem> map;

   /**
    * Configuration object for this application, used to retrieve names of
    * options.
    */
   protected Configuration config; 
   
   /** 
    * Creates a new MenuBar using the language setting from the given 
    * configuration. 
    */
   public MultilingualMenuBar(Configuration config) 
   {
      this.config = config;
      menuBar = new JMenuBar();
      set = new HashSet<MenuElement>();
      map = new HashMap<Integer, JMenuItem>();
   }

   private void setItemName(JMenuItem item, String name) 
   {
      HotString s = PoliqarpUtilities.getHotKey(name);      
      item.setText(s.toString());
      if (s.isHotKey())
         item.setMnemonic(s.getHotKey());
//      item.setMnemonic((int)(Character.toUpperCase(hotkey)));
   }

   /** 
    * Adds a new element to the menu and sets its name as needed. 
    *
    * @param elementId a unique identifier of the component, corresponding to
    * its definition in the proper .properties file
    * @param parent the parent to which to add the new component. If this is
    * null, the component will be added directly to the MenuBar.
    * @param item the new item to add
    */
   public void add(int elementId, JMenuItem parent, JMenuItem item) 
   {
      setItemName(item, config.getMenuComponentName(elementId));
      addAllLang(elementId, parent, item);
      set.add(new MenuElement(elementId, item));
   }

   /**
    * Roughly the same as add(), but does not cause the name of this
    * element to change each time updateNames() is called.
    * @see #add
    */
   public void addAllLang(int elementId, JMenuItem parent, JMenuItem item) 
   {
      item.setActionCommand("" + elementId);
      map.put(elementId, item);
      
      if (parent == null)
         menuBar.add(item);
      else
         parent.add(item);
   }

   /**
    * Updates names of all menu components according to the current language
    * setting.
    */
   public void updateLanguage(Configuration cfg)
   {
      for (MenuElement el : set) {
         setItemName(el.item, cfg.getMenuComponentName(el.number));
      }
   }

   /**
    * Returns the JMenuBar object representing this bar.
    */
   public JMenuBar getBar() 
   {
      return menuBar;
   }
   
   /**
    * Returns the JMenuItem with the specified identificator.
    */
   public JMenuItem getItem(int id)
   {
      return map.get(id);
   }
}
