1 11 package org.eclipse.ui.internal.navigator.resources.actions; 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.part.ResourceTransfer; 30 31 39 class PasteAction extends SelectionListenerAction { 40 41 44 public static final String ID = PlatformUI.PLUGIN_ID + ".PasteAction"; 46 49 private Shell shell; 50 51 54 private Clipboard clipboard; 55 56 62 public PasteAction(Shell shell, Clipboard clipboard) { 63 super("Paste"); Assert.isNotNull(shell); 65 Assert.isNotNull(clipboard); 66 this.shell = shell; 67 this.clipboard = clipboard; 68 setToolTipText("Paste ToolTip"); setId(PasteAction.ID); 70 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, "HelpId"); } 73 74 80 private IResource getTarget() { 81 List selectedResources = getSelectedResources(); 82 83 for (int i = 0; i < selectedResources.size(); i++) { 84 IResource resource = (IResource) selectedResources.get(i); 85 86 if (resource instanceof IProject && !((IProject) resource).isOpen()) { 87 return null; 88 } 89 if (resource.getType() == IResource.FILE) { 90 resource = resource.getParent(); 91 } 92 if (resource != null) { 93 return resource; 94 } 95 } 96 return null; 97 } 98 99 106 private boolean isLinked(IResource[] resources) { 107 for (int i = 0; i < resources.length; i++) { 108 if (resources[i].isLinked()) { 109 return true; 110 } 111 } 112 return false; 113 } 114 115 118 public void run() { 119 ResourceTransfer resTransfer = ResourceTransfer.getInstance(); 121 IResource[] resourceData = (IResource[]) clipboard 122 .getContents(resTransfer); 123 124 if (resourceData != null && resourceData.length > 0) { 125 if (resourceData[0].getType() == IResource.PROJECT) { 126 for (int i = 0; i < resourceData.length; i++) { 128 CopyProjectOperation operation = new CopyProjectOperation( 129 this.shell); 130 operation.copyProject((IProject) resourceData[i]); 131 } 132 } else { 133 IContainer container = getContainer(); 135 136 CopyFilesAndFoldersOperation operation = new CopyFilesAndFoldersOperation( 137 this.shell); 138 operation.copyResources(resourceData, container); 139 } 140 return; 141 } 142 143 FileTransfer fileTransfer = FileTransfer.getInstance(); 145 String [] fileData = (String []) clipboard.getContents(fileTransfer); 146 147 if (fileData != null) { 148 IContainer container = getContainer(); 150 151 CopyFilesAndFoldersOperation operation = new CopyFilesAndFoldersOperation( 152 this.shell); 153 operation.copyFiles(fileData, container); 154 } 155 } 156 157 160 private IContainer getContainer() { 161 List selection = getSelectedResources(); 162 if (selection.get(0) instanceof IFile) { 163 return ((IFile) selection.get(0)).getParent(); 164 } 165 return (IContainer) selection.get(0); 166 } 167 168 179 protected boolean updateSelection(IStructuredSelection selection) { 180 if (!super.updateSelection(selection)) { 181 return false; 182 } 183 184 final IResource[][] clipboardData = new IResource[1][]; 185 shell.getDisplay().syncExec(new Runnable () { 186 public void run() { 187 ResourceTransfer resTransfer = ResourceTransfer.getInstance(); 189 clipboardData[0] = (IResource[]) clipboard 190 .getContents(resTransfer); 191 } 192 }); 193 IResource[] resourceData = clipboardData[0]; 194 boolean isProjectRes = resourceData != null && resourceData.length > 0 195 && resourceData[0].getType() == IResource.PROJECT; 196 197 if (isProjectRes) { 198 for (int i = 0; i < resourceData.length; i++) { 199 if (resourceData[i].getType() != IResource.PROJECT 202 || ((IProject) resourceData[i]).isOpen() == false) { 203 return false; 204 } 205 } 206 return true; 207 } 208 209 if (getSelectedNonResources().size() > 0) { 210 return false; 211 } 212 213 IResource targetResource = getTarget(); 214 if (targetResource == null) { 217 return false; 218 } 219 220 List selectedResources = getSelectedResources(); 223 if (selectedResources.size() > 1) { 224 for (int i = 0; i < selectedResources.size(); i++) { 225 IResource resource = (IResource) selectedResources.get(i); 226 if (resource.getType() != IResource.FILE) { 227 return false; 228 } 229 if (!targetResource.equals(resource.getParent())) { 230 return false; 231 } 232 } 233 } 234 if (resourceData != null) { 235 if (isLinked(resourceData) 237 && targetResource.getType() != IResource.PROJECT) { 238 return false; 239 } 240 241 if (targetResource.getType() == IResource.FOLDER) { 242 for (int i = 0; i < resourceData.length; i++) { 244 if (targetResource.equals(resourceData[i])) { 245 return false; 246 } 247 } 248 } 249 return true; 250 } 251 TransferData[] transfers = clipboard.getAvailableTypes(); 252 FileTransfer fileTransfer = FileTransfer.getInstance(); 253 for (int i = 0; i < transfers.length; i++) { 254 if (fileTransfer.isSupportedType(transfers[i])) { 255 return true; 256 } 257 } 258 return false; 259 } 260 } 261 262 | Popular Tags |