Package org.geoforge.guihlp

Source Code of org.geoforge.guihlp.GfrCSH$GfrDisplayHelpFromSource

/*
*  Copyright (C) 2011-2014 GeoForge Project
*
*  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 3 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, see <http://www.gnu.org/licenses/>.
*/




package org.geoforge.guihlp;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import java.util.ArrayList;
import java.util.logging.Logger;
import javax.help.CSH;
import javax.help.HelpBroker;
import javax.help.HelpSet;
import javax.swing.JOptionPane;
import org.geoforge.java.enumeration.GfrEnuSystemPropertiesKeys;
import org.geoforge.lang.util.GfrResBundleLang;
import org.geoforge.java.util.logging.filehandler.FileHandlerLogger;


/**
*
* @author bantchao
*
* email: bantchao_AT_gmail.com
* ... please remove "_AT_" from the above string to get the right email address
*
*/
public class GfrCSH extends CSH
{
    // ----
    // begin: instantiate logger for this class
    final static private Logger _LOGGER_ = Logger.getLogger(GfrCSH.class.getName());

    static
    {
        GfrCSH._LOGGER_.addHandler(FileHandlerLogger.s_getInstance());
    }

    // end: instantiate logger for this class
    // ----

    // --------------
    // STATIC PRIVATE

    static private java.util.ArrayList<Component> ALT_CMP_SET_ID = new java.util.ArrayList<Component>();



    // -------------
    // STATIC PUBLIC

    // first checkNDump main helpset, then do job for additionals
    static public boolean s_checkAndDumpCmp2ID(HelpSet hst)
    {
      String str = System.getProperty(GfrEnuSystemPropertiesKeys.DEBUG_APPLI.getLabel());

      if (str == null )
            return true;

      if (str.compareToIgnoreCase("true") != 0)
            return true;

        if (GfrCSH.ALT_CMP_SET_ID == null)
        {
            GfrCSH._LOGGER_.severe("nil GfrCSH.ALT_CMP_SET_ID");
            return false;
        }


        boolean blnGotNoValidId = false;

        for (int i=0; i<GfrCSH.ALT_CMP_SET_ID.size(); i++)
        {
            Component cmpCur = (Component) GfrCSH.ALT_CMP_SET_ID.get(i);

            if (cmpCur == null)
            {
                GfrCSH._LOGGER_.severe("nil cmpCur");
                return false;
            }

            String strIDCur = CSH.getHelpIDString(cmpCur);

            if (strIDCur == null)
            {
                GfrCSH._LOGGER_.severe("nil strIDCur");
                return false;
            }


            if (! hst.getCombinedMap().isValidID(strIDCur, hst))
            {
                blnGotNoValidId = true;
                GfrCSH._LOGGER_.warning("not isValidID strIDCur=" + strIDCur + ", ignoring");
                // continuing in order to get all invalid Ids
            }
        }


        if (blnGotNoValidId)
        {
            GfrCSH._LOGGER_.warning("Got at least one invalid ID");
            return false;
        }

        // check for additional integrated helps, ie g4g
        if (! GfrCSH._s_checkAndDumpCmp2ID())
          return false;

        // ending
        return true;
    }

    static private boolean _s_checkAndDumpCmp2ID()
    {
      if (GfrCSH._MAP_HPR2ALT_CMP_SET_ID==null || GfrCSH._MAP_HPR2ALT_CMP_SET_ID.isEmpty())
        return true;

      Set<HelpBroker> set= GfrCSH._MAP_HPR2ALT_CMP_SET_ID.keySet();
        Iterator<HelpBroker> itrHelpBroker = set.iterator();

        boolean blnGotNoValidId = false;

        while (itrHelpBroker.hasNext())
        {
           HelpBroker hbrCur = (HelpBroker) itrHelpBroker.next();

           if (hbrCur == null)
           {
               GfrCSH._LOGGER_.severe("nil hbrCur, break");
               return false;
           }

           GfrCSH._LOGGER_.info("hbrCur.getCurrentView()=" + hbrCur.getCurrentView());


          HelpSet hstCur = hbrCur.getHelpSet();

          if (hstCur == null)
          {
              GfrCSH._LOGGER_.severe("nil hstCur");
              return false;
          }

          ArrayList<Component> altCur = GfrCSH._MAP_HPR2ALT_CMP_SET_ID.get(hbrCur);

          if (altCur == null)
          {
              GfrCSH._LOGGER_.severe("nil altCur");
              return false;
          }

          for (int i=0; i<altCur.size(); i++)
          {
              Component cmpCur = (Component) altCur.get(i);

              if (cmpCur == null)
              {
                  GfrCSH._LOGGER_.severe("nil cmpCur");
                  return false;
              }

              String strIDCur = CSH.getHelpIDString(cmpCur);

              if (strIDCur == null)
              {
                  GfrCSH._LOGGER_.severe("nil strIDCur");
                  return false;
              }

              if (! hstCur.getCombinedMap().isValidID(strIDCur, hstCur))
            {
                blnGotNoValidId = true;
                GfrCSH._LOGGER_.severe("not isValidID strIDCur=" + strIDCur +", ignoring");
                  // continuing in order to get all invalid Ids
            }
          }

        }  // end of map iteration


        if (blnGotNoValidId)
        {
            GfrCSH._LOGGER_.severe("Got at least one invalid ID");
            return false;
        }

      return true;
    }


