001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.ui.actions;
011:
012: import java.util.ArrayList;
013: import java.util.Iterator;
014: import java.util.List;
015:
016: import org.eclipse.core.runtime.IPath;
017:
018: import org.eclipse.core.resources.IResource;
019:
020: import org.eclipse.swt.SWTError;
021: import org.eclipse.swt.dnd.Clipboard;
022: import org.eclipse.swt.dnd.DND;
023: import org.eclipse.swt.dnd.FileTransfer;
024: import org.eclipse.swt.dnd.TextTransfer;
025: import org.eclipse.swt.dnd.Transfer;
026: import org.eclipse.swt.graphics.Point;
027:
028: import org.eclipse.jface.dialogs.MessageDialog;
029: import org.eclipse.jface.viewers.ISelection;
030: import org.eclipse.jface.viewers.IStructuredSelection;
031:
032: import org.eclipse.jface.text.ITextSelection;
033: import org.eclipse.jface.text.source.ISourceViewer;
034:
035: import org.eclipse.ui.IWorkbenchSite;
036: import org.eclipse.ui.part.ResourceTransfer;
037:
038: import org.eclipse.jdt.core.IClassFile;
039: import org.eclipse.jdt.core.ICompilationUnit;
040: import org.eclipse.jdt.core.IImportDeclaration;
041: import org.eclipse.jdt.core.IJavaElement;
042: import org.eclipse.jdt.core.IMember;
043: import org.eclipse.jdt.core.IPackageDeclaration;
044: import org.eclipse.jdt.core.IPackageFragment;
045: import org.eclipse.jdt.core.ITypeRoot;
046: import org.eclipse.jdt.core.JavaModelException;
047: import org.eclipse.jdt.core.dom.ASTNode;
048: import org.eclipse.jdt.core.dom.AnonymousClassDeclaration;
049: import org.eclipse.jdt.core.dom.CompilationUnit;
050: import org.eclipse.jdt.core.dom.Expression;
051: import org.eclipse.jdt.core.dom.IBinding;
052: import org.eclipse.jdt.core.dom.ImportDeclaration;
053: import org.eclipse.jdt.core.dom.MemberRef;
054: import org.eclipse.jdt.core.dom.MemberValuePair;
055: import org.eclipse.jdt.core.dom.MethodDeclaration;
056: import org.eclipse.jdt.core.dom.MethodInvocation;
057: import org.eclipse.jdt.core.dom.Name;
058: import org.eclipse.jdt.core.dom.PackageDeclaration;
059: import org.eclipse.jdt.core.dom.Type;
060: import org.eclipse.jdt.core.dom.TypeDeclaration;
061: import org.eclipse.jdt.core.dom.TypeParameter;
062: import org.eclipse.jdt.core.dom.VariableDeclaration;
063:
064: import org.eclipse.jdt.internal.corext.dom.NodeFinder;
065:
066: import org.eclipse.jdt.ui.JavaElementLabels;
067: import org.eclipse.jdt.ui.JavaUI;
068: import org.eclipse.jdt.ui.SharedASTProvider;
069: import org.eclipse.jdt.ui.actions.SelectionDispatchAction;
070:
071: import org.eclipse.jdt.internal.ui.JavaPlugin;
072: import org.eclipse.jdt.internal.ui.JavaPluginImages;
073: import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor;
074:
075: public class CopyQualifiedNameAction extends SelectionDispatchAction {
076:
077: private static final long LABEL_FLAGS = new Long(
078: JavaElementLabels.F_FULLY_QUALIFIED
079: | JavaElementLabels.M_FULLY_QUALIFIED
080: | JavaElementLabels.I_FULLY_QUALIFIED
081: | JavaElementLabels.T_FULLY_QUALIFIED
082: | JavaElementLabels.M_PARAMETER_TYPES
083: | JavaElementLabels.USE_RESOLVED
084: | JavaElementLabels.T_TYPE_PARAMETERS
085: | JavaElementLabels.CU_QUALIFIED
086: | JavaElementLabels.CF_QUALIFIED).longValue();
087:
088: //TODO: Make API
089: public static final String ACTION_DEFINITION_ID = "org.eclipse.jdt.ui.edit.text.java.copy.qualified.name"; //$NON-NLS-1$
090:
091: //TODO: Make API
092: public static final String ACTION_HANDLER_ID = "org.eclipse.jdt.ui.actions.CopyQualifiedName"; //$NON-NLS-1$
093:
094: private JavaEditor fEditor;
095:
096: public CopyQualifiedNameAction(JavaEditor editor) {
097: this (editor.getSite());
098: fEditor = editor;
099: setEnabled(true);
100: }
101:
102: public CopyQualifiedNameAction(IWorkbenchSite site) {
103: super (site);
104:
105: setText(ActionMessages.CopyQualifiedNameAction_ActionName);
106: setToolTipText(ActionMessages.CopyQualifiedNameAction_ToolTipText);
107: setDisabledImageDescriptor(JavaPluginImages.DESC_DLCL_COPY_QUALIFIED_NAME);
108: setImageDescriptor(JavaPluginImages.DESC_ELCL_COPY_QUALIFIED_NAME);
109: }
110:
111: /**
112: * {@inheritDoc}
113: */
114: public void selectionChanged(IStructuredSelection selection) {
115: setEnabled(canEnable(selection.toArray()));
116: }
117:
118: public void selectionChanged(ITextSelection selection) {
119: //Must not create an AST
120: }
121:
122: private boolean canEnable(Object[] objects) {
123: for (int i = 0; i < objects.length; i++) {
124: Object element = objects[i];
125: if (isValideElement(element))
126: return true;
127: }
128:
129: return false;
130: }
131:
132: private boolean isValideElement(Object element) {
133: if (element instanceof IMember)
134: return true;
135:
136: if (element instanceof IClassFile)
137: return true;
138:
139: if (element instanceof ICompilationUnit)
140: return true;
141:
142: if (element instanceof IPackageDeclaration)
143: return true;
144:
145: if (element instanceof IImportDeclaration)
146: return true;
147:
148: if (element instanceof IPackageFragment)
149: return true;
150:
151: return false;
152: }
153:
154: /* (non-Javadoc)
155: * @see org.eclipse.jface.action.Action#run()
156: */
157: public void run() {
158:
159: try {
160: IJavaElement[] elements = getSelectedElements();
161: if (elements == null) {
162: MessageDialog
163: .openInformation(
164: getShell(),
165: ActionMessages.CopyQualifiedNameAction_InfoDialogTitel,
166: ActionMessages.CopyQualifiedNameAction_NoElementToQualify);
167: return;
168: }
169:
170: Object[] data = null;
171: Transfer[] dataTypes = null;
172:
173: if (elements.length == 1) {
174: String qualifiedName = JavaElementLabels
175: .getElementLabel(elements[0], LABEL_FLAGS);
176: IResource resource = elements[0]
177: .getCorrespondingResource();
178:
179: if (resource != null) {
180: IPath location = resource.getLocation();
181: if (location != null) {
182: data = new Object[] { qualifiedName, resource,
183: new String[] { location.toOSString() } };
184: dataTypes = new Transfer[] {
185: TextTransfer.getInstance(),
186: ResourceTransfer.getInstance(),
187: FileTransfer.getInstance() };
188: } else {
189: data = new Object[] { qualifiedName, resource };
190: dataTypes = new Transfer[] {
191: TextTransfer.getInstance(),
192: ResourceTransfer.getInstance() };
193: }
194: } else {
195: data = new Object[] { qualifiedName };
196: dataTypes = new Transfer[] { TextTransfer
197: .getInstance() };
198: }
199: } else {
200: StringBuffer buf = new StringBuffer();
201: buf.append(JavaElementLabels.getElementLabel(
202: elements[0], LABEL_FLAGS));
203: for (int i = 1; i < elements.length; i++) {
204: IJavaElement element = elements[i];
205:
206: String qualifiedName = JavaElementLabels
207: .getElementLabel(element, LABEL_FLAGS);
208: buf.append('\r').append('\n').append(qualifiedName);
209: }
210: data = new Object[] { buf.toString() };
211: dataTypes = new Transfer[] { TextTransfer.getInstance() };
212: }
213:
214: Clipboard clipboard = new Clipboard(getShell().getDisplay());
215: try {
216: clipboard.setContents(data, dataTypes);
217: } catch (SWTError e) {
218: if (e.code != DND.ERROR_CANNOT_SET_CLIPBOARD) {
219: throw e;
220: }
221: if (MessageDialog
222: .openQuestion(
223: getShell(),
224: ActionMessages.CopyQualifiedNameAction_ErrorTitle,
225: ActionMessages.CopyQualifiedNameAction_ErrorDescription)) {
226: clipboard.setContents(data, dataTypes);
227: }
228: } finally {
229: clipboard.dispose();
230: }
231: } catch (JavaModelException e) {
232: JavaPlugin.log(e);
233: }
234: }
235:
236: private IJavaElement[] getSelectedElements()
237: throws JavaModelException {
238: if (fEditor != null) {
239: IJavaElement element = getSelectedElement(fEditor);
240: if (element == null)
241: return null;
242:
243: return new IJavaElement[] { element };
244: }
245:
246: ISelection selection = getSelection();
247: if (!(selection instanceof IStructuredSelection))
248: return null;
249:
250: List result = new ArrayList();
251: for (Iterator iter = ((IStructuredSelection) selection)
252: .iterator(); iter.hasNext();) {
253: Object element = iter.next();
254: if (isValideElement(element))
255: result.add(element);
256: }
257: if (result.isEmpty())
258: return null;
259:
260: return (IJavaElement[]) result.toArray(new IJavaElement[result
261: .size()]);
262: }
263:
264: private IJavaElement getSelectedElement(JavaEditor editor) {
265: ISourceViewer viewer = editor.getViewer();
266: if (viewer == null)
267: return null;
268:
269: Point selectedRange = viewer.getSelectedRange();
270: int length = selectedRange.y;
271: int offset = selectedRange.x;
272:
273: ITypeRoot element = JavaUI.getEditorInputTypeRoot(editor
274: .getEditorInput());
275: if (element == null)
276: return null;
277:
278: CompilationUnit ast = SharedASTProvider.getAST(element,
279: SharedASTProvider.WAIT_YES, null);
280: if (ast == null)
281: return null;
282:
283: NodeFinder finder = new NodeFinder(offset, length);
284: ast.accept(finder);
285: ASTNode node = finder.getCoveringNode();
286:
287: IBinding binding = null;
288: if (node instanceof Name) {
289: binding = ((Name) node).resolveBinding();
290: } else if (node instanceof MethodInvocation) {
291: binding = ((MethodInvocation) node).resolveMethodBinding();
292: } else if (node instanceof MethodDeclaration) {
293: binding = ((MethodDeclaration) node).resolveBinding();
294: } else if (node instanceof Type) {
295: binding = ((Type) node).resolveBinding();
296: } else if (node instanceof AnonymousClassDeclaration) {
297: binding = ((AnonymousClassDeclaration) node)
298: .resolveBinding();
299: } else if (node instanceof TypeDeclaration) {
300: binding = ((TypeDeclaration) node).resolveBinding();
301: } else if (node instanceof CompilationUnit) {
302: return ((CompilationUnit) node).getJavaElement();
303: } else if (node instanceof Expression) {
304: binding = ((Expression) node).resolveTypeBinding();
305: } else if (node instanceof ImportDeclaration) {
306: binding = ((ImportDeclaration) node).resolveBinding();
307: } else if (node instanceof MemberRef) {
308: binding = ((MemberRef) node).resolveBinding();
309: } else if (node instanceof MemberValuePair) {
310: binding = ((MemberValuePair) node)
311: .resolveMemberValuePairBinding();
312: } else if (node instanceof PackageDeclaration) {
313: binding = ((PackageDeclaration) node).resolveBinding();
314: } else if (node instanceof TypeParameter) {
315: binding = ((TypeParameter) node).resolveBinding();
316: } else if (node instanceof VariableDeclaration) {
317: binding = ((VariableDeclaration) node).resolveBinding();
318: }
319:
320: if (binding != null)
321: return binding.getJavaElement();
322:
323: return null;
324: }
325:
326: }
|