Java 3D Desktop Environment旨在使用Java 3D来创建一个3D桌面环境。功能包括:分布式的应用程序
源代码在线查看: abstractuitext.java
package org.j3de.ui.impl;
import javax.media.j3d.Appearance;
import javax.media.j3d.BoundingBox;
import javax.media.j3d.Font3D;
import javax.media.j3d.Node;
import javax.media.j3d.Shape3D;
import javax.media.j3d.Text3D;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;
import org.j3de.exception.ExceptionHandler;
import org.j3de.datamodel.RemoteObjectModel;
import org.j3de.ui.UILocalElement;
import org.j3de.ui.Skin;
import org.j3de.ui.Skinable;
import org.j3de.ui.SkinException;
public abstract class AbstractUIText implements UILocalElement, Skinable {
private RemoteObjectModel textModel;
private Shape3D sh;
private Text3D txt;
private Font3D font;
private Skin skin;
private Class textAppearanceClass;
private Class textFontClass;
private BoundsChangeHelper boundsHelper;
private ChangeListener changeListener =
new ChangeListener() {
public void stateChanged(ChangeEvent e) {
updateText();
}
};
public AbstractUIText() {
boundsHelper = new BoundsChangeHelper(this);
txt = new Text3D();
txt.setCapability(Text3D.ALLOW_STRING_WRITE);
txt.setCapability(Text3D.ALLOW_FONT3D_WRITE);
txt.setCapability(Text3D.ALLOW_BOUNDING_BOX_READ);
sh = new Shape3D();
sh.setGeometry(txt);
sh.setCapability(Shape3D.ALLOW_APPEARANCE_WRITE);
sh.setCapability(Shape3D.ALLOW_BOUNDS_WRITE);
sh.setCapability(Shape3D.ALLOW_BOUNDS_READ);
sh.setBoundsAutoCompute(false);
textAppearanceClass = getTextAppearanceClass();
textFontClass = getTextFontClass();
}
public Node getNode() {
return sh;
}
protected abstract Class getTextAppearanceClass();
protected abstract Class getTextFontClass();
protected void updateBounds() {
boundsHelper.updateBounds();
}
public void addBoundsChangeListener(ChangeListener listener) {
boundsHelper.addBoundsChangeListener(listener);
}
public void removeBoundsChangeListener(ChangeListener listener) {
boundsHelper.removeBoundsChangeListener(listener);
}
private void updateTextBounds() {
BoundingBox bb = new BoundingBox();
txt.getBoundingBox(bb);
sh.setBounds(bb);
}
public void setSkin(Skin skin) throws SkinException {
this.skin = skin;
this.setTextAppearance(skin.getAppearance(textAppearanceClass));
this.setFont3D(skin.getFont3D(textFontClass));
updateTextBounds();
}
protected void refreshSkin() throws SkinException {
this.setSkin(skin);
}
private void setTextAppearance(Appearance appearance) {
sh.setAppearance(appearance);
}
private void setFont3D(Font3D font) {
this.font = font;
txt.setFont3D(font);
}
protected Font3D getFont3D() {
return font;
}
protected void setRemoteObjectModel(RemoteObjectModel textModel) {
if (this.textModel != null)
this.textModel.removeChangeListener(changeListener);
this.textModel = textModel;
textModel.addChangeListener(changeListener);
updateText();
}
protected RemoteObjectModel getRemoteObjectModel() {
return textModel;
}
private void updateText() {
if (txt != null) {
txt.setString(textModel.getObject().toString());
updateTextBounds();
}
updateBounds();
}
protected class RemoteUIText {
/** Set the appearance class for the displayed Text.
* @param textAppearanceClass new text appearance. It has to implement the
* interface returned by getTextAppearanceClass() */
public void setTextAppearanceClass(Class textAppearanceClass) {
if (!getTextAppearanceClass().isAssignableFrom(textAppearanceClass))
return;
AbstractUIText.this.textAppearanceClass = textAppearanceClass;
}
/** Set thefont class for the displayed Text.
* @param textFontClass new text appearance. It has to implement the
* interface returned by getTextAppearanceClass() */
public void setFontAppearanceClass(Class textFontClass) {
if (!getTextFontClass().isAssignableFrom(textFontClass))
return;
AbstractUIText.this.textFontClass = textFontClass;
}
public String getText() {
return getRemoteObjectModel().getObject().toString();
}
}
}