bump product version to 4.1.6.2
[LibreOffice.git] / bean / test / applet / oooapplet / OOoViewer.java
blob6ea9270b199f1dfdda3bf6ce1863836881424c5f
1 /*
2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 package oooapplet;
20 import java.lang.reflect.Method;
21 import java.lang.reflect.Array;
22 import java.net.*;
23 import java.io.*;
24 import java.awt.*;
25 import com.sun.star.comp.beans.*;
26 import java.applet.Applet;
27 import java.awt.Graphics;
28 import java.util.*;
30 public class OOoViewer extends Applet {
32 private OOoBean oBean;
34 static private CustomURLClassLoader m_loader;
36 Object m_objBean;
38 public void init() {
39 try {
40 if (m_loader == null) {
41 String s = getParameter("office");
42 System.out.println("sun.awt.noxembed: " + System.getProperty("sun.awt.noxembed"));
43 System.setProperty("sun.awt.xembedserver", "true");
45 File f = new File(s);
46 URL url = f.toURI().toURL();
47 String officeURL = url.toString();
48 URL[] arURL = new URL[] {
49 new URL(officeURL + "/program/classes/officebean.jar"),
50 new URL(officeURL + "/program/classes/jurt.jar"),
51 new URL(officeURL + "/program/classes/ridl.jar"),
52 new URL(officeURL + "/program/classes/unoil.jar"),
53 new URL(officeURL + "/program/classes/java_uno.jar"),
54 new URL(officeURL + "/program/classes/juh.jar")
56 m_loader = new CustomURLClassLoader(arURL);
57 File fileProg = new File(s + "/program");
58 m_loader.addResourcePath(fileProg.toURI().toURL());
60 } catch (MalformedURLException e) {
61 e.printStackTrace();
65 public void start() {
66 try {
67 Class beanClass = m_loader.loadClass("com.sun.star.comp.beans.OOoBean");
68 m_objBean = beanClass.newInstance();
69 setLayout(new BorderLayout());
70 add((java.awt.Container)m_objBean, BorderLayout.CENTER);
71 setVisible(true);
72 //this does not work here. Why?
73 // Class arPropValClass = m_loader.loadClass("[Lcom.sun.star.beans.PropertyValue;");
74 Object arProp = Array.newInstance(
75 m_loader.loadClass("com.sun.star.beans.PropertyValue"), 1);
76 Class<? extends Object> clazz = arProp.getClass();
78 Method methLoad = beanClass.getMethod(
79 "loadFromURL", new Class[] {
80 String.class, arProp.getClass() });
82 methLoad.invoke(m_objBean, new Object[] {"private:factory/swriter", null});
83 } catch (ClassNotFoundException e) {
84 e.printStackTrace();
85 } catch (InstantiationException e) {
86 e.printStackTrace();
87 } catch (IllegalAccessException e) {
88 e.printStackTrace();
89 } catch (ClassCastException e) {
90 e.printStackTrace();
91 } catch (java.lang.reflect.InvocationTargetException e) {
92 e.printStackTrace();
93 } catch (java.lang.NoSuchMethodException e) {
94 e.printStackTrace(); }
98 validate();
101 public void stop() {
102 try {
103 Method methStop = m_objBean.getClass().getMethod(
104 "stopOOoConnection", new Class[0]);
105 methStop.invoke(m_objBean, null);
106 } catch (java.lang.NoSuchMethodException e) {
107 e.printStackTrace();
108 } catch (java.lang.IllegalAccessException e) {
109 e.printStackTrace();
111 catch (java.lang.reflect.InvocationTargetException e) {
112 e.printStackTrace();
117 public void destroy() {
120 public void paint(Graphics g) {
125 final class CustomURLClassLoader extends URLClassLoader {
127 private ArrayList<URL> resourcePaths;
129 public CustomURLClassLoader( URL[] urls ) {
130 super( urls );
133 protected Class findClass( String name ) throws ClassNotFoundException {
134 // This is only called via this.loadClass -> super.loadClass ->
135 // this.findClass, after this.loadClass has already called
136 // super.findClass, so no need to call super.findClass again:
137 throw new ClassNotFoundException( name );
138 // return super.findClass(name);
143 protected Class loadClass( String name, boolean resolve )
144 throws ClassNotFoundException
146 Class c = findLoadedClass( name );
147 if ( c == null ) {
148 try {
149 c = super.findClass( name );
150 } catch ( ClassNotFoundException e ) {
151 return super.loadClass( name, resolve );
152 } catch ( SecurityException e ) {
153 // A SecurityException "Prohibited package name: java.lang"
154 // may occur when the user added the JVM's rt.jar to the
155 // java.class.path:
156 return super.loadClass( name, resolve );
159 if ( resolve ) {
160 resolveClass( c );
162 return c;
165 public void addResourcePath(URL rurl) {
166 if (resourcePaths == null) resourcePaths = new ArrayList<URL>();
167 resourcePaths.add(rurl);
170 public URL getResource(String name) {
171 if (resourcePaths == null) return null;
173 URL result = super.getResource(name);
174 if (result != null) {
175 return result;
178 URI uri = null;
179 for (URL u : resourcePaths) {
180 if (u.getProtocol().startsWith("file")){
181 try {
182 File f1 = new File(u.getPath());
183 File f2 = new File(f1, name);
184 if (f2.exists()) {
185 return new URL(f2.toURI().toASCIIString());
187 } catch (MalformedURLException e1) {
188 System.err.println("malformed url: "+e1.getMessage());
189 continue;
193 return null;