KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > callhierarchy > LocationCopyAction


1 /*******************************************************************************
2  * Copyright (c) 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.jdt.internal.ui.callhierarchy;
12
13 import java.util.Iterator JavaDoc;
14
15 import org.eclipse.swt.SWTError;
16 import org.eclipse.swt.dnd.Clipboard;
17 import org.eclipse.swt.dnd.DND;
18 import org.eclipse.swt.dnd.TextTransfer;
19 import org.eclipse.swt.dnd.Transfer;
20
21 import org.eclipse.jface.action.Action;
22 import org.eclipse.jface.dialogs.MessageDialog;
23 import org.eclipse.jface.viewers.ISelectionChangedListener;
24 import org.eclipse.jface.viewers.IStructuredSelection;
25 import org.eclipse.jface.viewers.SelectionChangedEvent;
26
27 import org.eclipse.ui.IViewSite;
28 import org.eclipse.ui.texteditor.IWorkbenchActionDefinitionIds;
29
30 import org.eclipse.jdt.internal.corext.callhierarchy.CallLocation;
31
32 /**
33  * Copies the selection from the location viewer.
34  */

35 class LocationCopyAction extends Action {
36     private final Clipboard fClipboard;
37     private final IViewSite fViewSite;
38     private final LocationViewer fLocationViewer;
39
40     LocationCopyAction(IViewSite viewSite, Clipboard clipboard, LocationViewer locationViewer) {
41         fClipboard= clipboard;
42         fViewSite= viewSite;
43         fLocationViewer= locationViewer;
44         
45         setText(CallHierarchyMessages.LocationCopyAction_copy);
46         setActionDefinitionId(IWorkbenchActionDefinitionIds.COPY);
47         
48         locationViewer.addSelectionChangedListener(new ISelectionChangedListener() {
49             public void selectionChanged(SelectionChangedEvent event) {
50                 setEnabled(! event.getSelection().isEmpty());
51             }
52         });
53     }
54     
55     public void run() {
56         IStructuredSelection selection= (IStructuredSelection) fLocationViewer.getSelection();
57         StringBuffer JavaDoc buf= new StringBuffer JavaDoc();
58         for (Iterator JavaDoc iterator= selection.iterator(); iterator.hasNext();) {
59             CallLocation location= (CallLocation) iterator.next();
60             buf.append(location.getLineNumber()).append('\t').append(location.getCallText());
61             buf.append('\n');
62         }
63         TextTransfer plainTextTransfer = TextTransfer.getInstance();
64         try {
65             fClipboard.setContents(
66                     new String JavaDoc[]{ CopyCallHierarchyAction.convertLineTerminators(buf.toString()) },
67                     new Transfer[]{ plainTextTransfer });
68         } catch (SWTError e){
69             if (e.code != DND.ERROR_CANNOT_SET_CLIPBOARD)
70                 throw e;
71             if (MessageDialog.openQuestion(fViewSite.getShell(), CallHierarchyMessages.CopyCallHierarchyAction_problem, CallHierarchyMessages.CopyCallHierarchyAction_clipboard_busy))
72                 run();
73         }
74     }
75 }
Popular Tags