KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > refactoring > reorg > CopyToClipboardAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.jdt.internal.ui.refactoring.reorg;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.HashSet JavaDoc;
15 import java.util.List JavaDoc;
16 import java.util.Set JavaDoc;
17
18 import org.eclipse.core.runtime.Assert;
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.core.runtime.IPath;
21
22 import org.eclipse.core.resources.IResource;
23
24 import org.eclipse.swt.SWTError;
25 import org.eclipse.swt.dnd.Clipboard;
26 import org.eclipse.swt.dnd.DND;
27 import org.eclipse.swt.dnd.FileTransfer;
28 import org.eclipse.swt.dnd.TextTransfer;
29 import org.eclipse.swt.dnd.Transfer;
30 import org.eclipse.swt.widgets.Shell;
31
32 import org.eclipse.jface.dialogs.MessageDialog;
33 import org.eclipse.jface.viewers.ILabelProvider;
34 import org.eclipse.jface.viewers.IStructuredSelection;
35
36 import org.eclipse.ui.ISharedImages;
37 import org.eclipse.ui.IWorkbenchSite;
38 import org.eclipse.ui.PlatformUI;
39 import org.eclipse.ui.part.ResourceTransfer;
40
41 import org.eclipse.jdt.core.ICompilationUnit;
42 import org.eclipse.jdt.core.IJavaElement;
43 import org.eclipse.jdt.core.IType;
44 import org.eclipse.jdt.core.JavaModelException;
45
46 import org.eclipse.jdt.internal.corext.refactoring.TypedSource;
47 import org.eclipse.jdt.internal.corext.refactoring.reorg.JavaElementTransfer;
48 import org.eclipse.jdt.internal.corext.refactoring.reorg.ParentChecker;
49 import org.eclipse.jdt.internal.corext.refactoring.reorg.ReorgUtils;
50 import org.eclipse.jdt.internal.corext.refactoring.util.JavaElementUtil;
51 import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
52
53 import org.eclipse.jdt.ui.JavaElementLabelProvider;
54 import org.eclipse.jdt.ui.actions.SelectionDispatchAction;
55
56 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
57 import org.eclipse.jdt.internal.ui.JavaPlugin;
58 import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
59
60
61 public class CopyToClipboardAction extends SelectionDispatchAction{
62
63     private final Clipboard fClipboard;
64     private boolean fAutoRepeatOnFailure= false;
65
66     public CopyToClipboardAction(IWorkbenchSite site, Clipboard clipboard) {
67         super(site);
68         setText(ReorgMessages.CopyToClipboardAction_0);
69         setDescription(ReorgMessages.CopyToClipboardAction_1);
70         Assert.isNotNull(clipboard);
71         fClipboard= clipboard;
72         ISharedImages workbenchImages= getWorkbenchSharedImages();
73         setDisabledImageDescriptor(workbenchImages.getImageDescriptor(ISharedImages.IMG_TOOL_COPY_DISABLED));
74         setImageDescriptor(workbenchImages.getImageDescriptor(ISharedImages.IMG_TOOL_COPY));
75         setHoverImageDescriptor(workbenchImages.getImageDescriptor(ISharedImages.IMG_TOOL_COPY));
76         update(getSelection());
77
78         PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.COPY_ACTION);
79     }
80
81     public void setAutoRepeatOnFailure(boolean autorepeatOnFailure){
82         fAutoRepeatOnFailure= autorepeatOnFailure;
83     }
84     
85     private static ISharedImages getWorkbenchSharedImages() {
86         return JavaPlugin.getDefault().getWorkbench().getSharedImages();
87     }
88
89     /* (non-Javadoc)
90      * @see org.eclipse.jdt.ui.actions.SelectionDispatchAction#selectionChanged(org.eclipse.jface.viewers.IStructuredSelection)
91      */

