1*ef39d40dSAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*ef39d40dSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*ef39d40dSAndrew Rist * or more contributor license agreements. See the NOTICE file 5*ef39d40dSAndrew Rist * distributed with this work for additional information 6*ef39d40dSAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*ef39d40dSAndrew Rist * to you under the Apache License, Version 2.0 (the 8*ef39d40dSAndrew Rist * "License"); you may not use this file except in compliance 9*ef39d40dSAndrew Rist * with the License. You may obtain a copy of the License at 10*ef39d40dSAndrew Rist * 11*ef39d40dSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12*ef39d40dSAndrew Rist * 13*ef39d40dSAndrew Rist * Unless required by applicable law or agreed to in writing, 14*ef39d40dSAndrew Rist * software distributed under the License is distributed on an 15*ef39d40dSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*ef39d40dSAndrew Rist * KIND, either express or implied. See the License for the 17*ef39d40dSAndrew Rist * specific language governing permissions and limitations 18*ef39d40dSAndrew Rist * under the License. 19*ef39d40dSAndrew Rist * 20*ef39d40dSAndrew Rist *************************************************************/ 21*ef39d40dSAndrew Rist 22*ef39d40dSAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir 25cdf0e10cSrcweir package helper; 26cdf0e10cSrcweir 27cdf0e10cSrcweir import com.sun.star.beans.UnknownPropertyException; 28cdf0e10cSrcweir import com.sun.star.beans.XPropertySet; 29cdf0e10cSrcweir import com.sun.star.container.XIndexContainer; 30cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory; 31cdf0e10cSrcweir import com.sun.star.ui.ActionTriggerSeparatorType; 32cdf0e10cSrcweir import com.sun.star.ui.ContextMenuInterceptorAction; 33cdf0e10cSrcweir import com.sun.star.ui.XContextMenuInterceptor; 34cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime; 35cdf0e10cSrcweir 36cdf0e10cSrcweir public class ContextMenuInterceptor implements XContextMenuInterceptor { 37cdf0e10cSrcweir notifyContextMenuExecute( com.sun.star.ui.ContextMenuExecuteEvent aEvent )38cdf0e10cSrcweir public ContextMenuInterceptorAction notifyContextMenuExecute( 39cdf0e10cSrcweir com.sun.star.ui.ContextMenuExecuteEvent aEvent ) throws RuntimeException { 40cdf0e10cSrcweir try { 41cdf0e10cSrcweir // Retrieve context menu container and query for service factory to 42cdf0e10cSrcweir // create sub menus, menu entries and separators 43cdf0e10cSrcweir XIndexContainer xContextMenu = aEvent.ActionTriggerContainer; 44cdf0e10cSrcweir XMultiServiceFactory xMenuElementFactory = 45cdf0e10cSrcweir (XMultiServiceFactory)UnoRuntime.queryInterface( 46cdf0e10cSrcweir XMultiServiceFactory.class, xContextMenu ); 47cdf0e10cSrcweir 48cdf0e10cSrcweir if ( xMenuElementFactory != null ) { 49cdf0e10cSrcweir 50cdf0e10cSrcweir // create root menu entry for sub menu and sub menu 51cdf0e10cSrcweir XPropertySet xRootMenuEntry = 52cdf0e10cSrcweir (XPropertySet)UnoRuntime.queryInterface( 53cdf0e10cSrcweir XPropertySet.class, 54cdf0e10cSrcweir xMenuElementFactory.createInstance("com.sun.star.ui.ActionTrigger" )); 55cdf0e10cSrcweir 56cdf0e10cSrcweir // create a line separator for our new help sub menu 57cdf0e10cSrcweir XPropertySet xSeparator = 58cdf0e10cSrcweir (XPropertySet)UnoRuntime.queryInterface( 59cdf0e10cSrcweir XPropertySet.class, 60cdf0e10cSrcweir xMenuElementFactory.createInstance("com.sun.star.ui.ActionTriggerSeparator" ) ); 61cdf0e10cSrcweir Short aSeparatorType = new Short( ActionTriggerSeparatorType.LINE ); 62cdf0e10cSrcweir xSeparator.setPropertyValue( "SeparatorType", (Object)aSeparatorType ); 63cdf0e10cSrcweir 64cdf0e10cSrcweir // query sub menu for index container to get access 65cdf0e10cSrcweir XIndexContainer xSubMenuContainer = 66cdf0e10cSrcweir (XIndexContainer)UnoRuntime.queryInterface( 67cdf0e10cSrcweir XIndexContainer.class, 68cdf0e10cSrcweir xMenuElementFactory.createInstance("com.sun.star.ui.ActionTriggerContainer" )); 69cdf0e10cSrcweir 70cdf0e10cSrcweir // intialize root menu entry "Help" 71cdf0e10cSrcweir xRootMenuEntry.setPropertyValue( "Text", new String( "Help" )); 72cdf0e10cSrcweir xRootMenuEntry.setPropertyValue( "CommandURL", new String( "slot:5410" )); 73cdf0e10cSrcweir xRootMenuEntry.setPropertyValue( "HelpURL", new String( "5410" )); 74cdf0e10cSrcweir xRootMenuEntry.setPropertyValue( "SubContainer", (Object)xSubMenuContainer ); 75cdf0e10cSrcweir 76cdf0e10cSrcweir // create menu entries for the new sub menu 77cdf0e10cSrcweir // intialize help/content menu entry 78cdf0e10cSrcweir // entry "Content" 79cdf0e10cSrcweir XPropertySet xMenuEntry = (XPropertySet)UnoRuntime.queryInterface( 80cdf0e10cSrcweir XPropertySet.class, xMenuElementFactory.createInstance( 81cdf0e10cSrcweir "com.sun.star.ui.ActionTrigger" )); 82cdf0e10cSrcweir xMenuEntry.setPropertyValue( "Text", new String( "Content" )); 83cdf0e10cSrcweir xMenuEntry.setPropertyValue( "CommandURL", new String( "slot:5401" )); 84cdf0e10cSrcweir xMenuEntry.setPropertyValue( "HelpURL", new String( "5401" )); 85cdf0e10cSrcweir 86cdf0e10cSrcweir // insert menu entry to sub menu 87cdf0e10cSrcweir xSubMenuContainer.insertByIndex( 0, (Object)xMenuEntry ); 88cdf0e10cSrcweir 89cdf0e10cSrcweir // intialize help/help agent 90cdf0e10cSrcweir // entry "Help Agent" 91cdf0e10cSrcweir xMenuEntry = (XPropertySet)UnoRuntime.queryInterface( 92cdf0e10cSrcweir XPropertySet.class, 93cdf0e10cSrcweir xMenuElementFactory.createInstance("com.sun.star.ui.ActionTrigger" )); 94cdf0e10cSrcweir xMenuEntry.setPropertyValue( "Text", new String( "Help Agent" )); 95cdf0e10cSrcweir xMenuEntry.setPropertyValue( "CommandURL", new String( "slot:5962" )); 96cdf0e10cSrcweir xMenuEntry.setPropertyValue( "HelpURL", new String( "5962" )); 97cdf0e10cSrcweir 98cdf0e10cSrcweir // insert menu entry to sub menu 99cdf0e10cSrcweir xSubMenuContainer.insertByIndex( 1, (Object)xMenuEntry ); 100cdf0e10cSrcweir // intialize help/tips 101cdf0e10cSrcweir // entry "Tips" 102cdf0e10cSrcweir xMenuEntry = (XPropertySet)UnoRuntime.queryInterface( 103cdf0e10cSrcweir XPropertySet.class, 104cdf0e10cSrcweir xMenuElementFactory.createInstance("com.sun.star.ui.ActionTrigger" )); 105cdf0e10cSrcweir xMenuEntry.setPropertyValue( "Text", new String( "Tips" )); 106cdf0e10cSrcweir xMenuEntry.setPropertyValue( "CommandURL", new String( "slot:5404" )); 107cdf0e10cSrcweir xMenuEntry.setPropertyValue( "HelpURL", new String( "5404" )); 108cdf0e10cSrcweir 109cdf0e10cSrcweir // insert menu entry to sub menu 110cdf0e10cSrcweir xSubMenuContainer.insertByIndex( 2, (Object)xMenuEntry ); 111cdf0e10cSrcweir 112cdf0e10cSrcweir // add separator into the given context menu 113cdf0e10cSrcweir xContextMenu.insertByIndex( 1, (Object)xSeparator ); 114cdf0e10cSrcweir 115cdf0e10cSrcweir // add new sub menu into the given context menu 116cdf0e10cSrcweir xContextMenu.insertByIndex( 1, (Object)xRootMenuEntry ); 117cdf0e10cSrcweir 118cdf0e10cSrcweir // The controller should execute the modified context menu and stop notifying other 119cdf0e10cSrcweir // interceptors. 120cdf0e10cSrcweir return ContextMenuInterceptorAction.EXECUTE_MODIFIED ; 121cdf0e10cSrcweir } 122cdf0e10cSrcweir } catch ( UnknownPropertyException ex ) { 123cdf0e10cSrcweir // do something useful 124cdf0e10cSrcweir // we used a unknown property 125cdf0e10cSrcweir } catch ( IndexOutOfBoundsException ex ) { 126cdf0e10cSrcweir // do something useful 127cdf0e10cSrcweir // we used an invalid index for accessing a container 128cdf0e10cSrcweir } catch ( Exception ex ) { 129cdf0e10cSrcweir // something strange has happend! 130cdf0e10cSrcweir } catch ( Throwable ex ) { 131cdf0e10cSrcweir // catch java exceptions and do something useful 132cdf0e10cSrcweir } 133cdf0e10cSrcweir 134cdf0e10cSrcweir return ContextMenuInterceptorAction.IGNORED; 135cdf0e10cSrcweir } 136cdf0e10cSrcweir }