/*
Copyright (c) 2003-2009 ITerative Consulting Pty Ltd. All Rights Reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted
provided that the following conditions are met:
o Redistributions of source code must retain the above copyright notice, this list of conditions and
the following disclaimer.
o Redistributions in binary form must reproduce the above copyright notice, this list of conditions
and the following disclaimer in the documentation and/or other materials provided with the distribution.
o This jcTOOL Helper Class software, whether in binary or source form may not be used within,
or to derive, any other product without the specific prior written permission of the copyright holder
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package DisplayProject.controls;
import java.awt.Color;
import java.awt.event.MouseListener;
import java.util.Enumeration;
import javax.swing.JTree;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;
import DisplayProject.DisplayNode;
import DisplayProject.TreeViewModel;
/**
* Wrapper for JTree so it knows who its parent is. Used in Events like NodeChangedListener.
* Also implements the OutlineFieldUI.
*
* @author Craig Mitchell
*/
@SuppressWarnings("serial")
public class OutlineFieldJTree extends JTree {
private final OutlineField outlineField;
private static final String uiClassID = "OutlineTreeUI";
public OutlineFieldJTree(OutlineField pOutlineField) {
this.outlineField = pOutlineField;
}
public OutlineField getOutlineField() {
return this.outlineField;
}
/* Override the new model to correctly expand nodes
*
* @see TreeFieldFactory
* @see TreeFieldFactory#newTreeView(TreeViewModel)
*/
public void setModel(TreeModel newModel) {
// TF:24/06/2008:We need to remove the old model as a listener prior to adding the new one
if (this.getModel() instanceof TreeViewModel) {
removeTreeWillExpandListener((TreeViewModel)this.getModel());
}
super.setModel(newModel);
if (newModel != null && newModel instanceof TreeViewModel) {
((TreeViewModel)newModel).setTree(this);
TreeNode tr = (TreeNode)newModel.getRoot();
if (tr != null){
checkExpand(new TreePath(tr));
}
addTreeWillExpandListener(((TreeViewModel)newModel));
}
}
@SuppressWarnings("unchecked")
private void checkExpand(TreePath parent){
DisplayNode node = (DisplayNode)parent.getLastPathComponent();
if (node.getChildCount() > 0) {
for (Enumeration<DisplayNode> e = node.children(); e.hasMoreElements();){
TreePath path = parent.pathByAddingChild(e.nextElement());
checkExpand(path);
}
}
if (node.getIsOpened()){
expandPath(parent);
} else {
collapsePath(parent);
}
}
@Override
public String getUIClassID() {
return OutlineFieldJTree.uiClassID;
}
@Override
public void updateUI() {
this.setUI(OutlineFieldUI.createUI(this));
this.invalidate();
}
/* (non-Javadoc)
* @see java.awt.Component#addMouseListener(java.awt.event.MouseListener)
*/
@Override
public synchronized void addMouseListener(MouseListener l) {
// AGD 11/3/08
// Make sure that the drag mouse listener is always the last registered
// This ensures that the other events get processed before the Drag
// happens. This is the same sequence as Forte.
MouseListener[] mlList = this.getMouseListeners();
// Check if the Drag mouse listener is there
MouseListener foundDragListener = null;
for (int i = 0; i < mlList.length; i++) {
// instanceof does not work with a static inner class so using the name instead
if (mlList[i].getClass().getName().equals("javax.swing.plaf.basic.BasicTreeUI$TreeDragGestureRecognizer")) {
foundDragListener = mlList[i];
// remove the found Drag Listener to be added later
this.removeMouseListener(foundDragListener);
//assume there is only one and break
break;
};
}
// Add the Listener
super.addMouseListener(l);
// Add the found Drag listener at the end if it exists
if (foundDragListener != null) {
super.addMouseListener(foundDragListener);
}
}
/**
* Get the background colour. This should be always the same as the outlineField
*/
@Override
public Color getBackground() {
return this.outlineField == null ? null : this.outlineField.getBackground();
}
}