1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 11 * OpenOffice.org is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU Lesser General Public License version 3 13 * only, as published by the Free Software Foundation. 14 * 15 * OpenOffice.org is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU Lesser General Public License version 3 for more details 19 * (a copy is included in the LICENSE file that accompanied this code). 20 * 21 * You should have received a copy of the GNU Lesser General Public License 22 * version 3 along with OpenOffice.org. If not, see 23 * <http://www.openoffice.org/license.html> 24 * for a copy of the LGPLv3 License. 25 * 26 ************************************************************************/ 27 28 29 package helper; 30 31 import com.sun.star.beans.UnknownPropertyException; 32 import com.sun.star.beans.XPropertySet; 33 import com.sun.star.container.XIndexContainer; 34 import com.sun.star.lang.XMultiServiceFactory; 35 import com.sun.star.ui.ActionTriggerSeparatorType; 36 import com.sun.star.ui.ContextMenuInterceptorAction; 37 import com.sun.star.ui.XContextMenuInterceptor; 38 import com.sun.star.uno.UnoRuntime; 39 40 public class ContextMenuInterceptor implements XContextMenuInterceptor { 41 42 public ContextMenuInterceptorAction notifyContextMenuExecute( 43 com.sun.star.ui.ContextMenuExecuteEvent aEvent ) throws RuntimeException { 44 try { 45 // Retrieve context menu container and query for service factory to 46 // create sub menus, menu entries and separators 47 XIndexContainer xContextMenu = aEvent.ActionTriggerContainer; 48 XMultiServiceFactory xMenuElementFactory = 49 (XMultiServiceFactory)UnoRuntime.queryInterface( 50 XMultiServiceFactory.class, xContextMenu ); 51 52 if ( xMenuElementFactory != null ) { 53 54 // create root menu entry for sub menu and sub menu 55 XPropertySet xRootMenuEntry = 56 (XPropertySet)UnoRuntime.queryInterface( 57 XPropertySet.class, 58 xMenuElementFactory.createInstance("com.sun.star.ui.ActionTrigger" )); 59 60 // create a line separator for our new help sub menu 61 XPropertySet xSeparator = 62 (XPropertySet)UnoRuntime.queryInterface( 63 XPropertySet.class, 64 xMenuElementFactory.createInstance("com.sun.star.ui.ActionTriggerSeparator" ) ); 65 Short aSeparatorType = new Short( ActionTriggerSeparatorType.LINE ); 66 xSeparator.setPropertyValue( "SeparatorType", (Object)aSeparatorType ); 67 68 // query sub menu for index container to get access 69 XIndexContainer xSubMenuContainer = 70 (XIndexContainer)UnoRuntime.queryInterface( 71 XIndexContainer.class, 72 xMenuElementFactory.createInstance("com.sun.star.ui.ActionTriggerContainer" )); 73 74 // intialize root menu entry "Help" 75 xRootMenuEntry.setPropertyValue( "Text", new String( "Help" )); 76 xRootMenuEntry.setPropertyValue( "CommandURL", new String( "slot:5410" )); 77 xRootMenuEntry.setPropertyValue( "HelpURL", new String( "5410" )); 78 xRootMenuEntry.setPropertyValue( "SubContainer", (Object)xSubMenuContainer ); 79 80 // create menu entries for the new sub menu 81 // intialize help/content menu entry 82 // entry "Content" 83 XPropertySet xMenuEntry = (XPropertySet)UnoRuntime.queryInterface( 84 XPropertySet.class, xMenuElementFactory.createInstance( 85 "com.sun.star.ui.ActionTrigger" )); 86 xMenuEntry.setPropertyValue( "Text", new String( "Content" )); 87 xMenuEntry.setPropertyValue( "CommandURL", new String( "slot:5401" )); 88 xMenuEntry.setPropertyValue( "HelpURL", new String( "5401" )); 89 90 // insert menu entry to sub menu 91 xSubMenuContainer.insertByIndex( 0, (Object)xMenuEntry ); 92 93 // intialize help/help agent 94 // entry "Help Agent" 95 xMenuEntry = (XPropertySet)UnoRuntime.queryInterface( 96 XPropertySet.class, 97 xMenuElementFactory.createInstance("com.sun.star.ui.ActionTrigger" )); 98 xMenuEntry.setPropertyValue( "Text", new String( "Help Agent" )); 99 xMenuEntry.setPropertyValue( "CommandURL", new String( "slot:5962" )); 100 xMenuEntry.setPropertyValue( "HelpURL", new String( "5962" )); 101 102 // insert menu entry to sub menu 103 xSubMenuContainer.insertByIndex( 1, (Object)xMenuEntry ); 104 // intialize help/tips 105 // entry "Tips" 106 xMenuEntry = (XPropertySet)UnoRuntime.queryInterface( 107 XPropertySet.class, 108 xMenuElementFactory.createInstance("com.sun.star.ui.ActionTrigger" )); 109 xMenuEntry.setPropertyValue( "Text", new String( "Tips" )); 110 xMenuEntry.setPropertyValue( "CommandURL", new String( "slot:5404" )); 111 xMenuEntry.setPropertyValue( "HelpURL", new String( "5404" )); 112 113 // insert menu entry to sub menu 114 xSubMenuContainer.insertByIndex( 2, (Object)xMenuEntry ); 115 116 // add separator into the given context menu 117 xContextMenu.insertByIndex( 1, (Object)xSeparator ); 118 119 // add new sub menu into the given context menu 120 xContextMenu.insertByIndex( 1, (Object)xRootMenuEntry ); 121 122 // The controller should execute the modified context menu and stop notifying other 123 // interceptors. 124 return ContextMenuInterceptorAction.EXECUTE_MODIFIED ; 125 } 126 } catch ( UnknownPropertyException ex ) { 127 // do something useful 128 // we used a unknown property 129 } catch ( IndexOutOfBoundsException ex ) { 130 // do something useful 131 // we used an invalid index for accessing a container 132 } catch ( Exception ex ) { 133 // something strange has happend! 134 } catch ( Throwable ex ) { 135 // catch java exceptions and do something useful 136 } 137 138 return ContextMenuInterceptorAction.IGNORED; 139 } 140 }