Get the style color and number just once
[LibreOffice.git] / odk / examples / DevelopersGuide / OfficeDev / DisableCommands / DisableCommandsTest.java
blob0d4b09bf09d3ffacf458cd21bda28419839d4084
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
5 * the BSD license.
7 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
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.configuration.theDefaultProvider;
37 import com.sun.star.uno.UnoRuntime;
38 import com.sun.star.uno.XComponentContext;
39 import com.sun.star.lang.XMultiComponentFactory;
40 import com.sun.star.lang.XMultiServiceFactory;
41 import com.sun.star.util.XURLTransformer;
42 import com.sun.star.frame.XComponentLoader;
43 import com.sun.star.text.XTextDocument;
46 * Provides example code how to enable/disable
47 * commands.
49 public class DisableCommandsTest {
52 * A list of command names
54 final private static String[] aCommandURLTestSet =
56 "Open",
57 "About",
58 "SelectAll",
59 "Quit",
62 private static XComponentContext xRemoteContext = null;
63 private static XMultiComponentFactory xRemoteServiceManager = null;
64 private static XURLTransformer xTransformer = null;
65 private static XMultiServiceFactory xConfigProvider = null;
68 * @param args the command line arguments
70 public static void main(String[] args) {
72 try {
73 // get the remote office context. If necessary a new office
74 // process is started
75 xRemoteContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
76 System.out.println("Connected to a running office ...");
77 xRemoteServiceManager = xRemoteContext.getServiceManager();
79 Object transformer = xRemoteServiceManager.createInstanceWithContext(
80 "com.sun.star.util.URLTransformer", xRemoteContext );
81 xTransformer = UnoRuntime.queryInterface(com.sun.star.util.XURLTransformer.class,
82 transformer );
84 xConfigProvider = theDefaultProvider.get(xRemoteContext);
86 // create a new test document
87 Object oDesktop = xRemoteServiceManager.createInstanceWithContext(
88 "com.sun.star.frame.Desktop", xRemoteContext);
90 XComponentLoader xCompLoader =UnoRuntime.queryInterface(XComponentLoader.class, oDesktop);
92 com.sun.star.lang.XComponent xComponent =
93 xCompLoader.loadComponentFromURL("private:factory/swriter",
94 "_blank", 0, new com.sun.star.beans.PropertyValue[0]);
96 XTextDocument xDoc =UnoRuntime.queryInterface(XTextDocument.class, xComponent);
97 xDoc.getText().setString("You can now check the disabled commands. The "
98 +"following commands are disabled:\n\n"
99 +" Open...\n Exit\n Select All\n "
100 +"About StarOffice|OpenOffice\n\nPress "
101 + "\"return\" in the shell where you have "
102 + "started the example to enable the "
103 + "commands!\n\nCheck the commands again and "
104 + "press once more \"return\" to finish the "
105 + "example and close the document.");
107 // ensure that the document content is optimal visible
108 com.sun.star.frame.XModel xModel =
109 UnoRuntime.queryInterface(
110 com.sun.star.frame.XModel.class, xDoc);
111 // get the frame for later usage
112 com.sun.star.frame.XFrame xFrame =
113 xModel.getCurrentController().getFrame();
115 com.sun.star.view.XViewSettingsSupplier xViewSettings =
116 UnoRuntime.queryInterface(
117 com.sun.star.view.XViewSettingsSupplier.class,
118 xModel.getCurrentController());
119 xViewSettings.getViewSettings().setPropertyValue(
120 "ZoomType", Short.valueOf((short)0));
122 // test document will be closed later
124 // First we need a defined starting point. So we have to remove
125 // all commands from the disabled set!
126 enableCommands();
128 // Check if the commands are usable
129 testCommands( false );
131 // Disable the commands
132 disableCommands();
134 // Now the commands shouldn't be usable anymore
135 testCommands( true );
137 // you can now check the test document and see which commands are
138 // disabled
139 System.out.println("\nYou can now check the disabled commands.\n"
140 +"Please press 'return' to enable the commands!");
141 waitForUserInput();
143 // Remove disable commands to make Office usable again
144 enableCommands();
146 // you can check the test document again and see that the commands
147 // are enabled now
148 System.out.println("Check again the now enabled commands.\n"
149 +"Please press 'return' to finish the example and "
150 +"close the document!");
151 waitForUserInput();
153 // close test document
154 com.sun.star.util.XCloseable xCloseable = UnoRuntime.queryInterface(com.sun.star.util.XCloseable.class,
155 xComponent );
157 if (xCloseable != null ) {
158 xCloseable.close(false);
159 } else
161 xComponent.dispose();
164 catch (java.lang.Exception e){
165 e.printStackTrace();
167 finally {
168 System.exit(0);
173 * Wait for user input -> until the user press 'return'
175 private static void waitForUserInput() throws java.io.IOException {
177 java.io.BufferedReader reader
178 = new java.io.BufferedReader(new java.io.InputStreamReader(System.in));
180 reader.read();
184 * Test the commands that we enabled/disabled
186 private static void testCommands( boolean bDisabledCmds )
187 throws com.sun.star.uno.Exception
189 // We need the desktop to get access to the current frame
190 Object desktop = xRemoteServiceManager.createInstanceWithContext(
191 "com.sun.star.frame.Desktop", xRemoteContext );
192 com.sun.star.frame.XDesktop xDesktop = UnoRuntime.queryInterface(com.sun.star.frame.XDesktop.class, desktop );
193 com.sun.star.frame.XFrame xFrame = xDesktop.getCurrentFrame();
194 com.sun.star.frame.XDispatchProvider xDispatchProvider = null;
195 if ( xFrame != null )
197 // We have a frame. Now we need access to the dispatch provider.
198 xDispatchProvider =
199 UnoRuntime.queryInterface(
200 com.sun.star.frame.XDispatchProvider.class, xFrame );
201 if ( xDispatchProvider != null )
203 // As we have the dispatch provider we can now check if we get
204 // a dispatch object or not.
205 for ( int n = 0; n < aCommandURLTestSet.length; n++ )
207 // Prepare the URL
208 com.sun.star.util.URL[] aURL = new com.sun.star.util.URL[1];
209 aURL[0] = new com.sun.star.util.URL();
210 com.sun.star.frame.XDispatch xDispatch = null;
212 aURL[0].Complete = ".uno:" + aCommandURLTestSet[n];
213 xTransformer.parseSmart( aURL, ".uno:" );
215 // Try to get a dispatch object for our URL
216 xDispatch = xDispatchProvider.queryDispatch( aURL[0], "", 0 );
218 if ( xDispatch != null )
220 if ( bDisabledCmds )
221 System.out.println(
222 "Something is wrong, I got dispatch object for "
223 + aURL[0].Complete );
224 else
225 System.out.println( "Ok, dispatch object for "
226 + aURL[0].Complete );
228 else
230 if ( !bDisabledCmds )
231 System.out.println("Something is wrong, I cannot get dispatch object for " + aURL[0].Complete );
232 else
233 System.out.println( "Ok, no dispatch object for "
234 + aURL[0].Complete );
236 resetURL( aURL[0] );
239 else
240 System.out.println( "Couldn't get XDispatchProvider from Frame!" );
242 else
243 System.out.println( "Couldn't get current Frame from Desktop!" );
247 * Ensure that there are no disabled commands in the user layer. The
248 * implementation removes all commands from the disabled set!
250 private static void enableCommands() {
251 // Set the root path for our configuration access
252 com.sun.star.beans.PropertyValue[] lParams =
253 new com.sun.star.beans.PropertyValue[1];
255 lParams[0] = new com.sun.star.beans.PropertyValue();
256 lParams[0].Name = "nodepath";
257 lParams[0].Value = "/org.openoffice.Office.Commands/Execute/Disabled";
259 try {
260 // Create configuration update access to have write access to the
261 // configuration
262 Object xAccess = xConfigProvider.createInstanceWithArguments(
263 "com.sun.star.configuration.ConfigurationUpdateAccess",
264 lParams );
266 com.sun.star.container.XNameAccess xNameAccess =
267 UnoRuntime.queryInterface(
268 com.sun.star.container.XNameAccess.class, xAccess );
270 if ( xNameAccess != null ) {
271 // We need the XNameContainer interface to remove the nodes by name
272 com.sun.star.container.XNameContainer xNameContainer =
273 UnoRuntime.queryInterface(
274 com.sun.star.container.XNameContainer.class, xAccess );
276 // Retrieves the names of all Disabled nodes
277 String[] aCommandsSeq = xNameAccess.getElementNames();
278 for ( int n = 0; n < aCommandsSeq.length; n++ ) {
279 try {
280 // remove the node
281 xNameContainer.removeByName( aCommandsSeq[n] );
283 catch ( com.sun.star.lang.WrappedTargetException e ) {
285 catch ( com.sun.star.container.NoSuchElementException e ) {
290 // Commit our changes
291 com.sun.star.util.XChangesBatch xFlush =
292 UnoRuntime.queryInterface(
293 com.sun.star.util.XChangesBatch.class, xAccess);
295 xFlush.commitChanges();
297 catch ( com.sun.star.uno.Exception e ) {
298 System.out.println( "Exception detected!" );
299 System.out.println( e );
304 * Disable all commands defined in the aCommandURLTestSet array
306 private static void disableCommands() {
307 // Set the root path for our configuration access
308 com.sun.star.beans.PropertyValue[] lParams =
309 new com.sun.star.beans.PropertyValue[1];
311 lParams[0] = new com.sun.star.beans.PropertyValue();
312 lParams[0].Name = "nodepath";
313 lParams[0].Value = "/org.openoffice.Office.Commands/Execute/Disabled";
315 try {
316 // Create configuration update access to have write access to the
317 // configuration
318 Object xAccess = xConfigProvider.createInstanceWithArguments(
319 "com.sun.star.configuration.ConfigurationUpdateAccess",
320 lParams );
322 com.sun.star.lang.XSingleServiceFactory xSetElementFactory =
323 UnoRuntime.queryInterface(
324 com.sun.star.lang.XSingleServiceFactory.class, xAccess );
326 com.sun.star.container.XNameContainer xNameContainer =
327 UnoRuntime.queryInterface(
328 com.sun.star.container.XNameContainer.class, xAccess );
330 if ( xSetElementFactory != null && xNameContainer != null ) {
331 Object[] aArgs = new Object[0];
333 for ( int i = 0; i < aCommandURLTestSet.length; i++ ) {
334 // Create the nodes with the XSingleServiceFactory of the
335 // configuration
336 Object xNewElement =
337 xSetElementFactory.createInstanceWithArguments( aArgs );
339 if ( xNewElement != null ) {
340 // We have a new node. To set the properties of the node
341 // we need the XPropertySet interface.
342 com.sun.star.beans.XPropertySet xPropertySet =
343 UnoRuntime.queryInterface(
344 com.sun.star.beans.XPropertySet.class,
345 xNewElement );
347 if ( xPropertySet != null ) {
348 // Create a unique node name.
349 String aCmdNodeName = "Command-";
350 aCmdNodeName += i;
352 // Insert the node into the Disabled set
353 xPropertySet.setPropertyValue( "Command",
354 aCommandURLTestSet[i] );
355 xNameContainer.insertByName( aCmdNodeName,
356 xNewElement );
361 // Commit our changes
362 com.sun.star.util.XChangesBatch xFlush =
363 UnoRuntime.queryInterface(
364 com.sun.star.util.XChangesBatch.class, xAccess);
365 xFlush.commitChanges();
368 catch ( com.sun.star.uno.Exception e )
370 System.err.println( "Exception detected!" + e);
371 e.printStackTrace();
376 * reset URL so it can be reused
378 * @param aURL
379 * the URL that should be reset
381 private static void resetURL( com.sun.star.util.URL aURL )
383 aURL.Protocol = "";
384 aURL.User = "";
385 aURL.Password = "";
386 aURL.Server = "";
387 aURL.Port = 0;
388 aURL.Path = "";
389 aURL.Name = "";
390 aURL.Arguments = "";
391 aURL.Mark = "";
392 aURL.Main = "";
393 aURL.Complete = "";
397 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */