Update ooo320-m1
[ooovba.git] / odk / examples / DevelopersGuide / OfficeDev / ContextMenuInterceptor.java
blobcbe7d38b287cd2dc8affda017c84682aafbc7a84
1 /*************************************************************************
3 * $RCSfile: ContextMenuInterceptor.java,v $
5 * $Revision: 1.7 $
7 * last change: $Author: vg $ $Date: 2006-03-15 09:28:21 $
9 * The Contents of this file are made available subject to the terms of
10 * the BSD license.
12 * Copyright (c) 2003 by Sun Microsystems, Inc.
13 * All rights reserved.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
17 * are met:
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
30 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
31 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
34 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *************************************************************************/
41 import com.sun.star.beans.XPropertySet;
42 import com.sun.star.ui.ActionTriggerSeparatorType;
43 import com.sun.star.ui.ContextMenuInterceptorAction;
44 import com.sun.star.ui.XContextMenuInterceptor;
45 import com.sun.star.uno.UnoRuntime;
47 public class ContextMenuInterceptor implements XContextMenuInterceptor {
49 /**
50 *Description of the Method
52 *@param args Description of Parameter
53 *@since
55 public static void main(String args[])
57 try {
58 OfficeConnect aConnect = OfficeConnect.createConnection();
60 com.sun.star.frame.XDesktop xDesktop =
61 (com.sun.star.frame.XDesktop)aConnect.createRemoteInstance(
62 com.sun.star.frame.XDesktop.class,"com.sun.star.frame.Desktop");
64 // create a new test document
65 com.sun.star.frame.XComponentLoader xCompLoader =
66 (com.sun.star.frame.XComponentLoader)UnoRuntime.queryInterface(
67 com.sun.star.frame.XComponentLoader.class, xDesktop);
69 com.sun.star.lang.XComponent xComponent =
70 xCompLoader.loadComponentFromURL("private:factory/swriter",
71 "_blank", 0, new com.sun.star.beans.PropertyValue[0]);
73 // intialize the test document
74 com.sun.star.frame.XFrame xFrame = null;
76 com.sun.star.text.XTextDocument xDoc =(com.sun.star.text.XTextDocument)
77 UnoRuntime.queryInterface(com.sun.star.text.XTextDocument.class,
78 xComponent);
80 String infoMsg = new String("All context menus of the created document frame contains now a 'Help' entry with the submenus 'Content', 'Help Agent' and 'Tips'.\n\nPress 'Return' in the shell to remove the context menu interceptor and finish the example!");
81 xDoc.getText().setString(infoMsg);
83 // ensure that the document content is optimal visible
84 com.sun.star.frame.XModel xModel =
85 (com.sun.star.frame.XModel)UnoRuntime.queryInterface(
86 com.sun.star.frame.XModel.class, xDoc);
87 // get the frame for later usage
88 xFrame = xModel.getCurrentController().getFrame();
90 com.sun.star.view.XViewSettingsSupplier xViewSettings =
91 (com.sun.star.view.XViewSettingsSupplier)UnoRuntime.queryInterface(
92 com.sun.star.view.XViewSettingsSupplier.class, xModel.getCurrentController());
93 xViewSettings.getViewSettings().setPropertyValue(
94 "ZoomType", new Short((short)0));
96 // test document will be closed later
98 // reuse the frame
99 com.sun.star.frame.XController xController = xFrame.getController();
100 if ( xController != null ) {
101 com.sun.star.ui.XContextMenuInterception xContextMenuInterception =
102 (com.sun.star.ui.XContextMenuInterception)UnoRuntime.queryInterface(
103 com.sun.star.ui.XContextMenuInterception.class, xController );
104 if( xContextMenuInterception != null ) {
105 ContextMenuInterceptor aContextMenuInterceptor = new ContextMenuInterceptor();
106 com.sun.star.ui.XContextMenuInterceptor xContextMenuInterceptor =
107 (com.sun.star.ui.XContextMenuInterceptor)UnoRuntime.queryInterface(
108 com.sun.star.ui.XContextMenuInterceptor.class, aContextMenuInterceptor );
109 xContextMenuInterception.registerContextMenuInterceptor( xContextMenuInterceptor );
111 System.out.println( "\n ... all context menus of the created document frame contains now a 'Help' entry with the\n submenus 'Content', 'Help Agent' and 'Tips'.\n\nPress 'Return' to remove the context menu interceptor and finish the example!");
113 java.io.BufferedReader reader
114 = new java.io.BufferedReader(new java.io.InputStreamReader(System.in));
115 reader.read();
117 xContextMenuInterception.releaseContextMenuInterceptor(
118 xContextMenuInterceptor );
119 System.out.println( " ... context menu interceptor removed!" );
123 // close test document
124 com.sun.star.util.XCloseable xCloseable = (com.sun.star.util.XCloseable)
125 UnoRuntime.queryInterface(com.sun.star.util.XCloseable.class,
126 xComponent );
128 if (xCloseable != null ) {
129 xCloseable.close(false);
130 } else
132 xComponent.dispose();
135 catch ( com.sun.star.uno.RuntimeException ex ) {
136 // something strange has happend!
137 System.out.println( " Sample caught exception! " + ex );
138 System.exit(1);
140 catch ( java.lang.Exception ex ) {
141 // catch java exceptions and do something useful
142 System.out.println( " Sample caught exception! " + ex );
143 System.exit(1);
146 System.out.println(" ... exit!\n");
147 System.exit( 0 );
151 *Description of the Method
153 *@param args Description of Parameter
154 *@since
156 public ContextMenuInterceptorAction notifyContextMenuExecute(
157 com.sun.star.ui.ContextMenuExecuteEvent aEvent ) throws RuntimeException {
159 try {
161 // Retrieve context menu container and query for service factory to
162 // create sub menus, menu entries and separators
163 com.sun.star.container.XIndexContainer xContextMenu = aEvent.ActionTriggerContainer;
164 com.sun.star.lang.XMultiServiceFactory xMenuElementFactory =
165 (com.sun.star.lang.XMultiServiceFactory)UnoRuntime.queryInterface(
166 com.sun.star.lang.XMultiServiceFactory.class, xContextMenu );
167 if ( xMenuElementFactory != null ) {
168 // create root menu entry and sub menu
169 com.sun.star.beans.XPropertySet xRootMenuEntry =
170 (XPropertySet)UnoRuntime.queryInterface(
171 com.sun.star.beans.XPropertySet.class,
172 xMenuElementFactory.createInstance( "com.sun.star.ui.ActionTrigger" ));
174 // create a line separator for our new help sub menu
175 com.sun.star.beans.XPropertySet xSeparator =
176 (com.sun.star.beans.XPropertySet)UnoRuntime.queryInterface(
177 com.sun.star.beans.XPropertySet.class,
178 xMenuElementFactory.createInstance( "com.sun.star.ui.ActionTriggerSeparator" ));
180 Short aSeparatorType = new Short( ActionTriggerSeparatorType.LINE );
181 xSeparator.setPropertyValue( "SeparatorType", (Object)aSeparatorType );
183 // query sub menu for index container to get access
184 com.sun.star.container.XIndexContainer xSubMenuContainer =
185 (com.sun.star.container.XIndexContainer)UnoRuntime.queryInterface(
186 com.sun.star.container.XIndexContainer.class,
187 xMenuElementFactory.createInstance(
188 "com.sun.star.ui.ActionTriggerContainer" ));
190 // intialize root menu entry
191 xRootMenuEntry.setPropertyValue( "Text", new String( "Help" ));
192 xRootMenuEntry.setPropertyValue( "CommandURL", new String( "slot:5410" ));
193 xRootMenuEntry.setPropertyValue( "HelpURL", new String( "5410" ));
194 xRootMenuEntry.setPropertyValue( "SubContainer", (Object)xSubMenuContainer );
196 // create menu entries for the new sub menu
198 // intialize help/content menu entry
199 XPropertySet xMenuEntry = (XPropertySet)UnoRuntime.queryInterface(
200 XPropertySet.class, xMenuElementFactory.createInstance(
201 "com.sun.star.ui.ActionTrigger" ));
203 xMenuEntry.setPropertyValue( "Text", new String( "Content" ));
204 xMenuEntry.setPropertyValue( "CommandURL", new String( "slot:5401" ));
205 xMenuEntry.setPropertyValue( "HelpURL", new String( "5401" ));
207 // insert menu entry to sub menu
208 xSubMenuContainer.insertByIndex( 0, (Object)xMenuEntry );
210 // intialize help/help agent
211 xMenuEntry = (com.sun.star.beans.XPropertySet)UnoRuntime.queryInterface(
212 com.sun.star.beans.XPropertySet.class,
213 xMenuElementFactory.createInstance(
214 "com.sun.star.ui.ActionTrigger" ));
215 xMenuEntry.setPropertyValue( "Text", new String( "Help Agent" ));
216 xMenuEntry.setPropertyValue( "CommandURL", new String( "slot:5962" ));
217 xMenuEntry.setPropertyValue( "HelpURL", new String( "5962" ));
219 // insert menu entry to sub menu
220 xSubMenuContainer.insertByIndex( 1, (Object)xMenuEntry );
222 // intialize help/tips
223 xMenuEntry = (com.sun.star.beans.XPropertySet)UnoRuntime.queryInterface(
224 com.sun.star.beans.XPropertySet.class,
225 xMenuElementFactory.createInstance(
226 "com.sun.star.ui.ActionTrigger" ));
227 xMenuEntry.setPropertyValue( "Text", new String( "Tips" ));
228 xMenuEntry.setPropertyValue( "CommandURL", new String( "slot:5404" ));
229 xMenuEntry.setPropertyValue( "HelpURL", new String( "5404" ));
231 // insert menu entry to sub menu
232 xSubMenuContainer.insertByIndex( 2, (Object)xMenuEntry );
234 // add separator into the given context menu
235 xContextMenu.insertByIndex( 0, (Object)xSeparator );
237 // add new sub menu into the given context menu
238 xContextMenu.insertByIndex( 0, (Object)xRootMenuEntry );
240 // The controller should execute the modified context menu and stop notifying other
241 // interceptors.
242 return com.sun.star.ui.ContextMenuInterceptorAction.EXECUTE_MODIFIED;
245 catch ( com.sun.star.beans.UnknownPropertyException ex ) {
246 // do something useful
247 // we used a unknown property
249 catch ( com.sun.star.lang.IndexOutOfBoundsException ex ) {
250 // do something useful
251 // we used an invalid index for accessing a container
253 catch ( com.sun.star.uno.Exception ex ) {
254 // something strange has happend!
256 catch ( java.lang.Exception ex ) {
257 // catch java exceptions and something useful
260 return com.sun.star.ui.ContextMenuInterceptorAction.IGNORED;