⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 defaultuifactory.java

📁 Java 3D Desktop Environment旨在使用Java 3D来创建一个3D桌面环境。功能包括:分布式的应用程序
💻 JAVA
字号:
package org.j3de.ui.impl;

import java.io.IOException;
import java.io.NotActiveException;
import java.io.ObjectInputStream;  
import java.lang.reflect.Constructor; 
import java.lang.reflect.InvocationTargetException;
import java.net.URL;
import java.rmi.Remote;
import java.rmi.RemoteException; 
import java.rmi.server.RemoteObject;
import java.rmi.server.RemoteStub;
import java.rmi.server.UnicastRemoteObject;
import java.util.Map;    
import java.util.HashMap;
                                  
import org.j3de.exception.ExceptionHandler;            
import org.j3de.ui.ActionElement;
import org.j3de.ui.Label3D;
import org.j3de.ui.PasswordField3D;
import org.j3de.ui.TextField3D;
import org.j3de.ui.Border3D;
import org.j3de.ui.Skin;
import org.j3de.ui.Skinable;
import org.j3de.ui.SkinException;
import org.j3de.ui.UIContainer;
import org.j3de.ui.UICreationException;
import org.j3de.ui.UIElement;
import org.j3de.ui.UIElementMapper;
import org.j3de.ui.UIFactory; 
import org.j3de.ui.UILocalElement;
import org.j3de.ui.LayoutManager3D;
import org.j3de.util.Configuration;
 
public class DefaultUIFactory implements UIFactory {
  private final static Class[] CONSTRUCTOR_PARAMS = {UIElementMapper.class}; 
  
  private URL skinURL;
  
  private transient Skin skin;        
  private transient Map remoteObjectMap;  
  private Map classMap; 
  private UIElementMapper uiElementMapper;
    
  public DefaultUIFactory(URL skinURL, Map classMap) {
    this.skinURL    = skinURL;       
    this.classMap   = classMap;
  }
  
  public UIElement createUIElement(Class remoteClass) throws UICreationException, RemoteException {      
    UILocalElement localResult    = null;
    UIElement      result         = null;
    String         classname      = null;
    
    try {
    
      classname = (String)classMap.get(remoteClass.getName());     
      
      if (classname == null)
        throw new UICreationException("No implementation assigned to " + remoteClass.getName()); 
      
      Class localElementClass = Class.forName(classname);
      
      try {
        Constructor constructor = localElementClass.getConstructor(CONSTRUCTOR_PARAMS); 
        Object[] params = new Object[1];
        params[0] = uiElementMapper;
        
        localResult = (UILocalElement)constructor.newInstance(params); 
      } catch (NoSuchMethodException e) {
        localResult = (UILocalElement)localElementClass.newInstance();  
      }   
    
    } catch (ClassNotFoundException e) {
      ExceptionHandler.handleException(e);
      throw new UICreationException("Class " + classname + " not found");
    } catch (IllegalAccessException e) {
      ExceptionHandler.handleException(e);
      throw new UICreationException("IllegalAccessException during element creation. Class " + classname + 
                                    " might not have an default constructor");
    } catch (InstantiationException e) {                                        
      ExceptionHandler.handleException(e);
      throw new UICreationException("Class " + classname +  " is abstract and cannot be instantiated.");
    } catch (ClassCastException e) {
      ExceptionHandler.handleException(e);
      throw new UICreationException("Class " + classname + " is not of type UILocalElement");   
    } catch (InvocationTargetException e) {
      e.printStackTrace();
      ExceptionHandler.handleException(e);
      throw new UICreationException("Constructor of class " + classname + " cannot be invocated : " + e.getMessage());   
    }           
    
    result = localResult.getRemoteElement();                 
    
    if (!(remoteClass.isInstance(result)))
      throw new UICreationException("Class " + remoteClass.getName() + " is not of type " + remoteClass.getName()); 
    
    if (localResult instanceof Skinable) { 
      try {
        ((Skinable)localResult).setSkin(skin);       
      } catch (Exception e) {               
        // there is something wrong with the configuration, or a bug in a UIComponent.
        // hopefully it is not fatal, so log it and continue ...
        ExceptionHandler.handleException(e);
      }
    } 
    
    RemoteStub stub;
    
    try {
      stub = UnicastRemoteObject.exportObject((Remote)result);  
      remoteObjectMap.put(stub, localResult);    
      remoteObjectMap.put(result, localResult);    
    } catch (Exception e) {
      ExceptionHandler.handleException(e);
      throw new UICreationException("Unable to export Object : " + e.getMessage());
    }
      
    return result;
  }                      
                                       
  public Label3D     createLabel3D() throws UICreationException, RemoteException {   
    return (Label3D)createUIElement(Label3D.class);   
  }

  public Label3D     createLabel3D(String text) throws UICreationException, RemoteException { 
    Label3D label = createLabel3D();
    label.setText(text);
    return label;
  }

  public PasswordField3D createPasswordField3D() throws UICreationException, RemoteException {     
    return (PasswordField3D)createUIElement(PasswordField3D.class);
  }
      
  public TextField3D createTextField3D() throws UICreationException, RemoteException {     
    return (TextField3D)createUIElement(TextField3D.class);
  }
  
  public Border3D    createBorder3D() throws UICreationException, RemoteException {
    return (Border3D)createUIElement(Border3D.class);  
  }        
  
  public UIContainer createContainer() throws UICreationException, RemoteException {
    return (UIContainer)createUIElement(UIContainer.class);
  }

  public LayoutManager3D createLayoutManager() throws UICreationException, RemoteException { 
    return (LayoutManager3D)createUIElement(LayoutManager3D.class);
  }            
  
  public ActionElement   createActionElement() throws UICreationException, RemoteException {
    return (ActionElement)createUIElement(ActionElement.class);
  }

  public UIElement       loadShape(Class shapeClass) throws UICreationException, RemoteException {
    NodeUIElement nodeUIElement;
    
    try {
      nodeUIElement =  new NodeUIElement(skin.loadShape(shapeClass));
      RemoteStub stub = UnicastRemoteObject.exportObject(nodeUIElement.getRemoteElement());  
      remoteObjectMap.put(stub, nodeUIElement);    
      remoteObjectMap.put(nodeUIElement.getRemoteElement(), nodeUIElement);    
    } catch (Exception e) {
      ExceptionHandler.handleException(e);
      throw new UICreationException("Unable to export Object : " + e.getMessage());
    }
    return nodeUIElement.getRemoteElement();
  }
  
  public void            invalidate(UIElement elements) throws RemoteException {
  } 

    
  /** Deserialize UIFactory and load Skin from the given URL. Initialize transient fields */
  private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
    try {
      in.defaultReadObject();
    } catch (NotActiveException e) {
      ExceptionHandler.handleException(e);
    }
        
    try {
      skin = (Skin)new Configuration(skinURL).getComponent("skin", this.getClass().getClassLoader());    
    } catch (Exception e) {     
      ExceptionHandler.handleException(e);
      throw new IOException("Deserialization Exception : Unable to load Skin : " + e.getMessage());
    }

    this.remoteObjectMap = new HashMap();      
    this.uiElementMapper = new UIElementMapper() {
      public UILocalElement getLocal(UIElement element) {
        return (UILocalElement)remoteObjectMap.get(element);
      }
    };

  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -