KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > mapping > action > NameMatchMappingAction


1 /**
2  * <copyright>
3  *
4  * Copyright (c) 2002-2005 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: NameMatchMappingAction.java,v 1.4 2005/06/12 13:39:41 emerks Exp $
16  */

17 package org.eclipse.emf.mapping.action;
18
19
20 import java.util.Collection JavaDoc;
21 import java.util.EventObject JavaDoc;
22 import java.util.HashSet JavaDoc;
23 import java.util.Iterator JavaDoc;
24
25 import org.eclipse.jface.action.Action;
26 import org.eclipse.jface.action.IAction;
27 import org.eclipse.jface.resource.ImageDescriptor;
28 import org.eclipse.jface.viewers.ISelection;
29 import org.eclipse.jface.viewers.ISelectionProvider;
30 import org.eclipse.ui.IWorkbenchPart;
31
32 import org.eclipse.emf.common.command.Command;
33 import org.eclipse.emf.common.command.CommandStackListener;
34 import org.eclipse.emf.common.command.CompoundCommand;
35 import org.eclipse.emf.edit.command.CommandActionDelegate;
36 import org.eclipse.emf.edit.command.CommandParameter;
37 import org.eclipse.emf.edit.domain.EditingDomain;
38 import org.eclipse.emf.edit.ui.action.CommandAction;
39 import org.eclipse.emf.edit.ui.provider.ExtendedImageRegistry;
40 import org.eclipse.emf.mapping.Mapping;
41 import org.eclipse.emf.mapping.MappingPlugin;
42 import org.eclipse.emf.mapping.MappingRoot;
43 import org.eclipse.emf.mapping.command.CreateMappingCommand;
44 import org.eclipse.emf.mapping.command.NameMatchMappingCommand;
45 import org.eclipse.emf.mapping.domain.MappingDomain;
46 import org.eclipse.emf.mapping.presentation.IComposedSelection;
47 import org.eclipse.emf.mapping.presentation.MappingUIPlugin;
48
49
50 /**
51  */

52 public class NameMatchMappingAction extends CommandAction implements CommandStackListener
53 {
54   public void setActiveWorkbenchPart(IWorkbenchPart workbenchPart)
55   {
56     if (editingDomain != null)
57     {
58       editingDomain.getCommandStack().removeCommandStackListener(this);
59     }
60
61     super.setActiveWorkbenchPart(workbenchPart);
62
63     if (editingDomain != null)
64     {
65       editingDomain.getCommandStack().addCommandStackListener(this);
66     }
67   }
68
69   public void commandStackChanged(EventObject JavaDoc event)
70   {
71     selectionChanged(action, ((ISelectionProvider)workbenchPart).getSelection());
72   }
73
74   public void selectionChanged(IAction action, ISelection selection)
75   {
76     if (selection instanceof IComposedSelection)
77     {
78       super.selectionChanged(action,((IComposedSelection)selection).getCombinedSelection());
79     }
80     else
81     {
82       super.selectionChanged(action, selection);
83     }
84   }
85
86   /**
87    * This returns the image that is used if the command does not provide an override.
88    */

89   protected Object JavaDoc getDefaultImage()
90   {
91     return MappingPlugin.getPlugin().getImage("full/etool16/MatchByName");
92   }
93
94   public static class DelegateCommand extends CompoundCommand implements CommandActionDelegate
95   {
96     protected MappingDomain mappingDomain;
97     protected Collection JavaDoc collection;
98     protected Command createMappingCommand;
99
100     public DelegateCommand(EditingDomain editingDomain, CommandParameter commandParameter)
101     {
102       super
103         (MappingUIPlugin.getPlugin().getString("_UI_NameMatchMappingAction_label"),
104          MappingUIPlugin.getPlugin().getString("_UI_NameMatchMappingAction_description"));
105
106       collection = commandParameter.getCollection();
107       mappingDomain = (MappingDomain)editingDomain;
108     }
109
110     protected boolean prepare()
111     {
112       boolean result = false;
113
114       if (collection != null)
115       {
116         Collection JavaDoc mappedObjects = new HashSet JavaDoc();
117         Collection JavaDoc mappingObjects = new HashSet JavaDoc();
118         MappingRoot mappingRoot = mappingDomain.getMappingRoot();
119     
120         for (Iterator JavaDoc objects = collection.iterator(); objects.hasNext(); )
121         {
122           Object JavaDoc object = objects.next();
123           if (object instanceof Mapping)
124           {
125             appendIfCanExecute(NameMatchMappingCommand.create(mappingDomain, (Mapping)object));
126             mappingObjects.add(object);
127           }
128           else if (mappingRoot.isInputObject(object) || mappingRoot.isOutputObject(object))
129           {
130             mappedObjects.add(object);
131           }
132         }
133
134         if (!mappedObjects.isEmpty())
135         {
136           Collection JavaDoc mappings = mappingRoot.getAllMappings(mappedObjects);
137           switch (mappings.size())
138           {
139             case 0:
140             {
141               createMappingCommand = CreateMappingCommand.create(mappingDomain, mappedObjects);
142               result = appendIfCanExecute(createMappingCommand);
143               break;
144             }
145             case 1:
146             {
147               result = appendIfCanExecute(NameMatchMappingCommand.create(mappingDomain, (Mapping)mappings.iterator().next()));
148               break;
149             }
150             default:
151             {
152               break;
153             }
154           }
155         }
156       }
157
158       result = result || !isEmpty();
159       return result;
160     }
161
162     public void execute()
163     {
164       super.execute();
165       if (createMappingCommand != null)
166       {
167         appendAndExecute(NameMatchMappingCommand.create(mappingDomain, (Mapping)createMappingCommand.getResult().iterator().next()));
168       }
169     }
170
171     /**
172      * This returns the icon, if any, of the action.
173      */

174     public Object JavaDoc getImage()
175     {
176       return "Placeholder";
177     }
178
179     public String JavaDoc getText()
180     {
181       return getLabel();
182     }
183
184     /**
185      * This returns the tool tip text, if any, of the action.
186      */

187     public String JavaDoc getToolTipText()
188     {
189       return getDescription();
190     }
191   }
192
193   protected ImageDescriptor objectToImageDescriptor(Object JavaDoc object)
194   {
195     ((Action)action).setHoverImageDescriptor
196       (ExtendedImageRegistry.getInstance().getImageDescriptor(MappingPlugin.getPlugin().getImage("full/ctool16/MatchByName")));
197
198     ((Action)action).setDisabledImageDescriptor
199       (ExtendedImageRegistry.getInstance().getImageDescriptor(MappingPlugin.getPlugin().getImage("full/dtool16/MatchByName")));
200
201     return
202       ExtendedImageRegistry.getInstance().getImageDescriptor(MappingPlugin.getPlugin().getImage("full/etool16/MatchByName"));
203   }
204
205   /**
206    * Match the command for this action
207    */

208   protected Command createActionCommand(EditingDomain editingDomain, final Collection JavaDoc collection)
209   {
210     return editingDomain.createCommand(DelegateCommand.class, new CommandParameter(null, null, collection));
211   }
212 }
213
Popular Tags