KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Jesper Kamstrup Linnet (eclipse@kamstrup-linnet.dk) - initial API and implementation
10  * (report 36180: Callers/Callees view)
11  *******************************************************************************/

12 package org.eclipse.jdt.internal.ui.callhierarchy;
13
14 import java.io.BufferedReader JavaDoc;
15 import java.io.IOException JavaDoc;
16 import java.io.PrintWriter JavaDoc;
17 import java.io.StringReader JavaDoc;
18 import java.io.StringWriter JavaDoc;
19
20 import org.eclipse.core.runtime.Assert;
21
22 import org.eclipse.swt.SWTError;
23 import org.eclipse.swt.dnd.Clipboard;
24 import org.eclipse.swt.dnd.DND;
25 import org.eclipse.swt.dnd.TextTransfer;
26 import org.eclipse.swt.dnd.Transfer;
27 import org.eclipse.swt.widgets.TreeItem;
28
29 import org.eclipse.jface.action.Action;
30 import org.eclipse.jface.dialogs.MessageDialog;
31 import org.eclipse.jface.viewers.ISelection;
32 import org.eclipse.jface.viewers.ISelectionProvider;
33
34 import org.eclipse.ui.PlatformUI;
35
36 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
37 import org.eclipse.jdt.internal.ui.util.SelectionUtil;
38
39 class CopyCallHierarchyAction extends Action {
40     private static final char INDENTATION= '\t';
41     
42     private CallHierarchyViewPart fView;
43     private CallHierarchyViewer fViewer;
44     
45     private final Clipboard fClipboard;
46
47     public CopyCallHierarchyAction(CallHierarchyViewPart view, Clipboard clipboard, CallHierarchyViewer viewer) {
48         super(CallHierarchyMessages.CopyCallHierarchyAction_label);
49         Assert.isNotNull(clipboard);
50         PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.CALL_HIERARCHY_COPY_ACTION);
51         fView= view;
52         fClipboard= clipboard;
53         fViewer= viewer;
54     }
55
56     public boolean canActionBeAdded() {
57         Object JavaDoc element = SelectionUtil.getSingleElement(getSelection());
58         return element != null;
59     }
60     
61     private ISelection getSelection() {
62         ISelectionProvider provider = fView.getSite().getSelectionProvider();
63
64         if (provider != null) {
65             return provider.getSelection();
66         }
67
68         return null;
69     }
70     
71     /*
72      * @see IAction#run()
73      */

74     public void run() {
75         StringBuffer JavaDoc buf= new StringBuffer JavaDoc();
76         addCalls(fViewer.getTree().getSelection()[0], 0, buf);
77
78         TextTransfer plainTextTransfer = TextTransfer.getInstance();
79         try{
80             fClipboard.setContents(
81                 new String JavaDoc[]{ convertLineTerminators(buf.toString()) },
82                 new Transfer[]{ plainTextTransfer });
83         } catch (SWTError e){
84             if (e.code != DND.ERROR_CANNOT_SET_CLIPBOARD)
85                 throw e;
86             if (MessageDialog.openQuestion(fView.getViewSite().getShell(), CallHierarchyMessages.CopyCallHierarchyAction_problem, CallHierarchyMessages.CopyCallHierarchyAction_clipboard_busy))
87                 run();
88         }
89     }
90     
91     /**
92      * Adds the specified TreeItem's text to the StringBuffer
93      *
94      * @param item
95      * @param buf
96      */

97     private void addCalls(TreeItem item, int indent, StringBuffer JavaDoc buf) {
98         for (int i= 0; i < indent; i++) {
99             buf.append(INDENTATION);
100         }
101
102         buf.append(item.getText());
103         buf.append('\n');
104         
105         if (item.getExpanded()) {
106             TreeItem[] items= item.getItems();
107             for (int i= 0; i < items.length; i++) {
108                 addCalls(items[i], indent + 1, buf);
109             }
110         }
111     }
112
113     static String JavaDoc convertLineTerminators(String JavaDoc in) {
114         StringWriter JavaDoc stringWriter= new StringWriter JavaDoc();
115         PrintWriter JavaDoc printWriter= new PrintWriter JavaDoc(stringWriter);
116         StringReader JavaDoc stringReader= new StringReader JavaDoc(in);
117         BufferedReader JavaDoc bufferedReader= new BufferedReader JavaDoc(stringReader);
118         try {
119             String JavaDoc line= bufferedReader.readLine();
120             while (line != null) {
121                 printWriter.print(line);
122                 line= bufferedReader.readLine();
123                 if (line != null && line.length() != 0)
124                     printWriter.println();
125                     
126             }
127         } catch (IOException JavaDoc e) {
128             return in; // return the call hierarchy unfiltered
129
}
130         return stringWriter.toString();
131     }
132 }
133
Popular Tags