001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 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: * Jesper Kamstrup Linnet (eclipse@kamstrup-linnet.dk) - initial API and implementation
010: * (report 36180: Callers/Callees view)
011: *******************************************************************************/package org.eclipse.jdt.internal.ui.callhierarchy;
012:
013: import java.io.BufferedReader;
014: import java.io.IOException;
015: import java.io.PrintWriter;
016: import java.io.StringReader;
017: import java.io.StringWriter;
018:
019: import org.eclipse.core.runtime.Assert;
020:
021: import org.eclipse.swt.SWTError;
022: import org.eclipse.swt.dnd.Clipboard;
023: import org.eclipse.swt.dnd.DND;
024: import org.eclipse.swt.dnd.TextTransfer;
025: import org.eclipse.swt.dnd.Transfer;
026: import org.eclipse.swt.widgets.TreeItem;
027:
028: import org.eclipse.jface.action.Action;
029: import org.eclipse.jface.dialogs.MessageDialog;
030: import org.eclipse.jface.viewers.ISelection;
031: import org.eclipse.jface.viewers.ISelectionProvider;
032:
033: import org.eclipse.ui.PlatformUI;
034:
035: import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
036: import org.eclipse.jdt.internal.ui.util.SelectionUtil;
037:
038: class CopyCallHierarchyAction extends Action {
039: private static final char INDENTATION = '\t';
040:
041: private CallHierarchyViewPart fView;
042: private CallHierarchyViewer fViewer;
043:
044: private final Clipboard fClipboard;
045:
046: public CopyCallHierarchyAction(CallHierarchyViewPart view,
047: Clipboard clipboard, CallHierarchyViewer viewer) {
048: super (CallHierarchyMessages.CopyCallHierarchyAction_label);
049: Assert.isNotNull(clipboard);
050: PlatformUI.getWorkbench().getHelpSystem().setHelp(this ,
051: IJavaHelpContextIds.CALL_HIERARCHY_COPY_ACTION);
052: fView = view;
053: fClipboard = clipboard;
054: fViewer = viewer;
055: }
056:
057: public boolean canActionBeAdded() {
058: Object element = SelectionUtil.getSingleElement(getSelection());
059: return element != null;
060: }
061:
062: private ISelection getSelection() {
063: ISelectionProvider provider = fView.getSite()
064: .getSelectionProvider();
065:
066: if (provider != null) {
067: return provider.getSelection();
068: }
069:
070: return null;
071: }
072:
073: /*
074: * @see IAction#run()
075: */
076: public void run() {
077: StringBuffer buf = new StringBuffer();
078: addCalls(fViewer.getTree().getSelection()[0], 0, buf);
079:
080: TextTransfer plainTextTransfer = TextTransfer.getInstance();
081: try {
082: fClipboard.setContents(
083: new String[] { convertLineTerminators(buf
084: .toString()) },
085: new Transfer[] { plainTextTransfer });
086: } catch (SWTError e) {
087: if (e.code != DND.ERROR_CANNOT_SET_CLIPBOARD)
088: throw e;
089: if (MessageDialog
090: .openQuestion(
091: fView.getViewSite().getShell(),
092: CallHierarchyMessages.CopyCallHierarchyAction_problem,
093: CallHierarchyMessages.CopyCallHierarchyAction_clipboard_busy))
094: run();
095: }
096: }
097:
098: /**
099: * Adds the specified TreeItem's text to the StringBuffer
100: *
101: * @param item
102: * @param buf
103: */
104: private void addCalls(TreeItem item, int indent, StringBuffer buf) {
105: for (int i = 0; i < indent; i++) {
106: buf.append(INDENTATION);
107: }
108:
109: buf.append(item.getText());
110: buf.append('\n');
111:
112: if (item.getExpanded()) {
113: TreeItem[] items = item.getItems();
114: for (int i = 0; i < items.length; i++) {
115: addCalls(items[i], indent + 1, buf);
116: }
117: }
118: }
119:
120: static String convertLineTerminators(String in) {
121: StringWriter stringWriter = new StringWriter();
122: PrintWriter printWriter = new PrintWriter(stringWriter);
123: StringReader stringReader = new StringReader(in);
124: BufferedReader bufferedReader = new BufferedReader(stringReader);
125: try {
126: String line = bufferedReader.readLine();
127: while (line != null) {
128: printWriter.print(line);
129: line = bufferedReader.readLine();
130: if (line != null && line.length() != 0)
131: printWriter.println();
132:
133: }
134: } catch (IOException e) {
135: return in; // return the call hierarchy unfiltered
136: }
137: return stringWriter.toString();
138: }
139: }
|