1 11 package org.eclipse.ui.views.navigator; 12 13 import java.util.Iterator ; 14 import java.util.List ; 15 16 import org.eclipse.core.resources.IContainer; 17 import org.eclipse.core.resources.IResource; 18 import org.eclipse.core.runtime.Assert; 19 import org.eclipse.core.runtime.IPath; 20 import org.eclipse.jface.dialogs.MessageDialog; 21 import org.eclipse.jface.viewers.IStructuredSelection; 22 import org.eclipse.swt.SWTError; 23 import org.eclipse.swt.dnd.Clipboard; 24 import org.eclipse.swt.dnd.DND; 25 import org.eclipse.swt.dnd.FileTransfer; 26 import org.eclipse.swt.dnd.TextTransfer; 27 import org.eclipse.swt.dnd.Transfer; 28 import org.eclipse.swt.widgets.Shell; 29 import org.eclipse.ui.PlatformUI; 30 import org.eclipse.ui.actions.SelectionListenerAction; 31 import org.eclipse.ui.internal.views.navigator.ResourceNavigatorMessages; 32 import org.eclipse.ui.part.ResourceTransfer; 33 34 42 class CopyAction extends SelectionListenerAction { 43 44 47 public static final String ID = PlatformUI.PLUGIN_ID + ".CopyAction"; 49 52 private Shell shell; 53 54 57 private Clipboard clipboard; 58 59 62 private PasteAction pasteAction; 63 64 70 public CopyAction(Shell shell, Clipboard clipboard) { 71 super(ResourceNavigatorMessages.CopyAction_title); 72 Assert.isNotNull(shell); 73 Assert.isNotNull(clipboard); 74 this.shell = shell; 75 this.clipboard = clipboard; 76 setToolTipText(ResourceNavigatorMessages.CopyAction_toolTip); 77 setId(CopyAction.ID); 78 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, 79 INavigatorHelpContextIds.COPY_ACTION); 80 } 81 82 91 public CopyAction(Shell shell, Clipboard clipboard, PasteAction pasteAction) { 92 this(shell, clipboard); 93 this.pasteAction = pasteAction; 94 } 95 96 97 100 public void run() { 101 106 List selectedResources = getSelectedResources(); 107 IResource[] resources = (IResource[]) selectedResources 108 .toArray(new IResource[selectedResources.size()]); 109 110 final int length = resources.length; 112 int actualLength = 0; 113 String [] fileNames = new String [length]; 114 StringBuffer buf = new StringBuffer (); 115 for (int i = 0; i < length; i++) { 116 IPath location = resources[i].getLocation(); 117 if (location != null) { 119 fileNames[actualLength++] = location.toOSString(); 120 } 121 if (i > 0) { 122 buf.append("\n"); } 124 buf.append(resources[i].getName()); 125 } 126 if (actualLength < length) { 128 String [] tempFileNames = fileNames; 129 fileNames = new String [actualLength]; 130 for (int i = 0; i < actualLength; i++) { 131 fileNames[i] = tempFileNames[i]; 132 } 133 } 134 setClipboard(resources, fileNames, buf.toString()); 135 136 if (pasteAction != null && pasteAction.getStructuredSelection() != null) { 139 pasteAction.selectionChanged(pasteAction.getStructuredSelection()); 140 } 141 } 142 143 150 private void setClipboard(IResource[] resources, String [] fileNames, 151 String names) { 152 try { 153 if (fileNames.length > 0) { 155 clipboard.setContents(new Object [] { resources, fileNames, 156 names }, 157 new Transfer[] { ResourceTransfer.getInstance(), 158 FileTransfer.getInstance(), 159 TextTransfer.getInstance() }); 160 } else { 161 clipboard.setContents(new Object [] { resources, names }, 162 new Transfer[] { ResourceTransfer.getInstance(), 163 TextTransfer.getInstance() }); 164 } 165 } catch (SWTError e) { 166 if (e.code != DND.ERROR_CANNOT_SET_CLIPBOARD) { 167 throw e; 168 } 169 if (MessageDialog 170 .openQuestion( 171 shell, 172 ResourceNavigatorMessages.CopyToClipboardProblemDialog_title, ResourceNavigatorMessages.CopyToClipboardProblemDialog_message)) { 173 setClipboard(resources, fileNames, names); 174 } 175 } 176 } 177 178 179 182 protected boolean updateSelection(IStructuredSelection selection) { 183 184 189 190 if (!super.updateSelection(selection)) { 191 return false; 192 } 193 194 if (getSelectedNonResources().size() > 0) { 195 return false; 196 } 197 198 List selectedResources = getSelectedResources(); 199 if (selectedResources.size() == 0) { 200 return false; 201 } 202 203 boolean projSelected = selectionIsOfType(IResource.PROJECT); 204 boolean fileFoldersSelected = selectionIsOfType(IResource.FILE 205 | IResource.FOLDER); 206 if (!projSelected && !fileFoldersSelected) { 207 return false; 208 } 209 210 if (projSelected && fileFoldersSelected) { 212 return false; 213 } 214 215 IContainer firstParent = ((IResource) selectedResources.get(0)) 217 .getParent(); 218 if (firstParent == null) { 219 return false; 220 } 221 222 Iterator resourcesEnum = selectedResources.iterator(); 223 while (resourcesEnum.hasNext()) { 224 IResource currentResource = (IResource) resourcesEnum.next(); 225 if (!currentResource.getParent().equals(firstParent)) { 226 return false; 227 } 228 } 229 230 return true; 231 } 232 233 } 234 235 | Popular Tags |