/* Copyright (C) 2001, 2011 United States Government as represented by
the Administrator of the National Aeronautics and Space Administration.
All Rights Reserved.
*/
package org.geoforge.worldwind.awt.annotation;
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.geom.Position;
import gov.nasa.worldwind.layers.Layer;
import gov.nasa.worldwind.layers.LayerList;
import gov.nasa.worldwind.layers.RenderableLayer;
import gov.nasa.worldwind.layers.placename.PlaceNameLayer;
import gov.nasa.worldwind.pick.PickedObject;
import gov.nasa.worldwind.util.Logging;
import java.util.HashMap;
import javax.swing.JOptionPane;
import org.geoforge.lang.util.GfrResBundleLang;
import org.geoforge.java.lang.string.GfrUtlString;
import org.geoforge.worldwind.handler.IGfrHandlerPhotoAlbumRender;
import org.geoforge.worldwind.x.examples.util.SlideShowAnnotation;
import org.geoforge.worldwind.x.examples.util.SlideShowAnnotationController;
/**
*
* * @author dcollins
* @version $Id: GfrCtrAnnCntImgs.java 1 2011-07-16 23:22:47Z dcollins $
*
* modif: bantchao
*/
public class GfrCtrAnnCntImgs implements SelectListener
{
final static public String IMAGES = "Images";
final static public String KEY_MAP_SOURCE_TO_TITLE = "MAP_SOURCE_TO_TITLE";
// ATTN: TO BE REPLACED
private static void _s_insertBeforePlacenames_(WorldWindow wwd, Layer layer)
{
// Insert the layer into the layer list just before the placenames.
int compassPosition = 0;
LayerList layers = wwd.getModel().getLayers();
for (Layer l : layers)
{
if (l instanceof PlaceNameLayer)
compassPosition = layers.indexOf(l);
}
layers.add(compassPosition, layer);
}
@SuppressWarnings( {"TypeParameterExplicitlyExtendsObject"})
private static GfrAnnCnt _s_createImageAnnotation_(
GfrCtrAnnCntImgs ctrAnnotationImages,
WorldWindow www,
Position position,
String title,
Iterable sources,
HashMap<String, String> mapSourceToTitle) throws Exception
{
if (position == null)
{
String message = Logging.getMessage("nullValue.PositionIsNull");
Logging.logger().severe(message);
throw new IllegalArgumentException(message);
}
if (title == null)
{
String message = Logging.getMessage("nullValue.StringIsNull");
Logging.logger().severe(message);
throw new IllegalArgumentException(message);
}
if (sources == null)
{
String message = Logging.getMessage("nullValue.IterableIsNull");
Logging.logger().severe(message);
throw new IllegalArgumentException(message);
}
SlideShowAnnotation annotation = new SlideShowAnnotation(position);
annotation.setAlwaysOnTop(true);
annotation.getTitleLabel().setText(title);
SlideShowAnnotationController controller = new SlideShowAnnotationController(www, annotation,
sources, mapSourceToTitle);
return new GfrAnnCntImgs(ctrAnnotationImages, annotation, controller);
}
@SuppressWarnings( {"StringEquality"})
private static GfrAnnCnt _s_createContentAnnotation_(
GfrCtrAnnCntImgs ctrAnnotationImages,
WorldWindow www,
Position position,
AVList params) throws Exception
{
if (position == null)
{
String message = Logging.getMessage("nullValue.PositionIsNull");
Logging.logger().severe(message);
throw new IllegalArgumentException(message);
}
if (params == null)
{
String message = Logging.getMessage("nullValue.ParamsIsNull");
Logging.logger().severe(message);
throw new IllegalArgumentException(message);
}
String strType = params.getStringValue(AVKey.DATA_TYPE);
String strTitle = params.getStringValue(AVKey.TITLE);
Object objListUrlImgs = params.getValue(AVKey.URL);
HashMap<String, String> mapSourceToTitle = (HashMap<String, String>) params.getValue(
GfrCtrAnnCntImgs.KEY_MAP_SOURCE_TO_TITLE);
if (strType.compareTo(GfrCtrAnnCntImgs.IMAGES) == 0)
{
return GfrCtrAnnCntImgs._s_createImageAnnotation_(
ctrAnnotationImages, www, position, strTitle, (Iterable) objListUrlImgs, mapSourceToTitle);
}
return null;
}
// end static
private IGfrHandlerPhotoAlbumRender _icnHighlight_;
private RenderableLayer _rlrContent_;
//protected BasicDragger dragger;
private WorldWindow _www_ = null;
// called in PmlMainWwdEarth
public GfrCtrAnnCntImgs(WorldWindow www) throws Exception
{
super();
this._www_ = www;
this._rlrContent_ = new RenderableLayer();
GfrCtrAnnCntImgs._s_insertBeforePlacenames_(this._www_, this._rlrContent_);
this._www_.addSelectListener((SelectListener) this);
}
@SuppressWarnings( {"StringEquality"})
@Override
public void selected(SelectEvent evtSelect)
{
if (evtSelect == null)
return;
PickedObject topPickedObject = evtSelect.getTopPickedObject();
if (evtSelect.getEventAction().compareTo(SelectEvent.LEFT_PRESS) == 0)
{
if (topPickedObject != null && topPickedObject.getObject() instanceof IGfrHandlerPhotoAlbumRender)
{
IGfrHandlerPhotoAlbumRender selected = (IGfrHandlerPhotoAlbumRender) topPickedObject.getObject();
this._highlight_(selected);
}
else
{
this._highlight_(null);
}
}
else if (evtSelect.getEventAction().compareTo(SelectEvent.LEFT_DOUBLE_CLICK) == 0)
{
if (topPickedObject != null && topPickedObject.getObject() instanceof IGfrHandlerPhotoAlbumRender)
{
IGfrHandlerPhotoAlbumRender selected = (IGfrHandlerPhotoAlbumRender) topPickedObject.getObject();
this._highlight_(selected);
try
{
this._openResource_(selected);
}
catch(Exception exc)
{
exc.printStackTrace();
String strWarning = exc.getMessage();
// TODO: log
strWarning = GfrUtlString.s_formatExceptionMessageForDialog(strWarning);
JOptionPane.showMessageDialog(null, strWarning,
GfrResBundleLang.s_getInstance().getValue("word.error"), JOptionPane.ERROR_MESSAGE);
}
}
}
}
public RenderableLayer getContentLayer()
{
return this._rlrContent_;
}
// ---
protected void _closeResource(GfrAnnCnt content)
{
if (content == null)
return;
content.detach();
}
// ---
private void _highlight_(IGfrHandlerPhotoAlbumRender icon)
{
if (this._icnHighlight_ == icon)
return;
if (this._icnHighlight_ != null)
{
this._icnHighlight_.setHighlighted(false);
this._icnHighlight_ = null;
}
if (icon != null)
{
this._icnHighlight_ = icon;
this._icnHighlight_.setHighlighted(true);
}
this._www_.redraw();
}
private void _openResource_(IGfrHandlerPhotoAlbumRender hndPhotoAlbum) throws Exception
{
if (hndPhotoAlbum == null)
return;
GfrAnnCnt content = this._createContent_(hndPhotoAlbum.getPosition(), (AVList) hndPhotoAlbum);
if (content != null)
{
content.attach();
}
}
private GfrAnnCnt _createContent_(Position pos, AVList params) throws Exception
{
return GfrCtrAnnCntImgs._s_createContentAnnotation_(
(GfrCtrAnnCntImgs) this, this._www_, pos, params);
}
}