1 17 package org.eclipse.emf.mapping.action; 18 19 20 import java.util.Collection ; 21 import java.util.EventObject ; 22 import java.util.HashSet ; 23 import java.util.Iterator ; 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 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 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 89 protected Object 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 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 mappedObjects = new HashSet (); 116 Collection mappingObjects = new HashSet (); 117 MappingRoot mappingRoot = mappingDomain.getMappingRoot(); 118 119 for (Iterator objects = collection.iterator(); objects.hasNext(); ) 120 { 121 Object 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 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 173 public Object getImage() 174 { 175 return "Placeholder"; 176 } 177 178 public String getText() 179 { 180 return getLabel(); 181 } 182 183 186 public String 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 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 216 protected Command createActionCommand(EditingDomain editingDomain, final Collection collection) 217 { 218 return editingDomain.createCommand(DelegateCommand.class, new CommandParameter(null, null, collection)); 219 } 220 } 221 | Popular Tags |