AutoScrollSelectionRightTree.java :  » IDE-Netbeans » soa » org » netbeans » modules » soa » mappercore » Java Open Source

Java Open Source » IDE Netbeans » soa 
soa » org » netbeans » modules » soa » mappercore » AutoScrollSelectionRightTree.java
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.netbeans.modules.soa.mappercore;

import java.awt.Rectangle;
import javax.swing.tree.TreePath;
import org.netbeans.modules.soa.mappercore.event.MapperSelectionEvent;
import org.netbeans.modules.soa.mappercore.event.MapperSelectionListener;

/**
 *
 * @author AlexanderPermyacov
 */
public class AutoScrollSelectionRightTree implements MapperSelectionListener {
    private RightTree rightTree;
    private TreePath currentPath;

    public AutoScrollSelectionRightTree(RightTree rightTree) {
        this.rightTree = rightTree;
        rightTree.getSelectionModel().addSelectionListener(this);
        currentPath = null;
    }

    public void mapperSelectionChanged(MapperSelectionEvent event) {
        TreePath treePath = rightTree.getSelectionModel().getSelectedPath();
        if (treePath == currentPath) { return; }
        
        currentPath = treePath;
        if (treePath == null) { return; }
        
        MapperNode node = rightTree.getMapper().getNode(treePath, true);
        int h = node.getContentHeight();
        int y = node.getContentCenterY();
        y = node.yToView(y);
        Rectangle rect = rightTree.getScrollPane().getViewport().getViewRect();
        Rectangle r;
        if (y + h / 2 > rect.y + rect.height && h < rect.height) {
            r = new Rectangle(rect.x, y + h / 2, 2, 2);
            rightTree.scrollRectToVisible(r);
        }
        if (y - h / 2 < rect.y && h < rect.height) {
            r = new Rectangle(rect.x, y - h / 2, 2, 2);
            rightTree.scrollRectToVisible(r);
        }
        if (y + h / 2 > rect.y + rect.height && h > rect.height) {
            r = new Rectangle(rect.x, y - h / 2, 2, rect.height);
            rightTree.scrollRectToVisible(r);
            return;
        }
        if (y - h / 2 < rect.y && h > rect.height) {
            r = new Rectangle(rect.x, y + h / 2 - rect.height, 2, rect.height);
            rightTree.scrollRectToVisible(r);
            return;
        }
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.