134dd1e25SAndrew Rist /**************************************************************
234dd1e25SAndrew Rist  *
334dd1e25SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
434dd1e25SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
534dd1e25SAndrew Rist  * distributed with this work for additional information
634dd1e25SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
734dd1e25SAndrew Rist  * to you under the Apache License, Version 2.0 (the
834dd1e25SAndrew Rist  * "License"); you may not use this file except in compliance
934dd1e25SAndrew Rist  * with the License.  You may obtain a copy of the License at
1034dd1e25SAndrew Rist  *
1134dd1e25SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
1234dd1e25SAndrew Rist  *
1334dd1e25SAndrew Rist  * Unless required by applicable law or agreed to in writing,
1434dd1e25SAndrew Rist  * software distributed under the License is distributed on an
1534dd1e25SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
1634dd1e25SAndrew Rist  * KIND, either express or implied.  See the License for the
1734dd1e25SAndrew Rist  * specific language governing permissions and limitations
1834dd1e25SAndrew Rist  * under the License.
1934dd1e25SAndrew Rist  *
2034dd1e25SAndrew Rist  *************************************************************/
2134dd1e25SAndrew Rist 
2234dd1e25SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir import com.sun.star.beans.XPropertySet;
25cdf0e10cSrcweir import com.sun.star.ui.ActionTriggerSeparatorType;
26cdf0e10cSrcweir import com.sun.star.ui.ContextMenuInterceptorAction;
27cdf0e10cSrcweir import com.sun.star.ui.XContextMenuInterceptor;
28cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
29cdf0e10cSrcweir 
30cdf0e10cSrcweir public class ContextMenuInterceptor implements XContextMenuInterceptor {
31cdf0e10cSrcweir 
32cdf0e10cSrcweir     /**
33cdf0e10cSrcweir      *Description of the Method
34cdf0e10cSrcweir      *
35cdf0e10cSrcweir      *@param  args  Description of Parameter
36cdf0e10cSrcweir      *@since
37cdf0e10cSrcweir      */
main(String args[])38cdf0e10cSrcweir     public static void main(String args[])
39cdf0e10cSrcweir     {
40cdf0e10cSrcweir         try {
41cdf0e10cSrcweir 			OfficeConnect aConnect = OfficeConnect.createConnection();
42cdf0e10cSrcweir 
43cdf0e10cSrcweir             com.sun.star.frame.XDesktop xDesktop =
44cdf0e10cSrcweir 			    (com.sun.star.frame.XDesktop)aConnect.createRemoteInstance(
45cdf0e10cSrcweir 			        com.sun.star.frame.XDesktop.class,"com.sun.star.frame.Desktop");
46cdf0e10cSrcweir 
47cdf0e10cSrcweir             // create a new test document
48cdf0e10cSrcweir             com.sun.star.frame.XComponentLoader xCompLoader =
49cdf0e10cSrcweir                 (com.sun.star.frame.XComponentLoader)UnoRuntime.queryInterface(
50cdf0e10cSrcweir                     com.sun.star.frame.XComponentLoader.class, xDesktop);
51cdf0e10cSrcweir 
52cdf0e10cSrcweir             com.sun.star.lang.XComponent xComponent =
53cdf0e10cSrcweir                 xCompLoader.loadComponentFromURL("private:factory/swriter",
54cdf0e10cSrcweir                     "_blank", 0, new com.sun.star.beans.PropertyValue[0]);
55cdf0e10cSrcweir 
56*7f5c89d5SJohn Bampton             // initialize the test document
57cdf0e10cSrcweir             com.sun.star.frame.XFrame xFrame = null;
58cdf0e10cSrcweir             {
59cdf0e10cSrcweir             com.sun.star.text.XTextDocument xDoc =(com.sun.star.text.XTextDocument)
60cdf0e10cSrcweir                 UnoRuntime.queryInterface(com.sun.star.text.XTextDocument.class,
61cdf0e10cSrcweir                                           xComponent);
62cdf0e10cSrcweir 
63cdf0e10cSrcweir             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!");
64cdf0e10cSrcweir             xDoc.getText().setString(infoMsg);
65cdf0e10cSrcweir 
66cdf0e10cSrcweir             // ensure that the document content is optimal visible
67cdf0e10cSrcweir             com.sun.star.frame.XModel xModel =
68cdf0e10cSrcweir                 (com.sun.star.frame.XModel)UnoRuntime.queryInterface(
69cdf0e10cSrcweir                     com.sun.star.frame.XModel.class, xDoc);
70cdf0e10cSrcweir             // get the frame for later usage
71cdf0e10cSrcweir             xFrame = xModel.getCurrentController().getFrame();
72cdf0e10cSrcweir 
73cdf0e10cSrcweir             com.sun.star.view.XViewSettingsSupplier xViewSettings =
74cdf0e10cSrcweir                 (com.sun.star.view.XViewSettingsSupplier)UnoRuntime.queryInterface(
75cdf0e10cSrcweir                     com.sun.star.view.XViewSettingsSupplier.class, xModel.getCurrentController());
76cdf0e10cSrcweir             xViewSettings.getViewSettings().setPropertyValue(
77cdf0e10cSrcweir                 "ZoomType", new Short((short)0));
78cdf0e10cSrcweir             }
79cdf0e10cSrcweir             // test document will be closed later
80cdf0e10cSrcweir 
81cdf0e10cSrcweir             // reuse the frame
82cdf0e10cSrcweir             com.sun.star.frame.XController xController = xFrame.getController();
83cdf0e10cSrcweir             if ( xController != null ) {
84cdf0e10cSrcweir                 com.sun.star.ui.XContextMenuInterception xContextMenuInterception =
85cdf0e10cSrcweir                     (com.sun.star.ui.XContextMenuInterception)UnoRuntime.queryInterface(
86cdf0e10cSrcweir                         com.sun.star.ui.XContextMenuInterception.class, xController );
87cdf0e10cSrcweir                 if( xContextMenuInterception != null ) {
88cdf0e10cSrcweir                     ContextMenuInterceptor aContextMenuInterceptor = new ContextMenuInterceptor();
89cdf0e10cSrcweir                     com.sun.star.ui.XContextMenuInterceptor xContextMenuInterceptor =
90cdf0e10cSrcweir                         (com.sun.star.ui.XContextMenuInterceptor)UnoRuntime.queryInterface(
91cdf0e10cSrcweir                             com.sun.star.ui.XContextMenuInterceptor.class, aContextMenuInterceptor );
92cdf0e10cSrcweir                     xContextMenuInterception.registerContextMenuInterceptor( xContextMenuInterceptor );
93cdf0e10cSrcweir 
94cdf0e10cSrcweir                     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!");
95cdf0e10cSrcweir 
96cdf0e10cSrcweir                     java.io.BufferedReader reader
97cdf0e10cSrcweir                         = new java.io.BufferedReader(new java.io.InputStreamReader(System.in));
98cdf0e10cSrcweir                     reader.read();
99cdf0e10cSrcweir 
100cdf0e10cSrcweir                     xContextMenuInterception.releaseContextMenuInterceptor(
101cdf0e10cSrcweir                         xContextMenuInterceptor );
102cdf0e10cSrcweir                     System.out.println( " ... context menu interceptor removed!" );
103cdf0e10cSrcweir                 }
104cdf0e10cSrcweir             }
105cdf0e10cSrcweir 
106cdf0e10cSrcweir             // close test document
107cdf0e10cSrcweir             com.sun.star.util.XCloseable xCloseable = (com.sun.star.util.XCloseable)
108cdf0e10cSrcweir                 UnoRuntime.queryInterface(com.sun.star.util.XCloseable.class,
109cdf0e10cSrcweir                                           xComponent );
110cdf0e10cSrcweir 
111cdf0e10cSrcweir             if (xCloseable != null ) {
112cdf0e10cSrcweir                 xCloseable.close(false);
113cdf0e10cSrcweir             } else
114cdf0e10cSrcweir             {
115cdf0e10cSrcweir                 xComponent.dispose();
116cdf0e10cSrcweir             }
117cdf0e10cSrcweir         }
118cdf0e10cSrcweir         catch ( com.sun.star.uno.RuntimeException ex ) {
119a893be29SPedro Giffuni             // something strange has happened!
120cdf0e10cSrcweir             System.out.println( " Sample caught exception! " + ex );
121cdf0e10cSrcweir             System.exit(1);
122cdf0e10cSrcweir         }
123cdf0e10cSrcweir         catch ( java.lang.Exception ex ) {
124cdf0e10cSrcweir             // catch java exceptions and do something useful
125cdf0e10cSrcweir             System.out.println( " Sample caught exception! " + ex );
126cdf0e10cSrcweir             System.exit(1);
127cdf0e10cSrcweir         }
128cdf0e10cSrcweir 
129cdf0e10cSrcweir         System.out.println(" ... exit!\n");
130cdf0e10cSrcweir         System.exit( 0 );
131cdf0e10cSrcweir     }
132cdf0e10cSrcweir 
133cdf0e10cSrcweir     /**
134cdf0e10cSrcweir      *Description of the Method
135cdf0e10cSrcweir      *
136cdf0e10cSrcweir      *@param  args  Description of Parameter
137cdf0e10cSrcweir      *@since
138cdf0e10cSrcweir      */
notifyContextMenuExecute( com.sun.star.ui.ContextMenuExecuteEvent aEvent )139cdf0e10cSrcweir     public ContextMenuInterceptorAction notifyContextMenuExecute(
140cdf0e10cSrcweir              com.sun.star.ui.ContextMenuExecuteEvent aEvent ) throws RuntimeException {
141cdf0e10cSrcweir 
142cdf0e10cSrcweir         try {
143cdf0e10cSrcweir 
144cdf0e10cSrcweir             // Retrieve context menu container and query for service factory to
145cdf0e10cSrcweir             // create sub menus, menu entries and separators
146cdf0e10cSrcweir             com.sun.star.container.XIndexContainer xContextMenu = aEvent.ActionTriggerContainer;
147cdf0e10cSrcweir             com.sun.star.lang.XMultiServiceFactory xMenuElementFactory =
148cdf0e10cSrcweir                 (com.sun.star.lang.XMultiServiceFactory)UnoRuntime.queryInterface(
149cdf0e10cSrcweir                 com.sun.star.lang.XMultiServiceFactory.class, xContextMenu );
150cdf0e10cSrcweir             if ( xMenuElementFactory != null ) {
151cdf0e10cSrcweir                 // create root menu entry and sub menu
152cdf0e10cSrcweir                 com.sun.star.beans.XPropertySet xRootMenuEntry =
153cdf0e10cSrcweir                     (XPropertySet)UnoRuntime.queryInterface(
154cdf0e10cSrcweir                         com.sun.star.beans.XPropertySet.class,
155cdf0e10cSrcweir                         xMenuElementFactory.createInstance( "com.sun.star.ui.ActionTrigger" ));
156cdf0e10cSrcweir 
157cdf0e10cSrcweir                 // create a line separator for our new help sub menu
158cdf0e10cSrcweir                 com.sun.star.beans.XPropertySet xSeparator =
159cdf0e10cSrcweir                     (com.sun.star.beans.XPropertySet)UnoRuntime.queryInterface(
160cdf0e10cSrcweir                         com.sun.star.beans.XPropertySet.class,
161cdf0e10cSrcweir                         xMenuElementFactory.createInstance( "com.sun.star.ui.ActionTriggerSeparator" ));
162cdf0e10cSrcweir 
163cdf0e10cSrcweir                 Short aSeparatorType = new Short( ActionTriggerSeparatorType.LINE );
164cdf0e10cSrcweir                 xSeparator.setPropertyValue( "SeparatorType", (Object)aSeparatorType );
165cdf0e10cSrcweir 
166cdf0e10cSrcweir                 // query sub menu for index container to get access
167cdf0e10cSrcweir                 com.sun.star.container.XIndexContainer xSubMenuContainer =
168cdf0e10cSrcweir                     (com.sun.star.container.XIndexContainer)UnoRuntime.queryInterface(
169cdf0e10cSrcweir                         com.sun.star.container.XIndexContainer.class,
170cdf0e10cSrcweir                             xMenuElementFactory.createInstance(
171cdf0e10cSrcweir                                 "com.sun.star.ui.ActionTriggerContainer" ));
172cdf0e10cSrcweir 
173*7f5c89d5SJohn Bampton                 // initialize root menu entry
174cdf0e10cSrcweir                 xRootMenuEntry.setPropertyValue( "Text", new String( "Help" ));
175cdf0e10cSrcweir                 xRootMenuEntry.setPropertyValue( "CommandURL", new String( "slot:5410" ));
176cdf0e10cSrcweir                 xRootMenuEntry.setPropertyValue( "HelpURL", new String( "5410" ));
177cdf0e10cSrcweir                 xRootMenuEntry.setPropertyValue( "SubContainer", (Object)xSubMenuContainer );
178cdf0e10cSrcweir 
179cdf0e10cSrcweir                 // create menu entries for the new sub menu
180cdf0e10cSrcweir 
181*7f5c89d5SJohn Bampton                 // initialize help/content menu entry
182cdf0e10cSrcweir                 XPropertySet xMenuEntry = (XPropertySet)UnoRuntime.queryInterface(
183cdf0e10cSrcweir                                               XPropertySet.class, xMenuElementFactory.createInstance(
184cdf0e10cSrcweir                                                   "com.sun.star.ui.ActionTrigger" ));
185cdf0e10cSrcweir 
186cdf0e10cSrcweir                 xMenuEntry.setPropertyValue( "Text", new String( "Content" ));
187cdf0e10cSrcweir                 xMenuEntry.setPropertyValue( "CommandURL", new String( "slot:5401" ));
188cdf0e10cSrcweir                 xMenuEntry.setPropertyValue( "HelpURL", new String( "5401" ));
189cdf0e10cSrcweir 
190cdf0e10cSrcweir                 // insert menu entry to sub menu
191cdf0e10cSrcweir                 xSubMenuContainer.insertByIndex( 0, (Object)xMenuEntry );
192cdf0e10cSrcweir 
193*7f5c89d5SJohn Bampton                 // initialize help/help agent
194cdf0e10cSrcweir                 xMenuEntry = (com.sun.star.beans.XPropertySet)UnoRuntime.queryInterface(
195cdf0e10cSrcweir                                  com.sun.star.beans.XPropertySet.class,
196cdf0e10cSrcweir                                      xMenuElementFactory.createInstance(
197cdf0e10cSrcweir                                          "com.sun.star.ui.ActionTrigger" ));
198cdf0e10cSrcweir                 xMenuEntry.setPropertyValue( "Text", new String( "Help Agent" ));
199cdf0e10cSrcweir                 xMenuEntry.setPropertyValue( "CommandURL", new String( "slot:5962" ));
200cdf0e10cSrcweir                 xMenuEntry.setPropertyValue( "HelpURL", new String( "5962" ));
201cdf0e10cSrcweir 
202cdf0e10cSrcweir                 // insert menu entry to sub menu
203cdf0e10cSrcweir                 xSubMenuContainer.insertByIndex( 1, (Object)xMenuEntry );
204cdf0e10cSrcweir 
205*7f5c89d5SJohn Bampton                 // initialize help/tips
206cdf0e10cSrcweir                 xMenuEntry = (com.sun.star.beans.XPropertySet)UnoRuntime.queryInterface(
207cdf0e10cSrcweir                                  com.sun.star.beans.XPropertySet.class,
208cdf0e10cSrcweir                                      xMenuElementFactory.createInstance(
209cdf0e10cSrcweir                                          "com.sun.star.ui.ActionTrigger" ));
210cdf0e10cSrcweir                 xMenuEntry.setPropertyValue( "Text", new String( "Tips" ));
211cdf0e10cSrcweir                 xMenuEntry.setPropertyValue( "CommandURL", new String( "slot:5404" ));
212cdf0e10cSrcweir                 xMenuEntry.setPropertyValue( "HelpURL", new String( "5404" ));
213cdf0e10cSrcweir 
214cdf0e10cSrcweir                 // insert menu entry to sub menu
215cdf0e10cSrcweir                 xSubMenuContainer.insertByIndex( 2, (Object)xMenuEntry );
216cdf0e10cSrcweir 
217cdf0e10cSrcweir                 // add separator into the given context menu
218cdf0e10cSrcweir                 xContextMenu.insertByIndex( 0, (Object)xSeparator );
219cdf0e10cSrcweir 
220cdf0e10cSrcweir                 // add new sub menu into the given context menu
221cdf0e10cSrcweir                 xContextMenu.insertByIndex( 0, (Object)xRootMenuEntry );
222cdf0e10cSrcweir 
223cdf0e10cSrcweir                 // The controller should execute the modified context menu and stop notifying other
224cdf0e10cSrcweir                 // interceptors.
225cdf0e10cSrcweir                 return com.sun.star.ui.ContextMenuInterceptorAction.EXECUTE_MODIFIED;
226cdf0e10cSrcweir             }
227cdf0e10cSrcweir         }
228cdf0e10cSrcweir         catch ( com.sun.star.beans.UnknownPropertyException ex ) {
229cdf0e10cSrcweir             // do something useful
230cdf0e10cSrcweir             // we used a unknown property
231cdf0e10cSrcweir         }
232cdf0e10cSrcweir         catch ( com.sun.star.lang.IndexOutOfBoundsException ex ) {
233cdf0e10cSrcweir             // do something useful
234cdf0e10cSrcweir             // we used an invalid index for accessing a container
235cdf0e10cSrcweir         }
236cdf0e10cSrcweir         catch ( com.sun.star.uno.Exception ex ) {
237a893be29SPedro Giffuni             // something strange has happened!
238cdf0e10cSrcweir         }
239cdf0e10cSrcweir         catch ( java.lang.Exception ex ) {
240cdf0e10cSrcweir             // catch java exceptions and something useful
241cdf0e10cSrcweir         }
242cdf0e10cSrcweir 
243cdf0e10cSrcweir         return com.sun.star.ui.ContextMenuInterceptorAction.IGNORED;
244cdf0e10cSrcweir     }
245cdf0e10cSrcweir }
246