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 25 package helper; 26 27 import com.sun.star.beans.UnknownPropertyException; 28 import com.sun.star.beans.XPropertySet; 29 import com.sun.star.container.XIndexContainer; 30 import com.sun.star.lang.XMultiServiceFactory; 31 import com.sun.star.ui.ActionTriggerSeparatorType; 32 import com.sun.star.ui.ContextMenuInterceptorAction; 33 import com.sun.star.ui.XContextMenuInterceptor; 34 import com.sun.star.uno.UnoRuntime; 35 36 public class ContextMenuInterceptor implements XContextMenuInterceptor { 37 notifyContextMenuExecute( com.sun.star.ui.ContextMenuExecuteEvent aEvent )38 public ContextMenuInterceptorAction notifyContextMenuExecute( 39 com.sun.star.ui.ContextMenuExecuteEvent aEvent ) throws RuntimeException { 40 try { 41 // Retrieve context menu container and query for service factory to 42 // create sub menus, menu entries and separators 43 XIndexContainer xContextMenu = aEvent.ActionTriggerContainer; 44 XMultiServiceFactory xMenuElementFactory = 45 (XMultiServiceFactory)UnoRuntime.queryInterface( 46 XMultiServiceFactory.class, xContextMenu ); 47 48 if ( xMenuElementFactory != null ) { 49 50 // create root menu entry for sub menu and sub menu 51 XPropertySet xRootMenuEntry = 52 (XPropertySet)UnoRuntime.queryInterface( 53 XPropertySet.class, 54 xMenuElementFactory.createInstance("com.sun.star.ui.ActionTrigger" )); 55 56 // create a line separator for our new help sub menu 57 XPropertySet xSeparator = 58 (XPropertySet)UnoRuntime.queryInterface( 59 XPropertySet.class, 60 xMenuElementFactory.createInstance("com.sun.star.ui.ActionTriggerSeparator" ) ); 61 Short aSeparatorType = new Short( ActionTriggerSeparatorType.LINE ); 62 xSeparator.setPropertyValue( "SeparatorType", (Object)aSeparatorType ); 63 64 // query sub menu for index container to get access 65 XIndexContainer xSubMenuContainer = 66 (XIndexContainer)UnoRuntime.queryInterface( 67 XIndexContainer.class, 68 xMenuElementFactory.createInstance("com.sun.star.ui.ActionTriggerContainer" )); 69 70 // intialize root menu entry "Help" 71 xRootMenuEntry.setPropertyValue( "Text", new String( "Help" )); 72 xRootMenuEntry.setPropertyValue( "CommandURL", new String( "slot:5410" )); 73 xRootMenuEntry.setPropertyValue( "HelpURL", new String( "5410" )); 74 xRootMenuEntry.setPropertyValue( "SubContainer", (Object)xSubMenuContainer ); 75 76 // create menu entries for the new sub menu 77 // intialize help/content menu entry 78 // entry "Content" 79 XPropertySet xMenuEntry = (XPropertySet)UnoRuntime.queryInterface( 80 XPropertySet.class, xMenuElementFactory.createInstance( 81 "com.sun.star.ui.ActionTrigger" )); 82 xMenuEntry.setPropertyValue( "Text", new String( "Content" )); 83 xMenuEntry.setPropertyValue( "CommandURL", new String( "slot:5401" )); 84 xMenuEntry.setPropertyValue( "HelpURL", new String( "5401" )); 85 86 // insert menu entry to sub menu 87 xSubMenuContainer.insertByIndex( 0, (Object)xMenuEntry ); 88 89 // intialize help/help agent 90 // entry "Help Agent" 91 xMenuEntry = (XPropertySet)UnoRuntime.queryInterface( 92 XPropertySet.class, 93 xMenuElementFactory.createInstance("com.sun.star.ui.ActionTrigger" )); 94 xMenuEntry.setPropertyValue( "Text", new String( "Help Agent" )); 95 xMenuEntry.setPropertyValue( "CommandURL", new String( "slot:5962" )); 96 xMenuEntry.setPropertyValue( "HelpURL", new String( "5962" )); 97 98 // insert menu entry to sub menu 99 xSubMenuContainer.insertByIndex( 1, (Object)xMenuEntry ); 100 // intialize help/tips 101 // entry "Tips" 102 xMenuEntry = (XPropertySet)UnoRuntime.queryInterface( 103 XPropertySet.class, 104 xMenuElementFactory.createInstance("com.sun.star.ui.ActionTrigger" )); 105 xMenuEntry.setPropertyValue( "Text", new String( "Tips" )); 106 xMenuEntry.setPropertyValue( "CommandURL", new String( "slot:5404" )); 107 xMenuEntry.setPropertyValue( "HelpURL", new String( "5404" )); 108 109 // insert menu entry to sub menu 110 xSubMenuContainer.insertByIndex( 2, (Object)xMenuEntry ); 111 112 // add separator into the given context menu 113 xContextMenu.insertByIndex( 1, (Object)xSeparator ); 114 115 // add new sub menu into the given context menu 116 xContextMenu.insertByIndex( 1, (Object)xRootMenuEntry ); 117 118 // The controller should execute the modified context menu and stop notifying other 119 // interceptors. 120 return ContextMenuInterceptorAction.EXECUTE_MODIFIED ; 121 } 122 } catch ( UnknownPropertyException ex ) { 123 // do something useful 124 // we used a unknown property 125 } catch ( IndexOutOfBoundsException ex ) { 126 // do something useful 127 // we used an invalid index for accessing a container 128 } catch ( Exception ex ) { 129 // something strange has happend! 130 } catch ( Throwable ex ) { 131 // catch java exceptions and do something useful 132 } 133 134 return ContextMenuInterceptorAction.IGNORED; 135 } 136 }