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