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