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