Source code: javax/ide/command/CommandProcessor.java
1 package javax.ide.command;
2
3 import javax.ide.Service;
4 import javax.ide.spi.ProviderNotFoundException;
5
6 /**
7 * The <tt>CommandProcessor</tt> is responsible for managing the
8 * execution of the IDE commands and maintining the undo stack.<p>
9 *
10 * In general, all {@link Controller} implementations should use the
11 * IDE command processor to execute the commands that controller handles. <p>
12 *
13 * IDE service providers must extend this class and provide an
14 * implementation of the {@link #invoke(Command)} method. It is up to the
15 * IDE service provider implementation to decide whether it supports
16 * undo/redo on a per document, or global basis.
17 * If the undo support provided is document based, implementors of this
18 * class must make sure that after a command is executed, the
19 * command processor checks the {@link Command#getAffectedDocuments} return
20 * value to detemine if other documents were affected by the current command
21 * execution.<p>
22 *
23 * If the {@link Command#getAffectedDocuments} returns one or more items, then
24 * the undo stacks of the affected documents must be flushed in order to
25 * maintain document consistency.
26 *
27 * @see Command
28 */
29 public abstract class CommandProcessor extends Service
30 {
31
32 /**
33 * Executes the actions associated with a specific command. A
34 * <code>Controller</code> uses this method to tell the
35 * <code>CommandProcessor</code> that a command should be executed.
36 *
37 * @param cmd the command to execute.
38 * @throws Exception if an error occurs processing the command.
39 * @return Whether the specified executed {@link Command#OK} or
40 * {@link Command#CANCEL}.
41 */
42 public abstract int invoke( Command cmd ) throws Exception;
43
44 /**
45 * Get the command processor implementation for this IDE.
46 *
47 * @return the command processor implementation.
48 */
49 public static CommandProcessor getCommandProcessor()
50 {
51 try
52 {
53 return (CommandProcessor) getService( CommandProcessor.class );
54 }
55 catch ( ProviderNotFoundException nse )
56 {
57 nse.printStackTrace();
58 throw new IllegalStateException( "No command processor." );
59 }
60 }
61 }