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