bump product version to 4.1.6.2
[LibreOffice.git] / odk / examples / DevelopersGuide / OfficeDev / ContextMenuInterceptor.java
blob608ad199c065a51c902277323d8605f0c8a8ab8e
1 /*************************************************************************
3 * The Contents of this file are made available subject to the terms of
4 * the BSD license.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
30 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
31 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 *************************************************************************/
35 import com.sun.star.beans.XPropertySet;
36 import com.sun.star.ui.ActionTriggerSeparatorType;
37 import com.sun.star.ui.ContextMenuInterceptorAction;
38 import com.sun.star.ui.XContextMenuInterceptor;
39 import com.sun.star.uno.UnoRuntime;
41 public class ContextMenuInterceptor implements XContextMenuInterceptor {
43 /**
44 *Description of the Method
46 *@param args Description of Parameter
47 *@since
49 public static void main(String args[])
51 try {
52 OfficeConnect aConnect = OfficeConnect.createConnection();
54 com.sun.star.frame.XDesktop xDesktop =
55 aConnect.createRemoteInstance(
56 com.sun.star.frame.XDesktop.class,"com.sun.star.frame.Desktop");
58 // create a new test document
59 com.sun.star.frame.XComponentLoader xCompLoader =
60 UnoRuntime.queryInterface(
61 com.sun.star.frame.XComponentLoader.class, xDesktop);
63 com.sun.star.lang.XComponent xComponent =
64 xCompLoader.loadComponentFromURL("private:factory/swriter",
65 "_blank", 0, new com.sun.star.beans.PropertyValue[0]);
67 // intialize the test document
68 com.sun.star.frame.XFrame xFrame = null;
70 com.sun.star.text.XTextDocument xDoc =UnoRuntime.queryInterface(com.sun.star.text.XTextDocument.class,
71 xComponent);
73 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!");
74 xDoc.getText().setString(infoMsg);
76 // ensure that the document content is optimal visible
77 com.sun.star.frame.XModel xModel =
78 UnoRuntime.queryInterface(
79 com.sun.star.frame.XModel.class, xDoc);
80 // get the frame for later usage
81 xFrame = xModel.getCurrentController().getFrame();
83 com.sun.star.view.XViewSettingsSupplier xViewSettings =
84 UnoRuntime.queryInterface(
85 com.sun.star.view.XViewSettingsSupplier.class, xModel.getCurrentController());
86 xViewSettings.getViewSettings().setPropertyValue(
87 "ZoomType", new Short((short)0));
89 // test document will be closed later
91 // reuse the frame
92 com.sun.star.frame.XController xController = xFrame.getController();
93 if ( xController != null ) {
94 com.sun.star.ui.XContextMenuInterception xContextMenuInterception =
95 UnoRuntime.queryInterface(
96 com.sun.star.ui.XContextMenuInterception.class, xController );
97 if( xContextMenuInterception != null ) {
98 ContextMenuInterceptor aContextMenuInterceptor = new ContextMenuInterceptor();
99 com.sun.star.ui.XContextMenuInterceptor xContextMenuInterceptor =
100 UnoRuntime.queryInterface(
101 com.sun.star.ui.XContextMenuInterceptor.class, aContextMenuInterceptor );
102 xContextMenuInterception.registerContextMenuInterceptor( xContextMenuInterceptor );
104 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!");
106 java.io.BufferedReader reader
107 = new java.io.BufferedReader(new java.io.InputStreamReader(System.in));
108 reader.read();
110 xContextMenuInterception.releaseContextMenuInterceptor(
111 xContextMenuInterceptor );
112 System.out.println( " ... context menu interceptor removed!" );
116 // close test document
117 com.sun.star.util.XCloseable xCloseable = UnoRuntime.queryInterface(com.sun.star.util.XCloseable.class,
118 xComponent );
120 if (xCloseable != null ) {
121 xCloseable.close(false);
122 } else
124 xComponent.dispose();
127 catch ( com.sun.star.uno.RuntimeException ex ) {
128 // something strange has happened!
129 System.out.println( " Sample caught exception! " + ex );
130 System.exit(1);
132 catch ( java.lang.Exception ex ) {
133 // catch java exceptions and do something useful
134 System.out.println( " Sample caught exception! " + ex );
135 System.exit(1);
138 System.out.println(" ... exit!\n");
139 System.exit( 0 );
143 *Description of the Method
145 *@param args Description of Parameter
146 *@since
148 public ContextMenuInterceptorAction notifyContextMenuExecute(
149 com.sun.star.ui.ContextMenuExecuteEvent aEvent ) throws RuntimeException {
151 try {
153 // Retrieve context menu container and query for service factory to
154 // create sub menus, menu entries and separators
155 com.sun.star.container.XIndexContainer xContextMenu = aEvent.ActionTriggerContainer;
156 com.sun.star.lang.XMultiServiceFactory xMenuElementFactory =
157 UnoRuntime.queryInterface(
158 com.sun.star.lang.XMultiServiceFactory.class, xContextMenu );
159 if ( xMenuElementFactory != null ) {
160 // create root menu entry and sub menu
161 com.sun.star.beans.XPropertySet xRootMenuEntry =
162 UnoRuntime.queryInterface(
163 com.sun.star.beans.XPropertySet.class,
164 xMenuElementFactory.createInstance( "com.sun.star.ui.ActionTrigger" ));
166 // create a line separator for our new help sub menu
167 com.sun.star.beans.XPropertySet xSeparator =
168 UnoRuntime.queryInterface(
169 com.sun.star.beans.XPropertySet.class,
170 xMenuElementFactory.createInstance( "com.sun.star.ui.ActionTriggerSeparator" ));
172 Short aSeparatorType = new Short( ActionTriggerSeparatorType.LINE );
173 xSeparator.setPropertyValue( "SeparatorType", aSeparatorType );
175 // query sub menu for index container to get access
176 com.sun.star.container.XIndexContainer xSubMenuContainer =
177 UnoRuntime.queryInterface(
178 com.sun.star.container.XIndexContainer.class,
179 xMenuElementFactory.createInstance(
180 "com.sun.star.ui.ActionTriggerContainer" ));
182 // intialize root menu entry
183 xRootMenuEntry.setPropertyValue( "Text", new String( "Help" ));
184 xRootMenuEntry.setPropertyValue( "CommandURL", new String( "slot:5410" ));
185 xRootMenuEntry.setPropertyValue( "HelpURL", new String( "5410" ));
186 xRootMenuEntry.setPropertyValue( "SubContainer", xSubMenuContainer );
188 // create menu entries for the new sub menu
190 // intialize help/content menu entry
191 XPropertySet xMenuEntry = UnoRuntime.queryInterface(
192 XPropertySet.class, xMenuElementFactory.createInstance(
193 "com.sun.star.ui.ActionTrigger" ));
195 xMenuEntry.setPropertyValue( "Text", new String( "Content" ));
196 xMenuEntry.setPropertyValue( "CommandURL", new String( "slot:5401" ));
197 xMenuEntry.setPropertyValue( "HelpURL", new String( "5401" ));
199 // insert menu entry to sub menu
200 xSubMenuContainer.insertByIndex( 0, xMenuEntry );
202 // intialize help/help agent
203 xMenuEntry = UnoRuntime.queryInterface(
204 com.sun.star.beans.XPropertySet.class,
205 xMenuElementFactory.createInstance(
206 "com.sun.star.ui.ActionTrigger" ));
207 xMenuEntry.setPropertyValue( "Text", new String( "Help Agent" ));
208 xMenuEntry.setPropertyValue( "CommandURL", new String( "slot:5962" ));
209 xMenuEntry.setPropertyValue( "HelpURL", new String( "5962" ));
211 // insert menu entry to sub menu
212 xSubMenuContainer.insertByIndex( 1, xMenuEntry );
214 // intialize help/tips
215 xMenuEntry = UnoRuntime.queryInterface(
216 com.sun.star.beans.XPropertySet.class,
217 xMenuElementFactory.createInstance(
218 "com.sun.star.ui.ActionTrigger" ));
219 xMenuEntry.setPropertyValue( "Text", new String( "Tips" ));
220 xMenuEntry.setPropertyValue( "CommandURL", new String( "slot:5404" ));
221 xMenuEntry.setPropertyValue( "HelpURL", new String( "5404" ));
223 // insert menu entry to sub menu
224 xSubMenuContainer.insertByIndex( 2, xMenuEntry );
226 // add separator into the given context menu
227 xContextMenu.insertByIndex( 0, xSeparator );
229 // add new sub menu into the given context menu
230 xContextMenu.insertByIndex( 0, xRootMenuEntry );
232 // The controller should execute the modified context menu and stop notifying other
233 // interceptors.
234 return com.sun.star.ui.ContextMenuInterceptorAction.EXECUTE_MODIFIED;
237 catch ( com.sun.star.beans.UnknownPropertyException ex ) {
238 // do something useful
239 // we used a unknown property
241 catch ( com.sun.star.lang.IndexOutOfBoundsException ex ) {
242 // do something useful
243 // we used an invalid index for accessing a container
245 catch ( com.sun.star.uno.Exception ex ) {
246 // something strange has happened!
248 catch ( java.lang.Exception ex ) {
249 // catch java exceptions and something useful
252 return com.sun.star.ui.ContextMenuInterceptorAction.IGNORED;