1 11 package org.eclipse.ui.actions; 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.resources.IWorkspace; 19 import org.eclipse.core.resources.IWorkspaceRoot; 20 import org.eclipse.core.runtime.IPath; 21 import org.eclipse.jface.viewers.IStructuredSelection; 22 import org.eclipse.swt.widgets.Shell; 23 import org.eclipse.ui.PlatformUI; 24 import org.eclipse.ui.dialogs.ContainerSelectionDialog; 25 import org.eclipse.ui.dialogs.ISelectionValidator; 26 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages; 27 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin; 28 import org.eclipse.ui.internal.ide.IIDEHelpContextIds; 29 30 37 public class CopyResourceAction extends SelectionListenerAction implements 38 ISelectionValidator { 39 40 43 public static final String ID = PlatformUI.PLUGIN_ID 44 + ".CopyResourceAction"; 46 49 private Shell shell; 50 51 55 protected CopyFilesAndFoldersOperation operation; 56 57 private String [] modelProviderIds; 58 59 73 public static IPath getNewNameFor(IPath originalName, IWorkspace workspace) { 74 return CopyFilesAndFoldersOperation.getAutoNewNameFor(originalName, 75 workspace); 76 } 77 78 83 public CopyResourceAction(Shell shell) { 84 this(shell, IDEWorkbenchMessages.CopyResourceAction_title); 85 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, 86 IIDEHelpContextIds.COPY_RESOURCE_ACTION); 87 } 88 89 96 CopyResourceAction(Shell shell, String name) { 97 super(name); 98 setToolTipText(IDEWorkbenchMessages.CopyResourceAction_toolTip); 99 setId(CopyResourceAction.ID); 100 if (shell == null) { 101 throw new IllegalArgumentException (); 102 } 103 this.shell = shell; 104 } 105 106 111 protected CopyFilesAndFoldersOperation createOperation() { 112 return new CopyFilesAndFoldersOperation(getShell()); 113 } 114 115 120 IContainer getInitialContainer() { 121 List resources = getSelectedResources(); 122 if (resources.size() > 0) { 123 IResource resource = (IResource) resources.get(0); 124 return resource.getParent(); 125 } 126 return null; 127 } 128 129 136 protected IResource[] getResources(List resourceList) { 137 return (IResource[]) resourceList.toArray(new IResource[resourceList 138 .size()]); 139 } 140 141 145 Shell getShell() { 146 return shell; 147 } 148 149 154 public String isValid(Object destination) { 155 IWorkspaceRoot root = IDEWorkbenchPlugin.getPluginWorkspace().getRoot(); 156 IContainer container = (IContainer) root 157 .findMember((IPath) destination); 158 159 if (container != null) { 160 CopyFilesAndFoldersOperation newOperation = createOperation(); 163 List sources = getSelectedResources(); 164 IResource[] resources = (IResource[]) sources 165 .toArray(new IResource[sources.size()]); 166 return newOperation.validateDestination(container, resources); 167 } 168 return null; 169 } 170 171 177 IPath queryDestinationResource() { 178 ContainerSelectionDialog dialog = new ContainerSelectionDialog(shell, 181 getInitialContainer(), true, IDEWorkbenchMessages.CopyResourceAction_selectDestination); 182 dialog.setValidator(this); 183 dialog.showClosedProjects(false); 184 dialog.open(); 185 Object [] result = dialog.getResult(); 186 if (result != null && result.length == 1) { 187 return (IPath) result[0]; 188 } 189 return null; 190 } 191 192 195 public void run() { 196 try { 197 operation = createOperation(); 198 operation.setModelProviderIds(getModelProviderIds()); 199 200 List sources = getSelectedResources(); 205 206 IPath destination = queryDestinationResource(); 207 if (destination == null) { 208 return; 209 } 210 211 IWorkspaceRoot root = IDEWorkbenchPlugin.getPluginWorkspace() 212 .getRoot(); 213 IContainer container = (IContainer) root.findMember(destination); 214 if (container == null) { 215 return; 216 } 217 218 runOperation(getResources(sources), container); 219 } finally { 220 operation = null; 221 } 222 } 223 224 230 protected void runOperation(IResource[] resources, IContainer destination) { 231 operation.copyResources(resources, destination); 232 } 233 234 240 protected boolean updateSelection(IStructuredSelection selection) { 241 if (!super.updateSelection(selection)) { 242 return false; 243 } 244 if (getSelectedNonResources().size() > 0) { 245 return false; 246 } 247 248 List selectedResources = getSelectedResources(); 250 if (selectedResources.size() == 0) { 251 return false; 252 } 253 IContainer firstParent = ((IResource) selectedResources.get(0)) 254 .getParent(); 255 if (firstParent == null) { 256 return false; 257 } 258 Iterator resourcesEnum = selectedResources.iterator(); 259 while (resourcesEnum.hasNext()) { 260 IResource currentResource = (IResource) resourcesEnum.next(); 261 if (!currentResource.exists()) { 262 return false; 263 } 264 if (currentResource.getType() == IResource.PROJECT) { 265 return false; 266 } 267 IContainer parent = currentResource.getParent(); 268 if ((parent != null) && (!parent.equals(firstParent))) { 269 return false; 270 } 271 } 272 return true; 273 } 274 275 283 public String [] getModelProviderIds() { 284 return modelProviderIds; 285 } 286 287 296 public void setModelProviderIds(String [] modelProviderIds) { 297 this.modelProviderIds = modelProviderIds; 298 } 299 } 300 | Popular Tags |