/*
* 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.panel;
import java.awt.Component;
import java.awt.Dimension;
import javax.swing.JToggleButton;
import javax.swing.JTree;
import org.geoforge.guillc.treenode.GfrNodAbs;
import org.geoforge.guillc.handler.IGfrHandlerToggableNode;
import org.geoforge.lang.handler.IGfrHandlerLifeCycleObject;
/**
*
* @author bantchao
*
* email: bantchao_AT_gmail.com
* ... please remove "_AT_" from the above string to get the right email address
*
*/
abstract public class PnlTreeCellRendererTogAbs extends PnlTreeCellRendererAbs
{
protected JToggleButton _tog = null;
protected PnlTreeCellRendererTogAbs()
{
super();
}
@Override
public boolean init()
{
if (!super.init())
return false;
if (! ((IGfrHandlerLifeCycleObject) this._tog).init())
return false;
this.add(this._tog);
return true;
}
@Override
public Component getTreeCellRendererComponent(JTree tree, Object value,
boolean isSelected, boolean expanded, boolean leaf, int row,
boolean hasFocus)
{
super.getTreeCellRendererComponent(tree, value, isSelected, expanded, leaf, row, hasFocus);
// Managing checkbox
GfrNodAbs nod = (GfrNodAbs) value;
if (nod instanceof IGfrHandlerToggableNode)
{
IGfrHandlerToggableNode nodChk = (IGfrHandlerToggableNode) nod;
if (!this._tog.isVisible())
this._tog.setVisible(true);
this._tog.setSelected(nodChk.isSelectedToggableNode());
}
else
{
this._tog.setVisible(false);
}
// Managing style
//if (nod instanceof NodeTreeDataRdrFolderMainCntAbs)
// super._lbl.setFont(new Font("Dialog", Font.BOLD, 12));
return this;
}
@Override
public Dimension getPreferredSize()
{
Dimension dimCheckbox = this._tog.getPreferredSize();
Dimension dimLabel = super._lbl.getPreferredSize();
return new Dimension(dimCheckbox.width + dimLabel.width,
(dimCheckbox.height < dimLabel.height ? dimLabel.height
: dimCheckbox.height));
}
@Override
public void doLayout()
{
Dimension dimCheckbox = this._tog.getPreferredSize();
Dimension dimLabel = super._lbl.getPreferredSize();
int intYCbx = 0;
int intYLbl = 0;
if (dimCheckbox.height < dimLabel.height)
{
intYCbx = (dimLabel.height - dimCheckbox.height) / 2;
}
else
{
intYLbl = (dimCheckbox.height - dimLabel.height) / 2;
}
this._tog.setLocation(0, intYCbx);
this._tog.setBounds(0, intYCbx, dimCheckbox.width, dimCheckbox.height);
if (this._tog.isVisible())
{
super._lbl.setLocation(dimCheckbox.width, intYLbl);
super._lbl.setBounds(dimCheckbox.width, intYLbl, dimLabel.width, dimLabel.height);
} else
{
super._lbl.setLocation(0, intYLbl);
super._lbl.setBounds(0, intYLbl, dimLabel.width, dimLabel.height);
}
}
}