Bump version to 6.0-36
[LibreOffice.git] / bean / test / applet / oooapplet / OOoViewer.java
blobd437611187f967bbb37fbdd4ef2cc1dff75da251
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 java.applet.Applet;
26 import java.awt.Graphics;
27 import java.util.*;
29 public class OOoViewer extends Applet {
31 private static CustomURLClassLoader m_loader;
33 Object m_objBean;
35 @Override
36 public void init() {
37 try {
38 if (m_loader == null) {
39 String s = getParameter("office");
40 System.out.println("sun.awt.noxembed: " + System.getProperty("sun.awt.noxembed"));
41 System.setProperty("sun.awt.xembedserver", "true");
43 File f = new File(s);
44 URL url = f.toURI().toURL();
45 String officeURL = url.toString();
46 URL[] arURL = new URL[] {
47 new URL(officeURL + "/program/classes/officebean.jar"),
48 new URL(officeURL + "/program/classes/jurt.jar"),
49 new URL(officeURL + "/program/classes/ridl.jar"),
50 new URL(officeURL + "/program/classes/unoil.jar"),
51 new URL(officeURL + "/program/classes/java_uno.jar"),
52 new URL(officeURL + "/program/classes/juh.jar")
54 m_loader = new CustomURLClassLoader(arURL);
55 File fileProg = new File(s + "/program");
56 m_loader.addResourcePath(fileProg.toURI().toURL());
58 } catch (MalformedURLException e) {
59 e.printStackTrace();
63 @Override
64 public void start() {
65 try {
66 Class<?> beanClass = m_loader.loadClass("com.sun.star.comp.beans.OOoBean");
67 m_objBean = beanClass.newInstance();
68 setLayout(new BorderLayout());
69 add((java.awt.Container)m_objBean, BorderLayout.CENTER);
70 setVisible(true);
71 //this does not work here. Why?
72 // Class arPropValClass = m_loader.loadClass("[Lcom.sun.star.beans.PropertyValue;");
73 Object arProp = Array.newInstance(
74 m_loader.loadClass("com.sun.star.beans.PropertyValue"), 1);
76 Method methLoad = beanClass.getMethod(
77 "loadFromURL", new Class[] {
78 String.class, arProp.getClass() });
80 methLoad.invoke(m_objBean, new Object[] {"private:factory/swriter", null});
81 } catch (ClassNotFoundException e) {
82 e.printStackTrace();
83 } catch (InstantiationException e) {
84 e.printStackTrace();
85 } catch (IllegalAccessException e) {
86 e.printStackTrace();
87 } catch (ClassCastException e) {
88 e.printStackTrace();
89 } catch (java.lang.reflect.InvocationTargetException e) {
90 e.printStackTrace();
91 } catch (java.lang.NoSuchMethodException e) {
92 e.printStackTrace(); }
96 validate();
99 @Override
100 public void stop() {
101 try {
102 Method methStop = m_objBean.getClass().getMethod(
103 "stopOOoConnection", new Class[0]);
104 methStop.invoke(m_objBean, (Object[]) null);
105 } catch (java.lang.NoSuchMethodException e) {
106 e.printStackTrace();
107 } catch (java.lang.IllegalAccessException e) {
108 e.printStackTrace();
110 catch (java.lang.reflect.InvocationTargetException e) {
111 e.printStackTrace();
116 @Override
117 public void destroy() {
120 @Override
121 public void paint(Graphics g) {
126 final class CustomURLClassLoader extends URLClassLoader {
128 private ArrayList<URL> resourcePaths;
130 public CustomURLClassLoader( URL[] urls ) {
131 super( urls );
134 @Override
135 protected Class<?> findClass( String name ) throws ClassNotFoundException {
136 // This is only called via this.loadClass -> super.loadClass ->
137 // this.findClass, after this.loadClass has already called
138 // super.findClass, so no need to call super.findClass again:
139 throw new ClassNotFoundException( name );
144 @Override
145 protected synchronized Class<?> loadClass( String name, boolean resolve )
146 throws ClassNotFoundException
148 Class c = findLoadedClass( name );
149 if ( c == null ) {
150 try {
151 c = super.findClass( name );
152 } catch ( ClassNotFoundException e ) {
153 return super.loadClass( name, resolve );
154 } catch ( SecurityException e ) {
155 // A SecurityException "Prohibited package name: java.lang"
156 // may occur when the user added the JVM's rt.jar to the
157 // java.class.path:
158 return super.loadClass( name, resolve );
161 if ( resolve ) {
162 resolveClass( c );
164 return c;
167 public void addResourcePath(URL rurl) {
168 if (resourcePaths == null) resourcePaths = new ArrayList<URL>();
169 resourcePaths.add(rurl);
172 @Override
173 public URL getResource(String name) {
174 if (resourcePaths == null) return null;
176 URL result = super.getResource(name);
177 if (result != null) {
178 return result;
181 for (URL u : resourcePaths) {
182 if (u.getProtocol().startsWith("file")){
183 try {
184 File f1 = new File(u.getPath());
185 File f2 = new File(f1, name);
186 if (f2.exists()) {
187 return new URL(f2.toURI().toASCIIString());
189 } catch (MalformedURLException e1) {
190 System.err.println("malformed url: "+e1.getMessage());
191 continue;
195 return null;