KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > edit > ui > action > CommandActionHandler


1 /**
2  * <copyright>
3  *
4  * Copyright (c) 2002-2004 IBM Corporation and others.
5  * All rights reserved. This program and the accompanying materials
6  * are made available under the terms of the Eclipse Public License v1.0
7  * which accompanies this distribution, and is available at
8  * http://www.eclipse.org/legal/epl-v10.html
9  *
10  * Contributors:
11  * IBM - Initial API and implementation
12  *
13  * </copyright>
14  *
15  * $Id: CommandActionHandler.java,v 1.3 2005/06/08 06:20:52 nickb Exp $
16  */

17 package org.eclipse.emf.edit.ui.action;
18
19
20 import java.util.ArrayList JavaDoc;
21 import java.util.Collection JavaDoc;
22 import java.util.Iterator JavaDoc;
23
24 import org.eclipse.jface.viewers.IStructuredSelection;
25 import org.eclipse.ui.actions.BaseSelectionListenerAction;
26
27 import org.eclipse.emf.common.command.Command;
28 import org.eclipse.emf.common.command.UnexecutableCommand;
29 import org.eclipse.emf.edit.domain.EditingDomain;
30
31
32 /*
33  * This base action class implements an action by creating a command and delegating to it;
34  * it's main use is as a base class for action handlers.
35  */

36 public class CommandActionHandler extends BaseSelectionListenerAction
37 {
38   /**
39    * This keeps track of the editing domain of the action.
40    */

41   protected EditingDomain domain;
42
43   /**
44    * This keeps track of the command delegate that is created by {@link #createCommand}.
45    */

46   protected Command command;
47
48   /**
49    * This contructs and instance in this editing domain.
50    */

51   public CommandActionHandler(EditingDomain domain)
52   {
53     // The label is not used.
54
//
55
super("");
56
57     this.domain = domain;
58   }
59
60   /**
61    * This contructs and instance in this editing domain.
62    */

63   public CommandActionHandler(EditingDomain domain, String JavaDoc label)
64   {
65     super(label);
66
67     this.domain = domain;
68   }
69
70   /**
71    * This returns the action's domain.
72    */

73   public EditingDomain getEditingDomain()
74   {
75     return domain;
76   }
77
78   /**
79    * This sets the action's domain.
80    */

81   public void setEditingDomain(EditingDomain domain)
82   {
83     this.domain = domain;
84   }
85
86   /**
87    * This simply execute the command.
88    */

89   public void run()
90   {
91     domain.getCommandStack().execute(command);
92   }
93
94   /**
95    * When the selection changes, this will call {@link #createCommand} with the appopriate collection of selected objects.
96    */

97   public boolean updateSelection(IStructuredSelection selection)
98   {
99     Collection JavaDoc collection = new ArrayList JavaDoc();
100     for (Iterator JavaDoc objects = selection.iterator(); objects.hasNext(); )
101     {
102       collection.add(objects.next());
103     }
104
105     command = createCommand(collection);
106
107     return command.canExecute();
108   }
109
110   /**
111    * This default implementation simply returns {@link org.eclipse.emf.common.command.UnexecutableCommand#INSTANCE}.
112    */

113   public Command createCommand(Collection JavaDoc selection)
114   {
115     return UnexecutableCommand.INSTANCE;
116   }
117 }
118
Popular Tags