Generated on Thu Nov 2 14:49:35 2006 for Gecode/J by doxygen 1.5.0

VisualNode.java

Go to the documentation of this file.
00001 /* -*- indent-tabs-mode: nil -*- */
00002 /*
00003  *  Main authors:
00004  *     Marco Kuhlmann <kuhlmann@ps.uni-sb.de>
00005  *     Guido Tack <tack@gecode.org>
00006  *
00007  *  Copyright:
00008  *     Marco Kuhlmann, 2005
00009  *     Guido Tack, 2006
00010  *
00011  *  Last modified:
00012  *     $Date: 2006-10-26 11:31:58 +0200 (Thu, 26 Oct 2006) $ by $Author: tack $
00013  *     $Revision: 3796 $
00014  *
00015  *  This file is part of Gecode, the generic constraint
00016  *  development environment:
00017  *     http://www.gecode.org
00018  *
00019  *  See the file "LICENSE" for information on usage and
00020  *  redistribution of this file, and for a
00021  *     DISCLAIMER OF ALL WARRANTIES.
00022  *
00023  */
00024 
00025 package org.gecode.gist;
00026 
00027 import java.util.*;
00028 
00029 abstract public class VisualNode extends Node {
00030 
00031     protected static int unit = 100;
00032     protected static int halfUnit = unit / 2;
00033     
00034     public static int horizontalOffset = halfUnit;
00035     public static int verticalOffset = unit + unit;
00036     
00037     // relative offset with respect to parent node
00038     protected int offset;
00039     
00040     // flag; true if the node needs re-display
00041     private boolean dirty = false;
00042     private boolean hidden = false;
00043     
00044     // shape and bounding box associated to the node
00045     private Shape shape;
00046     private BoundingBox boundingBox;
00047         
00048     public VisualNode() {
00049         super();
00050     }
00051     
00052     // return the width of the node
00053     abstract public int getWidth();
00054     
00055     protected abstract Shape getUnknownShape();
00056     protected abstract Shape getHiddenShape();
00057 
00058     public void hide() {
00059         if (! hidden) {
00060             hidden = true;
00061             dirtyUp();
00062         }
00063     }
00064     
00065     public void unhide() {
00066         if (hidden) {
00067             hidden = false;
00068             dirtyUp();
00069         }
00070     }
00071     
00072     public void toggleHidden() {
00073         hidden = ! hidden;
00074         dirtyUp();
00075     }
00076 
00077     public boolean isHidden() {
00078         return hidden;
00079     }
00080 
00081     public void unhideAll() {
00082         DefaultNodeCursor cursor =
00083             new DefaultNodeCursor(this) {
00084                 public void processCurrentNode() {
00085                     ((VisualNode) getCurrentNode()).unhide();
00086                 }
00087             };
00088         PreOrderNodeVisitor visitor = new PreOrderNodeVisitor(cursor);
00089         visitor.run();
00090     }
00091     
00092     public void dirtyUp() {
00093             VisualNode cur = this;
00094         while (! cur.dirty) {
00095             cur.dirty = true;
00096             if (! cur.isRoot()) {
00097                 cur = (VisualNode) cur.getParent();
00098             }
00099         }
00100     }
00101     
00102     public void layout() {
00103         LayoutCursor cursor = new LayoutCursor(this);
00104         PostOrderNodeVisitor visitor = new PostOrderNodeVisitor(cursor);
00105         visitor.run();
00106     }
00107         
00108     public boolean containsCoordinateAtDepth(int x, int depth) {
00109         if (x < boundingBox.left ||
00110             x > boundingBox.right ||
00111             depth > boundingBox.depth) {
00112             return false;
00113         }
00114         Extent theExtent = shape.getExtentAtDepth(depth);
00115         if (theExtent != null) {
00116             return (theExtent.extentL <= x && x <= theExtent.extentR);
00117         } else {
00118             return false;
00119         }
00120     }
00121     
00122     public VisualNode findNode(int x, int y, Coordinate coordinate) {
00123         VisualNode cur = this;
00124         int depth = y / verticalOffset;
00125 
00126         while (depth > 0 && cur != null) {
00127             if (cur.isHidden()) {
00128                 break;
00129             }
00130             Iterator childrenIterator = cur.childrenIterator();
00131             cur = null;
00132             while (childrenIterator.hasNext()) {
00133                 VisualNode nextChild = (VisualNode) childrenIterator.next();
00134                 int newX = x - nextChild.offset;
00135                 if (nextChild.containsCoordinateAtDepth(newX, depth - 1)) {
00136                     cur = nextChild;
00137                     x = newX;
00138                     break;
00139                 }
00140             }
00141             depth = depth-1;
00142             y = y - verticalOffset;
00143         }
00144         coordinate.set(x, y);
00145         return cur;
00146     }
00147 
00148     public Coordinate coordinates() {
00149         VisualNode cur = this;
00150         Coordinate p = new Coordinate(0,0);
00151         while (cur != null) {
00152             p.set(p.x() + cur.offset,
00153                   p.y() + verticalOffset);
00154             cur = (VisualNode) cur.getParent();
00155         }
00156         return p;
00157     }
00158     
00159     public abstract void drawNode(Object graphicsContext, int x, int y);
00160     public abstract void connect(Object graphicsContext, int x, int y, int momX, int momY);
00161     
00162     public void draw(Object graphicsContext, Rect clippingRect) {
00163         DrawingCursor cursor = new DrawingCursor(this, graphicsContext, clippingRect);
00164         PreOrderNodeVisitor visitor = new PreOrderNodeVisitor(cursor);
00165         visitor.run();
00166     }
00167     
00168     public BoundingBox getBoundingBox() {
00169         return boundingBox;
00170     }
00171     
00172     public int getOffset() {
00173         return offset;
00174     }
00175     
00176     public void setOffset(int theOffset) {
00177         offset = theOffset;
00178     }
00179     
00180     public int getVerticalOffset() {
00181         return verticalOffset;
00182     }
00183     
00184     public boolean isDirty() {
00185         return dirty;
00186     }
00187     
00188     public void setDirty(boolean newDirty) {
00189         dirty = newDirty;
00190     }
00191     
00192     public int getHorizontalOffset() {
00193         return horizontalOffset;
00194     }
00195     
00196     public void setBoundingBox(BoundingBox theBoundingBox) {
00197         boundingBox = theBoundingBox;
00198     }
00199     
00200     public void setShape(Shape theShape) {
00201         shape = theShape;
00202     }
00203     
00204     public Shape getShape() {
00205         return shape;
00206     }
00207     
00208 }