1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * The Contents of this file are made available subject to the terms of
7 * Copyright 2000, 2010 Oracle and/or its affiliates.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
29 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
30 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
31 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
32 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *************************************************************************/
36 import com
.sun
.star
.beans
.XPropertySet
;
37 import com
.sun
.star
.ui
.ActionTriggerSeparatorType
;
38 import com
.sun
.star
.ui
.ContextMenuInterceptorAction
;
39 import com
.sun
.star
.ui
.XContextMenuInterceptor
;
40 import com
.sun
.star
.uno
.UnoRuntime
;
42 public class ContextMenuInterceptor
implements XContextMenuInterceptor
{
45 *Description of the Method
47 *@param args Description of Parameter
49 public static void main(String args
[])
52 OfficeConnect aConnect
= OfficeConnect
.createConnection();
54 com
.sun
.star
.frame
.XDesktop xDesktop
=
55 aConnect
.createRemoteInstance(
56 com
.sun
.star
.frame
.XDesktop
.class,"com.sun.star.frame.Desktop");
58 // create a new test document
59 com
.sun
.star
.frame
.XComponentLoader xCompLoader
=
60 UnoRuntime
.queryInterface(
61 com
.sun
.star
.frame
.XComponentLoader
.class, xDesktop
);
63 com
.sun
.star
.lang
.XComponent xComponent
=
64 xCompLoader
.loadComponentFromURL("private:factory/swriter",
65 "_blank", 0, new com
.sun
.star
.beans
.PropertyValue
[0]);
67 // initialize the test document
68 com
.sun
.star
.frame
.XFrame xFrame
= null;
70 com
.sun
.star
.text
.XTextDocument xDoc
=UnoRuntime
.queryInterface(com
.sun
.star
.text
.XTextDocument
.class,
73 String infoMsg
= "All context menus of the created document frame contains now a 'Help' entry with the submenus 'Content', 'Help on Help' and 'Tips'.\n\nPress 'Return' in the shell to remove the context menu interceptor and finish the example!";
74 xDoc
.getText().setString(infoMsg
);
76 // ensure that the document content is optimal visible
77 com
.sun
.star
.frame
.XModel xModel
=
78 UnoRuntime
.queryInterface(
79 com
.sun
.star
.frame
.XModel
.class, xDoc
);
80 // get the frame for later usage
81 xFrame
= xModel
.getCurrentController().getFrame();
83 com
.sun
.star
.view
.XViewSettingsSupplier xViewSettings
=
84 UnoRuntime
.queryInterface(
85 com
.sun
.star
.view
.XViewSettingsSupplier
.class, xModel
.getCurrentController());
86 xViewSettings
.getViewSettings().setPropertyValue(
87 "ZoomType", Short
.valueOf((short)0));
89 // test document will be closed later
92 com
.sun
.star
.frame
.XController xController
= xFrame
.getController();
93 if ( xController
!= null ) {
94 com
.sun
.star
.ui
.XContextMenuInterception xContextMenuInterception
=
95 UnoRuntime
.queryInterface(
96 com
.sun
.star
.ui
.XContextMenuInterception
.class, xController
);
97 if( xContextMenuInterception
!= null ) {
98 ContextMenuInterceptor aContextMenuInterceptor
= new ContextMenuInterceptor();
99 com
.sun
.star
.ui
.XContextMenuInterceptor xContextMenuInterceptor
=
100 UnoRuntime
.queryInterface(
101 com
.sun
.star
.ui
.XContextMenuInterceptor
.class, aContextMenuInterceptor
);
102 xContextMenuInterception
.registerContextMenuInterceptor( xContextMenuInterceptor
);
104 System
.out
.println( "\n ... all context menus of the created document frame contains now a 'Help' entry with the\n submenus 'Content', 'Help on Help' and 'Tips'.\n\nPress 'Return' to remove the context menu interceptor and finish the example!");
106 java
.io
.BufferedReader reader
107 = new java
.io
.BufferedReader(new java
.io
.InputStreamReader(System
.in
));
110 xContextMenuInterception
.releaseContextMenuInterceptor(
111 xContextMenuInterceptor
);
112 System
.out
.println( " ... context menu interceptor removed!" );
116 // close test document
117 com
.sun
.star
.util
.XCloseable xCloseable
= UnoRuntime
.queryInterface(com
.sun
.star
.util
.XCloseable
.class,
120 if (xCloseable
!= null ) {
121 xCloseable
.close(false);
124 xComponent
.dispose();
127 catch ( com
.sun
.star
.uno
.RuntimeException ex
) {
128 // something strange has happened!
129 System
.out
.println( " Sample caught exception! " + ex
);
132 catch ( java
.lang
.Exception ex
) {
133 // catch java exceptions and do something useful
134 System
.out
.println( " Sample caught exception! " + ex
);
138 System
.out
.println(" ... exit!\n");
143 *Description of the Method
145 public ContextMenuInterceptorAction
notifyContextMenuExecute(
146 com
.sun
.star
.ui
.ContextMenuExecuteEvent aEvent
) throws RuntimeException
{
150 // Retrieve context menu container and query for service factory to
151 // create sub menus, menu entries and separators
152 com
.sun
.star
.container
.XIndexContainer xContextMenu
= aEvent
.ActionTriggerContainer
;
153 com
.sun
.star
.lang
.XMultiServiceFactory xMenuElementFactory
=
154 UnoRuntime
.queryInterface(
155 com
.sun
.star
.lang
.XMultiServiceFactory
.class, xContextMenu
);
156 if ( xMenuElementFactory
!= null ) {
157 // create root menu entry and sub menu
158 com
.sun
.star
.beans
.XPropertySet xRootMenuEntry
=
159 UnoRuntime
.queryInterface(
160 com
.sun
.star
.beans
.XPropertySet
.class,
161 xMenuElementFactory
.createInstance( "com.sun.star.ui.ActionTrigger" ));
163 // create a line separator for our new help sub menu
164 com
.sun
.star
.beans
.XPropertySet xSeparator
=
165 UnoRuntime
.queryInterface(
166 com
.sun
.star
.beans
.XPropertySet
.class,
167 xMenuElementFactory
.createInstance( "com.sun.star.ui.ActionTriggerSeparator" ));
169 Short aSeparatorType
= Short
.valueOf( ActionTriggerSeparatorType
.LINE
);
170 xSeparator
.setPropertyValue( "SeparatorType", aSeparatorType
);
172 // query sub menu for index container to get access
173 com
.sun
.star
.container
.XIndexContainer xSubMenuContainer
=
174 UnoRuntime
.queryInterface(
175 com
.sun
.star
.container
.XIndexContainer
.class,
176 xMenuElementFactory
.createInstance(
177 "com.sun.star.ui.ActionTriggerContainer" ));
179 // initialize root menu entry
180 xRootMenuEntry
.setPropertyValue( "Text", "Help");
181 xRootMenuEntry
.setPropertyValue( "CommandURL", "slot:5410");
182 xRootMenuEntry
.setPropertyValue( "HelpURL", "5410");
183 xRootMenuEntry
.setPropertyValue( "SubContainer", xSubMenuContainer
);
185 // create menu entries for the new sub menu
187 // initialize help/content menu entry
188 XPropertySet xMenuEntry
= UnoRuntime
.queryInterface(
189 XPropertySet
.class, xMenuElementFactory
.createInstance(
190 "com.sun.star.ui.ActionTrigger" ));
192 xMenuEntry
.setPropertyValue( "Text", "Content" );
193 xMenuEntry
.setPropertyValue( "CommandURL", "slot:5401" );
194 xMenuEntry
.setPropertyValue( "HelpURL", "5401" );
196 // insert menu entry to sub menu
197 xSubMenuContainer
.insertByIndex( 0, xMenuEntry
);
199 // initialize help/help on help
200 xMenuEntry
= UnoRuntime
.queryInterface(
201 com
.sun
.star
.beans
.XPropertySet
.class,
202 xMenuElementFactory
.createInstance(
203 "com.sun.star.ui.ActionTrigger" ));
204 xMenuEntry
.setPropertyValue("Text", "Help on Help");
205 xMenuEntry
.setPropertyValue("CommandURL", "slot:5400");
206 xMenuEntry
.setPropertyValue("HelpURL", "5400");
208 // insert menu entry to sub menu
209 xSubMenuContainer
.insertByIndex( 1, xMenuEntry
);
211 // initialize help/tips
212 xMenuEntry
= UnoRuntime
.queryInterface(
213 com
.sun
.star
.beans
.XPropertySet
.class,
214 xMenuElementFactory
.createInstance(
215 "com.sun.star.ui.ActionTrigger" ));
216 xMenuEntry
.setPropertyValue( "Text", "Tips" );
217 xMenuEntry
.setPropertyValue( "CommandURL", "slot:5404" );
218 xMenuEntry
.setPropertyValue( "HelpURL", "5404" );
220 // insert menu entry to sub menu
221 xSubMenuContainer
.insertByIndex( 2, xMenuEntry
);
223 // add separator into the given context menu
224 xContextMenu
.insertByIndex( 0, xSeparator
);
226 // add new sub menu into the given context menu
227 xContextMenu
.insertByIndex( 0, xRootMenuEntry
);
229 // The controller should execute the modified context menu and stop notifying other
231 return com
.sun
.star
.ui
.ContextMenuInterceptorAction
.EXECUTE_MODIFIED
;
234 catch ( com
.sun
.star
.beans
.UnknownPropertyException ex
) {
235 // do something useful
236 // we used an unknown property
238 catch ( com
.sun
.star
.lang
.IndexOutOfBoundsException ex
) {
239 // do something useful
240 // we used an invalid index for accessing a container
242 catch ( com
.sun
.star
.uno
.Exception ex
) {
243 // something strange has happened!
245 catch ( java
.lang
.Exception ex
) {
246 // catch java exceptions and something useful
249 return com
.sun
.star
.ui
.ContextMenuInterceptorAction
.IGNORED
;
253 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */