KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > actions > CopyQualifiedNameAction


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.actions;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.core.runtime.IPath;
18
19 import org.eclipse.core.resources.IResource;
20
21 import org.eclipse.swt.SWTError;
22 import org.eclipse.swt.dnd.Clipboard;
23 import org.eclipse.swt.dnd.DND;
24 import org.eclipse.swt.dnd.FileTransfer;
25 import org.eclipse.swt.dnd.TextTransfer;
26 import org.eclipse.swt.dnd.Transfer;
27 import org.eclipse.swt.graphics.Point;
28
29 import org.eclipse.jface.dialogs.MessageDialog;
30 import org.eclipse.jface.viewers.ISelection;
31 import org.eclipse.jface.viewers.IStructuredSelection;
32
33 import org.eclipse.jface.text.ITextSelection;
34 import org.eclipse.jface.text.source.ISourceViewer;
35
36 import org.eclipse.ui.IWorkbenchSite;
37 import org.eclipse.ui.part.ResourceTransfer;
38
39 import org.eclipse.jdt.core.IClassFile;
40 import org.eclipse.jdt.core.ICompilationUnit;
41 import org.eclipse.jdt.core.IImportDeclaration;
42 import org.eclipse.jdt.core.IJavaElement;
43 import org.eclipse.jdt.core.IMember;
44 import org.eclipse.jdt.core.IPackageDeclaration;
45 import org.eclipse.jdt.core.IPackageFragment;
46 import org.eclipse.jdt.core.JavaModelException;
47 import org.eclipse.jdt.core.dom.ASTNode;
48 import org.eclipse.jdt.core.dom.AnonymousClassDeclaration;
49 import org.eclipse.jdt.core.dom.CompilationUnit;
50 import org.eclipse.jdt.core.dom.Expression;
51 import org.eclipse.jdt.core.dom.IBinding;
52 import org.eclipse.jdt.core.dom.ImportDeclaration;
53 import org.eclipse.jdt.core.dom.MemberRef;
54 import org.eclipse.jdt.core.dom.MemberValuePair;
55 import org.eclipse.jdt.core.dom.MethodDeclaration;
56 import org.eclipse.jdt.core.dom.MethodInvocation;
57 import org.eclipse.jdt.core.dom.Name;
58 import org.eclipse.jdt.core.dom.PackageDeclaration;
59 import org.eclipse.jdt.core.dom.Type;
60 import org.eclipse.jdt.core.dom.TypeDeclaration;
61 import org.eclipse.jdt.core.dom.TypeParameter;
62 import org.eclipse.jdt.core.dom.VariableDeclaration;
63
64 import org.eclipse.jdt.internal.corext.dom.NodeFinder;
65
66 import org.eclipse.jdt.ui.JavaElementLabels;
67 import org.eclipse.jdt.ui.JavaUI;
68 import org.eclipse.jdt.ui.actions.SelectionDispatchAction;
69
70 import org.eclipse.jdt.internal.ui.JavaPlugin;
71 import org.eclipse.jdt.internal.ui.JavaPluginImages;
72 import org.eclipse.jdt.internal.ui.javaeditor.ASTProvider;
73 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor;
74
75 public class CopyQualifiedNameAction extends SelectionDispatchAction {
76     
77     private static final long LABEL_FLAGS= new Long JavaDoc(JavaElementLabels.F_FULLY_QUALIFIED | JavaElementLabels.M_FULLY_QUALIFIED | JavaElementLabels.I_FULLY_QUALIFIED | JavaElementLabels.T_FULLY_QUALIFIED | JavaElementLabels.M_PARAMETER_TYPES | JavaElementLabels.USE_RESOLVED | JavaElementLabels.T_TYPE_PARAMETERS | JavaElementLabels.CU_QUALIFIED | JavaElementLabels.CF_QUALIFIED).longValue();
78
79     //TODO: Make API
80
public static final String JavaDoc ACTION_DEFINITION_ID= "org.eclipse.jdt.ui.edit.text.java.copy.qualified.name"; //$NON-NLS-1$
81

82     //TODO: Make API
83
public static final String JavaDoc ACTION_HANDLER_ID= "org.eclipse.jdt.ui.actions.CopyQualifiedName"; //$NON-NLS-1$
84

85     private JavaEditor fEditor;
86
87     public CopyQualifiedNameAction(JavaEditor editor) {
88         this(editor.getSite());
89         fEditor= editor;
90         setEnabled(true);
91     }
92
93     public CopyQualifiedNameAction(IWorkbenchSite site) {
94         super(site);
95         
96         setText(ActionMessages.CopyQualifiedNameAction_ActionName);
97         setToolTipText(ActionMessages.CopyQualifiedNameAction_ToolTipText);
98         setDisabledImageDescriptor(JavaPluginImages.DESC_DLCL_COPY_QUALIFIED_NAME);
99         setImageDescriptor(JavaPluginImages.DESC_ELCL_COPY_QUALIFIED_NAME);
100     }
101     
102     /**
103      * {@inheritDoc}
104      */

105     public void selectionChanged(IStructuredSelection selection) {
106         setEnabled(canEnable(selection.toArray()));
107     }
108     
109     public void selectionChanged(ITextSelection selection) {
110         //Must not create an AST
111
}
112
113     private boolean canEnable(Object JavaDoc[] objects) {
114         for (int i= 0; i < objects.length; i++) {
115             Object JavaDoc element= objects[i];
116             if (isValideElement(element))
117                 return true;
118         }
119
120         return false;
121     }
122     
123     private boolean isValideElement(Object JavaDoc element) {
124         if (element instanceof IMember)
125             return true;
126         
127         if (element instanceof IClassFile)
128             return true;
129         
130         if (element instanceof ICompilationUnit)
131             return true;
132         
133         if (element instanceof IPackageDeclaration)
134             return true;
135         
136         if (element instanceof IImportDeclaration)
137             return true;
138         
139         if (element instanceof IPackageFragment)
140             return true;
141         
142         return false;
143     }
144     
145     /* (non-Javadoc)
146      * @see org.eclipse.jface.action.Action#run()
147      */

148     public void run() {
149         
150         try {
151             IJavaElement[] elements= getSelectedElements();
152             if (elements == null) {
153                 MessageDialog.openInformation(getShell(), ActionMessages.CopyQualifiedNameAction_InfoDialogTitel, ActionMessages.CopyQualifiedNameAction_NoElementToQualify);
154                 return;
155             }
156
157             Object JavaDoc[] data= null;
158             Transfer[] dataTypes= null;
159             
160             if (elements.length == 1) {
161                 String JavaDoc qualifiedName= JavaElementLabels.getElementLabel(elements[0], LABEL_FLAGS);
162                 IResource resource= elements[0].getCorrespondingResource();
163                 
164                 if (resource != null) {
165                     IPath location= resource.getLocation();
166                     if (location != null) {
167                         data= new Object JavaDoc[] {qualifiedName, resource, new String JavaDoc[] {location.toOSString()}};
168                         dataTypes= new Transfer[] {TextTransfer.getInstance(), ResourceTransfer.getInstance(), FileTransfer.getInstance()};
169                     } else {
170                         data= new Object JavaDoc[] {qualifiedName, resource};
171                         dataTypes= new Transfer[] {TextTransfer.getInstance(), ResourceTransfer.getInstance()};
172                     }
173                 } else {
174                     data= new Object JavaDoc[] {qualifiedName};
175                     dataTypes= new Transfer[] {TextTransfer.getInstance()};
176                 }
177             } else {
178                 StringBuffer JavaDoc buf= new StringBuffer JavaDoc();
179                 buf.append(JavaElementLabels.getElementLabel(elements[0], LABEL_FLAGS));
180                 for (int i= 1; i < elements.length; i++) {
181                     IJavaElement element= elements[i];
182                     
183                     String JavaDoc qualifiedName= JavaElementLabels.getElementLabel(element, LABEL_FLAGS);
184                     buf.append('\r').append('\n').append(qualifiedName);
185                 }
186                 data= new Object JavaDoc[] {buf.toString()};
187                 dataTypes= new Transfer[] {TextTransfer.getInstance()};
188             }
189             
190             Clipboard clipboard= new Clipboard(getShell().getDisplay());
191             try {
192                 clipboard.setContents(data, dataTypes);
193             } catch (SWTError e) {
194                 if (e.code != DND.ERROR_CANNOT_SET_CLIPBOARD) {
195                     throw e;
196                 }
197                 if (MessageDialog.openQuestion(getShell(), ActionMessages.CopyQualifiedNameAction_ErrorTitle, ActionMessages.CopyQualifiedNameAction_ErrorDescription)) {
198                     clipboard.setContents(data, dataTypes);
199                 }
200             } finally {
201                 clipboard.dispose();
202             }
203         } catch (JavaModelException e) {
204             JavaPlugin.log(e);
205         }
206     }
207
208     private IJavaElement[] getSelectedElements() throws JavaModelException {
209         if (fEditor != null) {
210             IJavaElement element= getSelectedElement(fEditor);
211             if (element == null)
212                 return null;
213             
214             return new IJavaElement[] {element};
215         }
216         
217         ISelection selection= getSelection();
218         if (!(selection instanceof IStructuredSelection))
219             return null;
220         
221         List JavaDoc result= new ArrayList JavaDoc();
222         for (Iterator JavaDoc iter= ((IStructuredSelection)selection).iterator(); iter.hasNext();) {
223             Object JavaDoc element= iter.next();
224             if (isValideElement(element))
225                 result.add(element);
226         }
227         if (result.isEmpty())
228             return null;
229         
230         return (IJavaElement[])result.toArray(new IJavaElement[result.size()]);
231     }
232
233     private IJavaElement getSelectedElement(JavaEditor editor) {
234         ISourceViewer viewer= editor.getViewer();
235         if (viewer == null)
236             return null;
237         
238         Point selectedRange= viewer.getSelectedRange();
239         int length= selectedRange.y;
240         int offset= selectedRange.x;
241         
242         IJavaElement element= JavaUI.getEditorInputJavaElement(editor.getEditorInput());
243         CompilationUnit ast= ASTProvider.getASTProvider().getAST(element, ASTProvider.WAIT_YES, null);
244         if (ast == null)
245             return null;
246
247         NodeFinder finder= new NodeFinder(offset, length);
248         ast.accept(finder);
249         ASTNode node= finder.getCoveringNode();
250         
251         IBinding binding= null;
252         if (node instanceof Name) {
253             binding= ((Name)node).resolveBinding();
254         } else if (node instanceof MethodInvocation) {
255             binding= ((MethodInvocation)node).resolveMethodBinding();
256         } else if (node instanceof MethodDeclaration) {
257             binding= ((MethodDeclaration)node).resolveBinding();
258         } else if (node instanceof Type) {
259             binding= ((Type)node).resolveBinding();
260         } else if (node instanceof AnonymousClassDeclaration) {
261             binding= ((AnonymousClassDeclaration)node).resolveBinding();
262         } else if (node instanceof TypeDeclaration) {
263             binding= ((TypeDeclaration)node).resolveBinding();
264         } else if (node instanceof CompilationUnit) {
265             return ((CompilationUnit)node).getJavaElement();
266         } else if (node instanceof Expression) {
267             binding= ((Expression)node).resolveTypeBinding();
268         } else if (node instanceof ImportDeclaration) {
269             binding= ((ImportDeclaration)node).resolveBinding();
270         } else if (node instanceof MemberRef) {
271             binding= ((MemberRef)node).resolveBinding();
272         } else if (node instanceof MemberValuePair) {
273             binding= ((MemberValuePair)node).resolveMemberValuePairBinding();
274         } else if (node instanceof PackageDeclaration) {
275             binding= ((PackageDeclaration)node).resolveBinding();
276         } else if (node instanceof TypeParameter) {
277             binding= ((TypeParameter)node).resolveBinding();
278         } else if (node instanceof VariableDeclaration) {
279             binding= ((VariableDeclaration)node).resolveBinding();
280         }
281             
282         if (binding != null)
283             return binding.getJavaElement();
284
285         return null;
286     }
287
288 }
289
Popular Tags