-
Notifications
You must be signed in to change notification settings - Fork 933
Expand file tree
/
Copy pathTreeNavigatorProviderImpl.java
More file actions
297 lines (246 loc) · 10.3 KB
/
Copy pathTreeNavigatorProviderImpl.java
File metadata and controls
297 lines (246 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.netbeans.modules.java.debug;
import com.sun.source.tree.MethodTree;
import com.sun.source.tree.Tree;
import com.sun.source.tree.VariableTree;
import com.sun.source.util.SourcePositions;
import com.sun.source.util.TreePath;
import org.netbeans.api.java.source.support.ErrorAwareTreePathScanner;
import java.awt.BorderLayout;
import java.awt.Color;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyVetoException;
import java.lang.ref.Reference;
import java.lang.ref.WeakReference;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ActionMap;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.ListSelectionModel;
import javax.swing.text.AttributeSet;
import javax.swing.text.Document;
import javax.swing.text.StyleConstants;
import org.netbeans.api.editor.settings.AttributesUtilities;
import org.netbeans.api.java.source.CancellableTask;
import org.netbeans.api.java.source.CompilationInfo;
import org.netbeans.api.java.source.support.CaretAwareJavaSourceTaskFactory;
import org.netbeans.spi.editor.highlighting.support.OffsetsBag;
import org.netbeans.spi.navigator.NavigatorPanel;
import org.openide.cookies.EditorCookie;
import org.openide.explorer.ExplorerManager;
import org.openide.explorer.ExplorerUtils;
import org.openide.explorer.view.BeanTreeView;
import org.openide.filesystems.FileObject;
import org.openide.loaders.DataObject;
import org.openide.loaders.DataObjectNotFoundException;
import org.openide.nodes.Node;
import org.openide.util.Lookup;
import org.openide.util.NbBundle;
/**
*
* @author Jan Lahoda
*/
public class TreeNavigatorProviderImpl implements NavigatorPanel {
private JComponent panel;
private final ExplorerManager manager = new ExplorerManager();
/**
* Default constructor for layer instance.
*/
public TreeNavigatorProviderImpl() {
manager.addPropertyChangeListener(new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent evt) {
if (ExplorerManager.PROP_SELECTED_NODES.equals(evt.getPropertyName())) {
setHighlights(TreeNavigatorJavaSourceFactory.getInstance().getFile(), manager);
}
}
});
}
public String getDisplayName() {
return NbBundle.getMessage(TreeNavigatorProviderImpl.class, "NM_Trees");
}
public String getDisplayHint() {
return NbBundle.getMessage(TreeNavigatorProviderImpl.class, "SD_Trees");
}
public JComponent getComponent() {
if (panel == null) {
final BeanTreeView view = new BeanTreeView();
view.setRootVisible(true);
view.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
class Panel extends JPanel implements ExplorerManager.Provider, Lookup.Provider {
// Make sure action context works correctly:
private final Lookup lookup = ExplorerUtils.createLookup(manager, new ActionMap());
{
setLayout(new BorderLayout());
add(view, BorderLayout.CENTER);
}
public ExplorerManager getExplorerManager() {
return manager;
}
public Lookup getLookup() {
return lookup;
}
}
panel = new Panel();
}
return panel;
}
public Lookup getLookup() {
return null;
}
public void panelActivated(Lookup context) {
TreeNavigatorJavaSourceFactory.getInstance().setLookup(context, new TaskImpl());
TreeNavigatorJavaSourceFactory.CaretAwareFactoryImpl.getInstance().setTask(new SelectingTaskImpl());
}
public void panelDeactivated() {
TreeNavigatorJavaSourceFactory.getInstance().setLookup(Lookup.EMPTY, null);
TreeNavigatorJavaSourceFactory.CaretAwareFactoryImpl.getInstance().setTask(null);
setHighlights(null, null);
}
static OffsetsBag getBag(Document doc) {
OffsetsBag bag = (OffsetsBag) doc.getProperty(TreeNavigatorProviderImpl.class);
if (bag == null) {
doc.putProperty(TreeNavigatorProviderImpl.class, bag = new OffsetsBag(doc));
}
return bag;
}
private static Reference<FileObject> lastFile = null;
static synchronized void setHighlights(FileObject file, ExplorerManager manager) {
FileObject last = lastFile != null ? lastFile.get() : null;
Document lastDoc = documentFor(last);
if (lastDoc != null) {
getBag(lastDoc).clear();
}
Document doc = documentFor(file);
if (doc == null) {
return;
}
OffsetsBag bag = new OffsetsBag(doc, true);
for (Node n : manager.getSelectedNodes()) {
if (n instanceof OffsetProvider) {
OffsetProvider p = (OffsetProvider) n;
final int start = p.getStart();
final int end = p.getEnd();
final int pref = p.getPreferredPosition();
if (start >= 0 && end >= 0) {
bag.addHighlight(start, end, HIGHLIGHT);
}
if (pref >= 0) {
bag.addHighlight(pref, pref+1, HIGHLIGHT_PREF);
}
}
}
getBag(doc).setHighlights(bag);
if (last != file) {
lastFile = new WeakReference<>(file);
}
}
private static Document documentFor(FileObject file) {
if (file == null) {
return null;
}
EditorCookie ec = file.getLookup().lookup(EditorCookie.class);
if (ec == null) {
return null;
}
return ec.getDocument();
}
private static final AttributeSet HIGHLIGHT = AttributesUtilities.createImmutable(StyleConstants.Background, new Color(220, 220, 220));
private static final AttributeSet HIGHLIGHT_PREF = AttributesUtilities.createImmutable(StyleConstants.Underline, new Color(30, 255, 0));
private final class TaskImpl implements CancellableTask<CompilationInfo> {
private final AtomicBoolean cancel = new AtomicBoolean();
public void cancel() {
cancel.set(true);
}
public void run(CompilationInfo info) {
cancel.set(false);
Node tree = TreeNode.getTree(info, new TreePath(info.getCompilationUnit()), cancel);
if (!cancel.get()) {
manager.setRootContext(tree);
}
}
}
private final class SelectingTaskImpl implements CancellableTask<CompilationInfo> {
public void cancel() {
}
public void run(CompilationInfo info) throws PropertyVetoException {
int pos = CaretAwareJavaSourceTaskFactory.getLastPosition(info.getFileObject());
TreePath tp = plainPathFor(info, pos);
Node toSelect = tp != null ? TreeNode.findNode(manager.getRootContext(), tp) : null;
if (toSelect != null) {
manager.setExploredContext(toSelect);
manager.setSelectedNodes(new Node[] {toSelect});
} else {
manager.setSelectedNodes(new Node[] {});
}
}
}
private static TreePath plainPathFor(final CompilationInfo info, int pos) {
//TODO: TreeUtilities.pathFor handles error trees in a strange way - not sure if intentional, but unusable for the tree navigator
class Result extends Error {
TreePath path;
Result(TreePath path) {
this.path = path;
}
}
class PathFinder extends ErrorAwareTreePathScanner<Void,Void> {
private int pos;
private SourcePositions sourcePositions;
private PathFinder(int pos, SourcePositions sourcePositions) {
this.pos = pos;
this.sourcePositions = sourcePositions;
}
@Override
public Void scan(Tree tree, Void p) {
if (tree != null) {
if (sourcePositions.getStartPosition(getCurrentPath().getCompilationUnit(), tree) < pos && sourcePositions.getEndPosition(getCurrentPath().getCompilationUnit(), tree) >= pos) {
super.scan(tree, p);
throw new Result(new TreePath(getCurrentPath(), tree));
}
}
return null;
}
@Override
public Void visitVariable(VariableTree node, Void p) {
int[] span = info.getTreeUtilities().findNameSpan(node);
if (span != null && span[0] <= pos && pos < span[1]) {
throw new Result(getCurrentPath());
}
return super.visitVariable(node, p);
}
@Override
public Void visitMethod(MethodTree node, Void p) {
int[] span = info.getTreeUtilities().findNameSpan(node);
if (span != null && span[0] <= pos && pos < span[1]) {
throw new Result(getCurrentPath());
}
return super.visitMethod(node, p);
}
}
try {
new PathFinder(pos, info.getTrees().getSourcePositions()).scan(new TreePath(info.getCompilationUnit()), null);
return null;
} catch (Result result) {
return result.path;
}
}
}