/*
* 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.treenode;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Observable;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;
import org.geoforge.guillc.handler.IGfrHandlerContainerObjectsVariableNode;
import org.geoforge.guillc.handler.IGfrHandlerToggableNode;
import org.geoforge.guillc.tree.GfrTreAbs;
import org.geoforge.java.util.logging.filehandler.FileHandlerLogger;
import org.geoforge.mdldat.event.GfrEvtMdlIdDatRenamedLloVar;
/**
*
* @author bantchao
*
* email: bantchao_AT_gmail.com
* ... please remove "_AT_" from the above string to get the right email address
*
*/
abstract public class GfrNodCtrFixFolderSetVarPrjAbs extends GfrNodCtrFixFolderAbs implements
IGfrHandlerContainerObjectsVariableNode
{
// ----
// begin: instantiate logger for this class
final private static Logger _LOGGER_ = Logger.getLogger(GfrNodCtrFixFolderSetVarPrjAbs.class.getName());
static
{
GfrNodCtrFixFolderSetVarPrjAbs._LOGGER_.addHandler(FileHandlerLogger.s_getInstance());
}
// end: instantiate logger for this class
// ----
protected String _strIdOwner = null;
transient protected ActionListener _alrControllerPerspective = null;
protected GfrNodCtrFixFolderSetVarPrjAbs(
ActionListener alrControllerSpcPrj,
ActionListener alrControllerPrs,
String userObject,
ImageIcon iin,
GfrTreAbs tre,
String strIdOwner)
{
super(alrControllerSpcPrj,
userObject,
iin,
tre);
this._alrControllerPerspective = alrControllerPrs;
this._strIdOwner = strIdOwner;
}
@Override
public void update(Observable obs, Object objEvt)
{
try
{
if (objEvt instanceof GfrEvtMdlIdDatRenamedLloVar)
{
GfrEvtMdlIdDatRenamedLloVar evt = (GfrEvtMdlIdDatRenamedLloVar) objEvt;
String strIdParent = evt.getIdParent();
if (strIdParent.compareTo(this._strIdOwner) != 0)
return;
String strIdChild = evt.getId();
String strNameNewChild = evt.getValueNew();
// ---
boolean blnGotIt = false;
for (int i=0; i<super.getChildCount(); i++)
{
GfrNodCtrMovAbs nodCur = (GfrNodCtrMovAbs) super.getChildAt(i);
String strIdCur = nodCur.getId();
if (strIdCur.compareTo(strIdChild) != 0)
continue;
blnGotIt = true;
break;
}
if (! blnGotIt)
return;
// ---
_renameObject(strIdChild, strNameNewChild);
return;
}
}
catch(Exception exc)
{
exc.printStackTrace();
String str = exc.getMessage();
GfrNodCtrFixFolderSetVarPrjAbs._LOGGER_.severe(str);
return;
}
super.update(obs, objEvt);
}
@Override
public boolean init()
{
if (! super.init())
return false;
try
{
addAllObjects();
}
catch(Exception exc)
{
exc.printStackTrace();
String str = exc.getMessage();
GfrNodCtrFixFolderSetVarPrjAbs._LOGGER_.severe(str);
return false;
}
return true;
}
@Override
public boolean removeObjectVariable(String strId) throws Exception
{
for (int i=0; i<super.getChildCount(); i++)
{
GfrNodCtrMovAbs nodCur = (GfrNodCtrMovAbs) super.getChildAt(i);
String strIdCur = (String) nodCur.getId();
if (strIdCur.compareTo(strId) != 0)
continue;
DefaultTreeModel dtm = (DefaultTreeModel) super._tree.getModel();
dtm.removeNodeFromParent(nodCur);
nodCur.releaseTransient();
nodCur.destroy();
return true;
}
return false;
}
@Override
public void removeAllObjects() throws Exception
{
if (super.getChildCount() < 1)
return;
ArrayList<GfrNodCtrAbs> nodsChild = new ArrayList<GfrNodCtrAbs>();
for (int i=0; i<super.getChildCount(); i++)
{
GfrNodCtrAbs nodCur = (GfrNodCtrAbs) super.getChildAt(i);
nodsChild.add(nodCur);
}
for (int i=0; i<nodsChild.size(); i++)
{
GfrNodCtrAbs nodCur = nodsChild.get(i);
nodCur.removeFromParent();
}
DefaultTreeModel dtm = (DefaultTreeModel) super._tree.getModel();
dtm.nodeStructureChanged(this);
// TODO: something wtith nodCur's children
for (int i=0; i<nodsChild.size(); i++)
{
GfrNodCtrAbs nodCur = nodsChild.get(i);
nodCur.releaseTransient();
nodCur.destroy();
nodCur = null;
}
nodsChild.clear();
nodsChild = null;
}
@Override
public void _addObjectVariableNode(GfrNodAbs nod) throws Exception
{
if (! nod.init())
{
GfrNodCtrFixFolderSetVarPrjAbs._LOGGER_.severe("! nod.init()");
throw new Exception("! nod.init()");
}
String strName = (String) nod.getUserObject();
int intPos = GfrSorterNodChildren.s_getPositionChildToAdd(this, strName);
DefaultTreeModel dtm = (DefaultTreeModel) super._tree.getModel();
dtm.insertNodeInto(nod, this, intPos);
// IN COMMENTS COZ OF DISPLAY TBRL, LABEL BEING TRUNCATED AT THE END !!! I.E., AT IMPORT TIME
//TreePath tphCur = new TreePath(nod.getPath());
//if (!super._tree.isVisible(tphCur))
{
TreePath tphThis = new TreePath(this.getPath());
super._tree.expandPath(tphThis);
}
}
protected void _renameObject(String strIdChild, String strNameNewChild)
throws Exception
{
for (int i=0; i<super.getChildCount(); i++)
{
GfrNodCtrMovAbs nodCur = (GfrNodCtrMovAbs) super.getChildAt(i);
String strIdCur = nodCur.getId();
if (strIdCur.compareTo(strIdChild) != 0)
continue;
String strNameCur = (String) nodCur.getUserObject();
if (strNameCur.compareTo(strNameNewChild) == 0) // !!!
return;
nodCur.setUserObject(strNameNewChild);
DefaultTreeModel dtm = (DefaultTreeModel) super._tree.getModel();
dtm.nodeChanged(nodCur);
return;
}
}
protected String[] _getIdsBrother() throws Exception
{
int intCount = super.getChildCount();
String[] strsId = new String[intCount];
if (intCount == 0)
return strsId;
for (int i=0; i<strsId.length; i++)
{
GfrNodCtrMovAbs nodCur = (GfrNodCtrMovAbs) super.getChildAt(i);
strsId[i] = nodCur.getId();
}
return strsId;
}
public void childrenAllCheck()
{
boolean blnShouldRedraw = _childrenAllCheck_(this);
if (blnShouldRedraw)
{
if (super._tree.isVisible())
super._tree.repaint();
}
}
private boolean _childrenAllCheck_(TreeNode nodParent)
{
boolean blnShouldRedraw = false;
for (int i=0; i<nodParent.getChildCount(); i++)
{
TreeNode nodCurChild = nodParent.getChildAt(i);
if (nodCurChild.isLeaf())
{
if (nodCurChild instanceof IGfrHandlerToggableNode)
{
IGfrHandlerToggableNode nodCheck = (IGfrHandlerToggableNode) nodCurChild;
if (nodCheck.isSelectedToggableNode())
continue;
nodCheck.setSelected(true);
blnShouldRedraw = true;
continue;
}
continue;
}
// folder
blnShouldRedraw |= _childrenAllCheck_(nodCurChild);
}
return blnShouldRedraw;
}
/*
* Could be optimezed with one call, for wrp calls inside
*/
private boolean _childrenAllUncheck_(TreeNode nodParent)
{
boolean blnShouldRedraw = false;
for (int i=0; i<nodParent.getChildCount(); i++)
{
TreeNode nodCurChild = nodParent.getChildAt(i);
if (nodCurChild.isLeaf())
{
if (nodCurChild instanceof IGfrHandlerToggableNode)
{
IGfrHandlerToggableNode nodCheck = (IGfrHandlerToggableNode) nodCurChild;
if (! nodCheck.isSelectedToggableNode())
continue;
nodCheck.setSelected(false);
blnShouldRedraw = true;
continue;
}
continue;
}
// folder
blnShouldRedraw |= _childrenAllUncheck_(nodCurChild);
}
return blnShouldRedraw;
}
public void childrenAllUncheck()
{
boolean blnShouldRedraw = _childrenAllUncheck_(this);
if (blnShouldRedraw)
{
if (super._tree.isVisible())
super._tree.repaint();
}
}
}