1 11 package org.eclipse.ui.views.navigator; 12 13 import java.util.List ; 14 15 import org.eclipse.core.resources.IContainer; 16 import org.eclipse.core.resources.IFile; 17 import org.eclipse.core.resources.IProject; 18 import org.eclipse.core.resources.IResource; 19 import org.eclipse.core.runtime.Assert; 20 import org.eclipse.jface.viewers.IStructuredSelection; 21 import org.eclipse.swt.dnd.Clipboard; 22 import org.eclipse.swt.dnd.FileTransfer; 23 import org.eclipse.swt.dnd.TransferData; 24 import org.eclipse.swt.widgets.Shell; 25 import org.eclipse.ui.PlatformUI; 26 import org.eclipse.ui.actions.CopyFilesAndFoldersOperation; 27 import org.eclipse.ui.actions.CopyProjectOperation; 28 import org.eclipse.ui.actions.SelectionListenerAction; 29 import org.eclipse.ui.internal.views.navigator.ResourceNavigatorMessages; 30 import org.eclipse.ui.part.ResourceTransfer; 31 32 40 class PasteAction extends SelectionListenerAction { 41 42 45 public static final String ID = PlatformUI.PLUGIN_ID + ".PasteAction"; 47 50 private Shell shell; 51 52 55 private Clipboard clipboard; 56 57 63 public PasteAction(Shell shell, Clipboard clipboard) { 64 super(ResourceNavigatorMessages.PasteAction_title); 65 Assert.isNotNull(shell); 66 Assert.isNotNull(clipboard); 67 this.shell = shell; 68 this.clipboard = clipboard; 69 setToolTipText(ResourceNavigatorMessages.PasteAction_toolTip); 70 setId(PasteAction.ID); 71 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, 72 INavigatorHelpContextIds.PASTE_ACTION); 73 } 74 75 81 private IResource getTarget() { 82 List selectedResources = getSelectedResources(); 83 84 for (int i = 0; i < selectedResources.size(); i++) { 85 IResource resource = (IResource) selectedResources.get(i); 86 87 if (resource instanceof IProject && !((IProject) resource).isOpen()) { 88 return null; 89 } 90 if (resource.getType() == IResource.FILE) { 91 resource = resource.getParent(); 92 } 93 if (resource != null) { 94 return resource; 95 } 96 } 97 return null; 98 } 99 100 107 private boolean isLinked(IResource[] resources) { 108 for (int i = 0; i < resources.length; i++) { 109 if (resources[i].isLinked()) { 110 return true; 111 } 112 } 113 return false; 114 } 115 116 119 public void run() { 120 ResourceTransfer resTransfer = ResourceTransfer.getInstance(); 122 IResource[] resourceData = (IResource[]) clipboard 123 .getContents(resTransfer); 124 125 if (resourceData != null && resourceData.length > 0) { 126 if (resourceData[0].getType() == IResource.PROJECT) { 127 for (int i = 0; i < resourceData.length; i++) { 129 CopyProjectOperation operation = new CopyProjectOperation( 130 this.shell); 131 operation.copyProject((IProject) resourceData[i]); 132 } 133 } else { 134 IContainer container = getContainer(); 136 137 CopyFilesAndFoldersOperation operation = new CopyFilesAndFoldersOperation( 138 this.shell); 139 operation.copyResources(resourceData, container); 140 } 141 return; 142 } 143 144 FileTransfer fileTransfer = FileTransfer.getInstance(); 146 String [] fileData = (String []) clipboard.getContents(fileTransfer); 147 148 if (fileData != null) { 149 IContainer container = getContainer(); 151 152 CopyFilesAndFoldersOperation operation = new CopyFilesAndFoldersOperation( 153 this.shell); 154 operation.copyFiles(fileData, container); 155 } 156 } 157 158 161 private IContainer getContainer() { 162 List selection = getSelectedResources(); 163 if (selection.get(0) instanceof IFile) { 164 return ((IFile) selection.get(0)).getParent(); 165 } else { 166 return (IContainer) selection.get(0); 167 } 168 } 169 170 181 protected boolean updateSelection(IStructuredSelection selection) { 182 if (!super.updateSelection(selection)) { 183 return false; 184 } 185 186 final IResource[][] clipboardData = new IResource[1][]; 187 shell.getDisplay().syncExec(new Runnable () { 188 public void run() { 189 ResourceTransfer resTransfer = ResourceTransfer.getInstance(); 191 clipboardData[0] = (IResource[]) clipboard 192 .getContents(resTransfer); 193 } 194 }); 195 IResource[] resourceData = clipboardData[0]; 196 boolean isProjectRes = resourceData != null && resourceData.length > 0 197 && resourceData[0].getType() == IResource.PROJECT; 198 199 if (isProjectRes) { 200 for (int i = 0; i < resourceData.length; i++) { 201 if (resourceData[i].getType() != IResource.PROJECT 204 || ((IProject) resourceData[i]).isOpen() == false) { 205 return false; 206 } 207 } 208 return true; 209 } 210 211 if (getSelectedNonResources().size() > 0) { 212 return false; 213 } 214 215 IResource targetResource = getTarget(); 216 if (targetResource == null) { 219 return false; 220 } 221 222 List selectedResources = getSelectedResources(); 225 if (selectedResources.size() > 1) { 226 for (int i = 0; i < selectedResources.size(); i++) { 227 IResource resource = (IResource) selectedResources.get(i); 228 if (resource.getType() != IResource.FILE) { 229 return false; 230 } 231 if (!targetResource.equals(resource.getParent())) { 232 return false; 233 } 234 } 235 } 236 if (resourceData != null) { 237 if (isLinked(resourceData) 239 && targetResource.getType() != IResource.PROJECT 240 && targetResource.getType() != IResource.FOLDER) { 241 return false; 242 } 243 244 if (targetResource.getType() == IResource.FOLDER) { 245 for (int i = 0; i < resourceData.length; i++) { 247 if (targetResource.equals(resourceData[i])) { 248 return false; 249 } 250 } 251 } 252 return true; 253 } 254 TransferData[] transfers = clipboard.getAvailableTypes(); 255 FileTransfer fileTransfer = FileTransfer.getInstance(); 256 for (int i = 0; i < transfers.length; i++) { 257 if (fileTransfer.isSupportedType(transfers[i])) { 258 return true; 259 } 260 } 261 return false; 262 } 263 } 264 265 | Popular Tags |