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.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 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 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 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 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 mappedObjects = new HashSet (); 117 Collection mappingObjects = new HashSet (); 118 MappingRoot mappingRoot = mappingDomain.getMappingRoot(); 119 120 for (Iterator objects = collection.iterator(); objects.hasNext(); ) 121 { 122 Object 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 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 174 public Object getImage() 175 { 176 return "Placeholder"; 177 } 178 179 public String getText() 180 { 181 return getLabel(); 182 } 183 184 187 public String getToolTipText() 188 { 189 return getDescription(); 190 } 191 } 192 193 protected ImageDescriptor objectToImageDescriptor(Object 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 208 protected Command createActionCommand(EditingDomain editingDomain, final Collection collection) 209 { 210 return editingDomain.createCommand(DelegateCommand.class, new CommandParameter(null, null, collection)); 211 } 212 } 213 | Popular Tags |