bump product version to 7.6.3.2-android
[LibreOffice.git] / bean / test / applet / oooapplet / OOoViewer.java
blob14851e0eceee6cee645ccac1d123cbe2f682bd0a
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/libreoffice.jar"),
49 new URL(officeURL + "/program/classes/java_uno.jar")
51 m_loader = new CustomURLClassLoader(arURL);
52 File fileProg = new File(s + "/program");
53 m_loader.addResourcePath(fileProg.toURI().toURL());
55 } catch (MalformedURLException e) {
56 e.printStackTrace();
60 @Override
61 public void start() {
62 try {
63 Class<?> beanClass = m_loader.loadClass("com.sun.star.comp.beans.OOoBean");
64 m_objBean = beanClass.newInstance();
65 setLayout(new BorderLayout());
66 add((java.awt.Container)m_objBean, BorderLayout.CENTER);
67 setVisible(true);
68 //this does not work here. Why?
69 // Class arPropValClass = m_loader.loadClass("[Lcom.sun.star.beans.PropertyValue;");
70 Object arProp = Array.newInstance(
71 m_loader.loadClass("com.sun.star.beans.PropertyValue"), 1);
73 Method methLoad = beanClass.getMethod(
74 "loadFromURL", new Class[] {
75 String.class, arProp.getClass() });
77 methLoad.invoke(m_objBean, new Object[] {"private:factory/swriter", null});
78 } catch (ClassNotFoundException e) {
79 e.printStackTrace();
80 } catch (InstantiationException e) {
81 e.printStackTrace();
82 } catch (IllegalAccessException e) {
83 e.printStackTrace();
84 } catch (ClassCastException e) {
85 e.printStackTrace();
86 } catch (java.lang.reflect.InvocationTargetException e) {
87 e.printStackTrace();
88 } catch (java.lang.NoSuchMethodException e) {
89 e.printStackTrace(); }
93 validate();
96 @Override
97 public void stop() {
98 try {
99 Method methStop = m_objBean.getClass().getMethod(
100 "stopOOoConnection", new Class[0]);
101 methStop.invoke(m_objBean, (Object[]) null);
102 } catch (java.lang.NoSuchMethodException e) {
103 e.printStackTrace();
104 } catch (java.lang.IllegalAccessException e) {
105 e.printStackTrace();
107 catch (java.lang.reflect.InvocationTargetException e) {
108 e.printStackTrace();
113 @Override
114 public void destroy() {
117 @Override
118 public void paint(Graphics g) {
123 final class CustomURLClassLoader extends URLClassLoader {
125 private ArrayList<URL> resourcePaths;
127 public CustomURLClassLoader( URL[] urls ) {
128 super( urls );
131 @Override
132 protected Class<?> findClass( String name ) throws ClassNotFoundException {
133 // This is only called via this.loadClass -> super.loadClass ->
134 // this.findClass, after this.loadClass has already called
135 // super.findClass, so no need to call super.findClass again:
136 throw new ClassNotFoundException( name );
141 @Override
142 protected synchronized Class<?> loadClass( String name, boolean resolve )
143 throws ClassNotFoundException
145 Class c = findLoadedClass( name );
146 if ( c == null ) {
147 try {
148 c = super.findClass( name );
149 } catch ( ClassNotFoundException e ) {
150 return super.loadClass( name, resolve );
151 } catch ( SecurityException e ) {
152 // A SecurityException "Prohibited package name: java.lang"
153 // may occur when the user added the JVM's rt.jar to the
154 // java.class.path:
155 return super.loadClass( name, resolve );
158 if ( resolve ) {
159 resolveClass( c );
161 return c;
164 public void addResourcePath(URL rurl) {
165 if (resourcePaths == null) resourcePaths = new ArrayList<URL>();
166 resourcePaths.add(rurl);
169 @Override
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 for (URL u : resourcePaths) {
179 if (u.getProtocol().startsWith("file")){
180 try {
181 File f1 = new File(u.getPath());
182 File f2 = new File(f1, name);
183 if (f2.exists()) {
184 return new URL(f2.toURI().toASCIIString());
186 } catch (MalformedURLException e1) {
187 System.err.println("malformed url: "+e1.getMessage());
188 continue;
192 return null;