    static private HashMap<HelpBroker, ArrayList<Component>> _MAP_HPR2ALT_CMP_SET_ID =
      new HashMap<HelpBroker, ArrayList<Component>>();

    // specific helpbroker, ie plugin
    static public void setHelpIDString(Component cmpTarget, String strID, HelpBroker hbr)
    {
        if (cmpTarget == null)
        {
            String str = "cmpTarget == null, strID=" + strID + ", exiting";
            GfrCSH._LOGGER_.severe(str);
            JOptionPane.showMessageDialog(null, str, GfrResBundleLang.s_getInstance().getValue("word.error"), JOptionPane.ERROR_MESSAGE);
            System.exit(1);
        }

        if (strID == null)
        {
            String str = "nil strId, cmpTarget.getClass().toString()=" + cmpTarget.getClass().toString() + "\n exiting";
            GfrCSH._LOGGER_.severe(str);
            JOptionPane.showMessageDialog(null, str, GfrResBundleLang.s_getInstance().getValue("word.error"), JOptionPane.ERROR_MESSAGE);
            System.exit(1);
        }

        if (hbr == null)
        {
            String str = "hbr == null";
            str += "\n";
            str += "cmpTarget.getClass().toString()=" + cmpTarget.getClass().toString();
            str += "\n";
            str += "strID=" + strID;

            // TODO! fix up JavaHelp bug
            GfrCSH._LOGGER_.severe(str);
            JOptionPane.showMessageDialog(null, str, GfrResBundleLang.s_getInstance().getValue("word.error"), JOptionPane.ERROR_MESSAGE);
            System.exit(1);
        }

        //GfrCSH._LOGGER_.info("hbr.getCurrentView()=" + hbr.getCurrentView());

       ArrayList<Component> altValue = GfrCSH._MAP_HPR2ALT_CMP_SET_ID.get(hbr);

       if (altValue == null)
       {
         altValue = new ArrayList<Component>();
           //GfrCSH._MAP_HPR2ALT_CMP_SET_ID.put(hbr, altValue);
           //GfrCSH._LOGGER_.info("GfrCSH._MAP_HPR2ALT_CMP_SET_ID.put(hbr, altValue)");
           //GfrCSH._LOGGER_.info("hbr.getCurrentView()=" + hbr.getCurrentView());
       }


       altValue.add(cmpTarget);

        CSH.setHelpIDString(cmpTarget, strID);
    }


    /**
        overwrites superclass's method
    **/
    static public void setHelpIDString(Component cmpTarget, String strID)
    {
        if (cmpTarget == null)
        {
            String str = "cmpTarget == null, strID=" + strID + ", exiting";
            GfrCSH._LOGGER_.severe(str);
            JOptionPane.showMessageDialog(null, str, GfrResBundleLang.s_getInstance().getValue("word.error"), JOptionPane.ERROR_MESSAGE);
            System.exit(1);
        }

        if (strID == null)
        {
            String str = "nil strId, cmpTarget.getClass().toString()=" + cmpTarget.getClass().toString() + ", exiting";
            GfrCSH._LOGGER_.severe(str);
            JOptionPane.showMessageDialog(null, str, GfrResBundleLang.s_getInstance().getValue("word.error"), JOptionPane.ERROR_MESSAGE);
            System.exit(1);
        }

        GfrCSH.ALT_CMP_SET_ID.add(cmpTarget);
        CSH.setHelpIDString(cmpTarget, strID);
       
    }


    // -----------------------------------------
    // BEGIN STATIC CLASS GfrDisplayHelpFromFocus

    static public class GfrDisplayHelpFromFocus implements ActionListener
    {

