1   /****************************************************************************
2    **
3    ** This file is part of yFiles-2.6. 
4    ** 
5    ** yWorks proprietary/confidential. Use is subject to license terms.
6    **
7    ** Redistribution of this file or of an unauthorized byte-code version
8    ** of this file is strictly forbidden.
9    **
10   ** Copyright (c) 2000-2008 by yWorks GmbH, Vor dem Kreuzberg 28, 
11   ** 72070 Tuebingen, Germany. All rights reserved.
12   **
13   ***************************************************************************/
14  package demo.view.applet;
15  
16  import javax.swing.*;
17  import java.net.URL;
18  import java.net.MalformedURLException;
19  import java.net.URLConnection;
20  import java.io.IOException;
21  
22  /**
23   * This class represents a simple graph editor applet. The applet can be used inside a web browser with
24   * a Java 2 plugin (version >= 1.4) installed.
25   * <p>
26   * This applet supports the applet parameter "graphsource" which allows to specify the graph that is initially
27   * displayed by the applet. The graph needs to be in either YGF or GML format. URLs are specified relative to
28   * the document base of the applet.
29   * </p>
30   * <p>
31   * To compile and deploy the applet it is best to use the Ant build script "build.xml" located in this directory.
32   * It compiles the application classes, jars them as "application.jar" in this directory and also copies "y.jar" into this directory.
33   * Once these Jar files are in place, the applet can be launched by opening the included HTML page
34   * "applet.html" with your browser.
35   * </p>
36   * <p>
37   * This demo has been successfully tested with IE7, IE6, Firefox 1.5, Firefox 2.0, and Safari 2.0.
38   * </p>
39   */
40  public class AppletDemo extends JApplet {
41  
42    DemoEditor demoEditor;
43  
44    /**
45     * Mandatory default constructor for an applet.
46     */
47    public AppletDemo()
48    {}
49  
50    /**
51     * Applet initialization. Create the application GUI.
52     */
53    public void init() {
54      super.init();
55      demoEditor = new DemoEditor();
56      getRootPane().setContentPane(demoEditor);
57      getRootPane().setJMenuBar(demoEditor.createMenuBar());
58    }
59  
60    /**
61     * Start the applet. Try to load the graph given by applet parameter "graphsource".
62     */
63    public void start() {
64      super.start();
65  
66      String graphSource = getParameter("graphsource");
67  
68      if (graphSource != null) {
69        try {
70          URL graphURL = new URL(getDocumentBase(), graphSource);
71          try {
72            URLConnection urlConnection = graphURL.openConnection();
73            urlConnection.connect();
74          }catch(IOException ioex) {
75            //try classpath if resource node located at document base
76            graphURL = getClass().getResource(graphSource);
77          }
78          if (graphURL != null) {
79            demoEditor.loadAndDisplayGraph(graphURL);
80          }
81        } catch (MalformedURLException muex) {} catch (IOException e) {
82          e.printStackTrace();
83        }
84      }
85    }
86  
87    /**
88     * Returns applet parameter information.
89     */
90    public String[][] getParameterInfo() {
91      String[][] info = {
92              // Parameter Name     Kind of Value   Description
93              {"graphsource", "URL", "an URL pointing to a diagram in YGF or GML format"}
94      };
95      return info;
96    }
97  }
98