1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 package basicrunner.basichelper;
28 
29 import com.sun.star.lang.XSingleServiceFactory;
30 import com.sun.star.lang.XServiceInfo;
31 import com.sun.star.lang.XTypeProvider;
32 import com.sun.star.uno.Type;
33 import com.sun.star.frame.XDispatchProviderInterceptor;
34 import com.sun.star.frame.XDispatchProvider;
35 import com.sun.star.frame.XDispatch;
36 import com.sun.star.frame.DispatchDescriptor;
37 import com.sun.star.util.URL;
38 
39 /**
40  * This implementation provides an implementation of an interceptor.
41  * @see com.sun.star.lang.XSingleServiceFactory
42  * @see com.sun.star.lang.XServiceInfo
43  */
44 public class DispatchProviderInterceptor implements XServiceInfo,
45                                                     XSingleServiceFactory {
46     /** The service name **/
47     static final String __serviceName =
48                             "basichelper.DispatchProviderInterceptor";
49 
50     /** Create an instance of the interceptor
51      * Arguments are not supported here, so they will be ignored.
52      * @param args The arguments.
53      * @return A new instance of the interceptor.
54      **/
55     public Object createInstanceWithArguments(Object[] args) {
56         return new InterceptorImpl();
57     }
58 
59     /** Create an instance of the interceptor
60      * @return A new instance of the interceptor.
61      **/
62     public Object createInstance() {
63         return createInstanceWithArguments(null);
64     }
65 
66     /** Get the unique id for this implementation
67      * @return The id.
68      */
69     public byte[] getImplementationId() {
70         return toString().getBytes();
71     }
72 
73     /** Get all implemented types.
74      * @return The implemented UNO types.
75      */
76     public Type[] getTypes() {
77         Class interfaces[] = getClass().getInterfaces();
78 
79         Type types[] = new Type[interfaces.length];
80         for(int i = 0; i < interfaces.length; ++ i)
81             types[i] = new Type(interfaces[i]);
82 
83         return types;
84     }
85 
86     /**
87      * Is this service supported?
88      * @param name The name of a service.
89      * @return True, if the service is supported.
90      */
91     public boolean supportsService(String name) {
92         return __serviceName.equals(name);
93     }
94 
95     /**
96      * Get all supported service names.
97      * @return All service names.
98      */
99     public String[] getSupportedServiceNames() {
100         return new String[] {__serviceName};
101     }
102 
103     /**
104      * Get the implementation name of this class.
105      * @return The name.
106      */
107     public String getImplementationName() {
108         return getClass().getName();
109     }
110 }
111 
112 /**
113  * The actual implementation of the interceptor.
114  * @see com.sun.star.lang.XTypeProvider
115  * @see com.sun.star.frame.XDispatchProviderInterceptor
116  * @see com.sun.star.frame.XDispatchProvider
117  */
118 class InterceptorImpl implements XDispatchProvider,
119                                 XDispatchProviderInterceptor, XTypeProvider {
120 
121     /** A master dispatch provider **/
122     public XDispatchProvider master = null;
123     /** A slave dispatch provider **/
124     public XDispatchProvider slave = null;
125 
126     /** Get the slave dispatch provider
127      * @return The slave.
128      */
129     public XDispatchProvider getSlaveDispatchProvider() {
130         return slave;
131     }
132     /** Get the master dispatch provider
133      * @return The master.
134      */
135     public XDispatchProvider getMasterDispatchProvider() {
136         return master;
137     }
138 
139     /** Set the slave dispatch provider
140      * @param prov The new slave.
141      */
142     public void setSlaveDispatchProvider(XDispatchProvider prov) {
143         slave = prov ;
144     }
145 
146     /** Set the master dispatch provider
147      * @param prov The new master.
148      */
149     public void setMasterDispatchProvider(XDispatchProvider prov) {
150         master = prov ;
151     }
152 
153     /** Searches for an <type>XDispatch</type> for the specified URL within
154      * the specified target frame.
155      * @param url The URL.
156      * @param frame The target frame
157      * @param flags Optional search flags.
158      * @return The dispatch object which provides the queried functionality
159      *         or null if no dispatch object is available.
160      * @see com.sun.star.frame.XDispatch
161      */
162     public XDispatch queryDispatch(URL url, String frame, int flags) {
163         return master.queryDispatch(url, frame, flags) ;
164     }
165 
166     /**
167      * Query for an array of <type>XDispatch</type>.
168      * @param desc A list of dipatch requests.
169      * @return A list of dispatch objects.
170      */
171     public XDispatch[] queryDispatches(DispatchDescriptor[] desc) {
172         return master.queryDispatches(desc) ;
173     }
174 
175     /** Get the unique id for this implementation
176      * @return The id.
177      */
178     public byte[] getImplementationId() {
179         return toString().getBytes();
180     }
181 
182     /** Get all implemented types.
183      * @return The implemented UNO types.
184      */
185     public Type[] getTypes() {
186         Class interfaces[] = getClass().getInterfaces();
187 
188         Type types[] = new Type[interfaces.length];
189         for(int i = 0; i < interfaces.length; ++ i)
190             types[i] = new Type(interfaces[i]);
191 
192         return types;
193     }
194 }
195