2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
22 import com
.sun
.star
.beans
.UnknownPropertyException
;
23 import com
.sun
.star
.beans
.XPropertySet
;
24 import com
.sun
.star
.container
.XIndexContainer
;
25 import com
.sun
.star
.lang
.XMultiServiceFactory
;
26 import com
.sun
.star
.ui
.ActionTriggerSeparatorType
;
27 import com
.sun
.star
.ui
.ContextMenuInterceptorAction
;
28 import com
.sun
.star
.ui
.XContextMenuInterceptor
;
29 import com
.sun
.star
.uno
.UnoRuntime
;
31 public class ContextMenuInterceptor
implements XContextMenuInterceptor
{
33 public ContextMenuInterceptorAction
notifyContextMenuExecute(
34 com
.sun
.star
.ui
.ContextMenuExecuteEvent aEvent
) throws RuntimeException
{
36 // Retrieve context menu container and query for service factory to
37 // create sub menus, menu entries and separators
38 XIndexContainer xContextMenu
= aEvent
.ActionTriggerContainer
;
39 XMultiServiceFactory xMenuElementFactory
=
40 UnoRuntime
.queryInterface(
41 XMultiServiceFactory
.class, xContextMenu
);
43 if ( xMenuElementFactory
!= null ) {
45 // create root menu entry for sub menu and sub menu
46 XPropertySet xRootMenuEntry
=
47 UnoRuntime
.queryInterface(
49 xMenuElementFactory
.createInstance("com.sun.star.ui.ActionTrigger" ));
51 // create a line separator for our new help sub menu
52 XPropertySet xSeparator
=
53 UnoRuntime
.queryInterface(
55 xMenuElementFactory
.createInstance("com.sun.star.ui.ActionTriggerSeparator" ) );
56 Short aSeparatorType
= Short
.valueOf( ActionTriggerSeparatorType
.LINE
);
57 xSeparator
.setPropertyValue( "SeparatorType", aSeparatorType
);
59 // query sub menu for index container to get access
60 XIndexContainer xSubMenuContainer
=
61 UnoRuntime
.queryInterface(
62 XIndexContainer
.class,
63 xMenuElementFactory
.createInstance("com.sun.star.ui.ActionTriggerContainer" ));
65 // initialize root menu entry "Help"
66 xRootMenuEntry
.setPropertyValue( "Text", "Help" );
67 xRootMenuEntry
.setPropertyValue( "CommandURL", "slot:5410" );
68 xRootMenuEntry
.setPropertyValue( "HelpURL", "5410" );
69 xRootMenuEntry
.setPropertyValue( "SubContainer", xSubMenuContainer
);
71 // create menu entries for the new sub menu
72 // initialize help/content menu entry
74 XPropertySet xMenuEntry
= UnoRuntime
.queryInterface(
75 XPropertySet
.class, xMenuElementFactory
.createInstance(
76 "com.sun.star.ui.ActionTrigger" ));
77 xMenuEntry
.setPropertyValue( "Text", "Content" );
78 xMenuEntry
.setPropertyValue( "CommandURL", "slot:5401" );
79 xMenuEntry
.setPropertyValue( "HelpURL", "5401" );
81 // insert menu entry to sub menu
82 xSubMenuContainer
.insertByIndex( 0, xMenuEntry
);
84 // initialize help/help on help
85 // entry "Help on Help"
86 xMenuEntry
= UnoRuntime
.queryInterface(
88 xMenuElementFactory
.createInstance("com.sun.star.ui.ActionTrigger" ));
89 xMenuEntry
.setPropertyValue("Text", "Help on Help");
90 xMenuEntry
.setPropertyValue("CommandURL", "slot:5400");
91 xMenuEntry
.setPropertyValue("HelpURL", "5400");
93 // insert menu entry to sub menu
94 xSubMenuContainer
.insertByIndex( 1, xMenuEntry
);
95 // initialize help/tips
97 xMenuEntry
= UnoRuntime
.queryInterface(
99 xMenuElementFactory
.createInstance("com.sun.star.ui.ActionTrigger" ));
100 xMenuEntry
.setPropertyValue( "Text", "Tips" );
101 xMenuEntry
.setPropertyValue( "CommandURL", "slot:5404" );
102 xMenuEntry
.setPropertyValue( "HelpURL", "5404" );
104 // insert menu entry to sub menu
105 xSubMenuContainer
.insertByIndex( 2, xMenuEntry
);
107 // add separator into the given context menu
108 xContextMenu
.insertByIndex( 1, xSeparator
);
110 // add new sub menu into the given context menu
111 xContextMenu
.insertByIndex( 1, xRootMenuEntry
);
113 // The controller should execute the modified context menu and stop notifying other
115 return ContextMenuInterceptorAction
.EXECUTE_MODIFIED
;
117 } catch ( UnknownPropertyException ex
) {
118 // do something useful
119 // we used a unknown property
120 } catch ( IndexOutOfBoundsException ex
) {
121 // do something useful
122 // we used an invalid index for accessing a container
123 } catch ( Exception ex
) {
124 // something strange has happened!
125 } catch ( Throwable ex
) {
126 // catch java exceptions and do something useful
129 return ContextMenuInterceptorAction
.IGNORED
;