Executing Content Commands

From Apache OpenOffice Wiki
Jump to: navigation, search

Each UCB content is able to execute commands. When the content object is created, commands are executed using its com.sun.star.ucb.XCommandProcessor interface. The execute() method at this interface expects a com.sun.star.ucb.Command, which is a struct containing the command name, command arguments and a handle:

Members of struct com.sun.star.ucb.Command
Name string, contains the name of the command
Handle long, contains an implementation-specific handle for the command
Argument any, contains the argument of the command

Refer to Appendix C Universal Content Providers for a complete list of predefined commands, the description of the UNO service com.sun.star.ucb.Content and the UCP reference that comes with the SDK. For the latest information, visit ucb.openoffice.org.

Documentation note.png Whenever we refer to UCB commands, we put them in double quotes as in "getPropertyValues" to make a distinction between UCB commands and methods in general that are written getPropertyValues().

If executing a command cannot proceed because of an error condition, the following occurs. If the execute call was supplied with a com.sun.star.ucb.XCommandEnvironment that contains a com.sun.star.task.XInteractionHandler, this interaction handler is used to resolve the problem. If no interaction handler is supplied by passing null to the execute() method, or it cannot resolve the problem, an exception describing the error condition is thrown.

The following method executeCommand() executes a command at a UCB content:

  import com.sun.star.uno.UnoRuntime;
  import com.sun.star.ucb.*;
  Object executeCommand(XContent xContent, String aCommandName, Object aArgument)
      throws com.sun.star.ucb.CommandAbortedException, com.sun.star.uno.Exception {
      // Obtain command processor interface from given content.
      XCommandProcessor xCmdProcessor = (XCommandProcessor)UnoRuntime.queryInterface( 
          XCommandProcessor.class, xContent);
      // Assemble command to execute.
      Command aCommand = new Command();
      aCommand.Name = aCommandName;
      aCommand.Handle = -1; // not available
      aCommand.Argument = aArgument;
      // Note: throws CommandAbortedException and Exception since
      // we pass null for the XCommandEnvironment parameter
      return xCmdProcessor.execute(aCommand, 0, null);
Documentation note.png The method executeCommand() from the example above is used in the following examples whenever a command is to be executed at a UCB content.
Content on this page is licensed under the Public Documentation License (PDL).
Personal tools
In other languages