View Javadoc

1   /*
2    * Copyright 2006-2016 The JGUIraffe Team.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License")
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package net.sf.jguiraffe.gui.cmd;
17  
18  /**
19   * <p>
20   * A wrapper implementation of the {@code Command} interface.
21   * </p>
22   * <p>
23   * An instance of this class is initialized with another {@code Command} object.
24   * It implements all methods defined by the {@code Command} interface by
25   * delegating to the wrapped {@code Command} object. Thus this class can serve
26   * as a base class for command implementations that need to alter certain
27   * behavior of other command objects.
28   * </p>
29   *
30   * @author Oliver Heger
31   * @version $Id: CommandWrapper.java 205 2012-01-29 18:29:57Z oheger $
32   */
33  public class CommandWrapper implements Command
34  {
35      /** Stores the wrapped command. */
36      private final Command wrappedCommand;
37  
38      /**
39       * Creates a new instance of {@code CommandWrapper} and initializes it with
40       * the wrapped command.
41       *
42       * @param wrappedCmd the wrapped command (must not be <b>null</b>)
43       * @throws IllegalArgumentException if the wrapped command is <b>null</b>
44       */
45      public CommandWrapper(Command wrappedCmd)
46      {
47          if (wrappedCmd == null)
48          {
49              throw new IllegalArgumentException(
50                      "Wrapped command must not be null!");
51          }
52  
53          wrappedCommand = wrappedCmd;
54      }
55  
56      /**
57       * Returns the {@code Command} object that is wrapped by this object.
58       *
59       * @return the wrapped {@code Command}
60       */
61      public final Command getWrappedCommand()
62      {
63          return wrappedCommand;
64      }
65  
66      /**
67       * Executes this command. This implementation delegates to the wrapped
68       * command.
69       *
70       * @throws Exception if an error occurs during execution
71       */
72      public void execute() throws Exception
73      {
74          getWrappedCommand().execute();
75      }
76  
77      /**
78       * Returns the object for the updating the UI after execution of the
79       * command. This implementation delegates to the wrapped command.
80       *
81       * @return the object for updating the UI
82       */
83      public Runnable getGUIUpdater()
84      {
85          return getWrappedCommand().getGUIUpdater();
86      }
87  
88      /**
89       * Notifies this command about an exception that occurred during execution.
90       * This implementation delegates to the wrapped command.
91       *
92       * @param t the exception
93       */
94      public void onException(Throwable t)
95      {
96          getWrappedCommand().onException(t);
97      }
98  
99      /**
100      * This method is called after the execution of the command. This
101      * implementation delegates to the wrapped command.
102      */
103     public void onFinally()
104     {
105         getWrappedCommand().onFinally();
106     }
107 }