001: /*
002: * $Header: /cvs/j3dfly/J3dEditor/src/org/jdesktop/j3dedit/scenegrapheditor/NodeEditControl.java,v 1.1 2005/04/20 22:20:47 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;
019:
020: import javax.media.j3d.Node;
021: import javax.media.j3d.Group;
022: import org.jdesktop.j3dedit.scenegrapheditor.nodeeditors.EditorManager;
023: import org.jdesktop.j3dedit.scenegraph.SGObject;
024: import org.jdesktop.j3dedit.scenegraph.SGGroup;
025: import org.jdesktop.j3dedit.scenegraph.SGBehavior;
026: import org.jdesktop.j3dedit.scenegraph.SGNode;
027: import org.jdesktop.j3dedit.scenegraph.SGLeaf;
028: import org.jdesktop.j3dfly.utils.developmenttools.DevelopmentLocale;
029:
030: /**
031: *
032: * @author Paul Byrne
033: * @version $Id: NodeEditControl.java,v 1.1 2005/04/20 22:20:47 paulby Exp $
034: *
035: * Handles all aspects of selecting a node and editing it's parameters
036: */
037: public class NodeEditControl extends Object {
038:
039: private SGNode currentNode = null; // Node currently selected
040: private SGNode pasteBuffer = null;
041: private boolean readOnly = false;
042: private EditorManager editorManager;
043:
044: /** Creates new NodeEditControl */
045: public NodeEditControl(boolean readOnly, EditorManager editorManager) {
046: this .readOnly = readOnly;
047: this .editorManager = editorManager;
048: }
049:
050: /**
051: * Select node
052: * Deselect Previous selected node
053: * If the user was editing the previous node apply any changes
054: */
055: public void selectNode(SGObject node) {
056: if (currentNode != null) {
057: currentNode.setSelected(false);
058: if (editorManager.editInProgress()) {
059: editorManager.editComplete();
060: }
061: }
062:
063: currentNode = (SGNode) node;
064:
065: if (currentNode != null) {
066: currentNode.setSelected(true);
067: } else
068: editorManager.clearEditorPanel();
069: }
070:
071: /**
072: * Check if the currently selected node is a group
073: * or a subclass of group
074: */
075: public boolean isCurrentNodeGroup() {
076: if (currentNode != null && (currentNode instanceof SGGroup))
077: return true;
078: else
079: return false;
080: }
081:
082: /**
083: * Check if the currently selected node is a behavior or
084: * one of it subclasses
085: */
086: public boolean isCurrentNodeBehavior() {
087: if (currentNode != null && (currentNode instanceof SGBehavior))
088: return true;
089: else
090: return false;
091: }
092:
093: /**
094: * Check if the current node is read only
095: */
096: public boolean isCurrentNodeReadOnly() {
097: if (currentNode != null)
098: return currentNode.getReadOnly();
099: else
100: return false;
101: }
102:
103: public SGNode getCurrentNode() {
104: return currentNode;
105: }
106:
107: /**
108: * Start editing the currently selected node
109: *
110: * Does nothing if currentNode is null
111: */
112: public void editCurrentNode() {
113: if (currentNode == null)
114: return;
115: editorManager.editNode(currentNode);
116: }
117:
118: /**
119: * Start viewing the current node. Node Editor will display in
120: * readOnly mode
121: */
122: public void viewCurrentNode() {
123: if (currentNode == null)
124: return;
125:
126: throw new RuntimeException("Not Implemented");
127: }
128:
129: /**
130: * Traverse the graph and remove all the names from namecontrol
131: */
132: private void removeNodesFromNameControl(SGNode node) {
133: currentNode.getContext().getNameControl().removeObject(
134: node.getSceneName(), node.getNodeName(),
135: node.getJ3dNode());
136:
137: if (node instanceof SGGroup) {
138: for (int i = 0; i < ((SGGroup) node).numChildren(); i++)
139: removeNodesFromNameControl(((SGGroup) node).getChild(i));
140: }
141: }
142:
143: public void deleteCurrentNode() {
144: editorManager.clearEditorPanel();
145: DevelopmentLocale locale = currentNode.getContext().getLocale();
146: boolean wasLive = locale.getLive();
147:
148: if (wasLive)
149: locale.setLive(false);
150:
151: removeNodesFromNameControl(currentNode);
152:
153: detachNode(currentNode.getJ3dNode());
154: currentNode.getParent().removeChild(currentNode);
155: currentNode.setSelected(false);
156: currentNode = null;
157:
158: if (wasLive)
159: locale.setLive(true);
160: }
161:
162: public boolean isPasteBufferNull() {
163: return (pasteBuffer == null);
164: }
165:
166: public void cutCurrentNode() {
167: pasteBuffer = currentNode;
168: deleteCurrentNode();
169: }
170:
171: public void copyCurrentNode() {
172: boolean wasLive = currentNode.isLive();
173:
174: if (wasLive)
175: currentNode.setLive(false);
176:
177: // TODO deal with duplicating names when this is pasted
178:
179: try {
180: pasteBuffer = (SGNode) currentNode.getContext()
181: .getSceneGraphControl().cloneTree(currentNode);
182: } catch (Exception e) {
183: WindowManager.getManager().showMessage("Internal Error",
184: "Can't copy node");
185: return;
186: }
187:
188: if (wasLive)
189: currentNode.setLive(true);
190: }
191:
192: public void pasteNode() {
193: if (pasteBuffer == null)
194: return;
195:
196: boolean wasLive = currentNode.isLive();
197:
198: // TODO deal with duplicating names when this is pasted
199:
200: if (wasLive)
201: currentNode.setLive(false);
202:
203: ((Group) currentNode.getJ3dNode()).addChild(pasteBuffer
204: .getJ3dNode());
205: ((SGGroup) currentNode).addChild(pasteBuffer);
206:
207: // Make a new copy of the paste buffer incase the object is pasted
208: // again.
209: try {
210: pasteBuffer = (SGNode) pasteBuffer.getContext()
211: .getSceneGraphControl().cloneTree(pasteBuffer);
212: } catch (Exception e) {
213: WindowManager.getManager().showMessage("Internal Error",
214: "Can't copy node");
215: return;
216: }
217:
218: if (wasLive)
219: currentNode.setLive(true);
220: }
221:
222: /**
223: * Detach this Java3D from it's parent
224: */
225: private void detachNode(Node node) {
226: Group parent = (Group) node.getParent();
227: Node next;
228:
229: if (parent == null)
230: return;
231:
232: parent.removeChild(node);
233: /*
234: java.util.Enumeration e = parent.getAllChildren();
235: int i=0;
236: while(e.hasMoreElements() && i>=0 ) {
237: next = (Node)e.nextElement();
238: if (next == node) {
239: parent.removeChild( i );
240: i=-1; // Finished
241: }
242: i++;
243: }
244: */
245: }
246:
247: }
|