1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 package basicrunner.basichelper;
24 
25 import com.sun.star.lang.XSingleServiceFactory;
26 import com.sun.star.lang.XServiceInfo;
27 import com.sun.star.lang.XTypeProvider;
28 import com.sun.star.uno.Type;
29 import com.sun.star.frame.XDispatchProviderInterceptor;
30 import com.sun.star.frame.XDispatchProvider;
31 import com.sun.star.frame.XDispatch;
32 import com.sun.star.frame.DispatchDescriptor;
33 import com.sun.star.util.URL;
34 
35 /**
36  * This implementation provides an implementation of an interceptor.
37  * @see com.sun.star.lang.XSingleServiceFactory
38  * @see com.sun.star.lang.XServiceInfo
39  */
40 public class DispatchProviderInterceptor implements XServiceInfo,
41                                                     XSingleServiceFactory {
42     /** The service name **/
43     static final String __serviceName =
44                             "basichelper.DispatchProviderInterceptor";
45 
46     /** Create an instance of the interceptor
47      * Arguments are not supported here, so they will be ignored.
48      * @param args The arguments.
49      * @return A new instance of the interceptor.
50      **/
createInstanceWithArguments(Object[] args)51     public Object createInstanceWithArguments(Object[] args) {
52         return new InterceptorImpl();
53     }
54 
55     /** Create an instance of the interceptor
56      * @return A new instance of the interceptor.
57      **/
createInstance()58     public Object createInstance() {
59         return createInstanceWithArguments(null);
60     }
61 
62     /** Get the unique id for this implementation
63      * @return The id.
64      */
getImplementationId()65     public byte[] getImplementationId() {
66         return toString().getBytes();
67     }
68 
69     /** Get all implemented types.
70      * @return The implemented UNO types.
71      */
getTypes()72     public Type[] getTypes() {
73         Class interfaces[] = getClass().getInterfaces();
74 
75         Type types[] = new Type[interfaces.length];
76         for(int i = 0; i < interfaces.length; ++ i)
77             types[i] = new Type(interfaces[i]);
78 
79         return types;
80     }
81 
82     /**
83      * Is this service supported?
84      * @param name The name of a service.
85      * @return True, if the service is supported.
86      */
supportsService(String name)87     public boolean supportsService(String name) {
88         return __serviceName.equals(name);
89     }
90 
91     /**
92      * Get all supported service names.
93      * @return All service names.
94      */
getSupportedServiceNames()95     public String[] getSupportedServiceNames() {
96         return new String[] {__serviceName};
97     }
98 
99     /**
100      * Get the implementation name of this class.
101      * @return The name.
102      */
getImplementationName()103     public String getImplementationName() {
104         return getClass().getName();
105     }
106 }
107 
108 /**
109  * The actual implementation of the interceptor.
110  * @see com.sun.star.lang.XTypeProvider
111  * @see com.sun.star.frame.XDispatchProviderInterceptor
112  * @see com.sun.star.frame.XDispatchProvider
113  */
114 class InterceptorImpl implements XDispatchProvider,
115                                 XDispatchProviderInterceptor, XTypeProvider {
116 
117     /** A master dispatch provider **/
118     public XDispatchProvider master = null;
119     /** A slave dispatch provider **/
120     public XDispatchProvider slave = null;
121 
122     /** Get the slave dispatch provider
123      * @return The slave.
124      */
getSlaveDispatchProvider()125     public XDispatchProvider getSlaveDispatchProvider() {
126         return slave;
127     }
128     /** Get the master dispatch provider
129      * @return The master.
130      */
getMasterDispatchProvider()131     public XDispatchProvider getMasterDispatchProvider() {
132         return master;
133     }
134 
135     /** Set the slave dispatch provider
136      * @param prov The new slave.
137      */
setSlaveDispatchProvider(XDispatchProvider prov)138     public void setSlaveDispatchProvider(XDispatchProvider prov) {
139         slave = prov ;
140     }
141 
142     /** Set the master dispatch provider
143      * @param prov The new master.
144      */
setMasterDispatchProvider(XDispatchProvider prov)145     public void setMasterDispatchProvider(XDispatchProvider prov) {
146         master = prov ;
147     }
148 
149     /** Searches for an <type>XDispatch</type> for the specified URL within
150      * the specified target frame.
151      * @param url The URL.
152      * @param frame The target frame
153      * @param flags Optional search flags.
154      * @return The dispatch object which provides the queried functionality
155      *         or null if no dispatch object is available.
156      * @see com.sun.star.frame.XDispatch
157      */
queryDispatch(URL url, String frame, int flags)158     public XDispatch queryDispatch(URL url, String frame, int flags) {
159         return master.queryDispatch(url, frame, flags) ;
160     }
161 
162     /**
163      * Query for an array of <type>XDispatch</type>.
164      * @param desc A list of dipatch requests.
165      * @return A list of dispatch objects.
166      */
queryDispatches(DispatchDescriptor[] desc)167     public XDispatch[] queryDispatches(DispatchDescriptor[] desc) {
168         return master.queryDispatches(desc) ;
169     }
170 
171     /** Get the unique id for this implementation
172      * @return The id.
173      */
getImplementationId()174     public byte[] getImplementationId() {
175         return toString().getBytes();
176     }
177 
178     /** Get all implemented types.
179      * @return The implemented UNO types.
180      */
getTypes()181     public Type[] getTypes() {
182         Class interfaces[] = getClass().getInterfaces();
183 
184         Type types[] = new Type[interfaces.length];
185         for(int i = 0; i < interfaces.length; ++ i)
186             types[i] = new Type(interfaces[i]);
187 
188         return types;
189     }
190 }
191