View Javadoc

1   /********************************************************************************
2    * InternetCafe is a software solution that helps the management of Cybercafes 
3    * according with the ITALIAN DECREE LAW ON ANTI-TERROR MEASURES, 27 JULY 2005.
4    * Copyright (C) 2006  Guido Angelo Ingenito
5   
6    * This program is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU General Public License
8    * as published by the Free Software Foundation; either version 2
9    * of the License, or (at your option) any later version.
10  
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   * 
16   * You should have received a copy of the GNU General Public License
17   * along with this program; if not, write to the Free Software
18   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19   *******************************************************************************/
20  
21  package ui.help;
22  
23  import java.io.File;
24  import java.net.URL;
25  
26  import javax.help.CSH;
27  import javax.help.HelpBroker;
28  import javax.help.HelpSet;
29  import javax.swing.JMenuItem;
30  
31  import org.apache.log4j.Logger;
32  
33  @SuppressWarnings("serial") //$NON-NLS-1$
34  public class HelpMenuItem extends JMenuItem{
35  	
36  	private static final transient Logger logger = Logger.getLogger(HelpMenuItem.class.getName());
37  
38  	/***The protocol can be one between http, file, etc...**/
39  	public static final String URL_PROTOCOL="file"; //$NON-NLS-1$
40  	/***The local machine.**/
41  	public static final String URL_HOST="localhost"; //$NON-NLS-1$
42  	/***This specifies the default port to use in the protocol.**/
43  	public static final int URL_PORT=-1;
44  	
45  	public HelpMenuItem(String menuName) {
46  		super(menuName);
47  		initialize();
48  	}
49  	
50  	protected void initialize(){
51  		// 1. create HelpSet and HelpBroker objects
52  		File file = new File("help"+File.separatorChar+"InternetCafe.hs"); //$NON-NLS-1$ //$NON-NLS-2$
53  		if(!file.exists())
54  		{
55  			logger.debug("The Help Set File named: "+file + " doesn't exists!"); //$NON-NLS-1$ //$NON-NLS-2$
56  			return;
57  		}else logger.debug("The Help Set File named: "+file + " exists!"); //$NON-NLS-1$ //$NON-NLS-2$
58  			
59  		HelpSet helpSet = getHelpSet(file.getAbsolutePath());
60  		HelpBroker helpBroker = helpSet.createHelpBroker();
61  		// 2. assign help to components
62  		CSH.setHelpIDString(this, getName());
63  		// 3. handle events
64  		this.addActionListener(new CSH.DisplayHelpFromSource(helpBroker));
65  	}
66  	
67  	/***
68  	 * find the helpset file and create a HelpSet object
69  	 */
70  	public HelpSet getHelpSet(String helpsetfile) {
71  		HelpSet helpSet = null;
72  		try {
73  			URL hsURL = new URL(URL_PROTOCOL,URL_HOST,URL_PORT,helpsetfile);
74  			helpSet = new HelpSet(this.getClass().getClassLoader(), hsURL);
75  		} catch(Exception ex) {
76  			ex.printStackTrace();
77  			logger.error("HelpSet: "+ex.getMessage()); //$NON-NLS-1$
78  			logger.error("HelpSet: "+ helpsetfile + " not found"); //$NON-NLS-1$ //$NON-NLS-2$
79  		}
80  		return helpSet;
81  	}
82  }
83