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