| 1 |
/** |
| 2 |
* Copyright (c) 2002-2006 IBM Corporation and others. |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v2.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* http://www.eclipse.org/legal/epl-v20.html |
| 7 |
* |
| 8 |
* Contributors: |
| 9 |
* IBM - Initial API and implementation |
| 10 |
*/ |
| 11 |
package org.eclipse.emf.edit.ui.action; |
| 12 |
|
| 13 |
|
| 14 |
import java.util.ArrayList; |
| 15 |
import java.util.Collection; |
| 16 |
import java.util.List; |
| 17 |
|
| 18 |
import org.eclipse.jface.viewers.IStructuredSelection; |
| 19 |
import org.eclipse.ui.actions.BaseSelectionListenerAction; |
| 20 |
|
| 21 |
import org.eclipse.emf.common.command.Command; |
| 22 |
import org.eclipse.emf.common.command.UnexecutableCommand; |
| 23 |
import org.eclipse.emf.edit.domain.EditingDomain; |
| 24 |
|
| 25 |
|
| 26 |
/* |
| 27 |
* This base action class implements an action by creating a command and delegating to it; |
| 28 |
* it's main use is as a base class for action handlers. |
| 29 |
*/ |
| 30 |
public class CommandActionHandler extends BaseSelectionListenerAction |
| 31 |
{ |
| 32 |
/** |
| 33 |
* This keeps track of the editing domain of the action. |
| 34 |
*/ |
| 35 |
protected EditingDomain domain; |
| 36 |
|
| 37 |
/** |
| 38 |
* This keeps track of the command delegate that is created by {@link #createCommand}. |
| 39 |
*/ |
| 40 |
protected Command command; |
| 41 |
|
| 42 |
/** |
| 43 |
* This constructs and instance in this editing domain. |
| 44 |
*/ |
| 45 |
public CommandActionHandler(EditingDomain domain) |
| 46 |
{ |
| 47 |
// The label is not used. |
| 48 |
// |
| 49 |
super(""); |
| 50 |
|
| 51 |
this.domain = domain; |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* This constructs and instance in this editing domain. |
| 56 |
*/ |
| 57 |
public CommandActionHandler(EditingDomain domain, String label) |
| 58 |
{ |
| 59 |
super(label); |
| 60 |
|
| 61 |
this.domain = domain; |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* This returns the action's domain. |
| 66 |
*/ |
| 67 |
public EditingDomain getEditingDomain() |
| 68 |
{ |
| 69 |
return domain; |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* This sets the action's domain. |
| 74 |
*/ |
| 75 |
public void setEditingDomain(EditingDomain domain) |
| 76 |
{ |
| 77 |
this.domain = domain; |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* This simply execute the command. |
| 82 |
*/ |
| 83 |
@Override |
| 84 |
public void run() |
| 85 |
{ |
| 86 |
domain.getCommandStack().execute(command); |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* When the selection changes, this will call {@link #createCommand} with the appropriate collection of selected objects. |
| 91 |
*/ |
| 92 |
@Override |
| 93 |
public boolean updateSelection(IStructuredSelection selection) |
| 94 |
{ |
| 95 |
List<?> list = selection.toList(); |
| 96 |
Collection<Object> collection = new ArrayList<Object>(list); |
| 97 |
command = createCommand(collection); |
| 98 |
|
| 99 |
return command.canExecute(); |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* This default implementation simply returns {@link org.eclipse.emf.common.command.UnexecutableCommand#INSTANCE}. |
| 104 |
*/ |
| 105 |
public Command createCommand(Collection<?> selection) |
| 106 |
{ |
| 107 |
return UnexecutableCommand.INSTANCE; |
| 108 |
} |
| 109 |
} |