/*
* 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.worldwind.render;
import org.geoforge.worldwind.handler.IGfrHandlerObjectRender;
import gov.nasa.worldwind.geom.LatLon;
import gov.nasa.worldwind.geom.Position;
import gov.nasa.worldwind.geom.Sector;
import gov.nasa.worldwind.render.BasicShapeAttributes;
import gov.nasa.worldwind.render.Material;
import gov.nasa.worldwind.render.SurfaceSector;
import java.awt.Color;
import java.awt.geom.Point2D;
import java.beans.PropertyChangeListener;
import java.util.Iterator;
import java.util.List;
import java.util.Observable;
import java.util.Observer;
import java.util.logging.Logger;
import org.geoforge.lang.handler.IGfrHandlerLifeCycleObject;
import org.geoforge.java.util.logging.filehandler.FileHandlerLogger;
import org.geoforge.mdl.event.GfrEvtMdlIdAbs;
import org.geoforge.mdldat.event.GfrEvtMdlIdDatRenamedTlo;
import org.geoforge.mdldsp.event.render.wwd.GfrEvtMdlIdDspRndWwdEarthShpDimTwo;
import org.geoforge.worldwind.handler.IGfrHandlerWwdEarthObjectGeoforgeRenderableLayer;
import org.geoforge.lang.util.geography.GfrUtilDmsOperation;
import org.geoforge.mdldat.event.GfrEvtMdlIdDatChangedGeometry;
import org.geoforge.mdldsp.event.render.wwd.EnuEvtDspRndWwdEarthShp;
import org.geoforge.mdldsp.event.render.wwd.EnuEvtDspRndWwdEarthDimTwo;
import org.geoforge.wrpbasprsdsp.render.wwd.GfrWrpBasPrsDspPrjRndWwdEarthShpDimTwo;
/**
*
* @author bantchao
*
* email: bantchao_AT_gmail.com
* ... please remove "_AT_" from the above string to get the right email address
*
*/
abstract public class GfrRndSurfaceSectorTloAbs extends SurfaceSector implements
IGfrHandlerLifeCycleObject,
IGfrHandlerWwdEarthObjectGeoforgeRenderableLayer,
Observer,
IGfrHandlerObjectRender
{
// ----
// begin: instantiate logger for this class
final private static Logger _LOGGER_ = Logger.getLogger(GfrRndSurfaceSectorTloAbs.class.getName());
static
{
GfrRndSurfaceSectorTloAbs._LOGGER_.addHandler(FileHandlerLogger.s_getInstance());
}
// end: instantiate logger for this class
// ----
private String _strId_ = null;
protected GfrRndSurfaceSectorTloAbs(
PropertyChangeListener lstShouldRedraw,
String strId,
String strName,
List<Position> lstPosition // should be changed by latLonMin & LatLonMax
)
throws Exception
{
super();
this._strId_ = strId;
Position posMin = lstPosition.get(0);
Position posMax = lstPosition.get(1);
Sector sec = Sector.boundingSector(posMin, posMax);
super.setSector(sec);
setAttributesThis();
setNameThis(strName);
setEnabledTooltipThis();
if (lstShouldRedraw != null)
super.addPropertyChangeListener(lstShouldRedraw);
}
@Override
public void update(Observable obs, Object objEvt)
{
// beg display
if (objEvt instanceof GfrEvtMdlIdDspRndWwdEarthShpDimTwo)
{
GfrEvtMdlIdDspRndWwdEarthShpDimTwo evt = (GfrEvtMdlIdDspRndWwdEarthShpDimTwo) objEvt;
String strId = evt.getId();
if (strId.compareTo(this._strId_) != 0)
return;
Object objWhat = evt.getWhat();
if (objWhat == EnuEvtDspRndWwdEarthShp.TOOLTIP)
{
Boolean boo = (Boolean) evt.getValue();
super.setValue(GfrKeysRnd.STR_HAS_TOOLTIP, boo.booleanValue());
// memo: no need to send event to redraw!
return;
}
if (objWhat == EnuEvtDspRndWwdEarthDimTwo.COLOR_IN) // what about transparency?
{
Color col = (Color) evt.getValue();
BasicShapeAttributes bsa = (BasicShapeAttributes) super.getAttributes();
bsa.setInteriorMaterial(new Material(col));
this.firePropertyChange(GfrShouldRedrawRnd.STR, null, null);
return;
}
if (objWhat == EnuEvtDspRndWwdEarthDimTwo.TRANSPARENCY_IN)
{
Float flo = (Float) evt.getValue();
float fltAlpha = flo.floatValue();
BasicShapeAttributes bsa = (BasicShapeAttributes) super.getAttributes();
bsa.setInteriorOpacity((double) fltAlpha);
this.firePropertyChange(GfrShouldRedrawRnd.STR, null, null);
return;
}
if (objWhat == EnuEvtDspRndWwdEarthDimTwo.COLOR_OUT) // what about transparency?
{
Color col = (Color) evt.getValue();
BasicShapeAttributes bsa = (BasicShapeAttributes) super.getAttributes();
bsa.setOutlineMaterial(new Material(col));
this.firePropertyChange(GfrShouldRedrawRnd.STR, null, null);
return;
}
if (objWhat == EnuEvtDspRndWwdEarthDimTwo.TRANSPARENCY_OUT)
{
Float flo = (Float) evt.getValue();
float fltAlpha = flo.floatValue();
BasicShapeAttributes bsa = (BasicShapeAttributes) super.getAttributes();
bsa.setOutlineOpacity((double) fltAlpha);
this.firePropertyChange(GfrShouldRedrawRnd.STR, null, null);
return;
}
return;
}
// end display
// beg data
GfrEvtMdlIdAbs objId = (GfrEvtMdlIdAbs) objEvt;
String strIdEvt = objId.getId();
if (strIdEvt.compareTo(this._strId_) != 0)
return;
if (objId instanceof GfrEvtMdlIdDatChangedGeometry)
{
/*
* memo:
* altPoint2d:
* . Point2D.Double[0].x ==> latMin
* . Point2D.Double[0].y ==> lonMin
*
* . Point2D.Double[1].x ==> latMax
* . Point2D.Double[1].y ==> lonMax
*/
GfrEvtMdlIdDatChangedGeometry evt = (GfrEvtMdlIdDatChangedGeometry) objEvt;
List<Point2D.Double> lstP2d = (List<Point2D.Double>) evt.getGeometry();
Point2D.Double p2dMin = lstP2d.get(0);
Point2D.Double p2dMax = lstP2d.get(1);
Position posMin = Position.fromDegrees(p2dMin.y, p2dMin.x);
Position posMax = Position.fromDegrees(p2dMax.y, p2dMax.x);
Sector sec = Sector.boundingSector(posMin, posMax);
super.setSector(sec);
this.firePropertyChange(GfrShouldRedrawRnd.STR, null, null);
return;
}
if (objId instanceof GfrEvtMdlIdDatRenamedTlo)
{
GfrEvtMdlIdDatRenamedTlo objRename = (GfrEvtMdlIdDatRenamedTlo) objId;
super.setValue(gov.nasa.worldwind.avlist.AVKey.DISPLAY_NAME, objRename.getValueNew());
// memo: no need to redraw, coz of just handling toolTip
return;
}
// end data
}
@Override
public String getIdObjectGeoforgeWwdEarth() { return this._strId_;}
@Override
public Position getPositionBarycentricObjectGeoforgeWwdEarth() throws Exception
{
return super.getReferencePosition();
}
@Override
public boolean init()
{
return true;
}
@Override
public void destroy()
{
}
@Override
public double getCharacteristicDimensionObjectGeoforgeRenderableLayer() throws Exception
{
Sector sec = super.getSector();
Iterator<? extends LatLon> itr = sec.asList().iterator();
if (! itr.hasNext())
{
String str = "! itr.hasNext()";
GfrRndSurfaceSectorTloAbs._LOGGER_.severe(str);
throw new Exception(str);
}
double dblMinLat = 500;
double dblMaxLat = -500;
double dblMinLon = 500;
double dblMaxLon = -500;
while (itr.hasNext())
{
LatLon llnCur = itr.next();
double[] dbls = llnCur.asDegreesArray();
if (dbls[0] < dblMinLat)//=a
dblMinLat = dbls[0];
if (dbls[1] < dblMinLon)//=c
dblMinLon = dbls[1];
if (dbls[0] > dblMaxLat)//=b
dblMaxLat = dbls[0];
if (dbls[1] > dblMaxLon)//=d
dblMaxLon = dbls[1];
}
double dist = GfrUtilDmsOperation.s_getDistanceFromDeg(dblMinLat, dblMinLon, dblMaxLat, dblMaxLon);
dist *= 1.5;
return dist;
}
public void setAttributesThis() throws Exception
{
BasicShapeAttributes bsa = new BasicShapeAttributes();
// beg out
bsa.setDrawOutline(true);
final double dblWidthOut = 1;
bsa.setOutlineWidth(dblWidthOut);
Color colRgbOut = GfrWrpBasPrsDspPrjRndWwdEarthShpDimTwo.getInstance().getColorOut(this._strId_);
bsa.setOutlineMaterial(new Material(colRgbOut));
float fltAlphaOut = GfrWrpBasPrsDspPrjRndWwdEarthShpDimTwo.getInstance().getTransparencyOut(this._strId_);
bsa.setOutlineOpacity((double) fltAlphaOut);
// end out
// beg in
bsa.setDrawInterior(true);
Color colRgbIn = GfrWrpBasPrsDspPrjRndWwdEarthShpDimTwo.getInstance().getColorIn(this._strId_);
bsa.setInteriorMaterial(new Material(colRgbIn));
float fltAlphaIn = GfrWrpBasPrsDspPrjRndWwdEarthShpDimTwo.getInstance().getTransparencyIn(this._strId_);
bsa.setInteriorOpacity((double) fltAlphaIn);
// end in
super.setAttributes(bsa);
}
@Override
public void setNameThis(String strName)
{
super.setValue(gov.nasa.worldwind.avlist.AVKey.DISPLAY_NAME, strName);
super.setValue(GfrKeysRnd.STR_HAS_CONTEXTUAL_MENU, true); // always true
}
@Override
public void setEnabledTooltipThis() throws Exception
{
boolean blnIsToolTip = GfrWrpBasPrsDspPrjRndWwdEarthShpDimTwo.getInstance().isTooltip(this._strId_);
super.setValue(GfrKeysRnd.STR_HAS_TOOLTIP, blnIsToolTip);
}
}