]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/jdt-before/ui/org/eclipse/jdt/internal/ui/callhierarchy/CopyCallHierarchyAction.java
2da17e129003601219b1956db14e8cb4ee97098d
[ifi-stolz-refaktor.git] / case-study / jdt-before / ui / org / eclipse / jdt / internal / ui / callhierarchy / CopyCallHierarchyAction.java
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
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;
15 import java.io.IOException;
16 import java.io.PrintWriter;
17 import java.io.StringReader;
18 import java.io.StringWriter;
19
20 import org.eclipse.osgi.util.TextProcessor;
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.core.runtime.Assert;
30
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;
35
36 import org.eclipse.ui.PlatformUI;
37
38 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
39 import org.eclipse.jdt.internal.ui.util.SelectionUtil;
40
41 class CopyCallHierarchyAction extends Action {
42         private static final char INDENTATION= '\t';
43
44         private CallHierarchyViewPart fView;
45
46         private CallHierarchyViewer fViewer;
47
48         private final Clipboard fClipboard;
49
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);
54                 fView= view;
55                 fClipboard= clipboard;
56                 fViewer= viewer;
57         }
58
59         public boolean canActionBeAdded() {
60                 Object element= SelectionUtil.getSingleElement(getSelection());
61                 return element != null;
62         }
63
64         private ISelection getSelection() {
65                 ISelectionProvider provider= fView.getSite().getSelectionProvider();
66
67                 if (provider != null) {
68                         return provider.getSelection();
69                 }
70
71                 return null;
72         }
73
74         /*
75          * @see IAction#run()
76          */
77         @Override
78         public void run() {
79                 StringBuffer buf= new StringBuffer();
80                 addCalls(fViewer.getTree().getSelection()[0], 0, buf);
81
82                 TextTransfer plainTextTransfer= TextTransfer.getInstance();
83                 try {
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)
89                                 throw e;
90                         if (MessageDialog.openQuestion(fView.getViewSite().getShell(), CallHierarchyMessages.CopyCallHierarchyAction_problem, CallHierarchyMessages.CopyCallHierarchyAction_clipboard_busy))
91                                 run();
92                 }
93         }
94
95         /**
96          * Adds the specified {@link TreeItem}'s text to the StringBuffer.
97          * 
98          * @param item the tree item
99          * @param indent the indent size
100          * @param buf the string buffer
101          */
102         private void addCalls(TreeItem item, int indent, StringBuffer buf) {
103                 for (int i= 0; i < indent; i++) {
104                         buf.append(INDENTATION);
105                 }
106
107                 buf.append(TextProcessor.deprocess(item.getText()));
108                 buf.append('\n');
109
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);
114                         }
115                 }
116         }
117
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);
123                 try {
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();
130
131                         }
132                 } catch (IOException e) {
133                         return in; // return the call hierarchy unfiltered
134                 }
135                 return stringWriter.toString();
136         }
137 }