Author: tchemit Date: 2013-03-17 19:33:49 +0100 (Sun, 17 Mar 2013) New Revision: 2630 Url: http://nuiton.org/projects/jaxx/repository/revisions/2630 Log: fixes #2603: Add new API to scan components as a tree Added: trunk/jaxx-runtime/src/main/java/jaxx/runtime/awt/ trunk/jaxx-runtime/src/main/java/jaxx/runtime/awt/visitor/ trunk/jaxx-runtime/src/main/java/jaxx/runtime/awt/visitor/BuildTreeVisitor.java trunk/jaxx-runtime/src/main/java/jaxx/runtime/awt/visitor/ComponentTreeNode.java trunk/jaxx-runtime/src/main/java/jaxx/runtime/awt/visitor/ComponentTreeNodeVisitor.java trunk/jaxx-runtime/src/main/java/jaxx/runtime/awt/visitor/GetCompopentAtPointVisitor.java trunk/jaxx-runtime/src/main/java/jaxx/runtime/awt/visitor/package.html Added: trunk/jaxx-runtime/src/main/java/jaxx/runtime/awt/visitor/BuildTreeVisitor.java =================================================================== --- trunk/jaxx-runtime/src/main/java/jaxx/runtime/awt/visitor/BuildTreeVisitor.java (rev 0) +++ trunk/jaxx-runtime/src/main/java/jaxx/runtime/awt/visitor/BuildTreeVisitor.java 2013-03-17 18:33:49 UTC (rev 2630) @@ -0,0 +1,71 @@ +package jaxx.runtime.awt.visitor; + +/* + * #%L + * JAXX :: Runtime + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2008 - 2013 CodeLutin, Tony Chemit + * %% + * 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 General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ + +import org.jdesktop.jxlayer.JXLayer; + +import javax.swing.JRootPane; +import java.awt.Component; +import java.awt.Container; + +/** + * A visitor to build the tree from a given component. + * <p/> + * To obtain a tree from a compoent use the method {@link #buildTree(Component)}. + * + * @author tchemit <chemit@codelutin.com> + * @since 2.5.14 + */ +public class BuildTreeVisitor implements ComponentTreeNodeVisitor { + + public static ComponentTreeNode buildTree(Component component) { + BuildTreeVisitor v = new BuildTreeVisitor(); + ComponentTreeNode rootNode = new ComponentTreeNode(component); + rootNode.visit(v); + return rootNode; + } + + @Override + public void startNode(ComponentTreeNode node) { + Component component = node.getUserObject(); + if (component instanceof Container) { + Container container = (Container) component; + //TODO-TC-2013-03-17 Should use some rules to add this. + for (Component child : container.getComponents()) { + if (child instanceof JXLayer<?>) { + child = ((JXLayer<?>) child).getView(); + } else if (child instanceof JRootPane) { + child = ((JRootPane) child).getLayeredPane(); + } + ComponentTreeNode childNode = new ComponentTreeNode(child); + node.add(childNode); + } + } + } + + @Override + public void endNode(ComponentTreeNode componentTree) { + } +} Property changes on: trunk/jaxx-runtime/src/main/java/jaxx/runtime/awt/visitor/BuildTreeVisitor.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Added: trunk/jaxx-runtime/src/main/java/jaxx/runtime/awt/visitor/ComponentTreeNode.java =================================================================== --- trunk/jaxx-runtime/src/main/java/jaxx/runtime/awt/visitor/ComponentTreeNode.java (rev 0) +++ trunk/jaxx-runtime/src/main/java/jaxx/runtime/awt/visitor/ComponentTreeNode.java 2013-03-17 18:33:49 UTC (rev 2630) @@ -0,0 +1,113 @@ +package jaxx.runtime.awt.visitor; + +/* + * #%L + * JAXX :: Runtime + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2008 - 2013 CodeLutin, Tony Chemit + * %% + * 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 General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ + +import org.apache.commons.collections.iterators.EnumerationIterator; + +import javax.swing.tree.DefaultMutableTreeNode; +import java.awt.Component; +import java.awt.Container; +import java.util.Iterator; + +/** + * A node where userObject is a {@link Component}. + * <p/> + * If the compoent is a {@link Container}, then his children are the components + * of the container. + * + * @author tchemit <chemit@codelutin.com> + * @since 2.5.14 + */ +public class ComponentTreeNode extends DefaultMutableTreeNode implements Iterable<ComponentTreeNode> { + + private static final long serialVersionUID = 1L; + + public ComponentTreeNode(Component userObject) { + super(userObject, true); + } + + /** + * To visit a node. + * + * @param visitor the visitor + */ + public void visit(ComponentTreeNodeVisitor visitor) { + visitor.startNode(this); + for (ComponentTreeNode child : this) { + child.visit(visitor); + } + visitor.endNode(this); + } + + @Override + public Component getUserObject() { + return (Component) super.getUserObject(); + } + + @Override + public ComponentTreeNode getParent() { + return (ComponentTreeNode) super.getParent(); + } + + @Override + public ComponentTreeNode getNextLeaf() { + return (ComponentTreeNode) super.getNextLeaf(); + } + + @Override + public ComponentTreeNode getNextNode() { + return (ComponentTreeNode) super.getNextNode(); + } + + @Override + public ComponentTreeNode getNextSibling() { + return (ComponentTreeNode) super.getNextSibling(); + } + + @Override + public ComponentTreeNode getPreviousLeaf() { + return (ComponentTreeNode) super.getPreviousLeaf(); + } + + @Override + public ComponentTreeNode getPreviousNode() { + return (ComponentTreeNode) super.getPreviousNode(); + } + + @Override + public ComponentTreeNode getPreviousSibling() { + return (ComponentTreeNode) super.getPreviousSibling(); + } + + @Override + public ComponentTreeNode getRoot() { + return (ComponentTreeNode) super.getRoot(); + } + + @Override + public Iterator<ComponentTreeNode> iterator() { + return new EnumerationIterator(children()); + } +} Property changes on: trunk/jaxx-runtime/src/main/java/jaxx/runtime/awt/visitor/ComponentTreeNode.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Added: trunk/jaxx-runtime/src/main/java/jaxx/runtime/awt/visitor/ComponentTreeNodeVisitor.java =================================================================== --- trunk/jaxx-runtime/src/main/java/jaxx/runtime/awt/visitor/ComponentTreeNodeVisitor.java (rev 0) +++ trunk/jaxx-runtime/src/main/java/jaxx/runtime/awt/visitor/ComponentTreeNodeVisitor.java 2013-03-17 18:33:49 UTC (rev 2630) @@ -0,0 +1,38 @@ +package jaxx.runtime.awt.visitor; + +/* + * #%L + * JAXX :: Runtime + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2008 - 2013 CodeLutin, Tony Chemit + * %% + * 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 General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ + +/** + * To visit a {@link ComponentTreeNode}. + * + * @author tchemit <chemit@codelutin.com> + * @since 2.5.14 + */ +public interface ComponentTreeNodeVisitor { + + void startNode(ComponentTreeNode componentTree); + + void endNode(ComponentTreeNode componentTree); +} Property changes on: trunk/jaxx-runtime/src/main/java/jaxx/runtime/awt/visitor/ComponentTreeNodeVisitor.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Added: trunk/jaxx-runtime/src/main/java/jaxx/runtime/awt/visitor/GetCompopentAtPointVisitor.java =================================================================== --- trunk/jaxx-runtime/src/main/java/jaxx/runtime/awt/visitor/GetCompopentAtPointVisitor.java (rev 0) +++ trunk/jaxx-runtime/src/main/java/jaxx/runtime/awt/visitor/GetCompopentAtPointVisitor.java 2013-03-17 18:33:49 UTC (rev 2630) @@ -0,0 +1,109 @@ +package jaxx.runtime.awt.visitor; + +/* + * #%L + * JAXX :: Runtime + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2008 - 2013 CodeLutin, Tony Chemit + * %% + * 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 General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ + +import com.google.common.collect.Lists; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import java.awt.Component; +import java.awt.Point; +import java.util.List; + +/** + * A visitor to get the deepest component given a point in a tree. + * <p/> + * To obtain the component use the method {@link #get(ComponentTreeNode, Point)}. + * + * @author tchemit <chemit@codelutin.com> + * @since 2.5.14 + */ +public class GetCompopentAtPointVisitor implements ComponentTreeNodeVisitor { + + /** Logger. */ + private static final Log log = + LogFactory.getLog(GetCompopentAtPointVisitor.class); + + int currentX; + + int currentY; + + final List<ComponentTreeNode> components = Lists.newArrayList(); + + public static Component get(ComponentTreeNode node, Point p) { + + GetCompopentAtPointVisitor v = new GetCompopentAtPointVisitor(p); + node.visit(v); + Component result = v.getLastComponent(); + v.components.clear(); + return result; + } + + public GetCompopentAtPointVisitor(Point p) { + currentX = (int) p.getX(); + currentY = (int) p.getY(); + } + + public Component getLastComponent() { + Component result = null; + if (!components.isEmpty()) { + ComponentTreeNode node = components.get(components.size() - 1); + result = node.getUserObject(); + } + return result; + } + + @Override + public void startNode(ComponentTreeNode node) { + Component component = node.getUserObject(); + + Point point = component.getLocation(); + currentX -= point.x; + currentY -= point.y; + + // check parent is ok + if (node.isRoot() || components.contains(node.getParent())) { + + boolean containsPoint = component.contains(currentX, currentY); + + if (containsPoint) { + + if (log.isDebugEnabled()) { + log.debug("Accept component: " + component); + } + + // keep this node + components.add(node); + } + } + } + + @Override + public void endNode(ComponentTreeNode componentTree) { + Point point = componentTree.getUserObject().getLocation(); + currentX += point.x; + currentY += point.y; + } +} Property changes on: trunk/jaxx-runtime/src/main/java/jaxx/runtime/awt/visitor/GetCompopentAtPointVisitor.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Added: trunk/jaxx-runtime/src/main/java/jaxx/runtime/awt/visitor/package.html =================================================================== --- trunk/jaxx-runtime/src/main/java/jaxx/runtime/awt/visitor/package.html (rev 0) +++ trunk/jaxx-runtime/src/main/java/jaxx/runtime/awt/visitor/package.html 2013-03-17 18:33:49 UTC (rev 2630) @@ -0,0 +1,36 @@ +<!-- + #%L + JAXX :: Runtime + $Id$ + $HeadURL$ + %% + Copyright (C) 2008 - 2013 CodeLutin, Tony Chemit + %% + 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 General Lesser Public License for more details. + + You should have received a copy of the GNU General Lesser Public + License along with this program. If not, see + <http://www.gnu.org/licenses/lgpl-3.0.html>. + #L% + --> +<html> +<body> +<h1>JAXX - awt tree node API</h1> + +This package contains a API to visit swing components. + +<h2>ComponentTreenode</h2> +The model of a node in the tree + +<h2>ComponentTreenodeVisitor</h2> +Object to load childs of Contract of a visitor on nodes. +</body> +</html> Property changes on: trunk/jaxx-runtime/src/main/java/jaxx/runtime/awt/visitor/package.html ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native