001: /*
002: * $Header: /cvs/j3dfly/J3dEditor/src/org/jdesktop/j3dedit/scenegrapheditor/treeview/TVObject.java,v 1.1 2005/04/20 22:21:29 paulby Exp $
003: *
004: * Sun Public License Notice
005: *
006: * The contents of this file are subject to the Sun Public License Version
007: * 1.0 (the "License"). You may not use this file except in compliance with
008: * the License. A copy of the License is available at http://www.sun.com/
009: *
010: * The Original Code is the Java 3D(tm) Scene Graph Editor.
011: * The Initial Developer of the Original Code is Paul Byrne.
012: * Portions created by Paul Byrne are Copyright (C) 2002.
013: * All Rights Reserved.
014: *
015: * Contributor(s): Paul Byrne.
016: *
017: **/
018: package org.jdesktop.j3dedit.scenegrapheditor.treeview;
019:
020: import java.awt.Graphics;
021: import java.awt.Graphics2D;
022: import java.awt.Color;
023: import java.awt.Point;
024: import java.awt.Dimension;
025: import java.awt.Image;
026: import java.awt.font.GlyphVector;
027: import java.awt.Font;
028: import java.awt.font.FontRenderContext;
029: import java.awt.geom.AffineTransform;
030: import java.util.HashMap;
031: import java.util.Properties;
032: import java.util.Vector;
033: import org.jdesktop.j3dedit.treelayout.DefaultTreeNode;
034: import org.jdesktop.j3dedit.treelayout.TreeNode;
035: import org.jdesktop.j3dedit.scenegraph.SGObject;
036: import org.jdesktop.j3dedit.scenegraph.SGNode;
037:
038: /**
039: * @author Paul Byrne
040: * @version 1.5, 01/18/02
041: */
042: public abstract class TVObject extends DefaultTreeNode implements
043: org.jdesktop.j3dedit.scenegraph.TreeViewInterface {
044:
045: protected boolean drawAsIcon = true;
046: protected java.awt.Image icon = null;
047:
048: protected Color currentColor; // Temporary Storage for color
049: protected GlyphVector nameLabel = null;
050: protected Dimension nameLabelBounds = null;
051: protected SGObject sgObject;
052:
053: public TVObject(int width, int height, int spacing,
054: SGObject sgObject) {
055: super (width, height, spacing);
056: this .sgObject = sgObject;
057: }
058:
059: public void computeLinkPoints(Point pos) {
060: linkExitPoint.x = pos.x + nodeSize.width / 2;
061: linkExitPoint.y = pos.y + nodeSize.height / 2;
062:
063: linkEntryPoint.x = pos.x + nodeSize.width / 2;
064: linkEntryPoint.y = pos.y + nodeSize.height / 2;
065: }
066:
067: /**
068: * Set the variable name for this node
069: */
070: public void setNodeName(String name) {
071: createNameLabel(name);
072: }
073:
074: public void setIcon(Image image) {
075: icon = image;
076: }
077:
078: public org.jdesktop.j3dedit.treelayout.TreeNode getChild(int index) {
079: return null;
080: }
081:
082: public int numChildren() {
083: return 0;
084: }
085:
086: public TreeNode getParent() {
087: if (sgObject instanceof SGNode) {
088: SGNode parent = ((SGNode) sgObject).getParent();
089: if (parent == null)
090: return null;
091: else
092: return (TreeNode) parent.getTreeViewObject();
093: } else
094: return null;
095: }
096:
097: /**
098: * Return the editor sgObject that this node represents
099: */
100: public SGObject getSGObject() {
101: return sgObject;
102: }
103:
104: /**
105: * Return the point where the behavior link should connect to this node.
106: * behaviorLocation is provided to allow the routine to determine the
107: * most appropriate point relative to the behavior nodes location
108: */
109: protected abstract Point getBehaviorLinkEntry(Point behaviorLocation);
110:
111: /**
112: * Highlight this node
113: */
114: public void setHighlight(boolean highlight) {
115: container.repaint();
116: }
117:
118: private void drawHighlight(Graphics2D g) {
119:
120: g.setColor(Color.red);
121: g.setXORMode(g.getBackground());
122: Point computedPosition = getComputedPosition();
123: Dimension nodeSize = getNodeSize();
124:
125: g.drawRect(computedPosition.x - 3, computedPosition.y - 3,
126: nodeSize.width + 8, nodeSize.height + 8);
127:
128: g.setPaintMode();
129: g.setColor(Color.black);
130: }
131:
132: public void paint(Graphics g) {
133:
134: // Draw the links of this node, recurse to all children
135: // and then draw this node.
136: // This ordering will ensure this node is drawn last and
137: // therefore ontop of the link lines.
138:
139: if (numChildren() > 0) {
140:
141: if (getLink() != null)
142: getLink().paint(g);
143: else
144: System.out.println("Null Link Error " + numChildren()
145: + " " + sgObject);
146:
147: // TODO Links should not be null, the city demo was
148: // causing this error
149:
150: /*
151: // Draw the subtreeSize for debugging
152: java.awt.Rectangle subtreeSize = getSubtreeSize();
153: g.setColor( Color.red );
154: g.drawRect( subtreeSize.x, subtreeSize.y,
155: subtreeSize.width, subtreeSize.height );
156: g.setColor( Color.black );
157: */
158: }
159:
160: for (int i = 0; i < numChildren(); i++)
161: getChild(i).paint(g);
162:
163: drawNode((Graphics2D) g);
164: }
165:
166: public void drawNode(Graphics2D g) {
167: // Draw the highlight for this node
168: if (sgObject.isSelected())
169: drawHighlight(g);
170: }
171:
172: /**
173: * Create a label of the nodeName
174: */
175: protected void createNameLabel(String nodeName) {
176: if (nodeName == null) {
177: nameLabel = null;
178: return;
179: }
180:
181: Font font = new Font("dialog", Font.PLAIN, 10);
182: FontRenderContext context = new FontRenderContext(
183: new AffineTransform(), false, false);
184: char[] chars = new char[nodeName.length()];
185: nodeName.getChars(0, chars.length, chars, 0);
186: nameLabel = font.createGlyphVector(context, chars);
187:
188: nameLabelBounds = new Dimension((int) nameLabel
189: .getVisualBounds().getWidth(), (int) font
190: .getMaxCharBounds(context).getHeight());
191: }
192:
193: /**
194: * Draw the name label for this node
195: */
196: protected void drawNameLabel(Graphics2D g) {
197: if (nameLabel == null)
198: return;
199:
200: g.setColor(Color.white);
201: g.fillRect(computedPosition.x - 2, computedPosition.y
202: - nameLabelBounds.height + 3,
203: nameLabelBounds.width + 4, nameLabelBounds.height);
204: g.setColor(Color.black);
205: g.drawGlyphVector(nameLabel, (float) (computedPosition.x),
206: (float) (computedPosition.y));
207: }
208:
209: /**
210: * Repaint this node
211: */
212: public void repaint() {
213: // TODO just repaint the area of the canvas that contains
214: // this node
215: container.repaint();
216: }
217:
218: }
|