1 11 package org.eclipse.team.internal.ui.synchronize.actions; 12 13 import java.util.Iterator ; 14 import java.util.List ; 15 import org.eclipse.core.resources.IResource; 16 import org.eclipse.core.runtime.IAdaptable; 17 import org.eclipse.jface.action.IMenuManager; 18 import org.eclipse.jface.action.MenuManager; 19 import org.eclipse.jface.viewers.*; 20 import org.eclipse.swt.widgets.Shell; 21 import org.eclipse.team.internal.ui.TeamUIMessages; 22 import org.eclipse.team.internal.ui.Utils; 23 import org.eclipse.team.ui.synchronize.ISynchronizePageSite; 24 import org.eclipse.ui.*; 25 import org.eclipse.ui.actions.*; 26 import org.eclipse.ui.navigator.INavigatorContentService; 27 28 33 public class RefactorActionGroup extends ActionGroup { 34 35 private CopyToClipboardAction copyAction; 36 private MoveResourceAction moveAction; 37 private RenameResourceAction renameAction; 38 private ISynchronizePageSite site; 39 private DeleteResourceAction deleteAction; 40 private final INavigatorContentService navigatorContentService; 41 42 public RefactorActionGroup(ISynchronizePageSite site) { 43 this(site, null); 44 } 45 46 public RefactorActionGroup(ISynchronizePageSite site, INavigatorContentService navigatorContentService) { 47 this.site = site; 48 this.navigatorContentService = navigatorContentService; 49 makeActions(); 50 } 51 52 public void fillContextMenu(IMenuManager parentMenu, String groupId) { 53 54 final MenuManager menu = new MenuManager(TeamUIMessages.RefactorActionGroup_0); 55 56 final IStructuredSelection selection= getSelection(); 57 final boolean anyResourceSelected = !selection.isEmpty() && allResourcesAreOfType(selection, IResource.PROJECT | IResource.FOLDER | IResource.FILE); 58 59 copyAction.selectionChanged(getObjectSelection()); 61 menu.add(copyAction); 62 63 if (anyResourceSelected) { 64 deleteAction.selectionChanged(selection); 65 moveAction.selectionChanged(selection); 66 renameAction.selectionChanged(selection); 67 menu.add(deleteAction); 68 menu.add(moveAction); 69 menu.add(renameAction); 70 } 71 parentMenu.appendToGroup(groupId, menu); 72 } 73 74 public void fillActionBars(IActionBars actionBars) { 75 actionBars.setGlobalActionHandler(ActionFactory.COPY.getId(), copyAction); 76 actionBars.setGlobalActionHandler(ActionFactory.DELETE.getId(), deleteAction); 77 actionBars.setGlobalActionHandler(ActionFactory.RENAME.getId(), renameAction); 78 actionBars.setGlobalActionHandler(ActionFactory.MOVE.getId(), moveAction); 79 } 80 81 public void updateActionBars() { 82 final IStructuredSelection structuredSelection= getSelection(); 83 copyAction.selectionChanged(getObjectSelection()); 84 deleteAction.selectionChanged(structuredSelection); 85 moveAction.selectionChanged(structuredSelection); 86 renameAction.selectionChanged(structuredSelection); 87 } 88 89 protected void makeActions() { 90 91 final Shell shell = site.getShell(); 92 final ISharedImages images = PlatformUI.getWorkbench().getSharedImages(); 93 94 copyAction= new CopyToClipboardAction(shell, navigatorContentService); 95 moveAction= new MoveResourceAction(shell); 96 renameAction= new RenameResourceAction(shell); 97 deleteAction = new DeleteResourceAction(shell) { 98 protected List getSelectedResources() { 99 return getSelection().toList(); } 101 }; 102 103 copyAction.setImageDescriptor(images.getImageDescriptor(ISharedImages.IMG_TOOL_COPY)); 104 copyAction.setDisabledImageDescriptor(images.getImageDescriptor(ISharedImages.IMG_TOOL_COPY_DISABLED)); 105 106 deleteAction.setImageDescriptor(images.getImageDescriptor(ISharedImages.IMG_TOOL_DELETE)); 107 deleteAction.setDisabledImageDescriptor(images.getImageDescriptor(ISharedImages.IMG_TOOL_DELETE_DISABLED)); 108 } 109 110 private IStructuredSelection getSelection() { 111 final ISelection selection= getContext().getSelection(); 112 113 if (!(selection instanceof IStructuredSelection)) 114 return new StructuredSelection(); 115 116 return new StructuredSelection(Utils.getResources(((IStructuredSelection)selection).toArray())); 117 } 118 119 private IStructuredSelection getObjectSelection() { 120 final ISelection selection= getContext().getSelection(); 121 122 if (!(selection instanceof IStructuredSelection)) 123 return new StructuredSelection(); 124 125 return (IStructuredSelection)selection; 126 } 127 128 private boolean allResourcesAreOfType(IStructuredSelection selection, int resourceMask) { 129 Iterator resources = selection.iterator(); 130 while (resources.hasNext()) { 131 Object next = resources.next(); 132 IResource resource = null; 133 if (next instanceof IResource) { 134 resource = (IResource)next; 135 } else if (next instanceof IAdaptable) { 136 IAdaptable adaptable = (IAdaptable)next; 137 resource = (IResource)adaptable.getAdapter(IResource.class); 138 } 139 if(resource == null) { 140 IResource[] r = Utils.getResources(new Object [] {next}); 141 if(r.length == 1) { 142 resource = r[0]; 143 } 144 } 145 if (resource == null || (resource.getType() & resourceMask) == 0) { 146 return false; 147 } 148 } 149 return true; 150 } 151 152 155 public void dispose() { 156 super.dispose(); 157 copyAction.dispose(); 158 } 159 } 160 | Popular Tags |