Branch libreoffice-5-0-4
[LibreOffice.git] / odk / examples / DevelopersGuide / OfficeDev / ContextMenuInterceptor.java
blobbb5a8bd6e1f40ea6a29211dca8fd4c8afa656e5a
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
48 public static void main(String args[])
50 try {
51 OfficeConnect aConnect = OfficeConnect.createConnection();
53 com.sun.star.frame.XDesktop xDesktop =
54 aConnect.createRemoteInstance(
55 com.sun.star.frame.XDesktop.class,"com.sun.star.frame.Desktop");
57 // create a new test document
58 com.sun.star.frame.XComponentLoader xCompLoader =
59 UnoRuntime.queryInterface(
60 com.sun.star.frame.XComponentLoader.class, xDesktop);
62 com.sun.star.lang.XComponent xComponent =
63 xCompLoader.loadComponentFromURL("private:factory/swriter",
64 "_blank", 0, new com.sun.star.beans.PropertyValue[0]);
66 // intialize the test document
67 com.sun.star.frame.XFrame xFrame = null;
69 com.sun.star.text.XTextDocument xDoc =UnoRuntime.queryInterface(com.sun.star.text.XTextDocument.class,
70 xComponent);
72 String infoMsg = "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!";
73 xDoc.getText().setString(infoMsg);
75 // ensure that the document content is optimal visible
76 com.sun.star.frame.XModel xModel =
77 UnoRuntime.queryInterface(
78 com.sun.star.frame.XModel.class, xDoc);
79 // get the frame for later usage
80 xFrame = xModel.getCurrentController().getFrame();
82 com.sun.star.view.XViewSettingsSupplier xViewSettings =
83 UnoRuntime.queryInterface(
84 com.sun.star.view.XViewSettingsSupplier.class, xModel.getCurrentController());
85 xViewSettings.getViewSettings().setPropertyValue(
86 "ZoomType", Short.valueOf((short)0));
88 // test document will be closed later
90 // reuse the frame
91 com.sun.star.frame.XController xController = xFrame.getController();
92 if ( xController != null ) {
93 com.sun.star.ui.XContextMenuInterception xContextMenuInterception =
94 UnoRuntime.queryInterface(
95 com.sun.star.ui.XContextMenuInterception.class, xController );
96 if( xContextMenuInterception != null ) {
97 ContextMenuInterceptor aContextMenuInterceptor = new ContextMenuInterceptor();
98 com.sun.star.ui.XContextMenuInterceptor xContextMenuInterceptor =
99 UnoRuntime.queryInterface(
100 com.sun.star.ui.XContextMenuInterceptor.class, aContextMenuInterceptor );
101 xContextMenuInterception.registerContextMenuInterceptor( xContextMenuInterceptor );
103 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!");
105 java.io.BufferedReader reader
106 = new java.io.BufferedReader(new java.io.InputStreamReader(System.in));
107 reader.read();
109 xContextMenuInterception.releaseContextMenuInterceptor(
110 xContextMenuInterceptor );
111 System.out.println( " ... context menu interceptor removed!" );
115 // close test document
116 com.sun.star.util.XCloseable xCloseable = UnoRuntime.queryInterface(com.sun.star.util.XCloseable.class,
117 xComponent );
119 if (xCloseable != null ) {
120 xCloseable.close(false);
121 } else
123 xComponent.dispose();
126 catch ( com.sun.star.uno.RuntimeException ex ) {
127 // something strange has happened!
128 System.out.println( " Sample caught exception! " + ex );
129 System.exit(1);
131 catch ( java.lang.Exception ex ) {
132 // catch java exceptions and do something useful
133 System.out.println( " Sample caught exception! " + ex );
134 System.exit(1);
137 System.out.println(" ... exit!\n");
138 System.exit( 0 );
142 *Description of the Method
144 public ContextMenuInterceptorAction notifyContextMenuExecute(
145 com.sun.star.ui.ContextMenuExecuteEvent aEvent ) throws RuntimeException {
147 try {
149 // Retrieve context menu container and query for service factory to
150 // create sub menus, menu entries and separators
151 com.sun.star.container.XIndexContainer xContextMenu = aEvent.ActionTriggerContainer;
152 com.sun.star.lang.XMultiServiceFactory xMenuElementFactory =
153 UnoRuntime.queryInterface(
154 com.sun.star.lang.XMultiServiceFactory.class, xContextMenu );
155 if ( xMenuElementFactory != null ) {
156 // create root menu entry and sub menu
157 com.sun.star.beans.XPropertySet xRootMenuEntry =
158 UnoRuntime.queryInterface(
159 com.sun.star.beans.XPropertySet.class,
160 xMenuElementFactory.createInstance( "com.sun.star.ui.ActionTrigger" ));
162 // create a line separator for our new help sub menu
163 com.sun.star.beans.XPropertySet xSeparator =
164 UnoRuntime.queryInterface(
165 com.sun.star.beans.XPropertySet.class,
166 xMenuElementFactory.createInstance( "com.sun.star.ui.ActionTriggerSeparator" ));
168 Short aSeparatorType = Short.valueOf( ActionTriggerSeparatorType.LINE );
169 xSeparator.setPropertyValue( "SeparatorType", aSeparatorType );
171 // query sub menu for index container to get access
172 com.sun.star.container.XIndexContainer xSubMenuContainer =
173 UnoRuntime.queryInterface(
174 com.sun.star.container.XIndexContainer.class,
175 xMenuElementFactory.createInstance(
176 "com.sun.star.ui.ActionTriggerContainer" ));
178 // intialize root menu entry
179 xRootMenuEntry.setPropertyValue( "Text", "Help");
180 xRootMenuEntry.setPropertyValue( "CommandURL", "slot:5410");
181 xRootMenuEntry.setPropertyValue( "HelpURL", "5410");
182 xRootMenuEntry.setPropertyValue( "SubContainer", xSubMenuContainer );
184 // create menu entries for the new sub menu
186 // intialize help/content menu entry
187 XPropertySet xMenuEntry = UnoRuntime.queryInterface(
188 XPropertySet.class, xMenuElementFactory.createInstance(
189 "com.sun.star.ui.ActionTrigger" ));
191 xMenuEntry.setPropertyValue( "Text", "Content" );
192 xMenuEntry.setPropertyValue( "CommandURL", "slot:5401" );
193 xMenuEntry.setPropertyValue( "HelpURL", "5401" );
195 // insert menu entry to sub menu
196 xSubMenuContainer.insertByIndex( 0, xMenuEntry );
198 // intialize help/help agent
199 xMenuEntry = UnoRuntime.queryInterface(
200 com.sun.star.beans.XPropertySet.class,
201 xMenuElementFactory.createInstance(
202 "com.sun.star.ui.ActionTrigger" ));
203 xMenuEntry.setPropertyValue( "Text", "Help Agent" );
204 xMenuEntry.setPropertyValue( "CommandURL", "slot:5962" );
205 xMenuEntry.setPropertyValue( "HelpURL", "5962" );
207 // insert menu entry to sub menu
208 xSubMenuContainer.insertByIndex( 1, xMenuEntry );
210 // intialize help/tips
211 xMenuEntry = UnoRuntime.queryInterface(
212 com.sun.star.beans.XPropertySet.class,
213 xMenuElementFactory.createInstance(
214 "com.sun.star.ui.ActionTrigger" ));
215 xMenuEntry.setPropertyValue( "Text", "Tips" );
216 xMenuEntry.setPropertyValue( "CommandURL", "slot:5404" );
217 xMenuEntry.setPropertyValue( "HelpURL", "5404" );
219 // insert menu entry to sub menu
220 xSubMenuContainer.insertByIndex( 2, xMenuEntry );
222 // add separator into the given context menu
223 xContextMenu.insertByIndex( 0, xSeparator );
225 // add new sub menu into the given context menu
226 xContextMenu.insertByIndex( 0, xRootMenuEntry );
228 // The controller should execute the modified context menu and stop notifying other
229 // interceptors.
230 return com.sun.star.ui.ContextMenuInterceptorAction.EXECUTE_MODIFIED;
233 catch ( com.sun.star.beans.UnknownPropertyException ex ) {
234 // do something useful
235 // we used a unknown property
237 catch ( com.sun.star.lang.IndexOutOfBoundsException ex ) {
238 // do something useful
239 // we used an invalid index for accessing a container
241 catch ( com.sun.star.uno.Exception ex ) {
242 // something strange has happened!
244 catch ( java.lang.Exception ex ) {
245 // catch java exceptions and something useful
248 return com.sun.star.ui.ContextMenuInterceptorAction.IGNORED;