📄 configcontainer.java
字号:
// instantiate the target object, and then apply the configuration // properties to it. try { objectClass = Class.forName("com.sun.j3d.utils.universe.Config" + cmd.baseName) ; } catch (ClassNotFoundException e) { throw new IllegalArgumentException ("\"" + cmd.baseName + "\"" + " is not a configurable object; ignoring command") ; } try { configObject = (ConfigObject)(objectClass.newInstance()) ; } catch (IllegalAccessException e) { System.out.println(e) ; throw new IllegalArgumentException("Ignoring command") ; } catch (InstantiationException e) { System.out.println(e) ; throw new IllegalArgumentException("Ignoring command") ; } // Process an Alias keyword if present. This option is available for // all New commands so it is processed here. The Alias keyword must // be the penultimate command element, followed by a String. for (int i = 2 ; i < cmd.argc ; i++) { if (cmd.argv[i] instanceof String && ((String)cmd.argv[i]).equals("Alias")) { if (i == (cmd.argc - 2) && cmd.argv[i+1] instanceof String) { addConfigObject(new ConfigAlias(cmd.baseName, (String)cmd.argv[i+1], configObject)) ; cmd.argc -= 2 ; } else { throw new IllegalArgumentException ("The alias name must be a string and " + "must be the last command argument") ; } } } // Initialize common fields. configObject.baseName = cmd.baseName ; configObject.instanceName = cmd.instanceName ; configObject.creatingCommand = cmd ; configObject.configContainer = this ; // Initialize specific fields and return the ConfigObject. configObject.setClassLoader(classLoader); configObject.initialize(cmd) ; return configObject ; } /** * Instantiate and initialize a ConfigObject base class containing alias * information. The command is of the form:<p> * * ({baseName}Alias {aliasName} {originalName}) * * @param cmd configuration command that creates a new alias * @return the new ConfigObject with alias information */ private ConfigObject createConfigAlias(ConfigCommand cmd) { ConfigObject original ; if (cmd.argc != 3 || ! (cmd.argv[2] instanceof String)) throw new IllegalArgumentException ("Command \"" + cmd.commandName + "\" requires an instance name as second argument") ; original = findConfigObject(cmd.baseName, (String)cmd.argv[2]) ; return new ConfigAlias(cmd.baseName, cmd.instanceName, original) ; } /** * A class that does nothing but reference another ConfigObject. Once * created, the alias name can be used in all commands that would accept * the original name. A lookup of the alias name will always return the * original instance. */ private static class ConfigAlias extends ConfigObject { ConfigAlias(String baseName, String instanceName, ConfigObject targ) { this.baseName = baseName ; this.instanceName = instanceName ; this.isAlias = true ; this.original = targ ; targ.aliases.add(instanceName) ; } } /** * Adds the specified ConfigObject instance into this container using the * given ConfigCommand's base name and instance name. * * @param object the ConfigObject instance to add into the database */ private void addConfigObject(ConfigObject object) { ArrayList instances ; instances = (ArrayList)baseNameMap.get(object.baseName) ; if (instances == null) { instances = new ArrayList() ; baseNameMap.put(object.baseName, instances) ; } // Disallow duplicate instance names. for (int i = 0 ; i < instances.size() ; i++) { ConfigObject co = (ConfigObject)instances.get(i) ; if (co.instanceName.equals(object.instanceName)) { // Don't confuse anybody using Window. String base = object.baseName ; if (base.equals("Screen")) base = "Screen or Window" ; throw new IllegalArgumentException ("Duplicate " + base + " instance name \"" + object.instanceName + "\" ignored") ; } } instances.add(object) ; } /** * Finds a config object matching the given base name and the instance * name. If an alias is found, then its original is returned. If the * object is not found, an IllegalArgumentException is thrown.<p> * * @param basename base name of the config object * @param instanceName name associated with this config object instance * @return the found ConfigObject */ ConfigObject findConfigObject(String baseName, String instanceName) { ArrayList instances ; ConfigObject configObject ; instances = (ArrayList)baseNameMap.get(baseName) ; if (instances != null) { for (int i = 0 ; i < instances.size() ; i++) { configObject = (ConfigObject)instances.get(i) ; if (configObject.instanceName.equals(instanceName)) { if (configObject.isAlias) return configObject.original ; else return configObject ; } } } // Throw an error, but don't confuse anybody using Window. if (baseName.equals("Screen")) baseName = "Screen or Window" ; throw new IllegalArgumentException (baseName + " \"" + instanceName + "\" not found") ; } /** * Find instances of config objects with the given base name. * This is the same as <code>findConfigObjects(baseName, true)</code>. * Aliases are filtered out so that all returned instances are unique. * * @param baseName base name of desired config object class * @return ArrayList of config object instances of the desired base * class, or null if instances of the base class don't exist */ Collection findConfigObjects(String baseName) { return findConfigObjects(baseName, true) ; } /** * Find instances of config objects with the given base name. * * @param baseName base name of desired config object class * @param filterAlias if true, aliases are filtered out so that all * returned instances are unique * @return ArrayList of config object instances of the desired base * class, or null if instances of the base class don't exist */ Collection findConfigObjects(String baseName, boolean filterAlias) { ArrayList instances ; instances = (ArrayList)baseNameMap.get(baseName) ; if (instances == null || instances.size() == 0) { return null ; // This is not an error. } if (filterAlias) { ArrayList output = new ArrayList() ; for (int i = 0 ; i < instances.size() ; i++) { ConfigObject configObject = (ConfigObject)instances.get(i) ; if (! configObject.isAlias) { output.add(configObject) ; } } return output ; } else { return instances ; } } /** * Returns the ConfigObject associated with the name in the given * ConfigCommand. This is used for evaluating retained built-in commands * after the config file has already been parsed. The parser won't catch * any of the exceptions generated by this method, so the error messages * are wrapped accordingly. * * @param basename base name of the config object * @param cmd command containing the name in argv[1] * @return the found ConfigObject */ private ConfigObject findConfigObject(String baseName, ConfigCommand cmd) { if (cmd.argc != 2 || !(cmd.argv[1] instanceof String)) throw new IllegalArgumentException (ConfigObject.errorMessage (cmd, "Parameter must be a single string")) ; try { return findConfigObject(baseName, (String)cmd.argv[1]) ; } catch (IllegalArgumentException e) { throw new IllegalArgumentException (ConfigObject.errorMessage(cmd, e.getMessage())) ; } } /** * This method gets called from a ConfigObject to evaluate a retained * built-in command nested within a property command. These are commands * that can't be evaluated until the entire config file is parsed. * * @param cmd the built-in command * @return object representing result of evaluation */ Object evaluateBuiltIn(ConfigCommand cmd) { int argc = cmd.argc ; Object[] argv = cmd.argv ; if (cmd.commandName.equals("ConfigContainer")) { // return a reference to this ConfigContainer return this ; } else if (cmd.commandName.equals("Canvas3D")) { // Look for canvases in the screen database. return ((ConfigScreen)findConfigObject("Screen", cmd)).j3dCanvas ; } else if (baseNameMap.get(cmd.commandName) != null) { // Handle commands of the form ({objectType} name) that return the // object associated with the name. return findConfigObject(cmd.commandName, cmd).targetObject ; } else { // So far no other retained built-in commands. throw new IllegalArgumentException (ConfigObject.errorMessage(cmd, "Unknown built-in command \"" + cmd.commandName + "\"")) ; } } /** * Process the configuration after parsing the configuration file. * Note: the processing order of the various config objects is * significant. * * @param setVisible true if Viewer components should be visible * @param transformCount number of TransformGroups with which * ViewingPlatforms should be created * @param attachBehaviors true if behaviors should be attached to * ViewingPlatforms */ private void processConfig(boolean setVisible, int transformCount, boolean attachBehaviors) { Collection c, s, pe, vp ; this.setVisible = setVisible ; this.transformCount = transformCount ; c = findConfigObjects("PhysicalBody") ; if (c != null) { processPhysicalBodies(c) ; } pe = findConfigObjects("PhysicalEnvironment") ; if (pe != null) { processPhysicalEnvironments(pe) ; } c = findConfigObjects("View") ; if (c != null) { processViews(c, setVisible) ; } c = findConfigObjects("Device") ; s = findConfigObjects("Sensor") ; if (c != null) { processDevices(c, s, pe) ; } vp = findConfigObjects("ViewPlatform") ; if (vp != null) { processViewPlatforms(vp, transformCount) ; } c = findConfigObjects("ViewPlatformBehavior") ; if (c != null) { processViewPlatformBehaviors(c, vp, attachBehaviors) ; } c = findConfigObjects("Object") ; if (c != null) { processGenericObjects(c) ; } } // Process config physical environments into Java 3D physical // environments. private void processPhysicalEnvironments(Collection c) { Iterator i = c.iterator() ; while (i.hasNext()) { ConfigPhysicalEnvironment e = (ConfigPhysicalEnvironment)i.next() ; e.targetObject = e.createJ3dPhysicalEnvironment() ; } } // Process config physical bodys into Java 3D physical bodies. private void processPhysicalBodies(Collection c) { Iterator i = c.iterator() ; while (i.hasNext()) { ConfigPhysicalBody b = (ConfigPhysicalBody)i.next() ; b.targetObject = b.createJ3dPhysicalBody() ; } } // Process config views into Java 3D Views and then create Viewer objects // for them. This should only be called after all physical bodies and // physical environments have been processed. private void processViews(Collection c, boolean setVisible) { Iterator i = c.iterator() ; while (i.hasNext()) { ConfigView v = (ConfigView)i.next() ; v.targetObject = v.createViewer(setVisible) ; } } // Process config devices into Java 3D input devices. This should be done // only after all views have been processed, as some InputDevice // implementations require the AWT components associated with a view. private void processDevices(Collection c, Collection s, Collection p) { ConfigDevice cd = null ; Iterator i = c.iterator() ; while (i.hasNext()) { cd = (ConfigDevice)i.next() ; cd.targetObject = cd.createInputDevice() ; } // Process device properties only after all InputDevices have been // instantiated. Some InputDevice properties require references // to other InputDevice implementations. i = c.iterator() ; while (i.hasNext()) ((ConfigDevice)i.next()).processProperties() ; // Initialize the devices only after all have been instantiated, as // some InputDevices implementations are slaved to the first one // created and will not initialize otherwise (e.g. LogitechTracker). i = c.iterator() ; while (i.hasNext()) { cd = (ConfigDevice)i.next() ; if (! cd.j3dInputDevice.initialize()) throw new RuntimeException (cd.errorMessage(cd.creatingCommand, "could not initialize device \"" + cd.instanceName + "\"")) ; } // An InputDevice implementation will have created all its Sensors by // the time initialize() returns. Retrieve and configure them here. if (s != null) { i = s.iterator() ; while (i.hasNext()) { ConfigSensor cs = (ConfigSensor)i.next() ; cs.configureSensor() ;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -