001    /* PAVLOV -- Multiple Choice Study System
002     * Copyright (C) 2000 - 2004 T.J. Willis
003     *
004     * This program is free software; you can redistribute it and/or
005     * modify it under the terms of the GNU General Public License
006     * as published by the Free Software Foundation; either version 2
007     * of the License, or (at your option) any later version.
008     *
009     * This program is distributed in the hope that it will be useful,
010     * but WITHOUT ANY WARRANTY; without even the implied warranty of
011     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012     * GNU General Public License for more details.
013     *
014     * You should have received a copy of the GNU General Public License
015     * along with this program; if not, write to the Free Software
016     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
017     *
018     * $Header: /cvsroot/pavlov/net/sourceforge/pavlov/pluglets/strategy/StrategyLoader.java,v 1.5 2004/06/11 06:17:34 tj_willis Exp $
019     */
020    package net.sourceforge.pavlov.pluglets.strategy;
021    
022    import net.sourceforge.pavlov.user.AbstractStrategy;
023    import java.util.*;
024    import javax.swing.*;
025    import java.awt.event.*;
026    import net.sourceforge.pavlov.event.*;
027    import net.sourceforge.pavlov.pluglets.*;
028    
029    
030    //FIXME: having more than 1 menu using this will be a disaster
031    //FIXME: having less than 1 menu using this will be a disaster
032    /**
033     * A plugin loader for question selection strategies.
034     *
035     * @author <a href="mailto:"></a>
036     * @version 1.0
037     */
038    public class StrategyLoader 
039        extends PlugletLoader 
040        implements ActionListener
041    {
042        // FIXME: minimize access
043        ButtonGroup radioButtons;
044    
045        /**
046         * Objects that want to be notified when user selects a new strategy.
047         *
048         */
049        protected Vector<StrategyListener> strategyListeners;
050    
051    
052        /**
053         * Creates a new <code>StrategyLoader</code> instance.
054         *
055         * @param baseClassName a <code>String</code> value
056         * @param directory a <code>String</code> value
057         */
058        public StrategyLoader(String baseClassName, String directory)
059        {
060            super(baseClassName,directory);
061            strategyListeners = new Vector<StrategyListener>();
062        }
063    
064        /**
065         * Adds a listener.
066         *
067         * @param l a <code>StrategyListener</code> value
068         */
069        public void addStrategyListener(StrategyListener l)
070        {
071            strategyListeners.add(l);
072        }
073    
074        /**
075         * Notify all my listeners that the strategy has changed.
076         *
077         * @param newStrategy an <code>AbstractStrategy</code> value
078         */
079        public void notify(AbstractStrategy newStrategy)
080        {
081            for (StrategyListener sl : strategyListeners) {
082                sl.strategyChanged(newStrategy);
083            }
084        }
085    
086    
087        /**
088         * Lets a non-listener know what current strategy is.
089         *
090         * @return an <code>AbstractStrategy</code> value
091         */
092        public AbstractStrategy getCurrentStrategy()
093        {
094            Object foo = radioButtons.getSelection();
095            //System.out.println("CLASS IS: " + foo.getClass());
096            JToggleButton.ToggleButtonModel b =(JToggleButton.ToggleButtonModel) radioButtons.getSelection();
097            if(b==null) return null;
098            //System.out.println("in loader name is " + b.getActionCommand());
099            return (AbstractStrategy)getPluginByName(b.getActionCommand());
100        }
101    
102        // FIXME: offhand, this looks exactly the same as superclass definition
103        /**
104         * Describe <code>addToJMenuAsRadioButtons</code> method here.
105         *
106         * @param menu a <code>JMenu</code> value
107         * @return a <code>ButtonGroup</code> value
108         */
109        public ButtonGroup addToJMenuAsRadioButtons(JMenu menu)
110        {
111            if(pluglets==null) return null;
112            if(menu==null) return null;
113            radioButtons = new ButtonGroup();
114            boolean first = true;
115            for (Pluglet ap : pluglets) {
116                JRadioButtonMenuItem mi = new JRadioButtonMenuItem(ap.getName());
117                mi.setToolTipText(ap.getDescription());
118                mi.setActionCommand(ap.getName());
119                radioButtons.add(mi);
120                if(first){
121                    mi.setSelected(true);
122                    first = false;
123                }
124                menu.add(mi);
125                mi.addActionListener(this);
126            }
127            return radioButtons;
128        }
129    
130       
131        /**
132         * User has clicked a JRadioButtonMenuItem on my menu.  Set the current
133         * strategy as requested and notify listeners.
134         *
135         * @param evt an <code>ActionEvent</code> value
136         */
137        public void actionPerformed(ActionEvent evt)
138        {
139            AbstractButton b = (AbstractButton)evt.getSource();
140            String stratName = b.getText();
141            AbstractStrategy strat = (AbstractStrategy)getPluginByName(stratName);
142            if(strat==null) return;
143            notify(strat);
144        }
145    
146    }
147    
148    
149    
150    
151    
152