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 | *******************************************************************************/ |
14 | package org.eclipse.jdt.astview.views; |
15 | |
16 | import java.util.ArrayList; |
17 | import java.util.HashMap; |
18 | import java.util.List; |
19 | |
20 | import org.eclipse.swt.dnd.Clipboard; |
21 | import org.eclipse.swt.dnd.TextTransfer; |
22 | import org.eclipse.swt.dnd.Transfer; |
23 | import org.eclipse.swt.widgets.Tree; |
24 | import org.eclipse.swt.widgets.TreeItem; |
25 | |
26 | import org.eclipse.jface.action.Action; |
27 | |
28 | import org.eclipse.ui.ISharedImages; |
29 | import org.eclipse.ui.IWorkbenchCommandConstants; |
30 | import org.eclipse.ui.PlatformUI; |
31 | import org.eclipse.ui.actions.ActionFactory; |
32 | |
33 | |
34 | public class TreeCopyAction extends Action { |
35 | |
36 | private static class TreeObject { |
37 | private final TreeItem fTreeItem; |
38 | private boolean fSelected; |
39 | private final List<TreeObject> fChildren; |
40 | public TreeObject(TreeItem element, boolean selected) { |
41 | fTreeItem= element; |
42 | fSelected= selected; |
43 | fChildren= new ArrayList<>(); |
44 | } |
45 | public void setSelected() { |
46 | fSelected= true; |
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<TreeObject> getChildren() { |
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 i= 0; i < fChildren.size(); i++) { |
67 | TreeObject child= fChildren.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(0, 60) + "..."; //$NON-NLS-1$ |
77 | else |
78 | return string; |
79 | } |
80 | } |
81 | |
82 | private final Tree[] fTrees; |
83 | |
84 | public TreeCopyAction(Tree[] trees) { |
85 | fTrees= trees; |
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 tree= null; |
97 | for (Tree t : fTrees) { |
98 | if (t.isFocusControl()) { |
99 | tree= t; |
100 | break; |
101 | } |
102 | } |
103 | if (tree == null) |
104 | return; |
105 | |
106 | TreeItem[] selection= tree.getSelection(); |
107 | if (selection.length == 0) |
108 | return; |
109 | |
110 | Clipboard clipboard= null; |
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(selection, clipboard); |
117 | } |
118 | } finally { |
119 | if (clipboard != null) |
120 | clipboard.dispose(); |
121 | } |
122 | } |
123 | |
124 | private void copyTree(TreeItem[] selection, Clipboard clipboard) { |
125 | HashMap<TreeItem, TreeObject> elementToTreeObj= new HashMap<>(); |
126 | List<TreeObject> roots= new ArrayList<>(); |
127 | int indent= Integer.MIN_VALUE; |
128 | |
129 | for (TreeItem item : selection) { |
130 | TreeObject treeObj= elementToTreeObj.get(item); |
131 | if (treeObj == null) { |
132 | treeObj= new TreeObject(item, true); |
133 | elementToTreeObj.put(item, treeObj); |
134 | } else { |
135 | treeObj.setSelected(); |
136 | continue; |
137 | } |
138 | // walk up to roots: |
139 | int level= 0; |
140 | item= item.getParentItem(); |
141 | while (item != null) { |
142 | TreeObject parentTreeObj= elementToTreeObj.get(item); |
143 | if (parentTreeObj == null) { |
144 | parentTreeObj= new TreeObject(item, false); |
145 | elementToTreeObj.put(item, parentTreeObj); |
146 | parentTreeObj.addChild(treeObj); |
147 | treeObj= parentTreeObj; |
148 | item= item.getParentItem(); |
149 | level--; |
150 | } else { |
151 | parentTreeObj.addChild(treeObj); |
152 | treeObj= parentTreeObj; |
153 | break; |
154 | } |
155 | } |
156 | if (item == null) { |
157 | roots.add(treeObj); |
158 | indent= Math.max(indent, level); |
159 | } |
160 | } |
161 | elementToTreeObj= null; |
162 | StringBuffer buf= new StringBuffer(); |
163 | appendSelectionObjects(buf, indent, roots); |
164 | String result= buf.length() > 0 ? buf.substring(1) : buf.toString(); |
165 | clipboard.setContents(new Object[] { result }, new Transfer[] { TextTransfer.getInstance() }); |
166 | } |
167 | |
168 | private void appendSelectionObjects(StringBuffer buffer, int indent, List<TreeObject> selObjs) { |
169 | for (TreeObject selObj : selObjs) { |
170 | if (selObj.isSelected()) { |
171 | buffer.append('\n'); |
172 | for (int d= 0; d < indent; d++) |
173 | buffer.append('\t'); |
174 | buffer.append(selObj.getTreeItem().getText()); |
175 | } |
176 | appendSelectionObjects(buffer, indent + 1, selObj.getChildren()); |
177 | } |
178 | } |
179 | } |
180 |
Members