1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 24 import com.sun.star.uno.UnoRuntime; 25 import com.sun.star.uno.XComponentContext; 26 import com.sun.star.lang.XMultiComponentFactory; 27 import com.sun.star.beans.XPropertySet; 28 import com.sun.star.beans.PropertyValue; 29 import com.sun.star.lang.XMultiServiceFactory; 30 import com.sun.star.lang.XSingleServiceFactory; 31 import com.sun.star.util.XURLTransformer; 32 import com.sun.star.frame.XDesktop; 33 import com.sun.star.frame.XComponentLoader; 34 import com.sun.star.text.XTextDocument; 35 36 /* 37 * 38 * @author Carsten Driesner 39 * Provides example code how to enable/disable 40 * commands. 41 */ 42 public class DisableCommandsTest extends java.lang.Object { 43 44 /* 45 * A list of command names 46 */ 47 final static private String[] aCommandURLTestSet = 48 { 49 new String( "Open" ), 50 new String( "About" ), 51 new String( "SelectAll" ), 52 new String( "Quit" ), 53 }; 54 55 private static XComponentContext xRemoteContext = null; 56 private static XMultiComponentFactory xRemoteServiceManager = null; 57 private static XURLTransformer xTransformer = null; 58 private static XMultiServiceFactory xConfigProvider = null; 59 60 /* 61 * @param args the command line arguments 62 */ main(String[] args)63 public static void main(String[] args) { 64 65 try { 66 // get the remote office context. If necessary a new office 67 // process is started 68 xRemoteContext = com.sun.star.comp.helper.Bootstrap.bootstrap(); 69 System.out.println("Connected to a running office ..."); 70 xRemoteServiceManager = xRemoteContext.getServiceManager(); 71 72 Object transformer = xRemoteServiceManager.createInstanceWithContext( 73 "com.sun.star.util.URLTransformer", xRemoteContext ); 74 xTransformer = (com.sun.star.util.XURLTransformer) 75 UnoRuntime.queryInterface(com.sun.star.util.XURLTransformer.class, 76 transformer ); 77 78 Object configProvider = xRemoteServiceManager.createInstanceWithContext( 79 "com.sun.star.configuration.ConfigurationProvider", 80 xRemoteContext ); 81 xConfigProvider = (com.sun.star.lang.XMultiServiceFactory) 82 UnoRuntime.queryInterface( 83 com.sun.star.lang.XMultiServiceFactory.class, configProvider ); 84 85 // create a new test document 86 Object oDesktop = xRemoteServiceManager.createInstanceWithContext( 87 "com.sun.star.frame.Desktop", xRemoteContext); 88 89 XComponentLoader xCompLoader =(XComponentLoader) 90 UnoRuntime.queryInterface(XComponentLoader.class, oDesktop); 91 92 com.sun.star.lang.XComponent xComponent = 93 xCompLoader.loadComponentFromURL("private:factory/swriter", 94 "_blank", 0, new com.sun.star.beans.PropertyValue[0]); 95 { 96 XTextDocument xDoc =(XTextDocument) 97 UnoRuntime.queryInterface(XTextDocument.class, xComponent); 98 xDoc.getText().setString("You can now check the disabled commands. The " 99 +"following commands are disabled:\n\n" 100 +" Open...\n Exit\n Select All\n " 101 +"About StarOffice|OpenOffice\n\nPress " 102 + "\"return\" in the shell where you have " 103 + "started the example to enable the " 104 + "commands!\n\nCheck the commands again and " 105 + "press once more \"return\" to finish the " 106 + "example and close the document."); 107 108 // ensure that the document content is optimal visible 109 com.sun.star.frame.XModel xModel = 110 (com.sun.star.frame.XModel)UnoRuntime.queryInterface( 111 com.sun.star.frame.XModel.class, xDoc); 112 // get the frame for later usage 113 com.sun.star.frame.XFrame xFrame = 114 xModel.getCurrentController().getFrame(); 115 116 com.sun.star.view.XViewSettingsSupplier xViewSettings = 117 (com.sun.star.view.XViewSettingsSupplier)UnoRuntime.queryInterface( 118 com.sun.star.view.XViewSettingsSupplier.class, 119 xModel.getCurrentController()); 120 xViewSettings.getViewSettings().setPropertyValue( 121 "ZoomType", new Short((short)0)); 122 } 123 // test document will be closed later 124 125 // First we need a defined starting point. So we have to remove 126 // all commands from the disabled set! 127 enableCommands(); 128 129 // Check if the commands are usable 130 testCommands( false ); 131 132 // Disable the commands 133 disableCommands(); 134 135 // Now the commands shouldn't be usable anymore 136 testCommands( true ); 137 138 // you can now check the test document and see which commands are 139 // disabled 140 System.out.println("\nYou can now check the disabled commands.\n" 141 +"Please press 'return' to enable the commands!"); 142 waitForUserInput(); 143 144 // Remove disable commands to make Office usable again 145 enableCommands(); 146 147 // you can check the test document again and see that the commands 148 // are enabled now 149 System.out.println("Check again the now enabled commands.\n" 150 +"Please press 'return' to finish the example and " 151 +"close the document!"); 152 waitForUserInput(); 153 154 // close test document 155 com.sun.star.util.XCloseable xCloseable = (com.sun.star.util.XCloseable) 156 UnoRuntime.queryInterface(com.sun.star.util.XCloseable.class, 157 xComponent ); 158 159 if (xCloseable != null ) { 160 xCloseable.close(false); 161 } else 162 { 163 xComponent.dispose(); 164 } 165 } 166 catch (java.lang.Exception e){ 167 e.printStackTrace(); 168 } 169 finally { 170 System.exit(0); 171 } 172 } 173 174 /** 175 * Wait for user input -> until the user press 'return' 176 */ waitForUserInput()177 private static void waitForUserInput() throws java.io.IOException { 178 179 java.io.BufferedReader reader 180 = new java.io.BufferedReader(new java.io.InputStreamReader(System.in)); 181 182 reader.read(); 183 } 184 185 /** 186 * Test the commands that we enabled/disabled 187 */ testCommands( boolean bDisabledCmds )188 private static void testCommands( boolean bDisabledCmds ) 189 throws com.sun.star.uno.Exception 190 { 191 // We need the desktop to get access to the current frame 192 Object desktop = xRemoteServiceManager.createInstanceWithContext( 193 "com.sun.star.frame.Desktop", xRemoteContext ); 194 com.sun.star.frame.XDesktop xDesktop = (com.sun.star.frame.XDesktop) 195 UnoRuntime.queryInterface(com.sun.star.frame.XDesktop.class, desktop ); 196 com.sun.star.frame.XFrame xFrame = xDesktop.getCurrentFrame(); 197 com.sun.star.frame.XDispatchProvider xDispatchProvider = null; 198 if ( xFrame != null ) 199 { 200 // We have a frame. Now we need access to the dispatch provider. 201 xDispatchProvider = 202 (com.sun.star.frame.XDispatchProvider)UnoRuntime.queryInterface( 203 com.sun.star.frame.XDispatchProvider.class, xFrame ); 204 if ( xDispatchProvider != null ) 205 { 206 // As we have the dispatch provider we can now check if we get 207 // a dispatch object or not. 208 for ( int n = 0; n < aCommandURLTestSet.length; n++ ) 209 { 210 // Prepare the URL 211 com.sun.star.util.URL[] aURL = new com.sun.star.util.URL[1]; 212 aURL[0] = new com.sun.star.util.URL(); 213 com.sun.star.frame.XDispatch xDispatch = null; 214 215 aURL[0].Complete = ".uno:" + aCommandURLTestSet[n]; 216 xTransformer.parseSmart( aURL, ".uno:" ); 217 218 // Try to get a dispatch object for our URL 219 xDispatch = xDispatchProvider.queryDispatch( aURL[0], "", 0 ); 220 221 if ( xDispatch != null ) 222 { 223 if ( bDisabledCmds ) 224 System.out.println( 225 "Something is wrong, I got dispatch object for " 226 + aURL[0].Complete ); 227 else 228 System.out.println( "Ok, dispatch object for " 229 + aURL[0].Complete ); 230 } 231 else 232 { 233 if ( !bDisabledCmds ) 234 System.out.println("Something is wrong, I cannot get dispatch object for " + aURL[0].Complete ); 235 else 236 System.out.println( "Ok, no dispatch object for " 237 + aURL[0].Complete ); 238 } 239 resetURL( aURL[0] ); 240 } 241 } 242 else 243 System.out.println( "Couldn't get XDispatchProvider from Frame!" ); 244 } 245 else 246 System.out.println( "Couldn't get current Frame from Desktop!" ); 247 } 248 249 /** 250 * Ensure that there are no disabled commands in the user layer. The 251 * implementation removes all commands from the disabled set! 252 */ enableCommands()253 private static void enableCommands() { 254 // Set the root path for our configuration access 255 com.sun.star.beans.PropertyValue[] lParams = 256 new com.sun.star.beans.PropertyValue[1]; 257 258 lParams[0] = new com.sun.star.beans.PropertyValue(); 259 lParams[0].Name = new String("nodepath"); 260 lParams[0].Value = "/org.openoffice.Office.Commands/Execute/Disabled"; 261 262 try { 263 // Create configuration update access to have write access to the 264 // configuration 265 Object xAccess = xConfigProvider.createInstanceWithArguments( 266 "com.sun.star.configuration.ConfigurationUpdateAccess", 267 lParams ); 268 269 com.sun.star.container.XNameAccess xNameAccess = 270 (com.sun.star.container.XNameAccess)UnoRuntime.queryInterface( 271 com.sun.star.container.XNameAccess.class, xAccess ); 272 273 if ( xNameAccess != null ) { 274 // We need the XNameContainer interface to remove the nodes by name 275 com.sun.star.container.XNameContainer xNameContainer = 276 (com.sun.star.container.XNameContainer) 277 UnoRuntime.queryInterface( 278 com.sun.star.container.XNameContainer.class, xAccess ); 279 280 // Retrieves the names of all Disabled nodes 281 String[] aCommandsSeq = xNameAccess.getElementNames(); 282 for ( int n = 0; n < aCommandsSeq.length; n++ ) { 283 try { 284 // remove the node 285 xNameContainer.removeByName( aCommandsSeq[n] ); 286 } 287 catch ( com.sun.star.lang.WrappedTargetException e ) { 288 } 289 catch ( com.sun.star.container.NoSuchElementException e ) { 290 } 291 } 292 } 293 294 // Commit our changes 295 com.sun.star.util.XChangesBatch xFlush = 296 (com.sun.star.util.XChangesBatch)UnoRuntime.queryInterface( 297 com.sun.star.util.XChangesBatch.class, xAccess); 298 299 xFlush.commitChanges(); 300 } 301 catch ( com.sun.star.uno.Exception e ) { 302 System.out.println( "Exception detected!" ); 303 System.out.println( e ); 304 } 305 } 306 307 /** 308 * Disable all commands defined in the aCommandURLTestSet array 309 */ disableCommands()310 private static void disableCommands() { 311 // Set the root path for our configuration access 312 com.sun.star.beans.PropertyValue[] lParams = 313 new com.sun.star.beans.PropertyValue[1]; 314 315 lParams[0] = new com.sun.star.beans.PropertyValue(); 316 lParams[0].Name = new String("nodepath"); 317 lParams[0].Value = "/org.openoffice.Office.Commands/Execute/Disabled"; 318 319 try { 320 // Create configuration update access to have write access to the 321 // configuration 322 Object xAccess = xConfigProvider.createInstanceWithArguments( 323 "com.sun.star.configuration.ConfigurationUpdateAccess", 324 lParams ); 325 326 com.sun.star.lang.XSingleServiceFactory xSetElementFactory = 327 (com.sun.star.lang.XSingleServiceFactory)UnoRuntime.queryInterface( 328 com.sun.star.lang.XSingleServiceFactory.class, xAccess ); 329 330 com.sun.star.container.XNameContainer xNameContainer = 331 (com.sun.star.container.XNameContainer)UnoRuntime.queryInterface( 332 com.sun.star.container.XNameContainer.class, xAccess ); 333 334 if ( xSetElementFactory != null && xNameContainer != null ) { 335 Object[] aArgs = new Object[0]; 336 337 for ( int i = 0; i < aCommandURLTestSet.length; i++ ) { 338 // Create the nodes with the XSingleServiceFactory of the 339 // configuration 340 Object xNewElement = 341 xSetElementFactory.createInstanceWithArguments( aArgs ); 342 343 if ( xNewElement != null ) { 344 // We have a new node. To set the properties of the node 345 // we need the XPropertySet interface. 346 com.sun.star.beans.XPropertySet xPropertySet = 347 (com.sun.star.beans.XPropertySet) 348 UnoRuntime.queryInterface( 349 com.sun.star.beans.XPropertySet.class, 350 xNewElement ); 351 352 if ( xPropertySet != null ) { 353 // Create a unique node name. 354 String aCmdNodeName = new String( "Command-" ); 355 aCmdNodeName += i; 356 357 // Insert the node into the Disabled set 358 xPropertySet.setPropertyValue( "Command", 359 aCommandURLTestSet[i] ); 360 xNameContainer.insertByName( aCmdNodeName, 361 xNewElement ); 362 } 363 } 364 } 365 366 // Commit our changes 367 com.sun.star.util.XChangesBatch xFlush = 368 (com.sun.star.util.XChangesBatch)UnoRuntime.queryInterface( 369 com.sun.star.util.XChangesBatch.class, xAccess); 370 xFlush.commitChanges(); 371 } 372 } 373 catch ( com.sun.star.uno.Exception e ) 374 { 375 System.err.println( "Exception detected!" + e); 376 e.printStackTrace(); 377 } 378 } 379 380 /** 381 * reset URL so it can be reused 382 * 383 * @param aURL 384 * the URL that should be reseted 385 */ resetURL( com.sun.star.util.URL aURL )386 private static void resetURL( com.sun.star.util.URL aURL ) 387 { 388 aURL.Protocol = ""; 389 aURL.User = ""; 390 aURL.Password = ""; 391 aURL.Server = ""; 392 aURL.Port = 0; 393 aURL.Path = ""; 394 aURL.Name = ""; 395 aURL.Arguments = ""; 396 aURL.Mark = ""; 397 aURL.Main = ""; 398 aURL.Complete = ""; 399 } 400 } 401