92     public void selectionChanged(IStructuredSelection selection) {
93         try {
94             List JavaDoc elements= selection.toList();
95             IResource[] resources= ReorgUtils.getResources(elements);
96             IJavaElement[] javaElements= ReorgUtils.getJavaElements(elements);
97             if (elements.size() != resources.length + javaElements.length)
98                 setEnabled(false);
99             else
100                 setEnabled(canEnable(resources, javaElements));
101         } catch (JavaModelException e) {
102             //no ui here - this happens on selection changes
103
// http://bugs.eclipse.org/bugs/show_bug.cgi?id=19253
104
if (JavaModelUtil.isExceptionToBeLogged(e))
105                 JavaPlugin.log(e);
106             setEnabled(false);
107         }
108     }
109
110     /* (non-Javadoc)
111      * @see org.eclipse.jdt.ui.actions.SelectionDispatchAction#run(org.eclipse.jface.viewers.IStructuredSelection)
112      */

113     public void run(IStructuredSelection selection) {
114         try {
115             List JavaDoc elements= selection.toList();
116             IResource[] resources= ReorgUtils.getResources(elements);
117             IJavaElement[] javaElements= ReorgUtils.getJavaElements(elements);
118             if (elements.size() == resources.length + javaElements.length && canEnable(resources, javaElements))
119                 doRun(resources, javaElements);
120         } catch (CoreException e) {
121             ExceptionHandler.handle(e, getShell(), ReorgMessages.CopyToClipboardAction_2, ReorgMessages.CopyToClipboardAction_3);
122         }
123     }
124
125     private void doRun(IResource[] resources, IJavaElement[] javaElements) throws CoreException {
126         new ClipboardCopier(resources, javaElements, fClipboard, getShell(), fAutoRepeatOnFailure).copyToClipboard();
127     }
128
129     private boolean canEnable(IResource[] resources, IJavaElement[] javaElements) throws JavaModelException {
130         return new CopyToClipboardEnablementPolicy(resources, javaElements).canEnable();
131     }
132     
133     //----------------------------------------------------------------------------------------//
134

