001: /*
002: * $Header: /cvs/j3dfly/J3dEditor/src/org/jdesktop/j3dedit/EditorControlFrame.java,v 1.1 2005/04/20 22:20:25 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;
019:
020: import javax.swing.tree.DefaultMutableTreeNode;
021: import javax.swing.tree.DefaultTreeModel;
022: import javax.swing.tree.TreePath;
023: import java.util.Iterator;
024: import org.jdesktop.j3dedit.scenegrapheditor.VisualToolManager;
025: import org.jdesktop.j3dedit.scenegrapheditor.visualtools.ViewPlatformMotionMonitorVisualTool;
026: import org.jdesktop.j3dfly.event.EventProcessor;
027: import org.jdesktop.j3dfly.namecontrol.NameControl;
028: import org.jdesktop.j3dfly.namecontrol.NameUpdateEvent;
029: import org.jdesktop.j3dfly.namecontrol.NameUpdateListener;
030: import org.jdesktop.j3dedit.event.NamedObjectSelectionEvent;
031:
032: /**
033: * The Frame for navigating around Named nodes and selecting various editor
034: * operations. By default this Frame appears on the far left of the screen.
035: *
036: * @author Paul Byrne
037: */
038: public class EditorControlFrame extends javax.swing.JFrame implements
039: javax.swing.event.TreeSelectionListener, NameUpdateListener,
040: ViewPlatformMotionMonitorVisualTool.ViewPlatformMotionListener {
041:
042: private DefaultTreeModel objectTreeModel;
043: private DefaultMutableTreeNode behaviorRoot;
044: private DefaultMutableTreeNode objectRoot;
045: private DefaultMutableTreeNode environmentRoot;
046: private DefaultMutableTreeNode loaderRoot;
047:
048: private J3dEditContext context;
049:
050: /** Creates new form EditorControlFrame */
051: public EditorControlFrame(J3dEditContext context) {
052: this .context = context;
053: DefaultMutableTreeNode treeRoot = new DefaultMutableTreeNode(
054: "Objects");
055: behaviorRoot = new DefaultMutableTreeNode(
056: "<html><b>Behaviors</b>");
057: //behaviorRoot = new DefaultMutableTreeNode("Behaviors");
058: treeRoot.add(behaviorRoot);
059:
060: objectRoot = new DefaultMutableTreeNode("<html><b>Objects</b>");
061: treeRoot.add(objectRoot);
062:
063: // Lights, Fog
064: environmentRoot = new DefaultMutableTreeNode(
065: "<html><b>Environment<b>");
066: treeRoot.add(environmentRoot);
067:
068: loaderRoot = new DefaultMutableTreeNode("<html><b>Loaders<b>");
069: treeRoot.add(loaderRoot);
070:
071: initComponents();
072:
073: TreePath treePath = null;
074:
075: objectTreeModel = new DefaultTreeModel(treeRoot);
076:
077: objectTree.addTreeSelectionListener(this );
078: objectTree.putClientProperty("JTree.lineStyle", "Angled");
079: objectTree.setModel(objectTreeModel);
080:
081: context.getNameControl().addNameUpdateListener(this );
082:
083: // Universe null check for interposer
084: if (context.getUniverse() != null) {
085: ViewPlatformMotionMonitorVisualTool motion = (ViewPlatformMotionMonitorVisualTool) VisualToolManager
086: .getManager().getVisualTool(
087: ViewPlatformMotionMonitorVisualTool.class);
088: if (motion == null) {
089: motion = new ViewPlatformMotionMonitorVisualTool(
090: context.getUniverse().getViewingPlatform()
091: .getViewPlatformTransform());
092: VisualToolManager.getManager().addTool(motion);
093: }
094: motion.addViewPlatformMotionListener(this );
095: }
096:
097: pack();
098: }
099:
100: /** This method is called from within the constructor to
101: * initialize the form.
102: * WARNING: Do NOT modify this code. The content of this method is
103: * always regenerated by the FormEditor.
104: */
105: private void initComponents() {//GEN-BEGIN:initComponents
106: java.awt.GridBagConstraints gridBagConstraints;
107:
108: worldModeButtonGroup = new javax.swing.ButtonGroup();
109: jScrollPane1 = new javax.swing.JScrollPane();
110: objectTree = new javax.swing.JTree();
111: centerPanel = new javax.swing.JPanel();
112: jPanel1 = new javax.swing.JPanel();
113: worldSelectB = new javax.swing.JToggleButton();
114: worldInteractB = new javax.swing.JToggleButton();
115: viewPosPanel = new javax.swing.JPanel();
116: viewPositionTF = new javax.swing.JTextField();
117: viewOrientationTF = new javax.swing.JTextField();
118:
119: addWindowListener(new java.awt.event.WindowAdapter() {
120: public void windowClosing(java.awt.event.WindowEvent evt) {
121: exitForm(evt);
122: }
123: });
124:
125: jScrollPane1.setPreferredSize(new java.awt.Dimension(150, 200));
126: objectTree.setModel(objectTreeModel);
127: objectTree.setRootVisible(false);
128: objectTree.setShowsRootHandles(true);
129: jScrollPane1.setViewportView(objectTree);
130:
131: getContentPane().add(jScrollPane1, java.awt.BorderLayout.NORTH);
132:
133: centerPanel.setLayout(new java.awt.CardLayout());
134:
135: centerPanel.setBorder(new javax.swing.border.EtchedBorder());
136: jPanel1.setLayout(new java.awt.GridBagLayout());
137:
138: worldSelectB.setText("World Selection");
139: worldSelectB.setToolTipText("Select Objects in the 3D scene");
140: worldModeButtonGroup.add(worldSelectB);
141: worldSelectB
142: .addActionListener(new java.awt.event.ActionListener() {
143: public void actionPerformed(
144: java.awt.event.ActionEvent evt) {
145: worldSelectBActionPerformed(evt);
146: }
147: });
148:
149: jPanel1.add(worldSelectB, new java.awt.GridBagConstraints());
150:
151: worldInteractB.setSelected(true);
152: worldInteractB.setText("World Interaction");
153: worldInteractB
154: .setToolTipText("Move around the universe using View Platform Behavior");
155: worldModeButtonGroup.add(worldInteractB);
156: worldInteractB
157: .addActionListener(new java.awt.event.ActionListener() {
158: public void actionPerformed(
159: java.awt.event.ActionEvent evt) {
160: worldInteractBActionPerformed(evt);
161: }
162: });
163:
164: gridBagConstraints = new java.awt.GridBagConstraints();
165: gridBagConstraints.gridx = 0;
166: gridBagConstraints.gridy = 1;
167: jPanel1.add(worldInteractB, gridBagConstraints);
168:
169: centerPanel.add(jPanel1, "jPanel1");
170:
171: getContentPane().add(centerPanel, java.awt.BorderLayout.CENTER);
172:
173: viewPosPanel.setLayout(new java.awt.GridBagLayout());
174:
175: viewPosPanel.setBorder(new javax.swing.border.TitledBorder(
176: "View Platform"));
177: viewPositionTF.setEditable(false);
178: viewPositionTF.setFont(new java.awt.Font("Dialog", 0, 10));
179: viewPositionTF
180: .setText(" ");
181: viewPositionTF
182: .setToolTipText("X, Y, Z Position of View Platform");
183: viewPositionTF
184: .addActionListener(new java.awt.event.ActionListener() {
185: public void actionPerformed(
186: java.awt.event.ActionEvent evt) {
187: viewPositionTFActionPerformed(evt);
188: }
189: });
190:
191: gridBagConstraints = new java.awt.GridBagConstraints();
192: gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
193: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
194: viewPosPanel.add(viewPositionTF, gridBagConstraints);
195:
196: viewOrientationTF.setEditable(false);
197: viewOrientationTF.setFont(new java.awt.Font("Dialog", 0, 10));
198: viewOrientationTF
199: .setText(" ");
200: viewOrientationTF
201: .setToolTipText("Roll, Pitch, Yaw Orientation of View Platform (Degrees)");
202: gridBagConstraints = new java.awt.GridBagConstraints();
203: gridBagConstraints.gridx = 1;
204: gridBagConstraints.gridy = 1;
205: gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
206: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
207: viewPosPanel.add(viewOrientationTF, gridBagConstraints);
208:
209: getContentPane().add(viewPosPanel, java.awt.BorderLayout.SOUTH);
210:
211: }//GEN-END:initComponents
212:
213: private void viewPositionTFActionPerformed(
214: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_viewPositionTFActionPerformed
215: // Add your handling code here:
216: }//GEN-LAST:event_viewPositionTFActionPerformed
217:
218: private void worldInteractBActionPerformed(
219: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_worldInteractBActionPerformed
220: // Add your handling code here:
221: }//GEN-LAST:event_worldInteractBActionPerformed
222:
223: private void worldSelectBActionPerformed(
224: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_worldSelectBActionPerformed
225: // Add your handling code here:
226: }//GEN-LAST:event_worldSelectBActionPerformed
227:
228: /** Exit the Application */
229: private void exitForm(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_exitForm
230: }//GEN-LAST:event_exitForm
231:
232: public void valueChanged(javax.swing.event.TreeSelectionEvent evt) {
233: //System.out.println( evt.getPath() );
234: //System.out.println( evt.getPath().getLastPathComponent() );
235: //System.out.println( NameControl.getNameControl().getNamedObject( ((ECTreeNode)evt.getPath().getLastPathComponent()).getObjectName() ));
236:
237: if (evt.getPath().getLastPathComponent() instanceof ECTreeNode) {
238: NameControl.ObjectName name = ((ECTreeNode) evt.getPath()
239: .getLastPathComponent()).getObjectName();
240: if (evt.getPath().getPathComponent(1) == behaviorRoot)
241: context.getEventProcessor().postEvent(
242: new NamedObjectSelectionEvent(name,
243: NamedObjectSelectionEvent.BEHAVIOR));
244: else if (evt.getPath().getPathComponent(1) == environmentRoot)
245: context.getEventProcessor().postEvent(
246: new NamedObjectSelectionEvent(name,
247: NamedObjectSelectionEvent.ENVIRONMENT));
248: else if (evt.getPath().getPathComponent(1) == loaderRoot)
249: context.getEventProcessor().postEvent(
250: new NamedObjectSelectionEvent(name,
251: NamedObjectSelectionEvent.LOADER));
252: else if (evt.getPath().getPathComponent(1) == objectRoot)
253: context.getEventProcessor().postEvent(
254: new NamedObjectSelectionEvent(name,
255: NamedObjectSelectionEvent.OBJECT));
256: }
257: }
258:
259: /**
260: * Populate the list with all the names in NameControl
261: */
262: private void populateList() {
263: Iterator it = context.getNameControl().getBehaviorNames();
264: while (it.hasNext()) {
265: NameControl.ObjectName objName = (NameControl.ObjectName) it
266: .next();
267: ECTreeNode n = new ECTreeNode(objName.objectName);
268: n.setObjectName(objName);
269: n.setAllowsChildren(false);
270: behaviorRoot.add(n);
271: }
272:
273: it = context.getNameControl().getEnvironmentNames();
274: while (it.hasNext()) {
275: NameControl.ObjectName objName = (NameControl.ObjectName) it
276: .next();
277: ECTreeNode n = new ECTreeNode(objName.objectName);
278: n.setObjectName(objName);
279: n.setAllowsChildren(false);
280: environmentRoot.add(n);
281: }
282:
283: it = context.getNameControl().getOtherNames();
284: while (it.hasNext()) {
285: NameControl.ObjectName objName = (NameControl.ObjectName) it
286: .next();
287: ECTreeNode n = new ECTreeNode(objName.objectName);
288: n.setObjectName(objName);
289: n.setAllowsChildren(false);
290: objectRoot.add(n);
291: }
292:
293: objectTreeModel.reload();
294: }
295:
296: /**
297: * Remove all the elements from the list
298: */
299: private void clearList() {
300: behaviorRoot.removeAllChildren();
301: environmentRoot.removeAllChildren();
302: objectRoot.removeAllChildren();
303: }
304:
305: /**
306: * NameUpdateListener callback
307: */
308: public void nameUpdate(NameUpdateEvent evt) {
309: if (evt.getType() == NameUpdateEvent.SCENE_ADDED) {
310: clearList();
311: populateList();
312: } else if (evt.getType() == NameUpdateEvent.OBJECT_ADDED) {
313: // TODO Just add the object rather than recreating the
314: // entire list
315: clearList();
316: populateList();
317: } else if (evt.getType() == NameUpdateEvent.NAME_CHANGED) {
318: // TODO Just add the object rather than recreating the
319: // entire list
320: clearList();
321: populateList();
322: } else if (evt.getType() == NameUpdateEvent.NAME_REMOVED) {
323: // TODO Just add the object rather than recreating the
324: // entire list
325: clearList();
326: populateList();
327: } else
328: throw new RuntimeException(
329: "Internal ERROR - unknown nameUpdateEvent type "
330: + evt.getType());
331: }
332:
333: /**
334: * The new view platform position and orientation
335: */
336: public void viewPlatformMoved(double x, double y, double z,
337: double roll, double pitch, double yaw) {
338: int space = 24; // Text Field is 24 characters
339:
340: space -= 4; // remove space for ', ' and ', '
341: space /= 3;
342:
343: String xStr = Double.toString(x);
344: if (xStr.length() > space)
345: xStr = xStr.substring(0, space - 1);
346:
347: String yStr = Double.toString(y);
348: if (yStr.length() > space)
349: yStr = yStr.substring(0, space - 1);
350:
351: String zStr = Double.toString(z);
352: if (zStr.length() > space)
353: zStr = zStr.substring(0, space - 1);
354:
355: viewPositionTF.setText(xStr + ", " + yStr + ", " + zStr);
356:
357: String rollStr = Double.toString(Math.toDegrees(roll));
358: if (rollStr.length() > space)
359: rollStr = rollStr.substring(0, space - 1);
360:
361: String pitchStr = Double.toString(Math.toDegrees(pitch));
362: if (pitchStr.length() > space)
363: pitchStr = pitchStr.substring(0, space - 1);
364:
365: String yawStr = Double.toString(Math.toDegrees(yaw));
366: if (yawStr.length() > space)
367: yawStr = yawStr.substring(0, space - 1);
368:
369: viewOrientationTF.setText(rollStr + ", " + pitchStr + ", "
370: + yawStr);
371: }
372:
373: // Variables declaration - do not modify//GEN-BEGIN:variables
374: private javax.swing.JTree objectTree;
375: private javax.swing.JPanel jPanel1;
376: private javax.swing.JScrollPane jScrollPane1;
377: private javax.swing.JPanel centerPanel;
378: private javax.swing.JTextField viewPositionTF;
379: private javax.swing.JTextField viewOrientationTF;
380: private javax.swing.JToggleButton worldInteractB;
381: private javax.swing.ButtonGroup worldModeButtonGroup;
382: private javax.swing.JPanel viewPosPanel;
383: private javax.swing.JToggleButton worldSelectB;
384:
385: // End of variables declaration//GEN-END:variables
386:
387: /**
388: * Subclass of DefaultMutableTreeNode that keeps a reference to
389: * the objectName of the item represented by the node.
390: *
391: * Can't use DefaultMutableTreeNode.setUserObject() because the tree
392: * displays the toString() result as the nodes name
393: */
394: class ECTreeNode extends javax.swing.tree.DefaultMutableTreeNode {
395: private NameControl.ObjectName objectName = null;
396:
397: public ECTreeNode(String name) {
398: super (name);
399: }
400:
401: public void setObjectName(NameControl.ObjectName name) {
402: objectName = name;
403: }
404:
405: public NameControl.ObjectName getObjectName() {
406: return objectName;
407: }
408: }
409: }
|