KickJava   Java API By Example, From Geeks To Geeks.

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


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: TypeMatchMappingAction.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.TypeMatchMappingCommand;
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 TypeMatchMappingAction 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 MappingUIPlugin.getPlugin().getImage("full/etool16/MatchByType");
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_TypeMatchMappingAction_label"),
104          MappingUIPlugin.getPlugin().getString("_UI_TypeMatchMappingAction_description"));
105       collection = commandParameter.getCollection();
106       mappingDomain = (MappingDomain)editingDomain;
107     }
108
109     protected boolean prepare()
110     {
111       boolean result = false;
112
113       if (collection != null)
114       {
115         Collection JavaDoc mappedObjects = new HashSet JavaDoc();
116         Collection JavaDoc mappingObjects = new HashSet JavaDoc();
117         MappingRoot mappingRoot = mappingDomain.getMappingRoot();
118     
119         for (Iterator JavaDoc objects = collection.iterator(); objects.hasNext(); )
120         {
121           Object JavaDoc object = objects.next();
122           if (object instanceof Mapping)
123           {
124             appendIfCanExecute(TypeMatchMappingCommand.create(mappingDomain, (Mapping)object));
125             mappingObjects.add(object);
126           }
127           else if (mappingRoot.isInputObject(object) || mappingRoot.isOutputObject(object))
128           {
129             mappedObjects.add(object);
130           }
131         }
132
133         if (!mappedObjects.isEmpty())
134         {
135           Collection JavaDoc mappings = mappingRoot.getAllMappings(mappedObjects);
136           switch (mappings.size())
137           {
138             case 0:
139             {
140               createMappingCommand = CreateMappingCommand.create(mappingDomain, mappedObjects);
141               result = appendIfCanExecute(createMappingCommand);
142               break;
143             }
144             case 1:
145             {
146               result = appendIfCanExecute(TypeMatchMappingCommand.create(mappingDomain, (Mapping)mappings.iterator().next()));
147               break;
148             }
149             default:
150             {
151               break;
152             }
153           }
154         }
155       }
156
157       result = result || !isEmpty();
158       return result;
159     }
160
161     public void execute()
162     {
163       super.execute();
164       if (createMappingCommand != null)
165       {
166         appendAndExecute(TypeMatchMappingCommand.create(mappingDomain, (Mapping)createMappingCommand.getResult().iterator().next()));
167       }
168     }
169
170     /**
171      * This returns the icon, if any, of the action.
172      */

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

186     public String JavaDoc getToolTipText()
187     {
188       return getDescription();
189     }
190
191     public void dispose()
192     {
193       if (createMappingCommand != null)
194       {
195         createMappingCommand.dispose();
196       }
197       super.dispose();
198     }
199   }
200
201   protected ImageDescriptor objectToImageDescriptor(Object JavaDoc object)
202   {
203     ((Action)action).setHoverImageDescriptor
204       (ExtendedImageRegistry.getInstance().getImageDescriptor(MappingPlugin.getPlugin().getImage("full/ctool16/MatchByType")));
205
206     ((Action)action).setDisabledImageDescriptor
207       (ExtendedImageRegistry.getInstance().getImageDescriptor(MappingPlugin.getPlugin().getImage("full/dtool16/MatchByType")));
208
209     return
210       ExtendedImageRegistry.getInstance().getImageDescriptor(MappingPlugin.getPlugin().getImage("full/etool16/MatchByType"));
211   }
212
213   /**
214    * Match the command for this action
215    */

216   protected Command createActionCommand(EditingDomain editingDomain, final Collection JavaDoc collection)
217   {
218     return editingDomain.createCommand(DelegateCommand.class, new CommandParameter(null, null, collection));
219   }
220 }
221
Popular Tags