1 11 package org.eclipse.team.internal.ui.synchronize.actions; 12 13 import java.util.ArrayList ; 14 import java.util.Arrays ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 18 import org.eclipse.compare.ITypedElement; 19 import org.eclipse.core.resources.IContainer; 20 import org.eclipse.core.resources.IResource; 21 import org.eclipse.core.runtime.Assert; 22 import org.eclipse.core.runtime.IPath; 23 import org.eclipse.jface.dialogs.MessageDialog; 24 import org.eclipse.jface.viewers.*; 25 import org.eclipse.swt.SWTError; 26 import org.eclipse.swt.dnd.*; 27 import org.eclipse.swt.widgets.Shell; 28 import org.eclipse.team.internal.ui.*; 29 import org.eclipse.ui.actions.SelectionListenerAction; 30 import org.eclipse.ui.navigator.INavigatorContentService; 31 import org.eclipse.ui.part.ResourceTransfer; 32 33 40 class CopyToClipboardAction extends SelectionListenerAction { 41 42 private static final String EOL = System.getProperty("line.separator", "\n"); 44 private final static String ID= TeamUIPlugin.PLUGIN_ID + ".synchronize.action.copy"; 46 private final Shell fShell; 47 private final Clipboard fClipboard; 48 49 private final INavigatorContentService navigatorContentService; 50 51 protected CopyToClipboardAction(Shell shell, INavigatorContentService navigatorContentService) { 52 super(TeamUIMessages.CopyToClipboardAction_1); 53 this.navigatorContentService = navigatorContentService; 54 Assert.isNotNull(shell); 55 fShell= shell; 56 fClipboard= new Clipboard(shell.getDisplay()); 57 setToolTipText(TeamUIMessages.CopyToClipboardAction_2); 58 setId(ID); 59 } 60 61 public void run() { 62 copyResources(getSelectedResources(), getTextualClipboardContents()); 63 } 64 65 70 private String getTextualClipboardContents() { 71 StringBuffer buf = new StringBuffer (); 72 int i = 0; 73 IStructuredSelection structuredSelection = getStructuredSelection(); 74 if (structuredSelection instanceof TreeSelection) { 75 TreeSelection ts = (TreeSelection) structuredSelection; 76 TreePath[] paths = ts.getPaths(); 77 for (int j = 0; j < paths.length; j++) { 78 TreePath path = paths[j]; 79 String text = getTextFor(path); 80 if (text != null && text.length() > 0) { 81 if (i > 0) 82 buf.append(EOL); 83 buf.append(text); 84 i++; 85 } 86 } 87 } else { 88 for (Iterator it = structuredSelection.iterator(); it.hasNext();) { 89 Object element = it.next(); 90 if (element instanceof ITypedElement) { 91 if (i > 0) 92 buf.append(EOL); 93 buf.append(((ITypedElement)element).getName()); 94 i++; 95 } else { 96 IResource resource = Utils.getResource(element); 97 if (resource != null) { 98 if (i > 0) 99 buf.append(EOL); 100 buf.append(resource.getName()); 101 i++; 102 } 103 } 104 } 105 } 106 return buf.toString(); 107 } 108 109 private String getTextFor(TreePath path) { 110 Object element = path.getLastSegment(); 111 if (element instanceof ITypedElement) { 112 return ((ITypedElement)element).getName(); 113 } 114 INavigatorContentService service = getNavigatorContentService(); 115 if (service != null) { 116 ILabelProvider provider = service.createCommonLabelProvider(); 117 if (provider instanceof ITreePathLabelProvider) { 118 ITreePathLabelProvider tplp = (ITreePathLabelProvider) provider; 119 ViewerLabel viewerLabel = new ViewerLabel("", null); tplp.updateLabel(viewerLabel, path); 121 return viewerLabel.getText(); 122 } 123 return provider.getText(element); 124 } 125 if (element instanceof IResource) { 126 IResource resource = (IResource) element; 127 return resource.getName(); 128 } 129 return null; 130 } 131 132 private INavigatorContentService getNavigatorContentService() { 133 return navigatorContentService; 134 } 135 136 private void copyResources(List selectedResources, String text) { 137 IResource[] resources = (IResource[]) selectedResources.toArray(new IResource[selectedResources.size()]); 138 final int length = resources.length; 140 int actualLength = 0; 141 String [] fileNames = new String [length]; 142 for (int i = 0; i < length; i++) { 143 final IPath location = resources[i].getLocation(); 144 if (location != null) 146 fileNames[actualLength++] = location.toOSString(); 147 } 148 if (actualLength < length) { 150 String [] tempFileNames = fileNames; 151 fileNames = new String [actualLength]; 152 for (int i = 0; i < actualLength; i++) 153 fileNames[i] = tempFileNames[i]; 154 } 155 setClipboard(resources, fileNames, text); 156 } 157 158 165 private void setClipboard(IResource[] resources, String [] fileNames, String names) { 166 try { 167 List data = new ArrayList (); 169 List dataTypes = new ArrayList (); 170 if (resources.length > 0) { 171 data.add(resources); 172 dataTypes.add(ResourceTransfer.getInstance()); 173 } 174 if (fileNames.length > 0) { 175 data.add(fileNames); 176 dataTypes.add(FileTransfer.getInstance()); 177 } 178 if (names != null && names.length() > 0) { 179 data.add(names); 180 dataTypes.add(TextTransfer.getInstance()); 181 } 182 if (!data.isEmpty()) 183 fClipboard.setContents( 184 data.toArray(), 185 (Transfer[]) dataTypes.toArray(new Transfer[dataTypes.size()])); 186 } catch (SWTError e) { 187 if (e.code != DND.ERROR_CANNOT_SET_CLIPBOARD) 188 throw e; 189 if (MessageDialog.openQuestion(fShell, TeamUIMessages.CopyToClipboardAction_3, TeamUIMessages.CopyToClipboardAction_4)) 190 setClipboard(resources, fileNames, names); 191 } 192 } 193 194 protected boolean updateSelection(IStructuredSelection selection) { 195 if (!super.updateSelection(selection)) 196 return false; 197 List selectedResources = getSelectedResources(); 201 List selectedNonResources = getSelectedNonResources(); 202 if (selectedResources.size() > 0 && selectedNonResources.size() == 0) { 203 boolean projSelected = selectionIsOfType(IResource.PROJECT); 204 boolean fileFoldersSelected = selectionIsOfType(IResource.FILE | IResource.FOLDER); 205 if (!projSelected && !fileFoldersSelected) 206 return false; 207 if (projSelected && fileFoldersSelected) 209 return false; 210 IContainer firstParent = ((IResource) selectedResources.get(0)).getParent(); 212 if (firstParent == null) 213 return false; 214 Iterator resourcesEnum = selectedResources.iterator(); 215 while (resourcesEnum.hasNext()) { 216 IResource currentResource = (IResource) resourcesEnum.next(); 217 if (!currentResource.getParent().equals(firstParent)) 218 return false; 219 if (currentResource.getLocation() == null) 221 return false; 222 } 223 return true; 224 } else if (selectedNonResources.size() > 0 && selectedResources.size() == 0) { 225 return true; 226 } 227 return false; 228 } 229 230 protected List getSelectedNonResources() { 231 return Arrays.asList(Utils.getNonResources(getStructuredSelection().toArray())); 232 } 233 234 protected List getSelectedResources() { 235 return Arrays.asList(Utils.getResources(getStructuredSelection().toArray())); 239 } 240 241 public void dispose() { 242 fClipboard.dispose(); 243 } 244 } 245 | Popular Tags |