Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[albireo-dev] SwingSet2 Example View

I've checked in a new, odd example view that embeds the standard Sun SwingSet2 demo applet into an RCP view. The main goal was to provide access to a very large variety of different Swing controls and to see how well they behave under Albireo. The exercise also raised some interesting questions.

Problems noticed:

1) The toolbar overlays the top of the frame contents. As a result the Demo/SourceCode tabs are completely obscured along with the top of the content in the demo tab.

2) I can't get the content to resize properly while resizing the RCP application shell.

3) Implementing this view required an override to SwingControl.addRootPaneContainer. With our latest changes to AWT size caching, it's no longer easy to override this method without losing some of the function that was added in the override of JApplet. I think we should provide a way to change the JApplet to something else, without losing the function in our overrides of invalidate() and validateTree().

Though this example is not a compelling use case, is there any value in specific support for applets in Albireo? Applets aren't popular anymore, but there are many of them in existence. And they cannot be embedded consistently in the SWT browser widget. See:

http://dev.eclipse.org/mhonarc/newsLists/news.eclipse.platform.swt/msg25238.html
### Eclipse Workspace Patch 1.0
#P org.eclipse.albireo.examples.plugin
Index: plugin.xml
===================================================================
RCS file: /cvsroot/technology/org.eclipse.albireo/org.eclipse.albireo.examples.plugin/plugin.xml,v
retrieving revision 1.9
diff -u -r1.9 plugin.xml
--- plugin.xml	5 Feb 2008 23:42:25 -0000	1.9
+++ plugin.xml	6 Feb 2008 22:36:13 -0000
@@ -68,6 +68,12 @@
             id=" org.eclipse.albireo.examples.plugin.relayoutExample "
             name="Relayout example">
       </view>
+      <view
+            category="org.eclipse.albireo"
+            class="org.eclipse.albireo.examples.plugin.views.SwingSetView"
+            id="org.eclipse.albireo.examples.plugin.swingSetView"
+            name="SwingSet View">
+      </view>
    </extension>
    <extension
          point="org.eclipse.ui.actionSets">
Index: src/org/eclipse/albireo/examples/plugin/views/SwingSetView.java
===================================================================
RCS file: src/org/eclipse/albireo/examples/plugin/views/SwingSetView.java
diff -N src/org/eclipse/albireo/examples/plugin/views/SwingSetView.java
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ src/org/eclipse/albireo/examples/plugin/views/SwingSetView.java	1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,111 @@
+package org.eclipse.albireo.examples.plugin.views;
+
+import java.awt.Frame;
+import java.io.File;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.net.URLClassLoader;
+
+import javax.swing.JApplet;
+import javax.swing.JComponent;
+import javax.swing.RootPaneContainer;
+
+import org.eclipse.albireo.core.SwingControl;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.custom.ScrolledComposite;
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.FileDialog;
+import org.eclipse.ui.PlatformUI;
+import org.eclipse.ui.part.ViewPart;
+
+/**
+ * This example allows for testing and experimentation with a wide variety of Swing controls
+ * by embedding the standard Sun SwingSet applet in an RCP view. This does not work perfectly,
+ * and, at least for now, the albireo API does not smoothly support the integration of an applet
+ * like this.  
+ */
+public class SwingSetView extends ViewPart {
+
+    private SwingControl control;
+    private static File jarFile;
+    private JApplet applet;
+    private ScrolledComposite scrolledComposite;
+
+    public SwingSetView() {
+    }
+
+    public void createPartControl(Composite parent) {
+        findSwingSet();
+        if (jarFile == null) {
+            return;
+        }
+        scrolledComposite = new ScrolledComposite(parent, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
+        control = new SwingControl(scrolledComposite, SWT.NONE) {
+
+            protected RootPaneContainer addRootPaneContainer(Frame frame) {
+                // TODO: this code does not add the appropriate updates to the AWT sizes cache. 
+                try {
+                    // This is a hack to avoid the need to include SwingSet source/binaries in this 
+                    // example project by loading it up through a separate classloader and using 
+                    // reflection. As with much of this example, it is not recommended behavior for 
+                    // a real app. 
+                    URL[] urls = new URL[1];
+                    urls[0] = jarFile.toURL();
+                    URLClassLoader classLoader = new URLClassLoader(urls);
+                    Class clazz = classLoader.loadClass("SwingSet2Applet");
+                    applet = (JApplet)clazz.newInstance();
+                } catch (MalformedURLException e) {
+                    throw new RuntimeException(e);
+                } catch (ClassNotFoundException e) {
+                    throw new RuntimeException(e);
+                } catch (InstantiationException e) {
+                    throw new RuntimeException(e);
+                } catch (IllegalAccessException e) {
+                    throw new RuntimeException(e);
+                }
+                frame.add(applet);
+                
+                // Emulate a browser by starting up the applet
+                applet.init();
+                applet.start();
+                
+                return applet;
+            }
+
+            protected JComponent createSwingComponent() {
+                return null;
+            }
+        };
+        
+        // TODO: the fixed size is because SwingSet does not automatically resize. Investigate. 
+        control.setSize(850, 650);
+        scrolledComposite.setContent(control);
+    }
+
+    private void findSwingSet() {
+        // Try to find SwingSet2.jar if the application was run from a JDK that has the demo code
+        // installed.
+        if (jarFile == null) {
+            File file = new File(System.getProperty("java.home") + "/../demo/jfc/SwingSet2/SwingSet2.jar");
+            while (!file.exists()) {
+                // Prompt user for jar location if it was not found
+                // TODO: try to access SwingSet from sun.com?
+                //        (e.g. http://java.sun.com/products/plugin/1.5.0/demos/jfc/SwingSet2/SwingSet2.jar)
+                // TODO: A more descriptive dialog would be nice
+                FileDialog dlg = new FileDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell());
+                dlg.setText("Select a location for SwingSet2.jar");
+                String name = dlg.open();
+                if (name == null) {
+                    return;
+                }
+                file = new File(name);
+            }
+            jarFile = file;
+        }
+    }
+
+    public void setFocus() {
+        control.setFocus();
+    }
+
+}
#P org.eclipse.albireo.core
Index: src/org/eclipse/albireo/core/SwingControl.java
===================================================================
RCS file: /cvsroot/technology/org.eclipse.albireo/org.eclipse.albireo.core/src/org/eclipse/albireo/core/SwingControl.java,v
retrieving revision 1.17
diff -u -r1.17 SwingControl.java
--- src/org/eclipse/albireo/core/SwingControl.java	5 Feb 2008 01:36:07 -0000	1.17
+++ src/org/eclipse/albireo/core/SwingControl.java	6 Feb 2008 22:36:14 -0000
@@ -164,7 +164,9 @@
             public void run() {
                 rootPaneContainer = addRootPaneContainer(frame);
                 swingComponent = createSwingComponent();
-                rootPaneContainer.getRootPane().getContentPane().add(swingComponent);
+                if (swingComponent != null) {
+                    rootPaneContainer.getRootPane().getContentPane().add(swingComponent);
+                }
                 setComponentFont();
                 setComponentColors(foreground, background);
                 // Invoke hooks, for use by the application.

Back to the top