All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.viaoa.jfc.control.Hub2TreeNode Maven / Gradle / Ivy

/*  Copyright 1999-2015 Vince Via [email protected]
    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
*/
package com.viaoa.jfc.control;

import java.awt.*;
import java.util.concurrent.atomic.AtomicInteger;

import javax.swing.*;
import javax.swing.tree.TreePath;

import com.viaoa.object.*;
import com.viaoa.util.OAArray;
import com.viaoa.hub.*;
import com.viaoa.jfc.*;
import com.viaoa.jfc.tree.*;

/** 
    Used by OATreeNodeData for each node in the tree that uses a Hub.
    @see OATreeNode
    @see OATreeTitleNode
*/
public class Hub2TreeNode extends HubListenerAdapter {
    OATreeNode node;
    OATreeNodeData parent;
    Hub hub;
    HubFilter hubFilter;  // this is so that HubFilter.close() will be called when this.close() is called.  
    private static AtomicInteger aiCnt = new AtomicInteger();
    public Hub2TreeNode(Hub hub, OATreeNode node, OATreeNodeData parent, HubFilter hf) {
        String pp = node.getPropertyPath(); 
    	if (pp != null) {
    	    if (pp.indexOf(".") < 0) {
                hub.addHubListener(this, pp);
    	    }
    	    else {
                hub.addHubListener(this, "hut2TreeNode"+aiCnt.getAndIncrement(), new String[] {pp});
    	    }
    	}
    	else hub.addHubListener(this);
        
        this.hub = hub;
        this.node = node;
        this.parent = parent;
        this.hubFilter = hf;
    }

    public void close() {
    	if (hub != null) {
        	if (node.getPropertyPath() != null && node.getPropertyPath().indexOf(".") < 0) {
                hub.removeHubListener(this);
        		//20101218 was: hub.removeHubListener(this, node.getPropertyPath());
        	}
        	else hub.removeHubListener(this);
    	}
    	if (hubFilter != null) hubFilter.close();
    }
    
    
    public @Override void afterPropertyChange(HubEvent e) {
        if (e.getObject() instanceof Hub) return;
        String s = e.getPropertyName();
        if (s == null) return;
        // if (s.equalsIgnoreCase("changed") || s.equalsIgnoreCase("new")) return;

        if (node == null) {
            return;
        }
        if (node.getPropertyPath() == null) {
            return;
        }
        
        if (node.getPropertyPath().toUpperCase().indexOf(s.toUpperCase()) < 0) {
        	if (node.getIconColorProperty() == null || node.getIconColorProperty().toUpperCase().indexOf(s.toUpperCase()) < 0) {
            	if (node.getImageProperty() == null || node.getImageProperty().toUpperCase().indexOf(s.toUpperCase()) < 0) {
            	    if (node.getDependentProperties() == null) return;
            	    boolean b = false;
            	    for (String sx : node.getDependentProperties()) {
            	        if (sx != null && sx.toUpperCase().indexOf(s.toUpperCase()) >= 0) {
            	            b = true;
            	            break;
            	        }
            	    }
            		if (!b) return;
            	}
        	}
        }
        
        int pos = parent.getChildIndex(e.getObject(), node);
        if (pos >= 0) {
            ((OATreeModel)node.getTree().getModel()).changedNode(parent, pos);  
            node.getTree().repaint(); 
        }            
    }

    public @Override void afterAdd(final HubEvent e) {
        if (node != null && node.getShowAll()) {
        	try {
        		if (parent.getChild(node, e.getObject()) == null) {
        			OATreeNodeData tnd = new OATreeNodeData(node, e.getObject(),parent);
        			int pos = getStartPos()+e.getPos();
        			parent.insertChild(pos, tnd);
        			((OATreeModel)node.getTree().getModel()).insertNode(parent, pos, e.getObject());        			
        		}
            }
            catch (Throwable ex) {
                //System.out.println("Hub2TreeNode.afterAdd "+ex+", will continue");
                //ex.printStackTrace();
                ((OATreeModel)node.getTree().getModel()).fireTreeStructureChanged(parent);
            }
        }
    }

    
    public @Override void afterInsert(HubEvent e) {
        afterAdd(e);
    }


    public @Override void afterRemove(HubEvent e) {
		if (node.getShowAll()) {
			OATreeNodeData tnd = parent.getChild(node, e.getObject());
			if (tnd != null) {
        		int pos = parent.getChildIndex(tnd);
        		parent.removeChild(tnd);
    			((OATreeModel)node.getTree().getModel()).removeNode(parent, pos, e.getObject());
    		}
        }
    }

    // from start of parent, not top of tree
    protected int getStartPos() {
    	int nodePos = 0;
    	for (int i=0; i < parent.node.getChildrenTreeNodes().length; i++) {
            OATreeNode tn = parent.node.getChildrenTreeNodes()[i];
            if (tn == node) {
            	nodePos = i;
            	break;
            }
        }
    	nodePos++;
    	
    	int i = 0;
    	parent.loadChildren();
    	for (; i < parent.listChildren.size(); i++) {
    		OATreeNodeData dx = (OATreeNodeData) parent.listChildren.get(i);
    		if (dx.node == node) return i;
        	for (int i2=nodePos; i2 < parent.node.getChildrenTreeNodes().length; i2++) {
                if (parent.node.getChildrenTreeNodes()[i2] == dx.node) return i; 
            }
    	}
    	return i;
    }
    
    
    public @Override void afterMove(HubEvent e) {
    	if (node.getShowAll()) {
            int pos1 = e.getFromPos();
            int pos2 = e.getToPos();
            
            int startPos = getStartPos();
			parent.moveChild(startPos+pos1, startPos+pos2);
            
			((OATreeModel)node.getTree().getModel()).movedNode(parent, startPos+pos1, startPos+pos2); // invokedLater
        }
    }

    private Object[] getObjectPath(int amt, Object object) {
        // amt = number of object from the top of the tree to get. -1 to get all
        int i,x;
        OATreeNodeData tnd = parent;

        for (x=0 ;tnd != null; x++ ) tnd = tnd.parent;
        
        if (amt < 0 || amt > x) amt = x;
        //else if (object != null) amt++; // 20130831

        if (object != null) amt++;
        
        Object[] objs = new Object[amt];
        tnd = parent;
        for (i=0; i




© 2015 - 2025 Weber Informatics LLC | Privacy Policy