001: /*
002: * $RCSfile: ConfigViewPlatform.java,v $
003: *
004: * Copyright (c) 2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * - Redistribution of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * - Redistribution in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * Neither the name of Sun Microsystems, Inc. or the names of
019: * contributors may be used to endorse or promote products derived
020: * from this software without specific prior written permission.
021: *
022: * This software is provided "AS IS," without a warranty of any
023: * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
024: * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
025: * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
026: * EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL
027: * NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF
028: * USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
029: * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR
030: * ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL,
031: * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND
032: * REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR
033: * INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
034: * POSSIBILITY OF SUCH DAMAGES.
035: *
036: * You acknowledge that this software is not designed, licensed or
037: * intended for use in the design, construction, operation or
038: * maintenance of any nuclear facility.
039: *
040: * $Revision: 1.4 $
041: * $Date: 2007/02/09 17:20:44 $
042: * $State: Exp $
043: */
044:
045: package com.sun.j3d.utils.universe;
046:
047: import java.util.ArrayList;
048: import javax.media.j3d.Node;
049: import javax.media.j3d.View;
050: import javax.media.j3d.ViewPlatform;
051: import javax.media.j3d.Transform3D;
052: import javax.media.j3d.TransformGroup;
053: import javax.vecmath.Matrix4d;
054: import com.sun.j3d.utils.behaviors.vp.ViewPlatformBehavior;
055:
056: class ConfigViewPlatform extends ConfigObject {
057:
058: private boolean allowPolicyRead = false;
059: private boolean allowLocalToVworldRead = false;
060: private boolean nominalViewingTransform = false;
061: private Transform3D initialViewingTransform = null;
062: private ArrayList configViews = new ArrayList();
063: private Viewer[] viewers = null;
064:
065: /**
066: * The corresponding ViewingPlatform instance.
067: */
068: ViewingPlatform viewingPlatform = null;
069:
070: /**
071: * Indicates the view attach policy specified in the configuration file.
072: * If none is set, it remains -1 even though a default may be in effect.
073: */
074: int viewAttachPolicy = -1;
075:
076: /**
077: * The associated ConfigViewPlatformBehavior, if any.
078: */
079: ConfigViewPlatformBehavior configBehavior = null;
080:
081: /**
082: * Overrides initialize() to do nothing.
083: */
084: protected void initialize(ConfigCommand command) {
085: }
086:
087: /**
088: * Processes attributes for this object. Handles commands of the form:<p>
089: * (ViewPlatformAttribute {instanceName} {attrName} {attrValue})<br>
090: * (ViewPlatformProperty {instanceName} {attrName} {attrValue})
091: *
092: * @param command the command that invoked this method
093: */
094: protected void setProperty(ConfigCommand command) {
095:
096: int argc = command.argc;
097: Object[] argv = command.argv;
098: String attribute;
099: Object value;
100:
101: if (argc != 4) {
102: syntaxError("Incorrect number of arguments to "
103: + command.commandName);
104: }
105:
106: if (!isName(argv[2])) {
107: syntaxError("The second argument to " + command.commandName
108: + " must be a property name");
109: }
110:
111: attribute = (String) argv[2];
112: value = argv[3];
113:
114: if (attribute.equals("NominalViewingTransform")) {
115: if (!(value instanceof Boolean)) {
116: syntaxError("NominalViewingTransform must be a boolean");
117: }
118: nominalViewingTransform = ((Boolean) value).booleanValue();
119: } else if (attribute.equals("InitialViewingTransform")) {
120: if (!(value instanceof Matrix4d)) {
121: syntaxError("InitialViewingTransform must be a Matrix4d");
122: }
123: initialViewingTransform = new Transform3D((Matrix4d) value);
124: } else if (attribute.equals("ViewAttachPolicy")) {
125: if (!(value instanceof String)) {
126: syntaxError("ViewAttachPolicy must be a string");
127: }
128:
129: String svalue = (String) value;
130:
131: if (svalue.equals("NOMINAL_HEAD"))
132: viewAttachPolicy = View.NOMINAL_HEAD;
133: else if (svalue.equals("NOMINAL_SCREEN"))
134: viewAttachPolicy = View.NOMINAL_SCREEN;
135: else if (svalue.equals("NOMINAL_FEET"))
136: viewAttachPolicy = View.NOMINAL_FEET;
137: else
138: syntaxError("Illegal value " + svalue
139: + " for ViewAttachPolicy");
140: } else if (attribute.equals("ViewPlatformBehavior")) {
141: if (!(value instanceof String)) {
142: syntaxError("ViewPlatformBehavior must be a name");
143: }
144: configBehavior = (ConfigViewPlatformBehavior) configContainer
145: .findConfigObject("ViewPlatformBehavior",
146: (String) value);
147: } else if (attribute.equals("AllowPolicyRead")) {
148: if (!(value instanceof Boolean)) {
149: syntaxError("value for AllowPolicyRead "
150: + "must be a boolean");
151: }
152: allowPolicyRead = ((Boolean) value).booleanValue();
153: } else if (attribute.equals("AllowLocalToVworldRead")) {
154: if (!(value instanceof Boolean)) {
155: syntaxError("value for AllowLocalToVworldRead "
156: + "must be a boolean");
157: }
158: allowLocalToVworldRead = ((Boolean) value).booleanValue();
159: } else {
160: syntaxError("Unknown " + command.commandName + " \""
161: + attribute + "\"");
162: }
163: }
164:
165: /**
166: * Add a ConfigView to this ConfigViewPlatform.
167: */
168: void addConfigView(ConfigView cv) {
169: configViews.add(cv);
170: }
171:
172: /**
173: * Creates a ViewingPlatform from attributes gathered by this object.
174: *
175: * @param transformCount the number of TransformGroups to attach to the
176: * ViewingPlatform
177: * @return the new ViewingPlatform
178: */
179: ViewingPlatform createViewingPlatform(int transformCount) {
180:
181: // Get the Viewers attached to this ViewingPlatform.
182: // All ConfigViews must be processed at this point.
183: if (configViews.size() == 0) {
184: viewers = new Viewer[0];
185: } else {
186: viewers = new Viewer[configViews.size()];
187: for (int i = 0; i < viewers.length; i++)
188: viewers[i] = ((ConfigView) configViews.get(i)).j3dViewer;
189: }
190:
191: // Create the viewing platform and get its ViewPlatform instance.
192: viewingPlatform = new ViewingPlatform(transformCount);
193: ViewPlatform vp = viewingPlatform.getViewPlatform();
194:
195: // Set defined policies.
196: if (allowPolicyRead)
197: vp.setCapability(ViewPlatform.ALLOW_POLICY_READ);
198:
199: if (allowLocalToVworldRead)
200: vp.setCapability(Node.ALLOW_LOCAL_TO_VWORLD_READ);
201:
202: if (viewAttachPolicy == -1) {
203: // Apply a default based on the eyepoint policy.
204: boolean nominalHead = true;
205: for (int i = 0; i < viewers.length; i++) {
206: if (viewers[i].getView().getWindowEyepointPolicy() != View.RELATIVE_TO_FIELD_OF_VIEW) {
207: nominalHead = false;
208: break;
209: }
210: }
211: if (nominalHead)
212: vp.setViewAttachPolicy(View.NOMINAL_HEAD);
213: else
214: vp.setViewAttachPolicy(View.NOMINAL_SCREEN);
215: } else {
216: vp.setViewAttachPolicy(viewAttachPolicy);
217: }
218:
219: // Assign the viewing platform to all viewers.
220: for (int i = 0; i < viewers.length; i++) {
221: viewers[i].setViewingPlatform(viewingPlatform);
222: }
223:
224: // Apply initial viewing transforms if defined.
225: if (nominalViewingTransform) {
226: viewingPlatform.setNominalViewingTransform();
227: }
228:
229: if (initialViewingTransform != null) {
230: TransformGroup tg = viewingPlatform
231: .getViewPlatformTransform();
232: tg.setTransform(initialViewingTransform);
233: }
234:
235: return viewingPlatform;
236: }
237:
238: /**
239: * Attach any ViewPlatformBehavior specified for this platform.
240: */
241: void processBehavior() {
242: if (configBehavior != null) {
243: viewingPlatform
244: .setViewPlatformBehavior(configBehavior.viewPlatformBehavior);
245: }
246: }
247: }
|