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/swing/HelpSystem.java,v 1.7 2004/07/01 05:50:21 tj_willis Exp $
019     */
020    package net.sourceforge.pavlov.swing;
021    
022    
023    import java.awt.event.*;
024    import java.awt.Container;
025    import java.net.*;
026    import javax.swing.*;
027    import java.util.Hashtable;
028    import org.apache.log4j.*;
029    
030    /**
031     * HelpSystem creates a JMenu of Help Topics, each of which will automatically
032     * open a HTMLInternalFrame with the specified URL when clicked.  This system
033     * is pretty lightweight and easy to use, but not as feature-rich as Sun's
034     * jHelp.
035     *
036     * @author <a href="mailto:tj_willis@users.sourceforge.net">T.J. Willis</a>
037     * @version 1.0
038     */
039    public class HelpSystem
040        extends JMenu
041        implements ActionListener
042    {
043    
044        /**
045         * Describe variable <code>pane</code> here.
046         *
047         */
048        protected Container pane;
049        /**
050         * Describe variable <code>browsers</code> here.
051         *
052         */
053        protected Hashtable<String,HTMLInternalFrame> browsers;
054    
055    
056        /**
057         * Creates a new <code>HelpSystem</code> instance.
058         *
059         * @param dPane a <code>Container</code> value
060         */
061        public HelpSystem(Container dPane)
062        {
063            super("Help");
064            pane = dPane;
065            browsers = new Hashtable<String,HTMLInternalFrame>();
066        }
067    
068        /**
069         * Creates a new <code>HelpSystem</code> instance.
070         *
071         * @param dPane a <code>Container</code> value
072         * @param aboutURL an <code>URL</code> value
073         * @param helpURL an <code>URL</code> value
074         */
075        public HelpSystem(Container dPane, URL aboutURL, URL helpURL)
076        {
077            super("Help");
078            browsers = new Hashtable<String,HTMLInternalFrame>();
079            init(dPane,aboutURL,helpURL);
080        }
081      
082        /**
083         * Describe <code>showBrowser</code> method here.
084         *
085         * @param url an <code>URL</code> value
086         * @param title a <code>String</code> value
087         */
088        protected void showBrowser(URL url,String title)
089        {
090            HTMLInternalFrame targ = browsers.get(title);
091            if(targ==null)
092                {
093                    targ = new HTMLInternalFrame(title,url);
094                    //pane.add(targ);
095                    browsers.put (title,targ);
096                }
097            //System.out.println("idx is: " + pane.getIndexOf(af) );
098    //      if(pane.getIndexOf(targ) < 0 ) 
099    //          pane.add(targ);
100    //FIXED: was buggy, changed in MVC push
101            /*if(!pane.isAncestorOf(targ))
102                pane.add(targ);*/
103            targ.pack();
104            targ.setVisible(true);
105    /*      I'm a JDialog now, don't need to setSelectedtry {
106                targ.setSelected(true);
107            } catch (java.beans.PropertyVetoException e  ){
108                
109            }*/
110        }
111    
112        /**
113         * Adds a HelpItem to the JMenu.
114         *
115         * @param url a <code>String</code> value
116         * @param title a <code>String</code> value
117         * @return a <code>boolean</code> value
118         */
119        public boolean addItem(String url, String title)
120        {
121            try {
122                URL x = new URL(url);
123                addItem(x,title);
124            } catch (Exception e) {
125                return false;
126            }
127            return true;
128        }
129    
130        /**
131         * Adds a HelpItem to the JMenu.
132         *
133         * @param url an <code>URL</code> value
134         * @param title a <code>String</code> value
135         */
136        public void addItem(URL url, String title)
137        {
138            JMenuItem it = new JMenuItem(title);
139            it.addActionListener(this);
140            it.setActionCommand(url.toString());
141            add(it);
142        }
143    
144    
145        /**
146         * Describe <code>init</code> method here.
147         *
148         * @param dPane a <code>Container</code> value
149         * @param aboutURL an <code>URL</code> value
150         * @param helpURL an <code>URL</code> value
151         * @deprecated Shouldn't have made it into 1.0.
152         */
153        @Deprecated protected void init(Container dPane, URL aboutURL, URL helpURL)
154        {
155            addItem(helpURL,"Help Topics");
156            addItem(aboutURL,"About");
157        }
158      
159    
160        /**
161         * Describe <code>getHelpMenu</code> method here.
162         *
163         * @return a <code>JMenu</code> value
164         * @deprecated This method predates this class being a subclass of JMenu.
165         */
166        @Deprecated public JMenu getHelpMenu()
167        {
168            return this;
169        }
170    
171        /**
172         * Creates the HTMLInternalFrame when a help item is selected.
173         *
174         * @param e an <code>ActionEvent</code> value
175         */
176        public void actionPerformed(ActionEvent e)
177        {
178            Object o = e.getActionCommand();
179            //if( !(o instanceof URL))
180            //    return;
181            URL u = null;
182            try {
183                u = new URL(e.getActionCommand());
184            } catch (Exception ex) {
185                return;
186            }
187            JMenuItem it = (JMenuItem)e.getSource();
188            showBrowser(u,it.getText());
189        }
190    }