/*
 This file is part of the software framework JStatCom, 
 a library for building applications for data based analysis.

 Copyright (C) 2000-2005  Markus Kraetzig

 This library 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 library 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 library; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 USA
 */

package com.jstatcom.component;

import java.awt.Component;
import java.net.URL;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.help.CSH;

import javolution37.javolution.xml.ObjectReader;
import javolution37.javolution.xml.XmlElement;
import javolution37.javolution.xml.XmlFormat;

/**
 * Manages the mapping between IDs for context sensitive help topics and the
 * respective components. The mapping is stored in an XML file
 * help_context.xml. Components that should use context
 * sensitive help must register with the JHelpContextMgr. This
 * class is used to maintain all help IDs in a single place that can be edited
 * without changing the code. Components are identified via their classname.
 * 
 * @author Markus Kraetzig 
 */
public final class JHelpContextMgr {

    public static final String helpIDFile = "help_context.xml";

    private final Map map = new HashMap();

    private static JHelpContextMgr instance = null;

    /**
     * XML format field for (de)serialization.
     */
    public static final XmlFormat JHelpContextMgr_XML = new XmlFormat(
            JHelpContextMgr.class) {
        public void format(JHelpContextMgr data, XmlElement xml) {
            for (String key : data.map.keySet()) {
                xml.add(new ContextID(key, data.map.get(key) + ""));

            }

        }

        public JHelpContextMgr parse(XmlElement xml) {
            JHelpContextMgr mgr = new JHelpContextMgr();

            while (xml.hasNext()) {
                ContextID id = (ContextID) xml.getNext();
                mgr.map.put(id.compname, id.helpid);
            }

            return mgr;
        }
    };

    /**
     * Inner class for storing mappings as XML elements.
     */
    public static class ContextID {
        // Fields are directly accessed by enclosing class.
        final String helpid;

        final String compname;

        /**
         * XML format field for (de)serialization.
         */
        public static final XmlFormat ContextID_XML = new XmlFormat(
                ContextID.class) {
            public void format(ContextID data, XmlElement xml) {
                xml.setAttribute("class", data.compname);
                xml.setAttribute("helpid", data.helpid);
            }

            public ContextID parse(XmlElement xml) {
                String comp = xml.getAttribute("class") + "";
                String id = xml.getAttribute("helpid") + "";

                return new ContextID(comp, id);
            }
        };

        public ContextID(String key, String helpid) {
            this.helpid = helpid;
            this.compname = key;
        }

    };

    /**
     * Creates an instance of this.
     */
    private JHelpContextMgr() {
        super();
        // map.put("tes", "sdas");
    }

    /**
     * Retrieves the helpID from the stored map for comp
     * according to the classname of that Component and sets the
     * helpID for context sensitive help.
     * 
     * @param comp
     *            the component to set the stored helpID for
     * @throws IllegalArgumentException
     *             if (comp == null)
     */
    public static void register(Component comp) {

        if (comp == null)
            throw new IllegalArgumentException("Component was null.");
        if (instance == null) {
            URL url = comp.getClass().getResource("/" + helpIDFile);
            try { // also catch possible security exception
                if (url != null) {
                    ObjectReader constructor = new ObjectReader();

                    instance = (JHelpContextMgr) constructor.read(url
                            .openStream());
                }
            } catch (Throwable t) {
                // do nothing
            }
            // create empty manager if failed
            if (instance == null) {
                instance = new JHelpContextMgr();
                return;
            }
        }
        Object helpID = instance.map.get(comp.getClass().getName());
        if (helpID != null)
            CSH.setHelpIDString(comp, helpID.toString());

    }

    /**
     * Gets an itertor of all available keys.
     * 
     * @return the names of the components that are currently mapped
     */
    public Iterator compNames() {
        return map.keySet().iterator();
    }

    /**
     * Gets the ID according to the component name.
     * 
     * @param key
     *            the name of a component
     * @return the mapped helpID
     */
    public String getID(Object key) {
        String k = key + "";
        Object rt = map.get(k);
        if (rt == null)
            return null;
        return rt.toString();

    }

}

 

Ads help maintain this website.