/*
* 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.layer;
import gov.nasa.worldwind.geom.LatLon;
import gov.nasa.worldwind.render.Renderable;
import java.beans.PropertyChangeListener;
import java.util.HashMap;
import java.util.Observable;
import java.util.Set;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import org.geoforge.lang.handler.IGfrHandlerLifeCycleObject;
import org.geoforge.lang.util.GfrResBundleLang;
import org.geoforge.java.util.logging.filehandler.FileHandlerLogger;
import org.geoforge.java.lang.string.GfrUtlString;
import org.geoforge.mdl.event.GfrEvtMdlIdAbs;
import org.geoforge.mdldat.event.*;
import org.geoforge.worldwind.handler.IGfrHandlerWwdEarthObjectGeoforgeAbs;
import org.geoforge.worldwind.render.GfrLabeledPathRndDimOneLineAbs;
import org.geoforge.worldwind.render.GfrRndSurfacePolylineWisNoMloAbs;
import org.geoforge.worldwind.render.GfrShouldRedrawRnd;
/**
*
* @author bantchao
*
* email: bantchao_AT_gmail.com
* ... please remove "_AT_" from the above string to get the right email address
*
*/
abstract public class GfrRlrObjTloRndMltSurfacePolylineMlosAbs extends GfrRlrObjTloRndMltSurfacePolylineAbs
{
// ----
// begin: instantiate logger for this class
final private static Logger _LOGGER_ = Logger.getLogger(GfrRlrObjTloRndMltSurfacePolylineMlosAbs.class.getName());
static
{
GfrRlrObjTloRndMltSurfacePolylineMlosAbs._LOGGER_.addHandler(FileHandlerLogger.s_getInstance());
}
// end: instantiate logger for this class
// ----
abstract protected void _addObject(String strId) throws Exception;
public HashMap<String, Iterable<? extends LatLon>> getMapIdsToLocations()
{
HashMap<String, Iterable<? extends LatLon>> mapIdToPositions = new HashMap<String, Iterable<? extends LatLon>>();
if (this._lstObject==null || this._lstObject.isEmpty())
return mapIdToPositions;
for (Renderable rndObjectCur: this._lstObject)
{
GfrRndSurfacePolylineWisNoMloAbs rndMloCur = (GfrRndSurfacePolylineWisNoMloAbs) rndObjectCur;
String strIdCurMlo = rndMloCur.getIdObjectGeoforgeWwdEarth();
Iterable<? extends LatLon> lstPosCur = rndMloCur.getLocations();
mapIdToPositions.put(strIdCurMlo, lstPosCur);
}
return mapIdToPositions;
}
protected GfrRlrObjTloRndMltSurfacePolylineMlosAbs(
PropertyChangeListener lstShouldRedraw,
String strId) throws Exception
{
super(lstShouldRedraw, strId);
if (lstShouldRedraw != null)
super.addPropertyChangeListener(lstShouldRedraw);
}
@Override
public void destroy()
{
if (super._lstShouldRedraw != null)
super.removePropertyChangeListener(super._lstShouldRedraw);
super.destroy();
}
@Override
public void update(Observable obs, Object objEvt)
{
try
{
if (objEvt instanceof GfrEvtMdlIdDatChangedGeometry)
{
GfrEvtMdlIdDatChangedGeometry evt = (GfrEvtMdlIdDatChangedGeometry) objEvt;
String strIdParent = evt.getId();
if (strIdParent.compareTo(super._strId) != 0)
return;
// to optimize
_removeAllObjects_();
Object objGeom = evt.getGeometry();
HashMap<String, Iterable<? extends LatLon>> mapIdsToLocations =
(HashMap<String, Iterable<? extends LatLon>>) objGeom;
Set<String> setIds = mapIdsToLocations.keySet();
for (String strIdChildCur: setIds)
{
_addObject(strIdChildCur);
}
this.firePropertyChange(GfrShouldRedrawRnd.STR, null, null);
}
if (objEvt instanceof GfrEvtMdlIdDatAddedMlo)
{
GfrEvtMdlIdDatAddedMlo evt = (GfrEvtMdlIdDatAddedMlo) objEvt;
String strIdParent = evt.getIdParent();
if (strIdParent.compareTo(super._strId) != 0)
return;
String strIdChild = evt.getId();
_addObject(strIdChild);
return;
}
if (objEvt instanceof GfrEvtMdlIdDatRemovedMlo)
{
GfrEvtMdlIdDatRemovedMlo evt = (GfrEvtMdlIdDatRemovedMlo) objEvt;
String strIdParent = evt.getIdParent();
if (strIdParent.compareTo(super._strId) != 0)
return;
String strIdChild = evt.getId();
_removeObject_(strIdChild);
return;
}
if (objEvt instanceof GfrEvtMdlIdDatRenamedMlo)
{
// don't care
return;
/*GfrEvtMdlIdDtaRenamedMlo evt = (GfrEvtMdlIdDtaRenamedMlo) objEvt;
String strIdParent = evt.getIdParent();
if (strIdParent.compareTo(super._strId) != 0)
return;
String strIdChild = evt.getId();
String strNameNewChild = evt.getNameNew();
_renameObject_(strIdChild, strNameNewChild);
return;*/
}
if (objEvt instanceof GfrEvtMdlIdDatRemovedAllMlo)
{
GfrEvtMdlIdAbs evt = (GfrEvtMdlIdAbs) objEvt;
String strId = evt.getId();
if (strId.compareTo(super._strId) != 0)
return;
_removeAllObjects_();
return;
}
}
catch (Exception exc)
{
exc.printStackTrace();
String str = exc.getMessage();
GfrRlrObjTloRndMltSurfacePolylineMlosAbs._LOGGER_.severe(str);
//--
str = GfrUtlString.s_formatExceptionMessageForDialog(str);
JOptionPane.showMessageDialog(
null,
str,
GfrResBundleLang.s_getInstance().getValue("word.error"),
JOptionPane.ERROR_MESSAGE);
return;
}
super.update(obs, objEvt);
}
// beg private
private void _removeObject_(String strId) throws Exception
{
for (Renderable rndCur : super._lstObject)
{
IGfrHandlerWwdEarthObjectGeoforgeAbs rndCurObject = (IGfrHandlerWwdEarthObjectGeoforgeAbs) rndCur;
String strIdCur = rndCurObject.getIdObjectGeoforgeWwdEarth();
if (strIdCur.compareTo(strId) != 0)
continue;
super.removeRenderable(rndCur);
super._lstObject.remove(rndCur);
((IGfrHandlerLifeCycleObject) rndCurObject).destroy();
break;
}
for (Renderable rndCur : super._lstAnnotation)
{
GfrLabeledPathRndDimOneLineAbs rndCurAnnotation = (GfrLabeledPathRndDimOneLineAbs) rndCur;
String strIdCur = rndCurAnnotation.getId();
if (strIdCur.compareTo(strId) != 0)
continue;
super.removeRenderable(rndCur);
super._lstAnnotation.remove(rndCur);
rndCurAnnotation.destroy();
break;
}
}
private void _removeAllObjects_()
{
// !!! ATTN: memory leaks !!!!!!
super.removeAllRenderables();
}
}