1 11 package org.eclipse.ui.internal.navigator.resources.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.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.part.ResourceTransfer; 32 33 41 class CopyAction extends SelectionListenerAction { 42 43 46 public static final String ID = PlatformUI.PLUGIN_ID + ".CopyAction"; 48 51 private Shell shell; 52 53 56 private Clipboard clipboard; 57 58 61 private PasteAction pasteAction; 62 63 69 public CopyAction(Shell shell, Clipboard clipboard) { 70 super("Copy"); Assert.isNotNull(shell); 72 Assert.isNotNull(clipboard); 73 this.shell = shell; 74 this.clipboard = clipboard; 75 setToolTipText("Copy Tooltip"); setId(CopyAction.ID); 77 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, "CopyHelpId"); } 80 81 90 public CopyAction(Shell shell, Clipboard clipboard, PasteAction pasteAction) { 91 this(shell, clipboard); 92 this.pasteAction = pasteAction; 93 } 94 95 100 public void run() { 101 List selectedResources = getSelectedResources(); 102 IResource[] resources = (IResource[]) selectedResources 103 .toArray(new IResource[selectedResources.size()]); 104 105 final int length = resources.length; 107 int actualLength = 0; 108 String [] fileNames = new String [length]; 109 StringBuffer buf = new StringBuffer (); 110 for (int i = 0; i < length; i++) { 111 IPath location = resources[i].getLocation(); 112 if (location != null) { 114 fileNames[actualLength++] = location.toOSString(); 115 } 116 if (i > 0) { 117 buf.append("\n"); } 119 buf.append(resources[i].getName()); 120 } 121 if (actualLength < length) { 123 String [] tempFileNames = fileNames; 124 fileNames = new String [actualLength]; 125 for (int i = 0; i < actualLength; i++) { 126 fileNames[i] = tempFileNames[i]; 127 } 128 } 129 setClipboard(resources, fileNames, buf.toString()); 130 131 if (pasteAction != null && pasteAction.getStructuredSelection() != null) { 134 pasteAction.selectionChanged(pasteAction.getStructuredSelection()); 135 } 136 } 137 138 145 private void setClipboard(IResource[] resources, String [] fileNames, 146 String names) { 147 try { 148 if (fileNames.length > 0) { 150 clipboard.setContents(new Object [] { resources, fileNames, 151 names }, 152 new Transfer[] { ResourceTransfer.getInstance(), 153 FileTransfer.getInstance(), 154 TextTransfer.getInstance() }); 155 } else { 156 clipboard.setContents(new Object [] { resources, names }, 157 new Transfer[] { ResourceTransfer.getInstance(), 158 TextTransfer.getInstance() }); 159 } 160 } catch (SWTError e) { 161 if (e.code != DND.ERROR_CANNOT_SET_CLIPBOARD) { 162 throw e; 163 } 164 if (MessageDialog 165 .openQuestion( 166 shell, 167 "Problem with copy title", "Problem with copy.")) { setClipboard(resources, fileNames, names); 170 } 171 } 172 } 173 174 179 protected boolean updateSelection(IStructuredSelection selection) { 180 if (!super.updateSelection(selection)) { 181 return false; 182 } 183 184 if (getSelectedNonResources().size() > 0) { 185 return false; 186 } 187 188 List selectedResources = getSelectedResources(); 189 if (selectedResources.size() == 0) { 190 return false; 191 } 192 193 boolean projSelected = selectionIsOfType(IResource.PROJECT); 194 boolean fileFoldersSelected = selectionIsOfType(IResource.FILE 195 | IResource.FOLDER); 196 if (!projSelected && !fileFoldersSelected) { 197 return false; 198 } 199 200 if (projSelected && fileFoldersSelected) { 202 return false; 203 } 204 205 IContainer firstParent = ((IResource) selectedResources.get(0)) 207 .getParent(); 208 if (firstParent == null) { 209 return false; 210 } 211 212 Iterator resourcesEnum = selectedResources.iterator(); 213 while (resourcesEnum.hasNext()) { 214 IResource currentResource = (IResource) resourcesEnum.next(); 215 if (!currentResource.getParent().equals(firstParent)) { 216 return false; 217 } 218 if (currentResource.getLocation() == null) { 220 return false; 221 } 222 } 223 224 return true; 225 } 226 227 } 228 229 | Popular Tags |