All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.eclipse.gef.commands.Command Maven / Gradle / Ivy

The newest version!
/*******************************************************************************
 * Copyright (c) 2000, 2010, 2012 IBM Corporation, Gerhardt Informatics Kft. and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *     Gerhardt Informatics Kft. - GEFGWT port
 *******************************************************************************/
package org.eclipse.gef.commands;

/**
 * An Abstract implementation of {@link Command}.
 * 
 * @author hudsonr
 * @since 2.0
 */
public abstract class Command {

	private String label;

	private String debugLabel;

	/**
	 * Constructs a Command with no label.
	 */
	public Command() {
	}

	/**
	 * Constructs a Command with the specified label.
	 * 
	 * @param label
	 *            the Command's label
	 */
	public Command(String label) {
		setLabel(label);
	}

	/**
	 * @return true if the command can be executed
	 */
	public boolean canExecute() {
		return true;
	}

	/**
	 * @return true if the command can be undone. This method
	 *         should only be called after execute() or
	 *         redo() has been called.
	 */
	public boolean canUndo() {
		return true;
	}

	/**
	 * Returns a Command that represents the chaining of a specified Command to
	 * this Command. The Command being chained will execute() after
	 * this command has executed, and it will undo() before this
	 * Command is undone.
	 * 
	 * @param command
	 *            null or the Command being chained
	 * @return a Command representing the union
	 */
	public Command chain(Command command) {
		if (command == null)
			return this;
		class ChainedCompoundCommand extends CompoundCommand {
			public Command chain(Command c) {
				add(c);
				return this;
			}
		}
		CompoundCommand result = new ChainedCompoundCommand();
		result.setDebugLabel("Chained Commands"); //$NON-NLS-1$
		result.add(this);
		result.add(command);
		return result;
	}

	/**
	 * This is called to indicate that the Command will not be used
	 * again. The Command may be in any state (executed, undone or redone) when
	 * dispose is called. The Command should not be referenced in any way after
	 * it has been disposed.
	 */
	public void dispose() {
	}

	/**
	 * executes the Command. This method should not be called if the Command is
	 * not executable.
	 */
	public void execute() {
	}

	/**
	 * @return an untranslated String used for debug purposes only
	 */
	public String getDebugLabel() {
		return debugLabel + ' ' + getLabel();
	}

	/**
	 * @return a String used to describe this command to the User
	 */
	public String getLabel() {
		return label;
	}

	/**
	 * Re-executes the Command. This method should only be called after
	 * undo() has been called.
	 */
	public void redo() {
		execute();
	}

	/**
	 * Sets the debug label for this command
	 * 
	 * @param label
	 *            a description used for debugging only
	 */
	public void setDebugLabel(String label) {
		debugLabel = label;
	}

	/**
	 * Sets the label used to describe this command to the User.
	 * 
	 * @param label
	 *            the label
	 */
	public void setLabel(String label) {
		this.label = label;
	}

	/**
	 * Undoes the changes performed during execute(). This method
	 * should only be called after execute has been called, and
	 * only when canUndo() returns true.
	 * 
	 * @see #canUndo()
	 */
	public void undo() {
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy