Generated on Fri Oct 6 16:26:43 2006 for Gecode/J by doxygen 1.4.7

TreeView.java

Go to the documentation of this file.
00001 package org.gecode.explorer.cocoa;
00002 
00003 import com.apple.cocoa.foundation.*;
00004 import com.apple.cocoa.application.*;
00005 
00006 import java.util.*;
00007 
00008 import org.gecode.*;
00009 import org.gecode.explorer.*;
00010 
00011 
00012 public class TreeView extends NSView {
00013     
00014     float scale = (float) 1;
00015     ExplorerNode root;
00016     BoundingBox rootBox;
00017     
00018     public void setRootNode(ExplorerNode theRoot) {
00019         this.root = theRoot;
00020         root.dirtyUp();
00021         layout();
00022     }
00023     
00024     public TreeView(NSRect frameRect) {
00025         super(frameRect);
00026     }
00027     
00028     public void layout() {
00029         root.layout();
00030         rootBox = root.getBoundingBox();
00031         contentSizeChanged();
00032     }
00033     
00034     private void drawUnitSquare() {
00035         NSPoint p = new NSPoint((float) -50, (float) 0);
00036         NSSize size = new NSSize((float) 100, (float) 100);
00037         NSRect rect = new NSRect(p, size);
00038         NSBezierPath.bezierPathWithRect(rect).stroke();
00039     }
00040     
00041     private void drawFrameRect() {
00042         NSBezierPath.bezierPathWithRect(frame()).stroke();
00043     }
00044     
00045     private NSAffineTransform getRootTransform() {
00046         float translateX = - (float) rootBox.left + sideMargin / 2;
00047         float translateY = topMargin;
00048         NSAffineTransform rootTransform = new NSAffineTransform();
00049         rootTransform.translateXYBy(translateX * scale, translateY * scale);
00050         rootTransform.scaleBy(scale);
00051         return rootTransform;
00052     }
00053         
00054     private void drawRootBox() {
00055         float rootBoxL = ((float) rootBox.left);
00056         float rootBoxR = ((float) rootBox.right);
00057         float rootBoxD = ((float) rootBox.depth * root.verticalOffset);
00058         
00059         NSPoint p = new NSPoint(rootBoxL, (float) 0.0);
00060         NSSize size = new NSSize(- rootBoxL + rootBoxR, rootBoxD);
00061         NSRect rect = new NSRect(p, size);
00062         NSBezierPath.bezierPathWithRect(rect).stroke();
00063     }
00064     
00065     private float topMargin = (float) 20;
00066     private float sideMargin = (float) root.horizontalOffset;
00067     
00068     private void contentSizeChanged() {
00069         if (rootBox != null) {
00070             float rootBoxL = ((float) rootBox.left);
00071             float rootBoxR = ((float) rootBox.right);
00072             float rootBoxD = ((float) rootBox.depth * root.verticalOffset);
00073             
00074             float width = - rootBoxL + rootBoxR + sideMargin;
00075             float depth = rootBoxD + topMargin;
00076             
00077             NSAffineTransform transform = new NSAffineTransform();
00078             transform.scaleBy(scale);
00079             NSSize newContentSize = new NSSize(width, depth);
00080             newContentSize = transform.transformSize(newContentSize);
00081             
00082             NSRect newContentRect = new NSRect(new NSPoint(), newContentSize);
00083             setFrame(newContentRect);
00084             
00085             NSScrollView scrollView = enclosingScrollView();
00086             scrollView.documentView().setFrame(newContentRect);
00087             scrollView.reflectScrolledClipView(scrollView.contentView());
00088             
00089             setNeedsDisplay(true);
00090         }
00091     }
00092     
00093     public boolean isFlipped() {
00094         return true;
00095     }
00096     
00097     public void drawRect(NSRect aRect) {
00098         NSAffineTransform rootTransform = getRootTransform();
00099         rootTransform.concat();
00100         
00101         rootTransform.invert();
00102         NSRect clippingRect = aRect;
00103         NSPoint clippingRectOrigin = clippingRect.origin();
00104         clippingRectOrigin = rootTransform.transformPoint(clippingRectOrigin);
00105         NSSize clippingRectSize = clippingRect.size();
00106         clippingRectSize = rootTransform.transformSize(clippingRectSize);
00107         
00108         int clipX = (int) clippingRectOrigin.x();
00109         int clipY = (int) clippingRectOrigin.y();
00110         int clipW = (int) clippingRectSize.width();
00111         int clipH = (int) clippingRectSize.height();
00112         Rect clipRect = new Rect(clipX, clipY, clipW, clipH);
00113         
00114         root.draw(null, clipRect);
00115     }
00116     
00117     public void setScale(float aScale) {
00118         scale = aScale / (float) 100 / (float) 3;
00119         contentSizeChanged();
00120         }
00121     
00122     public void mouseDown(NSEvent theEvent) {
00123         NSPoint theMouseLocation = convertPointFromView(theEvent.locationInWindow(), null);
00124         NSAffineTransform transform = new NSAffineTransform(getRootTransform());
00125         transform.invert();
00126         theMouseLocation = transform.transformPoint(theMouseLocation);
00127         int clickedX = (int) theMouseLocation.x();
00128         int clickedY = (int) theMouseLocation.y();
00129         VisualNode clickedNode = root.findNodeForPoint(clickedX, clickedY);
00130         if (clickedNode != null) {
00131             clickedNode.toggleHidden();
00132             clickedNode.dirtyUp();
00133             layout();
00134         }
00135     }
00136 
00137     // drag and drop
00138     
00139 //    public int draggingSourceOperationMaskForLocal(boolean isLocal) {
00140 //        return NSDraggingInfo.DragOperationCopy;
00141 //    }
00142     
00143 //    public void mouseDown(NSEvent theEvent) {
00144 //        NSPasteboard dragBoard =
00145 //            NSPasteboard.pasteboardWithName(NSPasteboard.DragPboard);
00146 //        NSArray typesArray = new NSArray(NSPasteboard.PDFPboardType);
00147 //        dragBoard.declareTypes(typesArray, this);
00148 //        writePDFInsideRectToPasteboard(frame(), dragBoard);
00149 //        NSImage image = new NSImage(dragBoard);
00150 //        NSImage dragImage = new NSImage(frame().size());
00151 //        dragImage.lockFocus();
00152 //        image.dissolveToPoint(new NSPoint(), (float) 0.5);
00153 //        dragImage.unlockFocus();
00154 //        NSPoint location = new NSPoint();
00155 //        dragImage(dragImage, new NSPoint(), new NSSize(), theEvent, dragBoard,
00156 //                  this, true);
00157 //    }
00158     
00159 }