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