// Standard imports import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.io.File; import java.net.MalformedURLException; import javax.media.j3d.GraphicsConfigTemplate3D; import org.ietf.uri.*; import java.net.URL; // Application Specific imports import org.web3d.browser.BrowserCore; import org.web3d.vrml.nodes.loader.WorldLoaderManager; import org.web3d.net.content.VRMLContentHandlerFactory; import org.web3d.net.content.VRMLFileNameMap; import org.web3d.net.protocol.Web3DResourceFactory; import org.web3d.net.resolve.Web3DURNResolver; /** * A basic frame that does all the setup needed for these demo programs. *

* * The window contains a textfield at the top and a Go button to tell it to * load the URL described. In the bottom of the frame is a text label for * information messages. * * @author Justin Couch * @version $Revision: 1.1.1.1 $ */ public abstract class DemoFrame extends JFrame implements ActionListener { // Constants for the URN setup /** Set this to the install directory that UMEL uses */ private static final String UMEL_INSTALL_DIR = null; /** Set this to the install directory that GEOVRML uses */ private static final String GEOVRML_INSTALL_DIR = null; //"c:/cygwin/home/justin/Xj3D/tests/geovrml/"; /** NSS prefix used by UMEL */ private static final String UMEL_PREFIX = "umel"; /** NSS prefix used by GeoVRML */ private static final String GEOVRML_PREFIX = "geovrml"; /** The graphics config template that is best to use */ protected GraphicsConfiguration gfxConfig; /** The textfield to read the values from */ protected JTextField urlTextField; /** The go button on the URl panel */ private JButton locationGoButton; /** The label for status messages */ protected JLabel statusLabel; public DemoFrame(String title) { super(title); GraphicsConfigTemplate3D template = new GraphicsConfigTemplate3D(); template.setDoubleBuffer(template.REQUIRED); GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment(); GraphicsDevice dev = env.getDefaultScreenDevice(); gfxConfig = dev.getBestConfiguration(template); urlTextField = new JTextField(); urlTextField.setText(System.getProperty("user.dir")); urlTextField.addActionListener(this); urlTextField.requestFocus(); locationGoButton = new JButton(" Go! "); locationGoButton.addActionListener(this); JLabel l1 = new JLabel("Location: "); JPanel p1 = new JPanel(new BorderLayout()); p1.add(l1, BorderLayout.WEST); p1.add(locationGoButton, BorderLayout.EAST); p1.add(urlTextField, BorderLayout.CENTER); Container content_pane = getContentPane(); content_pane.add(p1, BorderLayout.NORTH); statusLabel = new JLabel("Enter a world to load"); content_pane.add(statusLabel, BorderLayout.SOUTH); setSize(600, 600); setLocation(40, 40); setDefaultCloseOperation(EXIT_ON_CLOSE); } /** * Go to the named URL location. No checking is done other than to make * sure it is a valid URL. * * @param url The URL to open */ public abstract void gotoLocation(URL url); /** * Load the named file. The file is checked to make sure that it exists * before calling this method. * * @param file The file to load */ public abstract void gotoLocation(File file); protected abstract void setWarning(String msg); protected abstract void setError(String msg); /** * Set up the system properties needed to run the browser. This involves * registering all the properties needed for content and protocol * handlers used by the URI system. Only needs to be run once at startup. * * @param core The core representation of the browser * @param loader Loader manager for doing async calls */ protected void setupProperties(BrowserCore core, WorldLoaderManager loader) { // Disable font cache to fix getBounds nullPointer bug System.setProperty("sun.awt.font.advancecache","off"); System.setProperty("uri.content.handler.pkgs", "vlc.net.content"); System.setProperty("uri.protocol.handler.pkgs", "vlc.net.protocol"); System.setProperty("java.content.handler.pkgs", "vlc.content"); URIResourceStreamFactory res_fac = URI.getURIResourceStreamFactory(); if(!(res_fac instanceof Web3DResourceFactory)) { res_fac = new Web3DResourceFactory(res_fac); URI.setURIResourceStreamFactory(res_fac); } ContentHandlerFactory c_fac = URI.getContentHandlerFactory(); if(!(c_fac instanceof VRMLContentHandlerFactory)) { c_fac = new VRMLContentHandlerFactory(core, loader, c_fac); URI.setContentHandlerFactory(c_fac); } FileNameMap fn_map = URI.getFileNameMap(); if(!(fn_map instanceof VRMLFileNameMap)) { fn_map = new VRMLFileNameMap(fn_map); URI.setFileNameMap(fn_map); } Web3DURNResolver resolver = new Web3DURNResolver(); resolver.registerPrefixLocation(UMEL_PREFIX, UMEL_INSTALL_DIR); resolver.registerPrefixLocation(GEOVRML_PREFIX, GEOVRML_INSTALL_DIR); URN.addResolver(resolver); } /** * An action has been performed. This is the Go button being pressed. * Grab the URL and check with the file to see if it exists first as * a local file, and then try to make a URL of it. Finally, if this all * works, call the abstract gotoLocation method. * * @param evt The event that caused this method to be called. */ public void actionPerformed(ActionEvent evt) { Object src = evt.getSource(); if((src == locationGoButton) || (src == urlTextField)) { String location = urlTextField.getText(); // try a file first File f = new File(location); if(f.exists()) { if(f.isDirectory()) setError("File is a directory"); else { gotoLocation(f); statusLabel.setText("World Loaded Successfully"); } } else { // Try a URL try { URL url = new URL(location); gotoLocation(url); statusLabel.setText("World Loaded Successfully"); } catch(MalformedURLException mue) { setError("Invalid URL: " + location); } } } } }