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 24 import com.sun.star.beans.XPropertySet; 25 import com.sun.star.ui.ActionTriggerSeparatorType; 26 import com.sun.star.ui.ContextMenuInterceptorAction; 27 import com.sun.star.ui.XContextMenuInterceptor; 28 import com.sun.star.uno.UnoRuntime; 29 30 public class ContextMenuInterceptor implements XContextMenuInterceptor { 31 32 /** 33 *Description of the Method 34 * 35 *@param args Description of Parameter 36 *@since 37 */ 38 public static void main(String args[]) 39 { 40 try { 41 OfficeConnect aConnect = OfficeConnect.createConnection(); 42 43 com.sun.star.frame.XDesktop xDesktop = 44 (com.sun.star.frame.XDesktop)aConnect.createRemoteInstance( 45 com.sun.star.frame.XDesktop.class,"com.sun.star.frame.Desktop"); 46 47 // create a new test document 48 com.sun.star.frame.XComponentLoader xCompLoader = 49 (com.sun.star.frame.XComponentLoader)UnoRuntime.queryInterface( 50 com.sun.star.frame.XComponentLoader.class, xDesktop); 51 52 com.sun.star.lang.XComponent xComponent = 53 xCompLoader.loadComponentFromURL("private:factory/swriter", 54 "_blank", 0, new com.sun.star.beans.PropertyValue[0]); 55 56 // intialize the test document 57 com.sun.star.frame.XFrame xFrame = null; 58 { 59 com.sun.star.text.XTextDocument xDoc =(com.sun.star.text.XTextDocument) 60 UnoRuntime.queryInterface(com.sun.star.text.XTextDocument.class, 61 xComponent); 62 63 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!"); 64 xDoc.getText().setString(infoMsg); 65 66 // ensure that the document content is optimal visible 67 com.sun.star.frame.XModel xModel = 68 (com.sun.star.frame.XModel)UnoRuntime.queryInterface( 69 com.sun.star.frame.XModel.class, xDoc); 70 // get the frame for later usage 71 xFrame = xModel.getCurrentController().getFrame(); 72 73 com.sun.star.view.XViewSettingsSupplier xViewSettings = 74 (com.sun.star.view.XViewSettingsSupplier)UnoRuntime.queryInterface( 75 com.sun.star.view.XViewSettingsSupplier.class, xModel.getCurrentController()); 76 xViewSettings.getViewSettings().setPropertyValue( 77 "ZoomType", new Short((short)0)); 78 } 79 // test document will be closed later 80 81 // reuse the frame 82 com.sun.star.frame.XController xController = xFrame.getController(); 83 if ( xController != null ) { 84 com.sun.star.ui.XContextMenuInterception xContextMenuInterception = 85 (com.sun.star.ui.XContextMenuInterception)UnoRuntime.queryInterface( 86 com.sun.star.ui.XContextMenuInterception.class, xController ); 87 if( xContextMenuInterception != null ) { 88 ContextMenuInterceptor aContextMenuInterceptor = new ContextMenuInterceptor(); 89 com.sun.star.ui.XContextMenuInterceptor xContextMenuInterceptor = 90 (com.sun.star.ui.XContextMenuInterceptor)UnoRuntime.queryInterface( 91 com.sun.star.ui.XContextMenuInterceptor.class, aContextMenuInterceptor ); 92 xContextMenuInterception.registerContextMenuInterceptor( xContextMenuInterceptor ); 93 94 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!"); 95 96 java.io.BufferedReader reader 97 = new java.io.BufferedReader(new java.io.InputStreamReader(System.in)); 98 reader.read(); 99 100 xContextMenuInterception.releaseContextMenuInterceptor( 101 xContextMenuInterceptor ); 102 System.out.println( " ... context menu interceptor removed!" ); 103 } 104 } 105 106 // close test document 107 com.sun.star.util.XCloseable xCloseable = (com.sun.star.util.XCloseable) 108 UnoRuntime.queryInterface(com.sun.star.util.XCloseable.class, 109 xComponent ); 110 111 if (xCloseable != null ) { 112 xCloseable.close(false); 113 } else 114 { 115 xComponent.dispose(); 116 } 117 } 118 catch ( com.sun.star.uno.RuntimeException ex ) { 119 // something strange has happend! 120 System.out.println( " Sample caught exception! " + ex ); 121 System.exit(1); 122 } 123 catch ( java.lang.Exception ex ) { 124 // catch java exceptions and do something useful 125 System.out.println( " Sample caught exception! " + ex ); 126 System.exit(1); 127 } 128 129 System.out.println(" ... exit!\n"); 130 System.exit( 0 ); 131 } 132 133 /** 134 *Description of the Method 135 * 136 *@param args Description of Parameter 137 *@since 138 */ 139 public ContextMenuInterceptorAction notifyContextMenuExecute( 140 com.sun.star.ui.ContextMenuExecuteEvent aEvent ) throws RuntimeException { 141 142 try { 143 144 // Retrieve context menu container and query for service factory to 145 // create sub menus, menu entries and separators 146 com.sun.star.container.XIndexContainer xContextMenu = aEvent.ActionTriggerContainer; 147 com.sun.star.lang.XMultiServiceFactory xMenuElementFactory = 148 (com.sun.star.lang.XMultiServiceFactory)UnoRuntime.queryInterface( 149 com.sun.star.lang.XMultiServiceFactory.class, xContextMenu ); 150 if ( xMenuElementFactory != null ) { 151 // create root menu entry and sub menu 152 com.sun.star.beans.XPropertySet xRootMenuEntry = 153 (XPropertySet)UnoRuntime.queryInterface( 154 com.sun.star.beans.XPropertySet.class, 155 xMenuElementFactory.createInstance( "com.sun.star.ui.ActionTrigger" )); 156 157 // create a line separator for our new help sub menu 158 com.sun.star.beans.XPropertySet xSeparator = 159 (com.sun.star.beans.XPropertySet)UnoRuntime.queryInterface( 160 com.sun.star.beans.XPropertySet.class, 161 xMenuElementFactory.createInstance( "com.sun.star.ui.ActionTriggerSeparator" )); 162 163 Short aSeparatorType = new Short( ActionTriggerSeparatorType.LINE ); 164 xSeparator.setPropertyValue( "SeparatorType", (Object)aSeparatorType ); 165 166 // query sub menu for index container to get access 167 com.sun.star.container.XIndexContainer xSubMenuContainer = 168 (com.sun.star.container.XIndexContainer)UnoRuntime.queryInterface( 169 com.sun.star.container.XIndexContainer.class, 170 xMenuElementFactory.createInstance( 171 "com.sun.star.ui.ActionTriggerContainer" )); 172 173 // intialize root menu entry 174 xRootMenuEntry.setPropertyValue( "Text", new String( "Help" )); 175 xRootMenuEntry.setPropertyValue( "CommandURL", new String( "slot:5410" )); 176 xRootMenuEntry.setPropertyValue( "HelpURL", new String( "5410" )); 177 xRootMenuEntry.setPropertyValue( "SubContainer", (Object)xSubMenuContainer ); 178 179 // create menu entries for the new sub menu 180 181 // intialize help/content menu entry 182 XPropertySet xMenuEntry = (XPropertySet)UnoRuntime.queryInterface( 183 XPropertySet.class, xMenuElementFactory.createInstance( 184 "com.sun.star.ui.ActionTrigger" )); 185 186 xMenuEntry.setPropertyValue( "Text", new String( "Content" )); 187 xMenuEntry.setPropertyValue( "CommandURL", new String( "slot:5401" )); 188 xMenuEntry.setPropertyValue( "HelpURL", new String( "5401" )); 189 190 // insert menu entry to sub menu 191 xSubMenuContainer.insertByIndex( 0, (Object)xMenuEntry ); 192 193 // intialize help/help agent 194 xMenuEntry = (com.sun.star.beans.XPropertySet)UnoRuntime.queryInterface( 195 com.sun.star.beans.XPropertySet.class, 196 xMenuElementFactory.createInstance( 197 "com.sun.star.ui.ActionTrigger" )); 198 xMenuEntry.setPropertyValue( "Text", new String( "Help Agent" )); 199 xMenuEntry.setPropertyValue( "CommandURL", new String( "slot:5962" )); 200 xMenuEntry.setPropertyValue( "HelpURL", new String( "5962" )); 201 202 // insert menu entry to sub menu 203 xSubMenuContainer.insertByIndex( 1, (Object)xMenuEntry ); 204 205 // intialize help/tips 206 xMenuEntry = (com.sun.star.beans.XPropertySet)UnoRuntime.queryInterface( 207 com.sun.star.beans.XPropertySet.class, 208 xMenuElementFactory.createInstance( 209 "com.sun.star.ui.ActionTrigger" )); 210 xMenuEntry.setPropertyValue( "Text", new String( "Tips" )); 211 xMenuEntry.setPropertyValue( "CommandURL", new String( "slot:5404" )); 212 xMenuEntry.setPropertyValue( "HelpURL", new String( "5404" )); 213 214 // insert menu entry to sub menu 215 xSubMenuContainer.insertByIndex( 2, (Object)xMenuEntry ); 216 217 // add separator into the given context menu 218 xContextMenu.insertByIndex( 0, (Object)xSeparator ); 219 220 // add new sub menu into the given context menu 221 xContextMenu.insertByIndex( 0, (Object)xRootMenuEntry ); 222 223 // The controller should execute the modified context menu and stop notifying other 224 // interceptors. 225 return com.sun.star.ui.ContextMenuInterceptorAction.EXECUTE_MODIFIED; 226 } 227 } 228 catch ( com.sun.star.beans.UnknownPropertyException ex ) { 229 // do something useful 230 // we used a unknown property 231 } 232 catch ( com.sun.star.lang.IndexOutOfBoundsException ex ) { 233 // do something useful 234 // we used an invalid index for accessing a container 235 } 236 catch ( com.sun.star.uno.Exception ex ) { 237 // something strange has happend! 238 } 239 catch ( java.lang.Exception ex ) { 240 // catch java exceptions and something useful 241 } 242 243 return com.sun.star.ui.ContextMenuInterceptorAction.IGNORED; 244 } 245 } 246