/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package org.geoforge.guillcogc.tabbedpane;
import java.awt.Component;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;
import java.util.logging.Logger;
import javax.swing.SwingUtilities;
import org.geoforge.guillc.optionpane.GfrOptionPaneAbs;
import org.geoforge.guillc.panel.GfrPnlStatusBarAbs;
import org.geoforge.guillc.tabbedpane.GfrTabDspSecClsYesAbs;
import org.geoforge.guillcogc.panel.GfrPnlPrintableSctAwtDspSecTloImgLgdLyrOgc;
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.GfrEvtMdlDtaRemovedAllTlo;
import org.geoforge.mdldat.event.GfrEvtMdlIdDatRemovedTlo;
import org.geoforge.mdldatogc.GfrMdlDatSetTlosOgcWms;
import org.geoforge.mdldsp.event.state.multiple.GfrEvtMdlTogMltSecCbx;
import org.geoforge.mdldsp.state.multiple.GfrMdlTogMltSecCbx;
import org.geoforge.wrpbasprsdatogc.GfrWrpBasDatObjNameTloWms;
import org.geoforge.wrpbasprsdsp.state.multiple.run.GfrWrpBasPrsDspPrjObjSecMltTogCbx;
import org.geoforge.wrpbasprssynogc.GfrWrpBasSynObjNameTloWms;
import org.geoforge.wrpbasprssynogc.GfrWrpBasTopSynOgcWmss;
/**
*
* @author robert
*/
public class GfrTabDspSecClsYesObjImgLgdLyrWms extends GfrTabDspSecClsYesAbs
{
// ----
// begin: instantiate logger for this class
final private static Logger _LOGGER_ = Logger.getLogger(GfrTabDspSecClsYesObjImgLgdLyrWms.class.getName());
static
{
GfrTabDspSecClsYesObjImgLgdLyrWms._LOGGER_.addHandler(FileHandlerLogger.s_getInstance());
}
// end: instantiate logger for this class
// ----
public GfrTabDspSecClsYesObjImgLgdLyrWms(
GfrPnlStatusBarAbs pnlStatusBar,
String strIdViewer,
ActionListener alrControllerSpcPrj
)
{
super(pnlStatusBar, strIdViewer);
GfrMdlDatSetTlosOgcWms.getInstance().addObserver( (Observer) this);
}
@Override
public boolean tabAboutToClose(int intIndex)
{
final GfrPnlPrintableSctAwtDspSecTloImgLgdLyrOgc pnl = (GfrPnlPrintableSctAwtDspSecTloImgLgdLyrOgc) super.getComponent(intIndex);
_tabAboutToCloseInvokedLater_(pnl);
return super.tabAboutToClose(intIndex);
// CANNOT USE THIS FOR NOW COZ OF SERIALIZATION TBRLS
/*String strTitle = getTabTitleAt(intIndex);
String strMessage = "You are about to close '" + strTitle
+ "'\nDo you want to proceed ?";
int intChoice = JOptionPane.showConfirmDialog(null, strMessage,
"Confirmation Dialog", JOptionPane.INFORMATION_MESSAGE);
System.out.println("intChoice=" + intChoice);
return intChoice == 0; // if returned false tab closing will be
// canceled
*/
}
private void _tabAboutToCloseInvokedLater_(final GfrPnlPrintableSctAwtDspSecTloImgLgdLyrOgc pnl)
{
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
try
{
String strIdLgd = pnl.getIdLegendLayer();
GfrMdlTogMltSecCbx.getInstance().doJob(GfrTabDspSecClsYesObjImgLgdLyrWms.this._strIdViewer_, strIdLgd, false);
pnl.destroy();
}
catch(Exception exc)
{
exc.printStackTrace();
String strException = exc.getMessage();
GfrTabDspSecClsYesObjImgLgdLyrWms._LOGGER_.severe(strException);
GfrOptionPaneAbs.s_showDialogError(null, strException);
}
}
});
}
@Override
protected void _load() throws Exception
{
String[] strsIdLyr = null;
// ---
strsIdLyr = GfrWrpBasTopSynOgcWmss.getInstance().getIdsLayerLeaf();
_loadLegendLayerCandidatesLeaf(strsIdLyr);
// ---
strsIdLyr = GfrWrpBasTopSynOgcWmss.getInstance().getIdsLayerFolder();
_loadLegendLayerCandidatesFolder(strsIdLyr);
}
@Override
public void update(Observable obs, Object objEvt)
{
if (objEvt instanceof GfrEvtMdlTogMltSecCbx)
{
GfrEvtMdlTogMltSecCbx evt = (GfrEvtMdlTogMltSecCbx) objEvt;
String strIdViewer = evt.getKeyViewer();
if (strIdViewer.compareTo(super._strIdViewer_) != 0)
return;
String strIdKey = evt.getKeyObject();
boolean blnValue = evt.getValueObject();
// check whether tlos is checked or not
try
{
if (GfrWrpBasTopSynOgcWmss.getInstance().containsTlo(strIdKey))
{
if (! blnValue)
_updateUncheckedOrRemovedTlo(strIdKey);
else
_updateCheckedOrAddedTlo(strIdKey);
return;
}
}
catch(Exception exc)
{
exc.printStackTrace();
String strMessage = exc.getMessage();
GfrTabDspSecClsYesObjImgLgdLyrWms._LOGGER_.warning(strMessage);
GfrOptionPaneAbs.s_showDialogWarning(null, strMessage);
return;
}
// update legends
try
{
_setVisibleLegend(strIdKey, blnValue);
}
catch(Exception exc)
{
exc.printStackTrace();
GfrTabDspSecClsYesObjImgLgdLyrWms._LOGGER_.warning(exc.getMessage());
String strMessage = "";
if (blnValue)
strMessage += "Sorry, failed to show map legend";
else
strMessage += "Sorry, failed to hide map legend";
GfrOptionPaneAbs.s_showDialogWarning(null, strMessage);
}
return;
}
if (objEvt instanceof GfrEvtMdlIdDatRemovedTlo)
{
String strIdTloRemoved = ((GfrEvtMdlIdAbs) objEvt).getId();
try
{
_updateUncheckedOrRemovedTlo(strIdTloRemoved);
}
catch(Exception exc)
{
exc.printStackTrace();
String strMessage = exc.getMessage();
GfrTabDspSecClsYesObjImgLgdLyrWms._LOGGER_.warning(strMessage);
GfrOptionPaneAbs.s_showDialogWarning(null, strMessage);
}
return;
}
if (objEvt instanceof GfrEvtMdlDtaRemovedAllTlo)
{
ArrayList<Component> alt = new ArrayList<Component>();
for (int i=0; i<super.getComponentCount(); i++)
{
Component cmpCur = super.getComponentAt(i);
alt.add(cmpCur);
}
if (alt.isEmpty())
return;
super.removeAll();
for (Component cmpCur: alt)
{
((IGfrHandlerLifeCycleObject) cmpCur).destroy();
}
alt.clear();
return;
}
}
@Override
public void destroy()
{
super.destroy();
GfrMdlDatSetTlosOgcWms.getInstance().deleteObserver( (Observer) this);
}
protected GfrPnlPrintableSctAwtDspSecTloImgLgdLyrOgc _addLegend(
String strIdTlo,
String strIdLgd,
String strTitleLgd) throws Exception
{
GfrPnlPrintableSctAwtDspSecTloImgLgdLyrOgc pnlDsp = new GfrPnlPrintableSctAwtDspSecTloImgLgdLyrOgc(
super._pnlStatusBar_,
super._strIdViewer_,
strIdTlo,
strIdLgd);
if (! pnlDsp.init())
{
String strError = "! pnlDsp.init()";
GfrTabDspSecClsYesObjImgLgdLyrWms._LOGGER_.severe(strError);
throw new Exception(strError);
}
super.addTab(strTitleLgd, pnlDsp);
return pnlDsp;
}
protected void _setVisibleLegend(String strIdLgdToHandle, boolean bln) throws Exception
{
if (! bln)
{
for (int i=0; i<super.getComponentCount(); i++)
{
GfrPnlPrintableSctAwtDspSecTloImgLgdLyrOgc pnlCur = (GfrPnlPrintableSctAwtDspSecTloImgLgdLyrOgc) super.getComponent(i);
String strIdCur = pnlCur.getIdLegendLayer();
if (strIdCur.compareTo(strIdLgdToHandle) != 0)
continue;
super.remove(pnlCur);
pnlCur.destroy();
if (super.getComponentCount() == 0)
super.setSize(super.getMinimumSize());
return;
}
return;
}
// bln = true
String[] strsIdLyrLeaf = GfrWrpBasTopSynOgcWmss.getInstance().getIdsLayerLeaf();
if (strsIdLyrLeaf!=null && strsIdLyrLeaf.length>0)
{
for (String strIdLyrCur: strsIdLyrLeaf)
{
String strIdLgdCur = GfrWrpBasDatObjNameTloWms.s_getIdChildLegendFromIdOwnerLayer(strIdLyrCur);
if (strIdLgdCur.compareTo(strIdLgdToHandle) != 0)
continue;
String strIdTlo = GfrWrpBasTopSynOgcWmss.getInstance().getIdTloOwnerFromLayerLeaf(strIdLyrCur);
String strTitle = GfrWrpBasSynObjNameTloWms.getInstance().getNameImageLegendLayerLeaf(strIdLgdToHandle);
GfrPnlPrintableSctAwtDspSecTloImgLgdLyrOgc pnlDsp = _addLegend(strIdTlo, strIdLgdToHandle, strTitle);
super.setSelectedComponent(pnlDsp);
return;
} // end of for
}
String[] strsIdLyrFolder = GfrWrpBasTopSynOgcWmss.getInstance().getIdsLayerFolder();
if (strsIdLyrFolder!=null && strsIdLyrFolder.length>0)
{
for (String strIdLyrCur: strsIdLyrFolder)
{
String strIdLgdCur = GfrWrpBasDatObjNameTloWms.s_getIdChildLegendFromIdOwnerLayer(strIdLyrCur);
if (strIdLgdCur.compareTo(strIdLgdToHandle) != 0)
continue;
String strIdTlo = GfrWrpBasTopSynOgcWmss.getInstance().getIdTloOwnerFromLayerFolder(strIdLyrCur);
String strTitle = GfrWrpBasSynObjNameTloWms.getInstance().getNameImageLegendLayerFolder(strIdLgdToHandle);
GfrPnlPrintableSctAwtDspSecTloImgLgdLyrOgc pnlDsp = _addLegend(strIdTlo, strIdLgdToHandle, strTitle);
super.setSelectedComponent(pnlDsp);
} // end of for
}
}
protected void _loadLegendLayerCandidatesLeaf(String[] strsIdLyr) throws Exception
{
if (strsIdLyr==null || strsIdLyr.length<1)
return;
for (int i=0; i<strsIdLyr.length; i++)
{
String strIdLgdCur = GfrWrpBasDatObjNameTloWms.s_getIdChildLegendFromIdOwnerLayer(strsIdLyr[i]);
if (! GfrWrpBasPrsDspPrjObjSecMltTogCbx.getInstance().isEnabled(super._strIdViewer_, strIdLgdCur))
continue;
String strIdTlo = GfrWrpBasTopSynOgcWmss.getInstance().getIdTloOwnerFromLayerLeaf(strsIdLyr[i]);
String strTitle = GfrWrpBasSynObjNameTloWms.getInstance().getNameImageLegendLayerLeaf(strIdLgdCur);
_addLegend(strIdTlo, strIdLgdCur, strTitle);
}
}
protected void _loadLegendLayerCandidatesFolder(String[] strsIdLyr) throws Exception
{
if (strsIdLyr==null || strsIdLyr.length<1)
return;
for (int i=0; i<strsIdLyr.length; i++)
{
String strIdLgdCur = GfrWrpBasDatObjNameTloWms.s_getIdChildLegendFromIdOwnerLayer(strsIdLyr[i]);
if (! GfrWrpBasPrsDspPrjObjSecMltTogCbx.getInstance().isEnabled(super._strIdViewer_, strIdLgdCur))
continue;
String strIdTlo = GfrWrpBasTopSynOgcWmss.getInstance().getIdTloOwnerFromLayerFolder(strsIdLyr[i]);
String strTitle = GfrWrpBasSynObjNameTloWms.getInstance().getNameImageLegendLayerFolder(strIdLgdCur);
_addLegend(strIdTlo, strIdLgdCur, strTitle);
}
}
private void _updateCheckedOrAddedTlo(String strIdTlo) throws Exception
{
String[] strsIdLyr = null;
// ---
strsIdLyr = GfrWrpBasSynObjNameTloWms.getInstance().getAllIdsLayerLeafFromTop(strIdTlo);
_loadLegendLayerCandidatesLeaf(strsIdLyr);
// ---
strsIdLyr = GfrWrpBasSynObjNameTloWms.getInstance().getAllIdsLayerFolderFromTop(strIdTlo);
_loadLegendLayerCandidatesFolder(strsIdLyr);
}
private void _updateUncheckedOrRemovedTlo(String strIdTlo) throws Exception
{
ArrayList<Component> alt = new ArrayList<Component>();
for (int i=0; i<super.getComponentCount(); i++)
{
GfrPnlPrintableSctAwtDspSecTloImgLgdLyrOgc pnlDspCur = (GfrPnlPrintableSctAwtDspSecTloImgLgdLyrOgc) super.getComponentAt(i);
String strIdTLoCur = pnlDspCur.getIdTlo();
if (strIdTLoCur.compareTo(strIdTlo) != 0)
continue;
alt.add(pnlDspCur);
}
if (alt.isEmpty())
return;
for (Component cmpCur: alt)
{
super.remove(cmpCur);
}
for (Component cmpCur: alt)
{
((IGfrHandlerLifeCycleObject) cmpCur).destroy();
}
alt.clear();
}
}