|
Added
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2008 IBM Corporation and others. |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* http://www.eclipse.org/legal/epl-v10.html |
| 7 |
* |
| 8 |
* Contributors: |
| 9 |
* IBM Corporation - initial API and implementation |
| 10 |
*******************************************************************************/ |
| 11 |
package org.eclipse.team.internal.ui.synchronize.actions; |
| 12 |
|
| 13 |
import java.util.ArrayList; |
| 14 |
import java.util.Arrays; |
| 15 |
import java.util.Iterator; |
| 16 |
import java.util.List; |
| 17 |
|
| 18 |
import org.eclipse.compare.ITypedElement; |
| 19 |
import org.eclipse.core.resources.IResource; |
| 20 |
import org.eclipse.core.runtime.Assert; |
| 21 |
import org.eclipse.core.runtime.IPath; |
| 22 |
import org.eclipse.jface.dialogs.MessageDialog; |
| 23 |
import org.eclipse.jface.viewers.*; |
| 24 |
import org.eclipse.swt.SWTError; |
| 25 |
import org.eclipse.swt.dnd.*; |
| 26 |
import org.eclipse.swt.widgets.Shell; |
| 27 |
import org.eclipse.team.internal.ui.*; |
| 28 |
import org.eclipse.ui.actions.SelectionListenerAction; |
| 29 |
import org.eclipse.ui.navigator.INavigatorContentService; |
| 30 |
import org.eclipse.ui.part.ResourceTransfer; |
| 31 |
|
| 32 |
/** |
| 33 |
* Based on org.eclipse.ui.views.navigator.CopyAction with the additional support for |
| 34 |
* copying any non-resource object in the selection and putting the toString() as |
| 35 |
* a text transfer. |
| 36 |
* |
| 37 |
* @since 3.1 |
| 38 |
*/ |
| 39 |
class CopyQualifiedNameToClipboardAction extends SelectionListenerAction { |
| 40 |
|
| 41 |
private static final String EOL = System.getProperty("line.separator", "\n"); //$NON-NLS-1$ //$NON-NLS-2$ |
| 42 |
|
| 43 |
private static final String SEP = "/"; //$NON-NLS-1$ |
| 44 |
|
| 45 |
private final static String ID= TeamUIPlugin.PLUGIN_ID + ".synchronize.action.copytext"; //$NON-NLS-1$ |
| 46 |
|
| 47 |
private final Shell fShell; |
| 48 |
private final Clipboard fClipboard; |
| 49 |
|
| 50 |
private final INavigatorContentService navigatorContentService; |
| 51 |
|
| 52 |
protected CopyQualifiedNameToClipboardAction(Shell shell, INavigatorContentService navigatorContentService) { |
| 53 |
super(TeamUIMessages.CopyQualifiedNameToClipboardAction_1); |
| 54 |
this.navigatorContentService = navigatorContentService; |
| 55 |
Assert.isNotNull(shell); |
| 56 |
fShell= shell; |
| 57 |
fClipboard= new Clipboard(shell.getDisplay()); |
| 58 |
setToolTipText(TeamUIMessages.CopyQualifiedNameToClipboardAction_2); |
| 59 |
setId(ID); |
| 60 |
} |
| 61 |
|
| 62 |
public void run() { |
| 63 |
copyResources(getSelectedResources(), getTextualClipboardContents()); |
| 64 |
} |
| 65 |
|
| 66 |
/* |
| 67 |
* Return a text representation of all selected elements. |
| 68 |
* Use the name from the tree node so what is copied |
| 69 |
* matches what appears in the tree. |
| 70 |
*/ |
| 71 |
private String getTextualClipboardContents() { |
| 72 |
StringBuffer buf = new StringBuffer(); |
| 73 |
int i = 0; |
| 74 |
IStructuredSelection structuredSelection = getStructuredSelection(); |
| 75 |
if (structuredSelection instanceof TreeSelection) { |
| 76 |
TreeSelection ts = (TreeSelection) structuredSelection; |
| 77 |
TreePath[] paths = ts.getPaths(); |
| 78 |
for (int j = 0; j < paths.length; j++) { |
| 79 |
TreePath path = paths[j]; |
| 80 |
String qualifiedText = getQualifiedTextFor(path); |
| 81 |
if (qualifiedText != null && qualifiedText.length() > 0) { |
| 82 |
if (i > 0) |
| 83 |
buf.append(EOL); |
| 84 |
buf.append(qualifiedText); |
| 85 |
i++; |
| 86 |
} |
| 87 |
} |
| 88 |
} else { |
| 89 |
for (Iterator it = structuredSelection.iterator(); it.hasNext();) { |
| 90 |
Object element = it.next(); |
| 91 |
if (element instanceof ITypedElement) { |
| 92 |
if (i > 0) |
| 93 |
buf.append(EOL); |
| 94 |
buf.append(((ITypedElement)element).getName()); |
| 95 |
i++; |
| 96 |
} else { |
| 97 |
IResource resource = Utils.getResource(element); |
| 98 |
if (resource != null) { |
| 99 |
if (i > 0) |
| 100 |
buf.append(EOL); |
| 101 |
buf.append(resource.getName()); |
| 102 |
i++; |
| 103 |
} |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
return buf.toString(); |
| 108 |
} |
| 109 |
|
| 110 |
private String getQualifiedTextFor(TreePath path) { |
| 111 |
StringBuffer qualifiedText = new StringBuffer(); |
| 112 |
TreePath parentPath = path.getParentPath(); |
| 113 |
if (parentPath != null) { |
| 114 |
String parentText = getQualifiedTextFor(parentPath); |
| 115 |
qualifiedText.append(parentText); |
| 116 |
qualifiedText.append(SEP); |
| 117 |
} |
| 118 |
String pathText = getTextFor(path); |
| 119 |
qualifiedText.append(pathText); |
| 120 |
return qualifiedText.toString(); |
| 121 |
} |
| 122 |
|
| 123 |
private String getTextFor(TreePath path) { |
| 124 |
Object element = path.getLastSegment(); |
| 125 |
if (element instanceof ITypedElement) { |
| 126 |
return ((ITypedElement)element).getName(); |
| 127 |
} |
| 128 |
INavigatorContentService service = getNavigatorContentService(); |
| 129 |
if (service != null) { |
| 130 |
ILabelProvider provider = service.createCommonLabelProvider(); |
| 131 |
if (provider instanceof ITreePathLabelProvider) { |
| 132 |
ITreePathLabelProvider tplp = (ITreePathLabelProvider) provider; |
| 133 |
ViewerLabel viewerLabel = new ViewerLabel("", null); //$NON-NLS-1$ |
| 134 |
tplp.updateLabel(viewerLabel, path); |
| 135 |
return viewerLabel.getText(); |
| 136 |
} |
| 137 |
return provider.getText(element); |
| 138 |
} |
| 139 |
if (element instanceof IResource) { |
| 140 |
IResource resource = (IResource) element; |
| 141 |
return resource.getName(); |
| 142 |
} |
| 143 |
return null; |
| 144 |
} |
| 145 |
|
| 146 |
private INavigatorContentService getNavigatorContentService() { |
| 147 |
return navigatorContentService; |
| 148 |
} |
| 149 |
|
| 150 |
private void copyResources(List selectedResources, String text) { |
| 151 |
IResource[] resources = (IResource[]) selectedResources.toArray(new IResource[selectedResources.size()]); |
| 152 |
// Get the file names and a string representation |
| 153 |
final int length = resources.length; |
| 154 |
int actualLength = 0; |
| 155 |
String[] fileNames = new String[length]; |
| 156 |
for (int i = 0; i < length; i++) { |
| 157 |
final IPath location = resources[i].getLocation(); |
| 158 |
// location may be null. See bug 29491. |
| 159 |
if (location != null) |
| 160 |
fileNames[actualLength++] = location.toOSString(); |
| 161 |
} |
| 162 |
// was one or more of the locations null? |
| 163 |
if (actualLength < length) { |
| 164 |
String[] tempFileNames = fileNames; |
| 165 |
fileNames = new String[actualLength]; |
| 166 |
for (int i = 0; i < actualLength; i++) |
| 167 |
fileNames[i] = tempFileNames[i]; |
| 168 |
} |
| 169 |
setClipboard(resources, fileNames, text); |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* Set the clipboard contents. Prompt to retry if clipboard is busy. |
| 174 |
* |
| 175 |
* @param resources the resources to copy to the clipboard |
| 176 |
* @param fileNames file names of the resources to copy to the clipboard |
| 177 |
* @param names string representation of all names |
| 178 |
*/ |
| 179 |
private void setClipboard(IResource[] resources, String[] fileNames, String names) { |
| 180 |
try { |
| 181 |
// set the clipboard contents |
| 182 |
List data = new ArrayList(); |
| 183 |
List dataTypes = new ArrayList(); |
| 184 |
if (resources.length > 0) { |
| 185 |
data.add(resources); |
| 186 |
dataTypes.add(ResourceTransfer.getInstance()); |
| 187 |
} |
| 188 |
if (fileNames.length > 0) { |
| 189 |
data.add(fileNames); |
| 190 |
dataTypes.add(FileTransfer.getInstance()); |
| 191 |
} |
| 192 |
if (names != null && names.length() > 0) { |
| 193 |
data.add(names); |
| 194 |
dataTypes.add(TextTransfer.getInstance()); |
| 195 |
} |
| 196 |
if (!data.isEmpty()) |
| 197 |
fClipboard.setContents( |
| 198 |
data.toArray(), |
| 199 |
(Transfer[]) dataTypes.toArray(new Transfer[dataTypes.size()])); |
| 200 |
} catch (SWTError e) { |
| 201 |
if (e.code != DND.ERROR_CANNOT_SET_CLIPBOARD) |
| 202 |
throw e; |
| 203 |
if (MessageDialog.openQuestion(fShell, TeamUIMessages.CopyQualifiedNameToClipboardAction_3, TeamUIMessages.CopyToClipboardAction_4)) |
| 204 |
setClipboard(resources, fileNames, names); |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
protected boolean updateSelection(IStructuredSelection selection) { |
| 209 |
if (!super.updateSelection(selection)) |
| 210 |
return false; |
| 211 |
return true; |
| 212 |
} |
| 213 |
|
| 214 |
protected List getSelectedNonResources() { |
| 215 |
return Arrays.asList(Utils.getNonResources(getStructuredSelection().toArray())); |
| 216 |
} |
| 217 |
|
| 218 |
protected List getSelectedResources() { |
| 219 |
// Calling our own selection utility because the elements in the |
| 220 |
// synchronize view can't adapt to IResource because we don't want the usual object |
| 221 |
// contribution/ on them. |
| 222 |
return Arrays.asList(Utils.getResources(getStructuredSelection().toArray())); |
| 223 |
} |
| 224 |
|
| 225 |
public void dispose() { |
| 226 |
fClipboard.dispose(); |
| 227 |
} |
| 228 |
} |