KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ui > synchronize > actions > CopyToClipboardAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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 JavaDoc;
14 import java.util.Arrays JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17
18 import org.eclipse.compare.ITypedElement;
19 import org.eclipse.core.resources.IContainer;
20 import org.eclipse.core.resources.IResource;
21 import org.eclipse.core.runtime.Assert;
22 import org.eclipse.core.runtime.IPath;
23 import org.eclipse.jface.dialogs.MessageDialog;
24 import org.eclipse.jface.viewers.*;
25 import org.eclipse.swt.SWTError;
26 import org.eclipse.swt.dnd.*;
27 import org.eclipse.swt.widgets.Shell;
28 import org.eclipse.team.internal.ui.*;
29 import org.eclipse.ui.actions.SelectionListenerAction;
30 import org.eclipse.ui.navigator.INavigatorContentService;
31 import org.eclipse.ui.part.ResourceTransfer;
32
33 /**
34  * Based on org.eclipse.ui.views.navigator.CopyAction with the additional support for
35  * copying any non-resource object in the selection and putting the toString() as
36  * a text transfer.
37  *
38  * @since 3.1
39  */

40 class CopyToClipboardAction extends SelectionListenerAction {
41     
42     private static final String JavaDoc EOL = System.getProperty("line.separator", "\n"); //$NON-NLS-1$ //$NON-NLS-2$
43

44     private final static String JavaDoc ID= TeamUIPlugin.PLUGIN_ID + ".synchronize.action.copy"; //$NON-NLS-1$
45

46     private final Shell fShell;
47     private final Clipboard fClipboard;
48
49     private final INavigatorContentService navigatorContentService;
50
51     protected CopyToClipboardAction(Shell shell, INavigatorContentService navigatorContentService) {
52         super(TeamUIMessages.CopyToClipboardAction_1);
53         this.navigatorContentService = navigatorContentService;
54         Assert.isNotNull(shell);
55         fShell= shell;
56         fClipboard= new Clipboard(shell.getDisplay());
57         setToolTipText(TeamUIMessages.CopyToClipboardAction_2);
58         setId(ID);
59     }
60     
61     public void run() {
62         copyResources(getSelectedResources(), getTextualClipboardContents());
63     }
64     
65     /*
66      * Return a text representation of all selected elements.
67      * Use the name from the tree node so what is copied
68      * matches what appears in the tree.
69      */

70     private String JavaDoc getTextualClipboardContents() {
71         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
72         int i = 0;
73         IStructuredSelection structuredSelection = getStructuredSelection();
74         if (structuredSelection instanceof TreeSelection) {
75             TreeSelection ts = (TreeSelection) structuredSelection;
76             TreePath[] paths = ts.getPaths();
77             for (int j = 0; j < paths.length; j++) {
78                 TreePath path = paths[j];
79                 String JavaDoc text = getTextFor(path);
80                 if (text != null && text.length() > 0) {
81                     if (i > 0)
82                         buf.append(EOL);
83                     buf.append(text);
84                     i++;
85                 }
86             }
87         } else {
88             for (Iterator JavaDoc it = structuredSelection.iterator(); it.hasNext();) {
89                 Object JavaDoc element = it.next();
90                 if (element instanceof ITypedElement) {
91                     if (i > 0)
92                         buf.append(EOL);
93                     buf.append(((ITypedElement)element).getName());
94                     i++;
95                 } else {
96                     IResource resource = Utils.getResource(element);
97                     if (resource != null) {
98                         if (i > 0)
99                             buf.append(EOL);
100                         buf.append(resource.getName());
101                         i++;
102                     }
103                 }
104             }
105         }
106         return buf.toString();
107     }
108
109     private String JavaDoc getTextFor(TreePath path) {
110         Object JavaDoc element = path.getLastSegment();
111         if (element instanceof ITypedElement) {
112             return ((ITypedElement)element).getName();
113         }
114         INavigatorContentService service = getNavigatorContentService();
115         if (service != null) {
116             ILabelProvider provider = service.createCommonLabelProvider();
117             if (provider instanceof ITreePathLabelProvider) {
118                 ITreePathLabelProvider tplp = (ITreePathLabelProvider) provider;
119                 ViewerLabel viewerLabel = new ViewerLabel("", null); //$NON-NLS-1$
120
tplp.updateLabel(viewerLabel, path);
121                 return viewerLabel.getText();
122             }
123             return provider.getText(element);
124         }
125         if (element instanceof IResource) {
126             IResource resource = (IResource) element;
127             return resource.getName();
128         }
129         return null;
130     }
131
132     private INavigatorContentService getNavigatorContentService() {
133         return navigatorContentService;
134     }
135
136     private void copyResources(List JavaDoc selectedResources, String JavaDoc text) {
137         IResource[] resources = (IResource[]) selectedResources.toArray(new IResource[selectedResources.size()]);
138         // Get the file names and a string representation
139
final int length = resources.length;
140         int actualLength = 0;
141         String JavaDoc[] fileNames = new String JavaDoc[length];
142         for (int i = 0; i < length; i++) {
143             final IPath location = resources[i].getLocation();
144             // location may be null. See bug 29491.
145
if (location != null)
146                 fileNames[actualLength++] = location.toOSString();
147         }
148         // was one or more of the locations null?
149
if (actualLength < length) {
150             String JavaDoc[] tempFileNames = fileNames;
151             fileNames = new String JavaDoc[actualLength];
152             for (int i = 0; i < actualLength; i++)
153                 fileNames[i] = tempFileNames[i];
154         }
155         setClipboard(resources, fileNames, text);
156     }
157
158     /**
159      * Set the clipboard contents. Prompt to retry if clipboard is busy.
160      *
161      * @param resources the resources to copy to the clipboard
162      * @param fileNames file names of the resources to copy to the clipboard
163      * @param names string representation of all names
164      */

165     private void setClipboard(IResource[] resources, String JavaDoc[] fileNames, String JavaDoc names) {
166         try {
167             // set the clipboard contents
168
List JavaDoc data = new ArrayList JavaDoc();
169             List JavaDoc dataTypes = new ArrayList JavaDoc();
170              if (resources.length > 0) {
171                  data.add(resources);
172                  dataTypes.add(ResourceTransfer.getInstance());
173              }
174             if (fileNames.length > 0) {
175                 data.add(fileNames);
176                 dataTypes.add(FileTransfer.getInstance());
177             }
178             if (names != null && names.length() > 0) {
179                 data.add(names);
180                 dataTypes.add(TextTransfer.getInstance());
181             }
182             if (!data.isEmpty())
183                 fClipboard.setContents(
184                         data.toArray(),
185                         (Transfer[]) dataTypes.toArray(new Transfer[dataTypes.size()]));
186         } catch (SWTError e) {
187             if (e.code != DND.ERROR_CANNOT_SET_CLIPBOARD)
188                 throw e;
189             if (MessageDialog.openQuestion(fShell, TeamUIMessages.CopyToClipboardAction_3, TeamUIMessages.CopyToClipboardAction_4))
190                 setClipboard(resources, fileNames, names);
191         }
192     }
193     
194     protected boolean updateSelection(IStructuredSelection selection) {
195         if (!super.updateSelection(selection))
196             return false;
197         // Calling our own selection utility because the elements in the
198
// synchronize view can't adapt to IResource because we don't want the usual object
199
// contribution/ on them.
200
List JavaDoc selectedResources = getSelectedResources();
201         List JavaDoc selectedNonResources = getSelectedNonResources();
202         if (selectedResources.size() > 0 && selectedNonResources.size() == 0) {
203             boolean projSelected = selectionIsOfType(IResource.PROJECT);
204             boolean fileFoldersSelected = selectionIsOfType(IResource.FILE | IResource.FOLDER);
205             if (!projSelected && !fileFoldersSelected)
206                 return false;
207             // selection must be homogeneous
208
if (projSelected && fileFoldersSelected)
209                 return false;
210             // must have a common parent
211
IContainer firstParent = ((IResource) selectedResources.get(0)).getParent();
212             if (firstParent == null)
213                 return false;
214             Iterator JavaDoc resourcesEnum = selectedResources.iterator();
215             while (resourcesEnum.hasNext()) {
216                 IResource currentResource = (IResource) resourcesEnum.next();
217                 if (!currentResource.getParent().equals(firstParent))
218                     return false;
219                 // resource location must exist
220
if (currentResource.getLocation() == null)
221                     return false;
222             }
223             return true;
224         } else if (selectedNonResources.size() > 0 && selectedResources.size() == 0) {
225             return true;
226         }
227         return false;
228     }
229      
230     protected List JavaDoc getSelectedNonResources() {
231         return Arrays.asList(Utils.getNonResources(getStructuredSelection().toArray()));
232     }
233     
234     protected List JavaDoc getSelectedResources() {
235         // Calling our own selection utility because the elements in the
236
// synchronize view can't adapt to IResource because we don't want the usual object
237
// contribution/ on them.
238
return Arrays.asList(Utils.getResources(getStructuredSelection().toArray()));
239     }
240     
241     public void dispose() {
242         fClipboard.dispose();
243     }
244 }
245
Popular Tags