1 11 package org.eclipse.team.internal.ui.synchronize.actions; 12 13 import org.eclipse.core.resources.IResource; 14 import org.eclipse.jface.action.IMenuManager; 15 import org.eclipse.jface.action.MenuManager; 16 import org.eclipse.jface.viewers.ISelection; 17 import org.eclipse.jface.viewers.IStructuredSelection; 18 import org.eclipse.team.internal.ui.TeamUIMessages; 19 import org.eclipse.team.internal.ui.Utils; 20 import org.eclipse.team.ui.synchronize.*; 21 import org.eclipse.ui.IWorkbenchSite; 22 import org.eclipse.ui.actions.ActionGroup; 23 import org.eclipse.ui.actions.OpenWithMenu; 24 25 29 public class OpenWithActionGroup extends ActionGroup { 30 31 private OpenFileInSystemEditorAction openFileAction; 32 private OpenInCompareAction openInCompareAction; 33 private final boolean includeOpenInCompare; 34 private final ISynchronizePageConfiguration configuration; 35 36 public OpenWithActionGroup(ISynchronizePageConfiguration configuration, boolean includeOpenInCompare) { 37 this.configuration = configuration; 38 this.includeOpenInCompare = includeOpenInCompare; 39 makeActions(); 40 } 41 42 protected void makeActions() { 43 IWorkbenchSite ws = getSite().getWorkbenchSite(); 44 if (ws != null) { 45 openFileAction = new OpenFileInSystemEditorAction(ws.getPage()); 46 if (includeOpenInCompare) 47 openInCompareAction = new OpenInCompareAction(configuration); 48 } 49 } 50 51 private ISynchronizeParticipant getParticipant() { 52 return configuration.getParticipant(); 53 } 54 55 private ISynchronizePageSite getSite() { 56 return configuration.getSite(); 57 } 58 59 public void fillContextMenu(IMenuManager menu, String groupId) { 60 ISelection selection = getSite().getSelectionProvider().getSelection(); 61 if (selection instanceof IStructuredSelection && !hasFileMenu(menu)) { 62 fillOpenWithMenu(menu, groupId, (IStructuredSelection)selection); 63 } 64 } 65 66 private boolean hasFileMenu(IMenuManager menu) { 67 return menu.find(openFileAction.getId()) != null; 68 } 69 70 76 private void fillOpenWithMenu(IMenuManager menu, String groupId, IStructuredSelection selection) { 77 78 if (selection == null || selection.size() < 1) 80 return; 81 Object [] elements = selection.toArray(); 82 IResource resources[] = Utils.getResources(elements); 83 if(resources.length == 0 && openInCompareAction != null) { 84 if (elements.length > 0) { 86 ISynchronizeParticipant participant = getParticipant(); 87 if (participant instanceof ModelSynchronizeParticipant) { 88 ModelSynchronizeParticipant msp = (ModelSynchronizeParticipant) participant; 89 boolean allElementsHaveCompareInput = true; 90 for (int i = 0; i < elements.length; i++) { 91 if (!msp.hasCompareInputFor(elements[i])) { 92 allElementsHaveCompareInput = false; 93 break; 94 } 95 } 96 if (allElementsHaveCompareInput) { 97 menu.appendToGroup(groupId, openInCompareAction); 98 } 99 } 100 } 101 return; 102 } 103 104 for (int i = 0; i < resources.length; i++) { 105 if (resources[i].getType() != IResource.FILE) { 106 return; 108 } 109 } 110 111 if (resources.length > 0 && openInCompareAction != null) { 112 menu.appendToGroup(groupId, openInCompareAction); 114 } 115 116 for (int i = 0; i < resources.length; i++) { 117 if (!resources[i].exists()) { 118 return; 120 } 121 } 122 123 if (openFileAction != null) { 124 openFileAction.selectionChanged(selection); 125 menu.appendToGroup(groupId, openFileAction); 126 } 127 128 if (resources.length == 1) { 129 IWorkbenchSite ws = getSite().getWorkbenchSite(); 131 if (ws != null) { 132 MenuManager submenu = 133 new MenuManager(TeamUIMessages.OpenWithActionGroup_0); 134 submenu.add(new OpenWithMenu(ws.getPage(), resources[0])); 135 menu.appendToGroup(groupId, submenu); 136 } 137 } 138 } 139 140 public void openInCompareEditor() { 141 if (openInCompareAction != null) 142 openInCompareAction.run(); 143 } 144 } 145 | Popular Tags |