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 .
19 package complex
.contextMenuInterceptor
;
21 import com
.sun
.star
.ui
.*;
22 import com
.sun
.star
.beans
.XPropertySet
;
23 import com
.sun
.star
.uno
.UnoRuntime
;
25 public class ContextMenuInterceptor
implements XContextMenuInterceptor
28 private final com
.sun
.star
.awt
.XBitmap myBitmap
;
30 public ContextMenuInterceptor(com
.sun
.star
.awt
.XBitmap aBitmap
)
35 public ContextMenuInterceptorAction
notifyContextMenuExecute(
36 com
.sun
.star
.ui
.ContextMenuExecuteEvent aEvent
) throws RuntimeException
40 // Retrieve context menu container and query for service factory to
41 // create sub menus, menu entries and separators
42 com
.sun
.star
.container
.XIndexContainer xContextMenu
= aEvent
.ActionTriggerContainer
;
43 com
.sun
.star
.lang
.XMultiServiceFactory xMenuElementFactory
=
44 UnoRuntime
.queryInterface(com
.sun
.star
.lang
.XMultiServiceFactory
.class, xContextMenu
);
46 if (xMenuElementFactory
!= null)
49 // create root menu entry for sub menu and sub menu
50 com
.sun
.star
.beans
.XPropertySet xRootMenuEntry
=
51 UnoRuntime
.queryInterface(com
.sun
.star
.beans
.XPropertySet
.class, xMenuElementFactory
.createInstance("com.sun.star.ui.ActionTrigger"));
53 // create a line separator for our new help sub menu
54 com
.sun
.star
.beans
.XPropertySet xSeparator
=
55 UnoRuntime
.queryInterface(com
.sun
.star
.beans
.XPropertySet
.class, 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 com
.sun
.star
.container
.XIndexContainer xSubMenuContainer
=
61 UnoRuntime
.queryInterface(com
.sun
.star
.container
.XIndexContainer
.class, xMenuElementFactory
.createInstance("com.sun.star.ui.ActionTriggerContainer"));
63 // initialize root menu entry "Help"
64 xRootMenuEntry
.setPropertyValue("Text", "Help");
65 xRootMenuEntry
.setPropertyValue("CommandURL", ".uno:HelpMenu");
66 xRootMenuEntry
.setPropertyValue("HelpURL", "5410");
67 xRootMenuEntry
.setPropertyValue("SubContainer", xSubMenuContainer
);
68 xRootMenuEntry
.setPropertyValue("Image", myBitmap
);
70 // create menu entries for the new sub menu
71 // initialize help/content menu entry
73 XPropertySet xMenuEntry
= UnoRuntime
.queryInterface(XPropertySet
.class, xMenuElementFactory
.createInstance("com.sun.star.ui.ActionTrigger"));
74 xMenuEntry
.setPropertyValue("Text", "Content");
75 xMenuEntry
.setPropertyValue("CommandURL", ".uno:HelpIndex");
76 xMenuEntry
.setPropertyValue("HelpURL", "5401");
78 // insert menu entry to sub menu
79 xSubMenuContainer
.insertByIndex(0, xMenuEntry
);
81 // initialize help/help on help
82 // entry "Help on Help"
83 xMenuEntry
= UnoRuntime
.queryInterface(com
.sun
.star
.beans
.XPropertySet
.class, xMenuElementFactory
.createInstance("com.sun.star.ui.ActionTrigger"));
84 xMenuEntry
.setPropertyValue("Text", "Help on Help");
85 xMenuEntry
.setPropertyValue("CommandURL", ".uno:HelpOnHelp");
86 xMenuEntry
.setPropertyValue("HelpURL", "5400");
88 // insert menu entry to sub menu
89 xSubMenuContainer
.insertByIndex(1, xMenuEntry
);
90 // initialize help/tips
92 xMenuEntry
= UnoRuntime
.queryInterface(com
.sun
.star
.beans
.XPropertySet
.class, xMenuElementFactory
.createInstance("com.sun.star.ui.ActionTrigger"));
93 xMenuEntry
.setPropertyValue("Text", "Tips");
94 xMenuEntry
.setPropertyValue("CommandURL", ".uno:HelpTip");
95 xMenuEntry
.setPropertyValue("HelpURL", "5404");
97 // insert menu entry to sub menu
98 xSubMenuContainer
.insertByIndex(2, xMenuEntry
);
100 // add separator into the given context menu
101 xContextMenu
.insertByIndex(0, xSeparator
);
103 // add new sub menu into the given context menu
104 xContextMenu
.insertByIndex(0, xRootMenuEntry
);
106 // The controller should execute the modified context menu and stop notifying other
108 return com
.sun
.star
.ui
.ContextMenuInterceptorAction
.EXECUTE_MODIFIED
;
111 catch (com
.sun
.star
.beans
.UnknownPropertyException ex
)
113 // do something useful
114 // we used an unknown property
116 catch (com
.sun
.star
.lang
.IndexOutOfBoundsException ex
)
118 // do something useful
119 // we used an invalid index for accessing a container
121 catch (com
.sun
.star
.uno
.Exception ex
)
123 // something strange has happened!
125 catch (java
.lang
.Throwable ex
)
127 // catch java exceptions do something useful
130 return com
.sun
.star
.ui
.ContextMenuInterceptorAction
.IGNORED
;