/*
* 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.tree;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.logging.Logger;
import javax.swing.JMenuItem;
import javax.swing.SwingUtilities;
import javax.swing.event.TreeExpansionListener;
import javax.swing.event.TreeModelListener;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;
import org.geoforge.guillc.handler.IGfrHandlerCloseSpace;
import org.geoforge.guillc.handler.IGfrHandlerOpenSpaceAny;
import org.geoforge.guillc.optionpane.GfrOptionPaneAbs;
import org.geoforge.guillc.popupmenu.GfrPmuCtlCtrAbs;
import org.geoforge.guillc.treenode.GfrNodAbs;
import org.geoforge.guillc.treenode.GfrNodCtrAbs;
import org.geoforge.guillc.treenode.GfrNodCtrFixFolderRootAbs;
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
*
*/
/*
*
* "Ctr" mewns "Control"
*/
abstract public class GfrTreCtrTopSpcAbs extends GfrTreAbs implements
ActionListener,
IGfrHandlerCloseSpace,
IGfrHandlerOpenSpaceAny
{
// ----
// begin: instantiate logger for this class
final private static Logger _LOGGER_ = Logger.getLogger(GfrTreCtrTopSpcAbs.class.getName());
static
{
GfrTreCtrTopSpcAbs._LOGGER_.addHandler(FileHandlerLogger.s_getInstance());
}
// end: instantiate logger for this class
// ----
abstract public boolean isAllowedFolderAllCollapse() throws Exception;
abstract public boolean isAllowedFolderAllExpand() throws Exception;
abstract public void folderAllCollapse();
abstract public void folderAllExpand();
protected ActionListener _alrParentPanelMvc = null;
transient private MouseMenuHandler _hnd = null;
protected boolean _blnIsOpen = false;
protected GfrTreCtrTopSpcAbs(
GfrNodAbs nodRoot,
ActionListener alrParentPanelMvc,
TreeSelectionListener lstTreeSelectionToolbar,
TreeExpansionListener lstTreeExpansionToolbar,
TreeModelListener lstTreeModelToolbar
)
{
super(nodRoot);
this._alrParentPanelMvc = alrParentPanelMvc;
//this._mapNodeToPopupmenu_ = new HashMap<DefaultMutableTreeNode, GfrPmuCtlCtrAbs>();
if (lstTreeSelectionToolbar != null)
super.addTreeSelectionListener(lstTreeSelectionToolbar);
if (lstTreeExpansionToolbar != null)
super.addTreeExpansionListener(lstTreeExpansionToolbar);
if (lstTreeModelToolbar != null)
super.getModel().addTreeModelListener(lstTreeModelToolbar);
}
@Override
public void openSpaceAny(ActionListener alrControllerSpc) throws Exception
{
this._blnIsOpen = true;
Object objRoot = super.getModel().getRoot();
GfrNodCtrFixFolderRootAbs nodRoot = (GfrNodCtrFixFolderRootAbs) objRoot;
if (nodRoot.getTree() == null)
nodRoot.setTree(this);
}
@Override
public void closeSpace() throws Exception
{
this._blnIsOpen = false;
Object objRoot = super.getModel().getRoot();
GfrNodCtrAbs nodRoot = (GfrNodCtrAbs) objRoot;
nodRoot.removeAllChildren();
DefaultTreeModel dtm = (DefaultTreeModel) super.getModel();
dtm.nodeStructureChanged(nodRoot);
}
@Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
}
@Override
public void folderAllExpand(GfrNodAbs nodSel)
{
if (! this._blnIsOpen)
return;
super.folderAllExpand(nodSel);
}
@Override
public void folderAllCollapse(GfrNodAbs nodSel)
{
if (! this._blnIsOpen)
return;
super.folderAllCollapse(nodSel);
}
@Override
public boolean isAllowedFolderAllCollapse(GfrNodAbs nodSel) throws Exception
{
if (! this._blnIsOpen)
return false;
return super.isAllowedFolderAllCollapse(nodSel);
}
@Override
public boolean isAllowedFolderAllExpand(GfrNodAbs nodSel) throws Exception
{
if (! this._blnIsOpen)
return false;
return super.isAllowedFolderAllExpand(nodSel);
}
@Override
public void expandAll()
{
if (! this._blnIsOpen)
return;
super.expandAll();
}
@Override
public boolean init()
{
if (! super.init())
return false;
return true;
}
@Override
public void destroy()
{
super.destroy();
if (this._hnd != null)
{
this.removeMouseListener(this._hnd);
this._hnd = null;
}
}
@Override
public void loadTransient() throws Exception
{
this._hnd = new MouseMenuHandler();
this.addMouseListener(this._hnd); // popup
}
@Override
public void releaseTransient() throws Exception
{
this.removeMouseListener(this._hnd); // popup
this._hnd = null;
}
// !!!!!!!!!!
@Override
public void actionPerformed(ActionEvent e)
{
if (! this._blnIsOpen)
return;
Object obj = e.getSource();
if (! (obj instanceof JMenuItem))
{
String str = "! (obj instanceof JMenuItem)";
GfrTreCtrTopSpcAbs._LOGGER_.severe(str);
GfrOptionPaneAbs.s_showDialogError(null, str);
System.exit(1);
}
String str = "uncaught JMenuItem: " + ((JMenuItem) obj).getText();
GfrTreCtrTopSpcAbs._LOGGER_.severe(str);
GfrOptionPaneAbs.s_showDialogError(null, str);
System.exit(1);
}
private void _displayValidPopupMenu_(Point pnt) throws Exception
{
if (! this._blnIsOpen)
return;
TreePath tph = super.getPathForLocation(pnt.x, pnt.y);
if (tph == null)
return;
GfrNodCtrAbs nod = (GfrNodCtrAbs) tph.getLastPathComponent();
// --- beg fixing up issue #339
DefaultTreeModel dtm = (DefaultTreeModel) super.getModel();
TreeNode[] nods = dtm.getPathToRoot(nod);
super.setSelectionPath(new TreePath(nods));
// --- end fixing up issue #339
GfrPmuCtlCtrAbs pop = nod.getPopup();
if (pop == null)
return;
pop.setHighlightedNode(nod);
pop.show(this, pnt.x, pnt.y);
}
// beg inner-class
final class MouseMenuHandler extends MouseAdapter
{
public MouseMenuHandler()
{
super();
}
@Override
public void mouseReleased(MouseEvent e)
{
if (! SwingUtilities.isRightMouseButton(e))
return;
try
{
_displayValidPopupMenu_(e.getPoint());
}
catch(Exception exc)
{
exc.printStackTrace();
String str = exc.getMessage();
GfrTreCtrTopSpcAbs._LOGGER_.severe(str);
GfrOptionPaneAbs.s_showDialogError(null, str);
return;
}
}
}
// end inner-class
}