        // ------
        // PUBLIC

        public GfrDisplayHelpFromFocus(HelpBroker hbr)
        {

            if (hbr == null)
            {
                String str = "hbr == null";
                GfrCSH._LOGGER_.severe(str);
                JOptionPane.showMessageDialog(null, str, GfrResBundleLang.s_getInstance().getValue("word.error"), JOptionPane.ERROR_MESSAGE);
                System.exit(1);
            }

            if (! (hbr instanceof GfrHelpBroker))
            {
                String str = "! (hbr instanceof GfrHelpBroker)";
                GfrCSH._LOGGER_.severe(str);
                JOptionPane.showMessageDialog(null, str, GfrResBundleLang.s_getInstance().getValue("word.error"), JOptionPane.ERROR_MESSAGE);
                System.exit(1);
            }

            this._mhb = (GfrHelpBroker) hbr;

            try
            {
                this._alr = new CSH.DisplayHelpFromFocus(hbr);
            }

            catch(NullPointerException excNullPointer)
            {
                excNullPointer.printStackTrace();
                String str = excNullPointer.getMessage();
                GfrCSH._LOGGER_.severe(str);
                JOptionPane.showMessageDialog(null, str, GfrResBundleLang.s_getInstance().getValue("word.error"), JOptionPane.ERROR_MESSAGE);
                System.exit(1);
            }

          
        }

        @Override
        public void actionPerformed(ActionEvent evtAction)
        {

            if (this._alr == null)
            {
                String str = "this._alr == null";
                GfrCSH._LOGGER_.severe(str);
                JOptionPane.showMessageDialog(null, str, GfrResBundleLang.s_getInstance().getValue("word.error"), JOptionPane.ERROR_MESSAGE);
                System.exit(1);
            }

            this._alr.actionPerformed(evtAction);

            javax.swing.SwingUtilities.invokeLater(new Runnable()
            {
                @Override
                public void run()
                {
                    if (_mhb == null)
                    {
                        return; // !!!!
                    }

                    if (! _mhb.assignStyleSheet())
                    {
                        // maybe destroying");
        
                        return; // !!!!
                    }

                }
            });
        }

        // -------
        // PRIVATE

        private ActionListener _alr = null;
        private GfrHelpBroker _mhb = null;
    }

    // ---------------------------------------
    // END STATIC CLASS GfrDisplayHelpFromFocus

    // ---------------------------------------------
    // BEGIN STATIC CLASS GfrDisplayHelpAfterTracking

    static public class GfrDisplayHelpAfterTracking implements ActionListener
    {

        // ------
        // PUBLIC

        public GfrDisplayHelpAfterTracking(HelpBroker hbr)
        {

            if (hbr == null)
            {
                String str = "hbr == null";
                GfrCSH._LOGGER_.severe(str);
                JOptionPane.showMessageDialog(null, str, GfrResBundleLang.s_getInstance().getValue("word.error"), JOptionPane.ERROR_MESSAGE);
                System.exit(1);
            }

            if (! (hbr instanceof GfrHelpBroker))
            {
                String str = "! (hbr instanceof GfrHelpBroker)";
                GfrCSH._LOGGER_.severe(str);
                JOptionPane.showMessageDialog(null, str, GfrResBundleLang.s_getInstance().getValue("word.error"), JOptionPane.ERROR_MESSAGE);
                System.exit(1);
            }

            this._mhb = (GfrHelpBroker) hbr;


            try
            {
                this._alr = new CSH.DisplayHelpAfterTracking(hbr);
            }

            catch(NullPointerException excNullPointer)
            {
                excNullPointer.printStackTrace();
                String str = excNullPointer.getMessage();
                GfrCSH._LOGGER_.severe(str);
                JOptionPane.showMessageDialog(null, str, GfrResBundleLang.s_getInstance().getValue("word.error"), JOptionPane.ERROR_MESSAGE);
                System.exit(1);
            }
        }

        @Override
        public void actionPerformed(ActionEvent evtAction)
        {

            if (this._alr == null)
            {
                String str = "this._alr == null";
                GfrCSH._LOGGER_.severe(str);
                JOptionPane.showMessageDialog(null, str, GfrResBundleLang.s_getInstance().getValue("word.error"), JOptionPane.ERROR_MESSAGE);
                System.exit(1);
            }

            // CANNOT CATCH EXCEPTION !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
            // workaround! assign VM arg "-D_geoforge.debug.appli="true" ==> call a static method that checks for valid IDs
            try
            {
                this._alr.actionPerformed(evtAction);
            }

            catch(javax.help.BadIDException excBadID)
            {
                excBadID.printStackTrace();
                String str = excBadID.getMessage();
                GfrCSH._LOGGER_.severe(str);
                JOptionPane.showMessageDialog(null, str, GfrResBundleLang.s_getInstance().getValue("word.error"), JOptionPane.ERROR_MESSAGE);
                System.exit(1);
            }


            javax.swing.SwingUtilities.invokeLater(new Runnable()
          {
              public void run()
              {
               

                    if (_mhb == null)
                    {
                        String str = "_mhb == null";
                        GfrCSH._LOGGER_.severe(str);
                        JOptionPane.showMessageDialog(null, str, GfrResBundleLang.s_getInstance().getValue("word.error"), JOptionPane.ERROR_MESSAGE);
                        System.exit(1);
                    }

                    if (! _mhb.assignStyleSheet())
                    {
                        // TODO !!!!

                        GfrCSH._LOGGER_.warning("failed, aborting");
                        return; // !!!!
                    }
              }
          });
        }

        // -------
        // PRIVATE

        private ActionListener _alr = null;
        private GfrHelpBroker _mhb = null;
    }

    // -------------------------------------------
    // END STATIC CLASS GfrDisplayHelpAfterTracking



    // ------------------------------------------
    // BEGIN STATIC CLASS GfrDisplayHelpFromSource

    static public class GfrDisplayHelpFromSource implements ActionListener
    {
        // ------
        // PUBLIC

        public GfrDisplayHelpFromSource(HelpBroker hbr)
        {
            if (hbr == null)
            {
                String str = "hbr == null";
                GfrCSH._LOGGER_.severe(str);
                JOptionPane.showMessageDialog(null, str, GfrResBundleLang.s_getInstance().getValue("word.error"), JOptionPane.ERROR_MESSAGE);
                System.exit(1);
            }

            if (! (hbr instanceof GfrHelpBroker))
            {
                String str = "! (hbr instanceof GfrHelpBroker)";
                GfrCSH._LOGGER_.severe(str);
                JOptionPane.showMessageDialog(null, str, GfrResBundleLang.s_getInstance().getValue("word.error"), JOptionPane.ERROR_MESSAGE);
                System.exit(1);
            }

            try
            {
                this._alr = new CSH.DisplayHelpFromSource(hbr);
            }

            catch(NullPointerException excNullPointer)
            {
                excNullPointer.printStackTrace();
                String str = excNullPointer.getMessage();
                GfrCSH._LOGGER_.severe(str);
                JOptionPane.showMessageDialog(null, str, GfrResBundleLang.s_getInstance().getValue("word.error"), JOptionPane.ERROR_MESSAGE);
                System.exit(1);
            }
        }

        @Override
        public void actionPerformed(ActionEvent evtAction)
        {
            if (this._alr == null)
            {
                String str = "this._alr == null";
                GfrCSH._LOGGER_.severe(str);
                JOptionPane.showMessageDialog(null, str, GfrResBundleLang.s_getInstance().getValue("word.error"), JOptionPane.ERROR_MESSAGE);
                System.exit(1);
            }

            try
            {
                this._alr.actionPerformed(evtAction);
            }
           
            catch(javax.help.BadIDException excBadId)
            {
               // TROUBLESHOOTING WITH JAVA HELP: not able to catch javaHelpExeception!
               excBadId.printStackTrace();
               String strBadId = excBadId.getMessage();
                GfrCSH._LOGGER_.severe(strBadId);
               
                JOptionPane.showMessageDialog(null, strBadId,
                        GfrResBundleLang.s_getInstance().getValue("word.error"),
                        JOptionPane.ERROR_MESSAGE);
               
                System.exit(1);
            }

            catch(Exception exc)
            {
                // TROUBLESHOOTING WITH JAVA HELP: not able to catch javaHelpExecption!
                String str = exc.getMessage();
                GfrCSH._LOGGER_.severe(str);
                JOptionPane.showMessageDialog(null, str,
                        GfrResBundleLang.s_getInstance().getValue("word.error"),
                        JOptionPane.ERROR_MESSAGE);
               
                System.exit(1);
            }

        }

        // -------
        // PRIVATE

        private ActionListener _alr = null;
    }

    // ----------------------------------------
    // END STATIC CLASS GfrDisplayHelpFromSource
}
TOP

Related Classes of org.geoforge.guihlp.GfrCSH$GfrDisplayHelpFromSource

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.