/*
* 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.actionmanager;
import java.awt.event.ActionListener;
import java.beans.EventHandler;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;
import javax.swing.Action;
import org.geoforge.guillc.action.GfrActDlg;
import org.geoforge.guillc.action.GfrActDlgLloAbs;
import org.geoforge.guillc.actioncontroller.GfrAcrAbs;
import org.geoforge.guillc.optionpane.GfrOptionPaneAbs;
import org.geoforge.lang.handler.IGfrHandlerLifeCycleObject;
import org.geoforge.java.util.logging.filehandler.FileHandlerLogger;
/**
*
* @author bantchao
*
* email: bantchao_AT_gmail.com
* ... please remove "_AT_" from the above string to get the right email address
*
*/
abstract public class GfrAmrAbs implements
IGfrHandlerLifeCycleObject
{
// ----
// begin: instantiate logger for this class
final private static Logger _LOGGER_ = Logger.getLogger(GfrAmrAbs.class.getName());
static
{
GfrAmrAbs._LOGGER_.addHandler(FileHandlerLogger.s_getInstance());
}
// end: instantiate logger for this class
// ----
final private static String _STR_SUFFIX_METHOD_ = "Handler";
private GfrActDlgLloAbs[] _acts_ = null;
private GfrAcrAbs _acr_ = null;
private Map<String, Action> _mapIdToAction_;
public void setActionController(GfrAcrAbs acr) { this._acr_ = acr; }
protected GfrAmrAbs(GfrActDlgLloAbs[] acts)
throws Exception
{
super();
this._mapIdToAction_ = new HashMap<String, Action>();
this._acts_ = acts;
}
protected GfrAmrAbs(GfrActDlgLloAbs[] acts1, GfrActDlgLloAbs[] acts2)
throws Exception
{
super();
this._mapIdToAction_ = new HashMap<String, Action>();
this._acts_ = new GfrActDlgLloAbs[acts1.length + acts2.length];
for (int i=0; i<acts1.length; i++)
this._acts_[i] = acts1[i];
int intCount = acts1.length;
for (int i=0; i<acts2.length; i++)
{
this._acts_[intCount++] = acts2[i];
}
}
protected void _registerCallback()
{
for (int i=0; i<this._acts_.length; i++)
this._registerCallback_(this._acts_[i],
this._acts_[i].getIdShort());
}
protected void _unregisterCallback()
{
for (int i=0; i<this._acts_.length; i++)
this._unregisterCallback_(this._acts_[i],
this._acts_[i].getIdShort());
}
protected void _loadActions() throws Exception
{
for (int i=0; i<this._acts_.length; i++)
this._addAction_(this._acts_[i].getId(),
this._acts_[i]);
}
protected void _unloadActions()
{
for (int i=0; i<this._acts_.length; i++)
this._mapIdToAction_.remove(this._acts_[i].getId());
}
public void load() throws Exception
{
_loadActions();
_registerCallback();
}
public void unload() throws Exception
{
_unregisterCallback();
_unloadActions();
}
protected void _unregisterCallback_(GfrActDlgLloAbs act, String strIdShort)
{
Object objId = act.getId();
String strMethod = strIdShort + GfrAmrAbs._STR_SUFFIX_METHOD_;
GfrActDlg dan = _getActionDelegateCandidate_(objId);
if (dan == null)
{
String str = "dan == null, objId=" + objId.getClass().toString();
str += "\nobjHandler.getClass().toString()=" + this._acr_.getClass().toString();
str += "\nstrMethod=" + strMethod;
GfrAmrAbs._LOGGER_.severe(str);
GfrOptionPaneAbs.s_showDialogError(null, str);
return;
}
}
protected void _registerCallback_(GfrActDlgLloAbs act, String strIdShort)
{
Object objId = act.getId();
String strMethod = strIdShort + GfrAmrAbs._STR_SUFFIX_METHOD_;
GfrActDlg dan = _getActionDelegateCandidate_(objId);
if (dan == null)
{
String str = "dan == null, objId=" + objId.getClass().toString();
str += "\nobjHandler.getClass().toString()=" + this._acr_.getClass().toString();
str += "\nstrMethod=" + strMethod;
GfrAmrAbs._LOGGER_.severe(str);
GfrOptionPaneAbs.s_showDialogError(null, str);
return;
}
// Create a new ActionListener using the dynamic proxy api.
ActionListener alr = EventHandler.create(
ActionListener.class, this._acr_, strMethod);
dan.addActionListener(alr, strMethod);
}
@Override
public boolean init()
{
if (this._acr_ == null)
{
String str = "DEV CODING ERROR: this._acr == null";
GfrAmrAbs._LOGGER_.severe(str);
return false;
}
return true;
}
@Override
public void destroy()
{
this._acr_ = null;
if (this._mapIdToAction_ != null)
{
this._mapIdToAction_.clear();
this._mapIdToAction_ = null;
}
this._acts_ = null;
}
// ----
private void _addAction_(String strId, Action act)
{
this._mapIdToAction_.put(strId, act);
}
private GfrActDlg _getActionDelegateCandidate_(Object objId)
{
Action act = _getAction_(objId);
if (act == null)
{
String str = "act == null, objId=" + (String) objId;
GfrAmrAbs._LOGGER_.severe(str);
return null;
}
if (act instanceof GfrActDlg)
{
return (GfrActDlg) act;
}
String str = "! act instanceof ActDlg: " + act.getClass().toString();
GfrAmrAbs._LOGGER_.severe(str);
return null;
}
private Action _getAction_(Object objId)
{
String strId = (String) objId;
Action act = this._mapIdToAction_.get(strId);
if (act == null)
{
String str = "act == null, objId=" + (String) objId;
GfrAmrAbs._LOGGER_.severe(str);
return null;
}
return act;
}
}