/* ========================
* JSynoptic : a free Synoptic editor
* ========================
*
* Project Info: http://jsynoptic.sourceforge.net/index.html
*
* This program is free software; you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Foundation;
* either version 2.1 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with this
* program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*
* (C) Copyright 2001-2005, by :
* Corporate:
* EADS Astrium SAS
* EADS CRC
* Individual:
* Claude Cazenave
*
* $Id: TemplateMenuBox.java,v 1.3 2008/07/22 10:20:36 ogor Exp $
*
* Changes
* -------
* 25 february 2008 : Initial public release (CC);
*
*/
package jsynoptic.ui;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.AbstractAction;
import javax.swing.Icon;
import javax.swing.JMenuItem;
import jsynoptic.base.Template;
import simtools.ui.MenuBox;
/**
* TemplateMenuBox is dedicated to display the current list of templates.
* Whenever the drop-down JMenu has to be displayed, its content is updated
* with the available templates, provided by Template class.
*
* @author zxpletran007
*
*/
public class TemplateMenuBox extends MenuBox{
protected ActionListener actionListener;
public TemplateMenuBox(Icon defaultIcon, ActionListener l) {
super(defaultIcon, null);
setCurrentActionFromCurrentTemplate();
actionListener = l;
}
public void setCurrentActionFromCurrentTemplate(){
setCurrentAction( new TemplateAction( Template.getCurrentTemplate()) );
}
/* (non-Javadoc)
* @see simtools.ui.MenuBox#updateMenuContents()
*/
protected void updateMenu(){
super.updateMenu();
// Create JMenu contents
menu.removeAll();
for(int i=0; i < Template.getTemplates().size(); i++){
Template t = (Template)Template.getTemplates().get(i);
JMenuItem mi = new JMenuItem( new TemplateAction(t));
menu.add(mi);
if (i <Template.getTemplates().size()-1){
menu.addSeparator();
}
}
}
protected class TemplateAction extends AbstractAction{
Template template;
public TemplateAction(Template template) {
super(template.getName(), template.getIcon());
this.template = template;
}
public void actionPerformed(ActionEvent e) {
Template.setCurrentTemplate(template);
setCurrentAction(this);
// do action
actionListener.actionPerformed(
new ActionEvent(TemplateMenuBox.this,
ActionEvent.ACTION_PERFORMED, ""));
}
}
}