1 /*******************************************************************************
2 * Copyright (c) 2000, 2011 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
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;
14 import java.io.BufferedReader;
15 import java.io.IOException;
16 import java.io.PrintWriter;
17 import java.io.StringReader;
18 import java.io.StringWriter;
20 import org.eclipse.osgi.util.TextProcessor;
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;
29 import org.eclipse.core.runtime.Assert;
31 import org.eclipse.jface.action.Action;
32 import org.eclipse.jface.dialogs.MessageDialog;
33 import org.eclipse.jface.viewers.ISelection;
34 import org.eclipse.jface.viewers.ISelectionProvider;
36 import org.eclipse.ui.PlatformUI;
38 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
39 import org.eclipse.jdt.internal.ui.util.SelectionUtil;
41 class CopyCallHierarchyAction extends Action {
42 private static final char INDENTATION= '\t';
44 private CallHierarchyViewPart fView;
46 private CallHierarchyViewer fViewer;
48 private final Clipboard fClipboard;
50 public CopyCallHierarchyAction(CallHierarchyViewPart view, Clipboard clipboard, CallHierarchyViewer viewer) {
51 super(CallHierarchyMessages.CopyCallHierarchyAction_label);
52 Assert.isNotNull(clipboard);
53 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.CALL_HIERARCHY_COPY_ACTION);
55 fClipboard= clipboard;
59 public boolean canActionBeAdded() {
60 Object element= SelectionUtil.getSingleElement(getSelection());
61 return element != null;
64 private ISelection getSelection() {
65 ISelectionProvider provider= fView.getSite().getSelectionProvider();
67 if (provider != null) {
68 return provider.getSelection();
79 StringBuffer buf= new StringBuffer();
80 addCalls(fViewer.getTree().getSelection()[0], 0, buf);
82 TextTransfer plainTextTransfer= TextTransfer.getInstance();
84 fClipboard.setContents(
85 new String[] { convertLineTerminators(buf.toString()) },
86 new Transfer[] { plainTextTransfer });
87 } catch (SWTError e) {
88 if (e.code != DND.ERROR_CANNOT_SET_CLIPBOARD)
90 if (MessageDialog.openQuestion(fView.getViewSite().getShell(), CallHierarchyMessages.CopyCallHierarchyAction_problem, CallHierarchyMessages.CopyCallHierarchyAction_clipboard_busy))
96 * Adds the specified {@link TreeItem}'s text to the StringBuffer.
98 * @param item the tree item
99 * @param indent the indent size
100 * @param buf the string buffer
102 private void addCalls(TreeItem item, int indent, StringBuffer buf) {
103 for (int i= 0; i < indent; i++) {
104 buf.append(INDENTATION);
107 buf.append(TextProcessor.deprocess(item.getText()));
110 if (item.getExpanded()) {
111 TreeItem[] items= item.getItems();
112 for (int i= 0; i < items.length; i++) {
113 addCalls(items[i], indent + 1, buf);
118 static String convertLineTerminators(String in) {
119 StringWriter stringWriter= new StringWriter();
120 PrintWriter printWriter= new PrintWriter(stringWriter);
121 StringReader stringReader= new StringReader(in);
122 BufferedReader bufferedReader= new BufferedReader(stringReader);
124 String line= bufferedReader.readLine();
125 while (line != null) {
126 printWriter.print(line);
127 line= bufferedReader.readLine();
128 if (line != null && line.length() != 0)
129 printWriter.println();
132 } catch (IOException e) {
133 return in; // return the call hierarchy unfiltered
135 return stringWriter.toString();