EclipseJDT ASTViewer Source Viewer

Home|eclipse_jdt_astviewer/src/org/eclipse/jdt/astview/views/TreeCopyAction.java
1/*******************************************************************************
2 * Copyright (c) 2000, 2015 IBM Corporation and others.
3 *
4 * This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License 2.0
6 * which accompanies this distribution, and is available at
7 * https://www.eclipse.org/legal/epl-2.0/
8 *
9 * SPDX-License-Identifier: EPL-2.0
10 *
11 * Contributors:
12 *     IBM Corporation - initial API and implementation
13 *******************************************************************************/
14package org.eclipse.jdt.astview.views;
15
16import java.util.ArrayList;
17import java.util.HashMap;
18import java.util.List;
19
20import org.eclipse.swt.dnd.Clipboard;
21import org.eclipse.swt.dnd.TextTransfer;
22import org.eclipse.swt.dnd.Transfer;
23import org.eclipse.swt.widgets.Tree;
24import org.eclipse.swt.widgets.TreeItem;
25
26import org.eclipse.jface.action.Action;
27
28import org.eclipse.ui.ISharedImages;
29import org.eclipse.ui.IWorkbenchCommandConstants;
30import org.eclipse.ui.PlatformUI;
31import org.eclipse.ui.actions.ActionFactory;
32
33
34public class TreeCopyAction extends Action {
35
36    private static class TreeObject {
37        private final TreeItem fTreeItem;
38        private boolean fSelected;
39        private final List<TreeObjectfChildren;
40        public TreeObject(TreeItem elementboolean selected) {
41            fTreeItemelement;
42            fSelectedselected;
43            fChildren= new ArrayList<>();
44        }
45        public void setSelected() {
46            fSelectedtrue;
47        }
48        public void addChild(TreeObject child) {
49            fChildren.add(child);
50        }
51        public boolean isSelected() {
52            return fSelected;
53        }
54        public TreeItem getTreeItem() {
55            return fTreeItem;
56        }
57        public List<TreeObjectgetChildren() {
58            return fChildren;
59        }
60        @Override
61        public String toString() {
62            StringBuilder buf= new StringBuilder();
63            if (fSelected)
64                buf.append("* "); //$NON-NLS-1$
65            buf.append(trim(fTreeItem.getText())).append(" ["); //$NON-NLS-1$
66            for (int i0i < fChildren.size(); i++) {
67                TreeObject childfChildren.get(i);
68                buf.append(trim(child.getTreeItem().getText()));
69                if (i > 0)
70                    buf.append(", "); //$NON-NLS-1$
71            }
72            return buf.append("]").toString(); //$NON-NLS-1$
73        }
74        private String trim(String string) {
75            if (string.length() > 60)
76                return string.substring(060) + "..."//$NON-NLS-1$
77            else
78                return string;
79        }
80    }
81
82    private final Tree[] fTrees;
83
84    public TreeCopyAction(Tree[] trees) {
85        fTreestrees;
86        setText("&Copy"); //$NON-NLS-1$
87        setToolTipText("Copy to Clipboard"); //$NON-NLS-1$
88        setEnabled(false);
89        setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(ISharedImages.IMG_TOOL_COPY));
90        setId(ActionFactory.COPY.getId());
91        setActionDefinitionId(IWorkbenchCommandConstants.EDIT_COPY);
92    }
93
94    @Override
95    public void run() {
96        Tree treenull;
97        for (Tree t : fTrees) {
98            if (t.isFocusControl()) {
99                treet;
100                break;
101            }
102        }
103        if (tree == null)
104            return;
105
106        TreeItem[] selectiontree.getSelection();
107        if (selection.length == 0)
108            return;
109
110        Clipboard clipboardnull;
111        try {
112            clipboard= new Clipboard(tree.getDisplay());
113            if (selection.length == 1) {
114                clipboard.setContents(new Object[]{selection[0].getText()}, new Transfer[]{TextTransfer.getInstance()});
115            } else if (selection.length > 1) {
116                copyTree(selectionclipboard);
117            }
118        } finally {
119            if (clipboard != null)
120                clipboard.dispose();
121        }
122    }
123
124    private void copyTree(TreeItem[] selectionClipboard clipboard) {
125        HashMap<TreeItemTreeObjectelementToTreeObj= new HashMap<>();
126        List<TreeObjectroots= new ArrayList<>();
127        int indentInteger.MIN_VALUE;
128
129        for (TreeItem item : selection) {
130            TreeObject treeObjelementToTreeObj.get(item);
131            if (treeObj == null) {
132                treeObj= new TreeObject(itemtrue);
133                elementToTreeObj.put(itemtreeObj);
134            } else {
135                treeObj.setSelected();
136                continue;
137            }
138            // walk up to roots:
139            int level0;
140            itemitem.getParentItem();
141            while (item != null) {
142                TreeObject parentTreeObjelementToTreeObj.get(item);
143                if (parentTreeObj == null) {
144                    parentTreeObj= new TreeObject(itemfalse);
145                    elementToTreeObj.put(itemparentTreeObj);
146                    parentTreeObj.addChild(treeObj);
147                    treeObjparentTreeObj;
148                    itemitem.getParentItem();
149                    level--;
150                } else {
151                    parentTreeObj.addChild(treeObj);
152                    treeObjparentTreeObj;
153                    break;
154                }
155            }
156            if (item == null) {
157                roots.add(treeObj);
158                indentMath.max(indentlevel);
159            }
160        }
161        elementToTreeObjnull;
162        StringBuffer buf= new StringBuffer();
163        appendSelectionObjects(bufindentroots);
164        String resultbuf.length() > 0 ? buf.substring(1) : buf.toString();
165        clipboard.setContents(new Object[] { result }, new Transfer[] { TextTransfer.getInstance() });
166    }
167
168    private void appendSelectionObjects(StringBuffer bufferint indentList<TreeObjectselObjs) {
169        for (TreeObject selObj : selObjs) {
170            if (selObj.isSelected()) {
171                buffer.append('\n');
172                for (int d0d < indentd++)
173                    buffer.append('\t');
174                buffer.append(selObj.getTreeItem().getText());
175            }
176            appendSelectionObjects(bufferindent + 1selObj.getChildren());
177        }
178    }
179}
180
MembersX
TreeCopyAction:TreeObject:getTreeItem
TreeCopyAction:run
TreeCopyAction:fTrees
TreeCopyAction:TreeObject:toString
TreeCopyAction:TreeObject:isSelected
TreeCopyAction:TreeCopyAction
TreeCopyAction:copyTree:Block:result
TreeCopyAction:copyTree:Block:Block:treeObj
TreeCopyAction:TreeObject:fSelected
TreeCopyAction:run:Block:selection
TreeCopyAction:TreeObject:fChildren
TreeCopyAction:TreeObject:toString:Block:buf
TreeCopyAction:TreeObject:fTreeItem
TreeCopyAction:appendSelectionObjects
TreeCopyAction:TreeObject:setSelected
TreeCopyAction:copyTree:Block:roots
TreeCopyAction:copyTree:Block:Block:level
TreeCopyAction:TreeObject:getChildren
TreeCopyAction:copyTree
TreeCopyAction:TreeObject:addChild
TreeCopyAction:TreeObject:toString:Block:Block:child
TreeCopyAction:copyTree:Block:buf
TreeCopyAction:run:Block:clipboard
TreeCopyAction:TreeObject:TreeObject
TreeCopyAction:TreeObject:trim
TreeCopyAction:copyTree:Block:Block:Block:parentTreeObj
TreeCopyAction:run:Block:tree
TreeCopyAction:copyTree:Block:indent
TreeCopyAction:copyTree:Block:elementToTreeObj
Members
X