update dev300-m58
[ooovba.git] / bean / test / applet / oooapplet / OOoViewer.java
blob575fe6f778edf92e61ce9ad291d729e798263bb9
1 //*************************************************************************
2 //
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: OOoViewer.java,v $
10 * $Revision: 1.3 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 //*************************************************************************
30 package oooapplet;
32 import java.lang.reflect.Method;
33 import java.lang.reflect.Array;
34 import java.net.*;
35 import java.io.*;
36 import java.awt.*;
37 import java.awt.event.*;
38 import com.sun.star.comp.beans.*;
39 import java.applet.Applet;
40 import java.awt.Graphics;
41 import java.util.*;
43 public class OOoViewer extends Applet {
45 private OOoBean oBean;
47 static private CustomURLClassLoader m_loader;
49 Object m_objBean;
51 public void init() {
52 try {
53 if (m_loader == null) {
54 String s = getParameter("office");
55 System.out.println("sun.awt.noxembed: " + System.getProperty("sun.awt.noxembed"));
56 System.setProperty("sun.awt.xembedserver", "true");
58 File f = new File(s);
59 URL url = f.toURL();
60 String officeURL = url.toString();
61 URL[] arURL = new URL[] {
62 new URL(officeURL + "/program/classes/officebean.jar"),
63 new URL(officeURL + "/program/classes/jurt.jar"),
64 new URL(officeURL + "/program/classes/ridl.jar"),
65 new URL(officeURL + "/program/classes/unoil.jar"),
66 new URL(officeURL + "/program/classes/java_uno.jar"),
67 new URL(officeURL + "/program/classes/juh.jar")
69 m_loader = new CustomURLClassLoader(arURL);
70 File fileProg = new File(s + "/program");
71 m_loader.addResourcePath(fileProg.toURL());
73 } catch (MalformedURLException e) {
74 e.printStackTrace();
78 public void start() {
79 try {
80 Class beanClass = m_loader.loadClass("com.sun.star.comp.beans.OOoBean");
81 m_objBean = beanClass.newInstance();
82 setLayout(new BorderLayout());
83 add((java.awt.Container)m_objBean, BorderLayout.CENTER);
84 setVisible(true);
85 //this does not work here. Why?
86 // Class arPropValClass = m_loader.loadClass("[Lcom.sun.star.beans.PropertyValue;");
87 Object arProp = Array.newInstance(
88 m_loader.loadClass("com.sun.star.beans.PropertyValue"), 1);
89 Class clazz = arProp.getClass();
91 Method methLoad = beanClass.getMethod(
92 "loadFromURL", new Class[] {
93 String.class, arProp.getClass() });
95 methLoad.invoke(m_objBean, new Object[] {"private:factory/swriter", null});
96 } catch (ClassNotFoundException e) {
97 e.printStackTrace();
98 } catch (InstantiationException e) {
99 e.printStackTrace();
100 } catch (IllegalAccessException e) {
101 e.printStackTrace();
102 } catch (ClassCastException e) {
103 e.printStackTrace();
104 } catch (java.lang.reflect.InvocationTargetException e) {
105 e.printStackTrace();
106 } catch (java.lang.NoSuchMethodException e) {
107 e.printStackTrace(); }
111 validate();
114 public void stop() {
115 try {
116 Method methStop = m_objBean.getClass().getMethod(
117 "stopOOoConnection", new Class[0]);
118 methStop.invoke(m_objBean, null);
119 } catch (java.lang.NoSuchMethodException e) {
120 e.printStackTrace();
121 } catch (java.lang.IllegalAccessException e) {
122 e.printStackTrace();
124 catch (java.lang.reflect.InvocationTargetException e) {
125 e.printStackTrace();
130 public void destroy() {
133 public void paint(Graphics g) {
138 final class CustomURLClassLoader extends URLClassLoader {
140 private Vector resourcePaths;
142 public CustomURLClassLoader( URL[] urls ) {
143 super( urls );
146 protected Class findClass( String name ) throws ClassNotFoundException {
147 // This is only called via this.loadClass -> super.loadClass ->
148 // this.findClass, after this.loadClass has already called
149 // super.findClass, so no need to call super.findClass again:
150 throw new ClassNotFoundException( name );
151 // return super.findClass(name);
156 protected Class loadClass( String name, boolean resolve )
157 throws ClassNotFoundException
159 Class c = findLoadedClass( name );
160 if ( c == null ) {
161 try {
162 c = super.findClass( name );
163 } catch ( ClassNotFoundException e ) {
164 return super.loadClass( name, resolve );
165 } catch ( SecurityException e ) {
166 // A SecurityException "Prohibited package name: java.lang"
167 // may occur when the user added the JVM's rt.jar to the
168 // java.class.path:
169 return super.loadClass( name, resolve );
172 if ( resolve ) {
173 resolveClass( c );
175 return c;
178 public void addResourcePath(URL rurl) {
179 if (resourcePaths == null) resourcePaths = new Vector();
180 resourcePaths.add(rurl);
183 public URL getResource(String name) {
184 if (resourcePaths == null) return null;
186 URL result = super.getResource(name);
187 if (result != null) {
188 return result;
191 URL u = null;
192 URI uri = null;
193 for (Enumeration e = resourcePaths.elements(); e.hasMoreElements();) {
194 u = (URL)e.nextElement();
195 if (u.getProtocol().startsWith("file")){
196 try {
197 File f1 = new File(u.getPath());
198 File f2 = new File(f1, name);
199 if (f2.exists()) {
200 return new URL(f2.toURI().toASCIIString());
202 } catch (MalformedURLException e1) {
203 System.err.println("malformed url: "+e1.getMessage());
204 continue;
208 return null;