/*
* 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.guillc.wwd.util;
import gov.nasa.worldwind.Disposable;
import gov.nasa.worldwind.WorldWindow;
import gov.nasa.worldwind.avlist.AVKey;
import gov.nasa.worldwind.avlist.AVList;
import gov.nasa.worldwind.event.SelectEvent;
import gov.nasa.worldwind.event.SelectListener;
import gov.nasa.worldwind.layers.AnnotationLayer;
import gov.nasa.worldwind.layers.Layer;
import gov.nasa.worldwind.util.Logging;
import gov.nasa.worldwind.util.WWUtil;
import org.geoforge.guillc.panel.GfrPnlMainWwdAbs;
import org.geoforge.lang.handler.IGfrHandlerLifeCycleObject;
import org.geoforge.worldwind.render.GfrKeysRnd;
import org.geoforge.worldwind.util.OurToolTipAnnotation;
/**
*
* @author bantchao
*/
abstract public class OurToolTipControllerAbs implements
SelectListener,
Disposable,
IGfrHandlerLifeCycleObject
{
transient protected String _strHoverKey = AVKey.HOVER_TEXT;
transient private WorldWindow _wwwWindow_ = null;
transient private String _strRolloverKey_ = AVKey.ROLLOVER_TEXT;
transient private Object _objLastRolloverObject_;
transient private Object _objLastHoverObject_;
transient private AnnotationLayer layer;
transient private OurToolTipAnnotation _ottAnnotation_;
protected OurToolTipControllerAbs(
WorldWindow wwd)
{
super();
this._wwwWindow_ = wwd;
// TODO: fix up the lines above, ? finalise
this._strRolloverKey_ = gov.nasa.worldwind.avlist.AVKey.DETAIL_HINT;
this._strHoverKey = gov.nasa.worldwind.avlist.AVKey.DISPLAY_NAME;
}
@Override
public boolean init()
{
this._wwwWindow_.addSelectListener(this);
return true;
}
@Override
public void destroy()
{
if (this._wwwWindow_ != null)
{
this._wwwWindow_.removeSelectListener(this);
this._wwwWindow_ = null;
}
this._strHoverKey = null;
this._strRolloverKey_ = null;
}
@Override
public void dispose() // TODO: check for disposing
{
// this._wwwWindow_.removeSelectListener(this);
this._wwwWindow_.shutdown();
}
@Override
public void selected(SelectEvent evt)
{
if (this._strRolloverKey_ == null)
return;
try
{
if (evt.isRollover())
_handleRollover_(evt);
else if (evt.isHover())
_handleHover_(evt);
}
catch (Exception e)
{
e.printStackTrace();
// Wrap the handler in a try/catch to keep exceptions from bubbling up
Logging.logger().warning(e.getMessage() != null ? e.getMessage() : e.toString());
}
}
protected String _getHoverText(SelectEvent evt)
{
Object objTop = evt.getTopObject();
if (objTop == null)
return null;
if (! (objTop instanceof AVList))
return null;
return ((AVList) objTop).getStringValue(this._strHoverKey);
}
protected void _hideToolTip_()
{
if (this.layer != null)
{
this.layer.removeAllAnnotations();
this._removeLayer_(this.layer);
this.layer.dispose();
this.layer = null;
}
if (this._ottAnnotation_ != null)
{
this._ottAnnotation_.dispose();
this._ottAnnotation_ = null;
}
}
protected String _getRolloverText_(SelectEvent evt)
{
if (evt == null)
return null;
Object objTop = evt.getTopObject();
if (objTop == null)
return null;
if (!(objTop instanceof AVList))
return null;
AVList lst = (AVList) objTop;
String strValueRolloverValue = lst.getStringValue(this._strRolloverKey_);
return strValueRolloverValue;
}
protected void _addLayer_(Layer layer)
{
if (!this._wwwWindow_.getModel().getLayers().contains(layer))
GfrPnlMainWwdAbs.s_insertBeforeCompass(this._wwwWindow_, layer); // !!!!
}
private void _removeLayer_(Layer layer)
{
this._wwwWindow_.getModel().getLayers().remove(layer);
}
protected void _handleRollover_(SelectEvent evt) throws Exception
{
String strValue = _getRolloverText_(evt);
Object objTop = evt.getTopObject();
if (this._objLastRolloverObject_ != null)
{
if (this._objLastRolloverObject_ == evt.getTopObject() && !WWUtil.isEmpty(strValue))
return;
this._hideToolTip_();
this._objLastRolloverObject_ = null;
this._wwwWindow_.redraw();
}
if (strValue == null)
return;
this._objLastRolloverObject_ = objTop;
strValue = strValue.replace("\\n", "\n");
AVList lstTop = (AVList) objTop;
Boolean booHasTooltip = (Boolean) lstTop.getValue(GfrKeysRnd.STR_HAS_TOOLTIP);
if (booHasTooltip == null)
return;
boolean blnHasToolTip = booHasTooltip.booleanValue();
this._showToolTipIfAny_(evt, strValue, blnHasToolTip);
this._wwwWindow_.redraw();
}
private void _showToolTipIfAny_(
SelectEvent evtSelect,
String strText,
boolean blnHasToolTip) throws Exception
{
if (! blnHasToolTip)
return;
if (_ottAnnotation_ != null)
{
_ottAnnotation_.setText(strText);
_ottAnnotation_.setScreenPoint(evtSelect.getPickPoint());
}
else
{
_ottAnnotation_ = new OurToolTipAnnotation(strText);
}
if (layer == null)
{
layer = new AnnotationLayer();
layer.setPickEnabled(false);
}
layer.removeAllAnnotations();
layer.addAnnotation(_ottAnnotation_);
_addLayer_(layer);
}
protected void _handleHover_(SelectEvent event) throws Exception
{
if (this._objLastHoverObject_ != null)
{
if (this._objLastHoverObject_ == event.getTopObject())
return;
this._hideToolTip_();
this._objLastHoverObject_ = null;
this._wwwWindow_.redraw();
}
String strHoverText = _getHoverText(event);
if (strHoverText == null)
return;
AVList lstTop = (AVList) event.getTopObject();
Boolean booHasTooltip = (Boolean) lstTop.getValue(GfrKeysRnd.STR_HAS_TOOLTIP);
if (booHasTooltip == null)
return;
boolean blnHasToolTip = booHasTooltip.booleanValue();
this._objLastHoverObject_ = event.getTopObject();
strHoverText = strHoverText.replace("\\n", "\n");
this._showToolTipIfAny_(event, strHoverText,
blnHasToolTip);
this._wwwWindow_.redraw();
}
}