135     private static class ClipboardCopier{
136         private final boolean fAutoRepeatOnFailure;
137         private final IResource[] fResources;
138         private final IJavaElement[] fJavaElements;
139         private final Clipboard fClipboard;
140         private final Shell fShell;
141         private final ILabelProvider fLabelProvider;
142         
143         private ClipboardCopier(IResource[] resources, IJavaElement[] javaElements, Clipboard clipboard, Shell shell, boolean autoRepeatOnFailure){
144             Assert.isNotNull(resources);
145             Assert.isNotNull(javaElements);
146             Assert.isNotNull(clipboard);
147             Assert.isNotNull(shell);
148             fResources= resources;
149             fJavaElements= javaElements;
150             fClipboard= clipboard;
151             fShell= shell;
152             fLabelProvider= createLabelProvider();
153             fAutoRepeatOnFailure= autoRepeatOnFailure;
154         }
155
156         public void copyToClipboard() throws CoreException{
157             //Set<String> fileNames
158
Set JavaDoc fileNames= new HashSet JavaDoc(fResources.length + fJavaElements.length);
159             StringBuffer JavaDoc namesBuf = new StringBuffer JavaDoc();
160             processResources(fileNames, namesBuf);
161             processJavaElements(fileNames, namesBuf);
162
163             IType[] mainTypes= ReorgUtils.getMainTypes(fJavaElements);
164             ICompilationUnit[] cusOfMainTypes= ReorgUtils.getCompilationUnits(mainTypes);
165             IResource[] resourcesOfMainTypes= ReorgUtils.getResources(cusOfMainTypes);
166             addFileNames(fileNames, resourcesOfMainTypes);
167             
168             IResource[] cuResources= ReorgUtils.getResources(getCompilationUnits(fJavaElements));
169             addFileNames(fileNames, cuResources);
170
171             IResource[] resourcesForClipboard= ReorgUtils.union(fResources, ReorgUtils.union(cuResources, resourcesOfMainTypes));
172             IJavaElement[] javaElementsForClipboard= ReorgUtils.union(fJavaElements, cusOfMainTypes);
173             
174             TypedSource[] typedSources= TypedSource.createTypedSources(javaElementsForClipboard);
175             String JavaDoc[] fileNameArray= (String JavaDoc[]) fileNames.toArray(new String JavaDoc[fileNames.size()]);
176             copyToClipboard(resourcesForClipboard, fileNameArray, namesBuf.toString(), javaElementsForClipboard, typedSources, 0);
177         }
178
179         private static IJavaElement[] getCompilationUnits(IJavaElement[] javaElements) {
180             List JavaDoc cus= ReorgUtils.getElementsOfType(javaElements, IJavaElement.COMPILATION_UNIT);
181             return (ICompilationUnit[]) cus.toArray(new ICompilationUnit[cus.size()]);
182         }
183
184         private void processResources(Set JavaDoc fileNames, StringBuffer JavaDoc namesBuf) {
185             for (int i= 0; i < fResources.length; i++) {
186                 IResource resource= fResources[i];
187                 addFileName(fileNames, resource);
188
189                 if (i > 0)
190                     namesBuf.append('\n');
191                 namesBuf.append(getName(resource));
192             }
193         }
194
195         private void processJavaElements(Set JavaDoc fileNames, StringBuffer JavaDoc namesBuf) {
196             for (int i= 0; i < fJavaElements.length; i++) {
197                 IJavaElement element= fJavaElements[i];
198                 switch (element.getElementType()) {
199                     case IJavaElement.JAVA_PROJECT :
200                     case IJavaElement.PACKAGE_FRAGMENT_ROOT :
201                     case IJavaElement.PACKAGE_FRAGMENT :
202                     case IJavaElement.COMPILATION_UNIT :
203                     case IJavaElement.CLASS_FILE :
204                         addFileName(fileNames, ReorgUtils.getResource(element));
205                         break;
206                     default :
207                         break;
208                 }
209
210                 if (fResources.length > 0 || i > 0)
211                     namesBuf.append('\n');
212                 namesBuf.append(getName(element));
213             }
214         }
215
216         private static void addFileNames(Set JavaDoc fileName, IResource[] resources) {
217             for (int i= 0; i < resources.length; i++) {
218                 addFileName(fileName, resources[i]);
219             }
220         }
221
222         private static void addFileName(Set JavaDoc fileName, IResource resource){
223             if (resource == null)
224                 return;
225             IPath location = resource.getLocation();
226             if (location != null) {
227                 fileName.add(location.toOSString());
228             } else {
229                 // not a file system path. skip file.
230
}
231         }
232         
233         private void copyToClipboard(IResource[] resources, String JavaDoc[] fileNames, String JavaDoc names, IJavaElement[] javaElements, TypedSource[] typedSources, int repeat){
234             final int repeat_max_count= 10;
235             try{
236                 fClipboard.setContents( createDataArray(resources, javaElements, fileNames, names, typedSources),
237                                         createDataTypeArray(resources, javaElements, fileNames, typedSources));
238             } catch (SWTError e) {
239                 if (e.code != DND.ERROR_CANNOT_SET_CLIPBOARD || repeat >= repeat_max_count)
240                     throw e;
241                 if (fAutoRepeatOnFailure) {
242                     try {
243                         Thread.sleep(500);
244                     } catch (InterruptedException JavaDoc e1) {
245                         // do nothing.
246
}
247                 }
248                 if (fAutoRepeatOnFailure || MessageDialog.openQuestion(fShell, ReorgMessages.CopyToClipboardAction_4, ReorgMessages.CopyToClipboardAction_5))
249                     copyToClipboard(resources, fileNames, names, javaElements, typedSources, repeat+1);
250             }
251         }
252         
253         private static Transfer[] createDataTypeArray(IResource[] resources, IJavaElement[] javaElements, String JavaDoc[] fileNames, TypedSource[] typedSources) {
254             List JavaDoc result= new ArrayList JavaDoc(4);
255             if (resources.length != 0)
256                 result.add(ResourceTransfer.getInstance());
257             if (javaElements.length != 0)
258                 result.add(JavaElementTransfer.getInstance());
259             if (fileNames.length != 0)
260                 result.add(FileTransfer.getInstance());
261             if (typedSources.length != 0)
262                 result.add(TypedSourceTransfer.getInstance());
263             result.add(TextTransfer.getInstance());
264             return (Transfer[]) result.toArray(new Transfer[result.size()]);
265         }
266
267         private static Object JavaDoc[] createDataArray(IResource[] resources, IJavaElement[] javaElements, String JavaDoc[] fileNames, String JavaDoc names, TypedSource[] typedSources) {
268             List JavaDoc result= new ArrayList JavaDoc(4);
269             if (resources.length != 0)
270                 result.add(resources);
271             if (javaElements.length != 0)
272                 result.add(javaElements);
273             if (fileNames.length != 0)
274                 result.add(fileNames);
275             if (typedSources.length != 0)
276                 result.add(typedSources);
277             result.add(names);
278             return result.toArray();
279         }
280
281         private static ILabelProvider createLabelProvider(){
282             return new JavaElementLabelProvider(
283                 JavaElementLabelProvider.SHOW_VARIABLE
284                 + JavaElementLabelProvider.SHOW_PARAMETERS
285                 + JavaElementLabelProvider.SHOW_TYPE
286             );
287         }
288         private String JavaDoc getName(IResource resource){
289             return fLabelProvider.getText(resource);
290         }
291         private String JavaDoc getName(IJavaElement javaElement){
292             return fLabelProvider.getText(javaElement);
293         }
294     }
295     
296     private static class CopyToClipboardEnablementPolicy {
297         private final IResource[] fResources;
298         private final IJavaElement[] fJavaElements;
299         public CopyToClipboardEnablementPolicy(IResource[] resources, IJavaElement[] javaElements){
300             Assert.isNotNull(resources);
301             Assert.isNotNull(javaElements);
302             fResources= resources;
303             fJavaElements= javaElements;
304         }
305
306         public boolean canEnable() throws JavaModelException{
307             if (fResources.length + fJavaElements.length == 0)
308                 return false;
309             if (hasProjects() && hasNonProjects())
310                 return false;
311             if (! canCopyAllToClipboard())
312                 return false;
313             if (! new ParentChecker(fResources, fJavaElements).haveCommonParent())
314                 return false;
315             return true;
316         }
317
318         private boolean canCopyAllToClipboard() throws JavaModelException {
319             for (int i= 0; i < fResources.length; i++) {
320                 if (! canCopyToClipboard(fResources[i])) return false;
321             }
322             for (int i= 0; i < fJavaElements.length; i++) {
323                 if (! canCopyToClipboard(fJavaElements[i])) return false;
324             }
325             return true;
326         }
327
328         private static boolean canCopyToClipboard(IJavaElement element) throws JavaModelException {
329             if (element == null || ! element.exists())
330                 return false;
331                 
332             if (JavaElementUtil.isDefaultPackage(element))
333                 return false;
334             
335             return true;
336         }
337
338         private static boolean canCopyToClipboard(IResource resource) {
339             return resource != null &&
340                     resource.exists() &&
341                     ! resource.isPhantom() &&
342                     resource.getType() != IResource.ROOT;
343         }
344
345         private boolean hasProjects() {
346             for (int i= 0; i < fResources.length; i++) {
347                 if (ReorgUtils.isProject(fResources[i])) return true;
348             }
349             for (int i= 0; i < fJavaElements.length; i++) {
350                 if (ReorgUtils.isProject(fJavaElements[i])) return true;
351             }
352             return false;
353         }
354
355         private boolean hasNonProjects() {
356             for (int i= 0; i < fResources.length; i++) {
357                 if (! ReorgUtils.isProject(fResources[i])) return true;
358             }
359             for (int i= 0; i < fJavaElements.length; i++) {
360                 if (! ReorgUtils.isProject(fJavaElements[i])) return true;
361             }
362             return false;
363         }
364     }
365 }
366
Popular Tags