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