1 17 package org.eclipse.emf.mapping.action; 18 19 20 import java.util.Collection ; 21 import java.util.EventObject ; 22 import java.util.Iterator ; 23 24 import org.eclipse.jface.action.Action; 25 import org.eclipse.jface.action.IAction; 26 import org.eclipse.jface.resource.ImageDescriptor; 27 import org.eclipse.jface.viewers.ISelection; 28 import org.eclipse.jface.viewers.ISelectionProvider; 29 import org.eclipse.ui.IWorkbenchPart; 30 31 import org.eclipse.emf.common.command.Command; 32 import org.eclipse.emf.common.command.CommandStackListener; 33 import org.eclipse.emf.common.command.CommandWrapper; 34 import org.eclipse.emf.edit.command.CommandActionDelegate; 35 import org.eclipse.emf.edit.command.CommandParameter; 36 import org.eclipse.emf.edit.domain.EditingDomain; 37 import org.eclipse.emf.edit.ui.action.CommandAction; 38 import org.eclipse.emf.edit.ui.provider.ExtendedImageRegistry; 39 import org.eclipse.emf.mapping.Mapping; 40 import org.eclipse.emf.mapping.MappingPlugin; 41 import org.eclipse.emf.mapping.command.RemoveMappingCommand; 42 import org.eclipse.emf.mapping.domain.MappingDomain; 43 import org.eclipse.emf.mapping.presentation.IComposedSelection; 44 import org.eclipse.emf.mapping.provider.MappingItemProvider; 45 46 47 49 public class RemoveMappingAction extends CommandAction implements CommandStackListener 50 { 51 public void setActiveWorkbenchPart(IWorkbenchPart workbenchPart) 52 { 53 if (editingDomain != null) 54 { 55 editingDomain.getCommandStack().removeCommandStackListener(this); 56 } 57 58 super.setActiveWorkbenchPart(workbenchPart); 59 60 if (editingDomain != null) 61 { 62 editingDomain.getCommandStack().addCommandStackListener(this); 63 } 64 } 65 66 public void commandStackChanged(EventObject event) 67 { 68 selectionChanged(action, ((ISelectionProvider)workbenchPart).getSelection()); 69 } 70 71 public void selectionChanged(IAction action, ISelection selection) 72 { 73 if (selection instanceof IComposedSelection) 74 { 75 super.selectionChanged(action,((IComposedSelection)selection).getCombinedSelection()); 76 } 77 else 78 { 79 super.selectionChanged(action, selection); 80 } 81 } 82 83 86 protected Object getDefaultImage() 87 { 88 return MappingPlugin.getPlugin().getImage("full/etool16/RemoveOneToOneMapping"); 89 } 90 91 public static class CommandDelegate extends CommandWrapper implements CommandActionDelegate 92 { 93 protected MappingDomain mappingDomain; 94 protected Collection collection; 95 public CommandDelegate(EditingDomain editingDomain, CommandParameter commandParameter) 96 { 97 super(); 98 mappingDomain = (MappingDomain)editingDomain; 99 collection = commandParameter.getCollection(); 100 } 101 102 105 public Object getImage() 106 { 107 return "Placeholder"; 108 } 109 110 public String getText() 111 { 112 return getLabel(); 113 } 114 115 118 public String getToolTipText() 119 { 120 return getDescription(); 121 } 122 123 public Command createCommand() 124 { 125 boolean allMappings = true; 126 for (Iterator objects = collection.iterator(); objects.hasNext(); ) 127 { 128 Object object = objects.next(); 129 if (!(object instanceof Mapping)) 130 { 131 allMappings = false; 132 break; 133 } 134 } 135 136 if (allMappings) 137 { 138 return RemoveMappingCommand.create(mappingDomain, collection); 139 } 140 else 141 { 142 Collection mappings = mappingDomain.getMappingRoot().getExactMappings(collection); 143 return RemoveMappingCommand.create(mappingDomain, mappings); 144 } 145 } 146 } 147 148 protected ImageDescriptor objectToImageDescriptor(Object object) 149 { 150 MappingDomain mappingDomain = (MappingDomain)editingDomain; 151 152 ((Action)action).setHoverImageDescriptor 153 (ExtendedImageRegistry.getInstance().getImageDescriptor 154 (MappingItemProvider.getImage(mappingDomain.getMappingRoot(), "full/ctool16/Remove", collection))); 155 156 ((Action)action).setDisabledImageDescriptor 157 (ExtendedImageRegistry.getInstance().getImageDescriptor 158 (MappingItemProvider.getImage(mappingDomain.getMappingRoot(), "full/dtool16/Remove", collection))); 159 160 ImageDescriptor result= 161 ExtendedImageRegistry.getInstance().getImageDescriptor 162 (MappingItemProvider.getImage(mappingDomain.getMappingRoot(), "full/etool16/Remove", collection)); 163 164 ((Action)action).setEnabled(!action.isEnabled()); 165 ((Action)action).setImageDescriptor(result); 166 ((Action)action).setEnabled(!action.isEnabled()); 167 168 return result; 169 } 170 171 174 protected Command createActionCommand(EditingDomain editingDomain, Collection collection) 175 { 176 return editingDomain.createCommand(CommandDelegate.class, new CommandParameter(null, null, collection)); 177 } 178 } 179 | Popular Tags |