001    /**
002     * <copyright> 
003     *
004     * Copyright (c) 2002-2006 IBM Corporation and others.
005     * All rights reserved.   This program and the accompanying materials
006     * are made available under the terms of the Eclipse Public License v1.0
007     * which accompanies this distribution, and is available at
008     * http://www.eclipse.org/legal/epl-v10.html
009     * 
010     * Contributors: 
011     *   IBM - Initial API and implementation
012     *
013     * </copyright>
014     *
015     * $Id: IdentityCommand.java,v 1.3 2006/12/05 20:19:53 emerks Exp $
016     */
017    package org.eclipse.emf.common.command;
018    
019    
020    import java.util.Collection;
021    import java.util.Collections;
022    
023    import org.eclipse.emf.common.CommonPlugin;
024    
025    
026    /**
027     * A command that always produces the same result.
028     */
029    public class IdentityCommand extends AbstractCommand 
030    {
031      /**
032       * An empty instance of this object.
033       */
034      public static final IdentityCommand INSTANCE = new IdentityCommand();
035    
036      /**
037       * Keeps track of the result returned from {@link #getResult}.
038       */
039      protected Collection<?> result;
040    
041      {
042        // This ensures that these useless state variables at least reflect the right value.
043        //
044        isPrepared = true;
045        isExecutable = true;
046      }
047    
048      /**
049       * Creates an empty instance.
050       */
051      public IdentityCommand() 
052      {
053        super();
054        this.result = Collections.EMPTY_LIST;
055      }
056    
057      /**
058       * Creates an instance with a result collection containing the given result object.
059       * @param result the one object in the result collection.
060       */
061      public IdentityCommand(Object result) 
062      {
063        super();
064        this.result = Collections.singleton(result);
065      }
066    
067      /**
068       * Creates an instance with the given result collection.
069       * @param result the result collection.
070       */
071      public IdentityCommand(Collection<?> result) 
072      {
073        super();
074        this.result = result;
075      }
076    
077      /**
078       * Creates an instance with the given label.
079       * @param label the label.
080       */
081      public IdentityCommand(String label)
082      {
083        this.label = label;
084        this.result = Collections.EMPTY_LIST;
085      }
086    
087      /**
088       * Creates an instance with the given label and a result collection containing the given result object.
089       * @param label the label.
090       * @param result the one object in the result collection.
091       */
092      public IdentityCommand(String label, Object result)
093      {
094        this.label = label;
095        this.result = Collections.singleton(result);
096      }
097    
098      /**
099       * Creates an instance with the given label the result collection.
100       * @param label the label.
101       * @param result the result collection.
102       */
103      public IdentityCommand(String label, Collection<?> result)
104      {
105        this.label = label;
106        this.result = result;
107      }
108    
109      /**
110       * Creates an instance with the given label and description.
111       * @param label the label.
112       * @param description the description.
113       */
114      public IdentityCommand(String label, String description)
115      {
116        this.label = label;
117        this.description = description;
118        this.result = Collections.EMPTY_LIST;
119      }
120    
121      /**
122       * Creates an instance with the given label, description, and a result collection containing the given result object.
123       * @param label the label.
124       * @param description the description.
125       * @param result the one object in the result collection.
126       */
127      public IdentityCommand(String label, String description, Object result)
128      {
129        this.label = label;
130        this.description = description;
131        this.result = Collections.singleton(result);
132      }
133    
134      /**
135       * Creates an instance with the given label, description, result collection.
136       * @param label the label.
137       * @param description the description.
138       * @param result the result collection.
139       */
140      public IdentityCommand(String label, String description, Collection<?> result)
141      {
142        this.label = label;
143        this.description = description;
144        this.result = result;
145      }
146    
147      /**
148       * Returns <code>true</code>.
149       * @return <code>true</code>.
150       */
151      @Override
152      public boolean canExecute() 
153      {
154        return true;
155      }
156    
157      /**
158       * Do nothing.
159       */
160      public void execute() 
161      {
162        // Do nothing.
163      }
164    
165      /**
166       * Do nothing.
167       */
168      @Override
169      public void undo() 
170      {
171        // Do nothing.
172      }
173    
174      /**
175       * Do nothing.
176       */
177      public void redo() 
178      {
179        // Do nothing.
180      }
181    
182      @Override
183      public String getLabel()
184      {
185        return label == null ? CommonPlugin.INSTANCE.getString("_UI_IdentityCommand_label") : label;
186      }
187    
188      @Override
189      public String getDescription()
190      {
191        return description == null ? CommonPlugin.INSTANCE.getString("_UI_IdentityCommand_description") : description;
192      }
193    
194      /**
195       * Return the identity result.
196       * @return the identity result.
197       */
198      @Override
199      public Collection<?> getResult()
200      {
201        return result;
202      }
203    }