📄 defaultskin.java
字号:
package org.j3de.ui.impl;
import java.awt.Font;
import java.net.URL;
import java.util.Enumeration;
import java.util.Map;
import java.util.HashMap;
import javax.media.j3d.Appearance;
import javax.media.j3d.Bounds;
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.ColoringAttributes;
import javax.media.j3d.Font3D;
import javax.media.j3d.FontExtrusion;
import javax.media.j3d.Geometry;
import javax.media.j3d.Material;
import javax.media.j3d.PointAttributes;
import javax.media.j3d.PolygonAttributes;
import javax.media.j3d.TransparencyAttributes;
import javax.vecmath.Color3f;
import javax.vecmath.Vector3f;
import javax.vecmath.Point3d;
import com.sun.j3d.utils.image.TextureLoader;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.j3de.exception.ExceptionHandler;
import org.j3de.ui.UIElement;
import org.j3de.ui.UIGeometry;
import org.j3de.ui.Skin;
import org.j3de.ui.Skinable;
import org.j3de.ui.SkinException;
import org.j3de.ui.LayoutManager3D;
import org.j3de.ui.LayoutConstraint;
import org.j3de.ui.constraint.ConstraintParam;
import org.j3de.ui.impl.NodeUIElement;
import org.j3de.util.AbstractComponent;
import org.j3de.util.ConfigurationException;
import org.j3de.util.ConfigHelper;
import org.j3de.util.EntryFactory;
import org.j3de.util.StringParser;
public class DefaultSkin extends AbstractComponent implements Skin {
private Map colors;
private Map materials;
private Map appearances;
private Map fonts;
private Map geometries;
private Map layoutManagers;
private Map layoutConstraints;
private Map bounds;
private Map xmlFragments;
private Map shapes;
public Appearance getAppearance(Class part) throws SkinException {
Enumeration enum = new PossibilitiesEnumeration(part);
while (enum.hasMoreElements()) {
Appearance result = (Appearance)appearances.get(enum.nextElement());
if (result != null)
return result;
}
throw new SkinException("appearance", part);
}
public Font3D getFont3D(Class part) throws SkinException {
Enumeration enum = new PossibilitiesEnumeration(part);
while (enum.hasMoreElements()) {
Font3D result = (Font3D)fonts.get(enum.nextElement());
if (result != null)
return result;
}
throw new SkinException("font", part);
}
public UIGeometry getGeometry(Class part) throws SkinException {
Enumeration enum = new PossibilitiesEnumeration(part);
while (enum.hasMoreElements()) {
UIGeometry geometry = (UIGeometry)geometries.get(enum.nextElement());
if (geometry != null)
return geometry;
}
throw new SkinException("geometry", part);
}
public Bounds getBounds(Class part) throws SkinException {
Enumeration enum = new PossibilitiesEnumeration(part);
while (enum.hasMoreElements()) {
Bounds elementBounds = (Bounds)bounds.get(enum.nextElement());
if (elementBounds != null)
return elementBounds;
}
throw new SkinException("bounds", part);
}
public LayoutManager3D getLayoutManager(Class part) throws SkinException {
Enumeration enum = new PossibilitiesEnumeration(part);
while (enum.hasMoreElements()) {
String layoutManagerName = (String)layoutManagers.get(enum.nextElement());
if (layoutManagerName != null) {
try {
LayoutManager3D layoutManager = (LayoutManager3D)Class.forName(layoutManagerName).newInstance();
if (layoutManager instanceof Skinable)
((Skinable)layoutManager).setSkin(this);
return layoutManager;
} catch (Exception e) {
ExceptionHandler.handleException(e);
}
}
}
throw new SkinException("layout manager", part);
}
public LayoutConstraint getLayoutConstraint(Class constraintClass, ConstraintParam param) throws SkinException {
Enumeration enum = new PossibilitiesEnumeration(constraintClass);
while (enum.hasMoreElements()) {
LayoutConstraint constraint = (LayoutConstraint)layoutConstraints.get(enum.nextElement());
if (constraint != null) {
try {
// Return a copy of the constraint found
constraint = constraint.cloneConstraint();
// Set parameters of the constraint
constraint.setParam(param);
return constraint;
} catch (Exception e) {
ExceptionHandler.handleException(e);
}
}
}
throw new SkinException("layout constraint", constraintClass);
}
public Node getXMLFragment(String name) throws SkinException {
Node node = (Node)xmlFragments.get(name);
if (node == null)
throw new SkinException("No xml fragment found for : " + name);
return node;
}
public Color3f getColorByName(String name) throws SkinException {
Color3f color = (Color3f)colors.get(name);
if (color == null)
throw new SkinException("Color not found : " + name);
return color;
}
public javax.media.j3d.Node loadShape(Class shapeClass) throws SkinException {
Enumeration enum = new PossibilitiesEnumeration(shapeClass);
while (enum.hasMoreElements()) {
javax.media.j3d.Node node = (javax.media.j3d.Node)shapes.get(enum.nextElement());
if (node != null) {
try {
return node.cloneTree();
} catch (Exception e) {
ExceptionHandler.handleException(e);
}
}
}
throw new SkinException("shape", shapeClass);
}
private class PossibilitiesEnumeration implements Enumeration {
private boolean hasMoreElements;
private Class part;
public PossibilitiesEnumeration(Class part) {
this.hasMoreElements = (part != null);
this.part = part;
}
public boolean hasMoreElements() {
return hasMoreElements;
}
public Object nextElement() {
String result = part.getName();
hasMoreElements = false;
Class superClass = part.getSuperclass();
if (superClass != Object.class) {
part = superClass;
hasMoreElements = true;
}
return result;
}
}
public void configure(Node node, Document nodeFactory) throws ConfigurationException {
super.configure(node, nodeFactory);
colors = helper.getMap("colors",
new EntryFactory() {
public Object createEntry(Node node) {
try {
NamedNodeMap attributes = node.getAttributes();
String color = attributes.getNamedItem("color").getNodeValue();
return StringParser.parseColor3f(color);
} catch (Exception e) {
ExceptionHandler.handleException(e);
}
return null;
}
});
materials = helper.getMap("materials",
new EntryFactory() {
public Object createEntry(Node node) {
try {
Material material = new Material();
NamedNodeMap attributes = node.getAttributes();
for (int i=0; i<attributes.getLength(); i++) {
Node item = attributes.item(i);
String name = item.getNodeName();
String value = item.getNodeValue();
if (name.equals("ambientcolor")) {
material.setAmbientColor((Color3f)colors.get(value));
} else if (name.equals("diffusecolor")) {
material.setDiffuseColor((Color3f)colors.get(value));
} else if (name.equals("emissivecolor")) {
material.setEmissiveColor((Color3f)colors.get(value));
} else if (name.equals("specularcolor")) {
material.setSpecularColor((Color3f)colors.get(value));
} else if (name.equals("shininess")) {
material.setShininess(Float.parseFloat(value));
} else if (name.equals("lightingenabled")) {
material.setLightingEnable(value.equals("true"));
}
}
return material;
} catch (Exception e) {
ExceptionHandler.handleException(e);
}
return null;
}
});
appearances = helper.getMap("appearances",
new EntryFactory() {
public Object createEntry(Node node) {
Appearance appearance = new Appearance();
try {
NamedNodeMap attributes = node.getAttributes();
for (int i=0; i<attributes.getLength(); i++) {
Node item = attributes.item(i);
String name = item.getNodeName();
String value = item.getNodeValue();
if (name.equals("material")) {
appearance.setMaterial((Material)materials.get(value));
} else if (name.equals("texture")) {
TextureLoader tex = new TextureLoader(new URL(value), null);
appearance.setTexture(tex.getTexture());
} else if (name.equals("coloringattributes")) {
appearance.setColoringAttributes(StringParser.parseColoringAttributes(value, DefaultSkin.this));
} else if (name.equals("polygonattributes")) {
appearance.setPolygonAttributes(StringParser.parsePolygonAttributes(value));
} else if (name.equals("pointattributes")) {
appearance.setPointAttributes(StringParser.parsePointAttributes(value));
} else if (name.equals("transparencyattributes")) {
appearance.setTransparencyAttributes(StringParser.parseTransparencyAttributes(value));
}
else if (name.equals("renderingattributes")) {
appearance.setRenderingAttributes(StringParser.parseRenderingtAttributes(value));
}
}
} catch (Exception e) {
ExceptionHandler.handleException(e);
}
return appearance;
}
});
fonts = helper.getMap("fonts",
new EntryFactory() {
public Object createEntry(Node node) {
try {
NamedNodeMap attributes = node.getAttributes();
String fontname = attributes.getNamedItem("fontname").getNodeValue();
int fontsize = Integer.parseInt(attributes.getNamedItem("fontsize").getNodeValue());
Font3D font3d =
new Font3D(new Font(fontname, Font.PLAIN, fontsize), new FontExtrusion());
return font3d;
} catch (Exception e) {
ExceptionHandler.handleException(e);
}
return null;
}
});
geometries = helper.getMap("geometries",
new EntryFactory() {
public Object createEntry(Node node) {
try {
return new ConfigHelper(node).getComponent("geometry", null, null, null, this.getClass().getClassLoader());
} catch (Exception e) {
ExceptionHandler.handleException(e);
}
return null;
}
});
bounds = helper.getMap("bounds",
new EntryFactory() {
public Object createEntry(Node node) {
try {
NamedNodeMap attributes = node.getAttributes();
String shape = attributes.getNamedItem("shape").getNodeValue();
if (shape.equals("sphere")) {
double radius = Double.parseDouble(attributes.getNamedItem("radius").getNodeValue());
return new BoundingSphere(new Point3d(0.0, 0.0, 0.0), radius);
} else
return null;
} catch (Exception e) {
ExceptionHandler.handleException(e);
}
return null;
}
});
layoutManagers = helper.getStringMap("layoutmanagers");
layoutConstraints = helper.getComponentMap("layoutconstraints", this.getClass().getClassLoader());
xmlFragments = helper.getMap("xmlFragments",
new EntryFactory() {
public Object createEntry(Node node) {
return node;
}
});
shapes = helper.getComponentMap("shapes", this.getClass().getClassLoader());
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -