/*
* 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.Container;
import java.awt.Window;
import java.awt.event.ActionListener;
import java.util.logging.Logger;
import javax.help.*;
import javax.swing.*;
import javax.swing.text.EditorKit;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.StyleSheet;
import org.geoforge.guihlp.text.html.GfrStyleSheet;
import org.geoforge.awt.image.GfrFactoryIconAbs;
import org.geoforge.io.awt.image.GfrFactoryIconAppGfr;
import org.geoforge.lang.handler.IGfrHandlerLifeCycleObject;
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
*
*/
final public class GfrHelpBroker extends DefaultHelpBroker implements
IGfrHandlerLifeCycleObject
{
// ----
// begin: instantiate logger for this class
final private static Logger _LOGGER_ = Logger.getLogger(GfrHelpBroker.class.getName());
static
{
GfrHelpBroker._LOGGER_.addHandler(FileHandlerLogger.s_getInstance());
}
// end: instantiate logger for this class
// ----
@Override
public void destroy()
{
JFrame frm = _getFrameWindow();
if (frm == null)
{
return; // unreached
}
frm.setVisible(false);
this._sst = null;
}
@Override
public boolean init()
{
// should be in a separate thread, see DefaultHelpBroker.initPresentation()
// AND _assignFrameIcon() in the same thread, else window not yet accessible
// catching Issue #203
try
{
super.initPresentation();
}
catch(Exception exc)
{
exc.printStackTrace();
String str = exc.getMessage();
if (str.compareTo("javax.help.resources.Constants_en") ==0)
{
String strMessage = "Issue # 203, don't care";
//GfrHelpBroker._LOGGER_.info(strMessage);
}
else
{
//GfrHelpBroker._LOGGER_.severe(str);
return false;
}
}
if (! _assignFrameIcon())
{
String str = "! _assignFrameIcon()";
//GfrHelpBroker._LOGGER_.severe(str);
return false;
}
//GfrHelpBroker._LOGGER_.info("TODO: fix up assignStyleSheet()");
/*if (! assignStyleSheet())
{
String str = "! assignStyleSheet()";
GfrHelpBroker._LOGGER_.severe(str);
return false;
}*/
if (this._blnSetLAFSwing)
{
if (! _assignLAFBtnTlb())
{
String str = "this._blnSetLAFSwing && ! _assignLAFBtnTlb()";
//GfrHelpBroker._LOGGER_.severe(str);
return false;
}
}
// ending
return true;
}
/**
memo: could be called in a separated thread, don't worry about nil fields
**/
public boolean assignStyleSheet()
{
JHelp hlp = _getJHelp();
if (hlp == null)
{
String str = "hlp == null"; // !!!!!!!!!!
//GfrHelpBroker._LOGGER_.info(str);
return true;
}
JHelpContentViewer hcv = hlp.getContentViewer();
if (hcv == null)
{
String str = "hcv == null"; // !!!!!!!!!!
//GfrHelpBroker._LOGGER_.info(str);
return true;
}
Component[] cmpsChild = null;
// --
cmpsChild = hcv.getComponents();
if (cmpsChild == null)
{
String str = "cmpsChild == null"; // !!!!!!!!!!
//GfrHelpBroker._LOGGER_.info(str);
return true;
}
// -------------
// 1) scrollPane
JScrollPane spn = null;
for (int i=0; i<cmpsChild.length; i++)
{
Component cmpCur = cmpsChild[i];
if (cmpCur instanceof JScrollPane)
{
spn = (JScrollPane) cmpCur;
break;
}
}
cmpsChild = null;
if (spn == null)
{
String str = "spn == null"; // !!!!!!!!!!
//GfrHelpBroker._LOGGER_.info(str);
return true;
}
cmpsChild = spn.getComponents();
if (cmpsChild == null)
{
String str = "cmpsChild == null"; // !!!!!!!!!!
//GfrHelpBroker._LOGGER_.info(str);
return true;
}
// -----------
// 2) viewPort
JViewport vpt = null;
for (int i=0; i<cmpsChild.length; i++)
{
Component cmpCur = cmpsChild[i];
if (cmpCur instanceof JViewport)
{
vpt = (JViewport) cmpCur;
break;
}
}
cmpsChild = null;
if (vpt == null)
{
String str = "vpt == null"; // !!!!!!!!!!
//GfrHelpBroker._LOGGER_.info(str);
return true;
}
// --------------
// 3) JEditorPane
Component cmpView = vpt.getView();
if (cmpView == null)
{
String str = "cmpView == null"; // !!!!!!!!!!
//GfrHelpBroker._LOGGER_.info(str);
return true;
}
if (! (cmpView instanceof JEditorPane))
{
String str = "! (cmpView instanceof JEditorPane)";
GfrHelpBroker._LOGGER_.severe(str);
return false;
}
JEditorPane epn = (JEditorPane) cmpView;
// ----------------
// 4) HTMLEditorKit
EditorKit ekt = epn.getEditorKit();
if (ekt == null)
{
String str = "ekt == null"; // !!!!!!!!!!
//GfrHelpBroker._LOGGER_.info(str);
return true;
}
if (! (ekt instanceof HTMLEditorKit))
{
String str = "! (ekt instanceof HTMLEditorKit)";
GfrHelpBroker._LOGGER_.severe(str);
return false;
}
HTMLEditorKit ektHtml = (HTMLEditorKit) ekt;
// -------------------------------------
// 5) assign styleSheet to editorKitHtml
StyleSheet sstCur = ektHtml.getStyleSheet();
if (sstCur == null)
{
String str = "sstCur == null"; // !!!!!!!!!!
//GfrHelpBroker._LOGGER_.info(str);
return true;
}
if (this._sst == sstCur)
{
String str = "this._sst == sstCur"; // !!!!!!!!!!
//GfrHelpBroker._LOGGER_.info(str);
return true;
}
if (ektHtml != null) // !!!!
ektHtml.setStyleSheet(this._sst);
else
{
String str = "ektHtml == null"; // !!!!!!!!!!
//GfrHelpBroker._LOGGER_.info(str);
}
return true;
}
public GfrHelpBroker(HelpSet hst, boolean blnSetLAFSwing)
{
super(hst);
this._blnSetLAFSwing = blnSetLAFSwing;
this._sst = GfrStyleSheet.s_getDefault();
}
// ---------
// PROTECTED
/**
* Returns the default DisplayHelpFromFocus listener.
*
* @see enableHelpKey
*/
// OVERWRITE SUPERCLASS'S METHOD
@Override
protected ActionListener getDisplayHelpFromFocus()
{
if (super.displayHelpFromFocus == null)
super.displayHelpFromFocus = new GfrCSH.GfrDisplayHelpFromFocus(this);
return super.displayHelpFromFocus;
}
// OVERWRITE SUPERCLASS'S METHOD
@Override
protected ActionListener getDisplayHelpFromSource()
{
if (super.displayHelpFromSource == null)
super.displayHelpFromSource = new GfrCSH.GfrDisplayHelpFromSource(this);
return super.displayHelpFromSource;
}
// -------
// PRIVATE
private StyleSheet _sst = null;
private boolean _blnSetLAFSwing;
private boolean _assignFrameIcon()
{
ImageIcon imgIcon = GfrFactoryIconAppGfr.s_getHelpAll(GfrFactoryIconAbs.INT_SIZE_XXSMALL);
if (imgIcon == null)
{
String str = "imgIcon == null";
GfrHelpBroker._LOGGER_.severe(str);
return false;
}
JFrame frm = _getFrameWindow();
if (frm == null)
{
String str = "frm == null";
GfrHelpBroker._LOGGER_.severe(str);
return false;
}
frm.setIconImage(imgIcon.getImage());
// ending
return true;
}
private JFrame _getFrameWindow()
{
WindowPresentation pres = this.getWindowPresentation();
if (pres == null)
{
String str = "pres == null";
GfrHelpBroker._LOGGER_.severe(str);
return null;
}
Window win = pres.getHelpWindow();
if (win == null)
{
String str = "win == null";
GfrHelpBroker._LOGGER_.severe(str);
return null;
}
if (! (win instanceof JFrame))
{
String str = "! (win instanceof JFrame)";
GfrHelpBroker._LOGGER_.severe(str);
return null;
}
return (JFrame) win;
}
private JHelp _getJHelp()
{
JFrame frm = _getFrameWindow();
if (frm == null)
{
String str = "frm == null";
GfrHelpBroker._LOGGER_.severe(str);
return null;
}
Container cnt = frm.getContentPane();
if (cnt == null)
{
String str = "cnt == null";
GfrHelpBroker._LOGGER_.severe(str);
return null;
}
int intCount = cnt.getComponentCount();
if (intCount < 1)
{
String str = "intCount < 1";
GfrHelpBroker._LOGGER_.severe(str);
return null;
}
for (int i=0; i<intCount; i++)
{
Component cmpCur = cnt.getComponent(i);
if (cmpCur instanceof JHelp)
{
return (JHelp) cmpCur;
}
}
String str = "failed to get JHelp";
GfrHelpBroker._LOGGER_.severe(str);
return null;
}
// toolbar's buttons look and feel
private boolean _assignLAFBtnTlb()
{
JHelp hlp = _getJHelp();
if (hlp == null)
{
String str = "hlp == null";
//GfrHelpBroker._LOGGER_.info(str);
return true;
}
Component[] cmpsChild = null;
// --
cmpsChild = hlp.getComponents();
if (cmpsChild == null)
{
String str = "cmpsChild == null";
//GfrHelpBroker._LOGGER_.info(str);
return true;
}
// get toolbar
JToolBar tbr = null;
for (int i=0; i<cmpsChild.length; i++)
{
Component cmpCur = cmpsChild[i];
if (cmpCur instanceof JToolBar)
{
tbr = (JToolBar) cmpCur;
break;
}
}
if (tbr == null)
{
String str = "tbr == null";
//GfrHelpBroker._LOGGER_.info(str);
return true;
}
// get buttons
cmpsChild = tbr.getComponents();
if (cmpsChild == null)
{
String str = "cmpsChild == null";
//GfrHelpBroker._LOGGER_.info(str);
return true;
}
for (int i=0; i<cmpsChild.length; i++)
{
Component cmpCur = cmpsChild[i];
if (cmpCur instanceof JButton)
{
final JButton btnCur = (JButton) cmpCur;
btnCur.setContentAreaFilled(false);
btnCur.setBorderPainted(false);
btnCur.addMouseListener(new java.awt.event.MouseAdapter()
{
@Override
public void mouseEntered(java.awt.event.MouseEvent evtMouse)
{
if (btnCur.isEnabled())
{
btnCur.setBorderPainted(true);
btnCur.setContentAreaFilled(true);
}
}
@Override
public void mouseExited(java.awt.event.MouseEvent evtMouse)
{
btnCur.setBorderPainted(false);
btnCur.setContentAreaFilled(false);
}
});
}
}
// ending
return true;
}
}