Line 0
Link Here
|
|
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 2016 Oracle and/or its affiliates. All rights reserved. |
5 |
* |
6 |
* Oracle and Java are registered trademarks of Oracle and/or its affiliates. |
7 |
* Other names may be trademarks of their respective owners. |
8 |
* |
9 |
* The contents of this file are subject to the terms of either the GNU |
10 |
* General Public License Version 2 only ("GPL") or the Common |
11 |
* Development and Distribution License("CDDL") (collectively, the |
12 |
* "License"). You may not use this file except in compliance with the |
13 |
* License. You can obtain a copy of the License at |
14 |
* http://www.netbeans.org/cddl-gplv2.html |
15 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
16 |
* specific language governing permissions and limitations under the |
17 |
* License. When distributing the software, include this License Header |
18 |
* Notice in each file and include the License file at |
19 |
* nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this |
20 |
* particular file as subject to the "Classpath" exception as provided |
21 |
* by Oracle in the GPL Version 2 section of the License file that |
22 |
* accompanied this code. If applicable, add the following below the |
23 |
* License Header, with the fields enclosed by brackets [] replaced by |
24 |
* your own identifying information: |
25 |
* "Portions Copyrighted [year] [name of copyright owner]" |
26 |
* |
27 |
* If you wish your version of this file to be governed by only the CDDL |
28 |
* or only the GPL Version 2, indicate your decision by adding |
29 |
* "[Contributor] elects to include this software in this distribution |
30 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
31 |
* single choice of license, a recipient has the option to distribute |
32 |
* your version of this file under either the CDDL, the GPL Version 2 or |
33 |
* to extend the choice of license to its licensees as provided above. |
34 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
35 |
* Version 2 license, then the option applies only if the new code is |
36 |
* made subject to such option by the copyright holder. |
37 |
* |
38 |
* Contributor(s): |
39 |
* |
40 |
* Portions Copyrighted 2016 Sun Microsystems, Inc. |
41 |
*/ |
42 |
package org.netbeans.spi.debugger.ui; |
43 |
|
44 |
import java.beans.Customizer; |
45 |
import java.beans.PropertyChangeEvent; |
46 |
import java.beans.PropertyChangeListener; |
47 |
import java.util.HashMap; |
48 |
import java.util.HashSet; |
49 |
import java.util.List; |
50 |
import java.util.Map; |
51 |
import java.util.Set; |
52 |
import org.netbeans.api.debugger.DebuggerEngine; |
53 |
import org.netbeans.api.debugger.DebuggerManager; |
54 |
import org.netbeans.api.debugger.DebuggerManagerAdapter; |
55 |
import org.netbeans.api.debugger.Watch; |
56 |
import org.netbeans.api.debugger.Watch.Pin; |
57 |
import org.netbeans.modules.debugger.ui.annotations.WatchAnnotationProvider; |
58 |
import org.netbeans.spi.debugger.DebuggerServiceRegistration; |
59 |
import org.openide.loaders.DataObjectNotFoundException; |
60 |
import org.openide.util.Exceptions; |
61 |
import org.openide.util.NbBundle; |
62 |
|
63 |
/** |
64 |
* Access to the default UI implementation of pin watches in editor. |
65 |
* Watches that are to be pinned by this support class, need to use the |
66 |
* {@link EditorPin} as the {@link Watch#getPin()} and an implementation |
67 |
* of {@link ValueProvider} needs to be register via {@link DebuggerServiceRegistration}, |
68 |
* which acts as a supplier of the watch value. |
69 |
* <p> |
70 |
* Use {@link #pin(org.netbeans.api.debugger.Watch, java.lang.String)} to pin |
71 |
* the watch into editor where the corresponding {@link EditorPin} points at |
72 |
* and provide <code>valueProviderId</code> of the corresponding registered |
73 |
* {@link ValueProvider} which handles the value updates. |
74 |
* |
75 |
* @author Martin Entlicher |
76 |
* @since 2.53 |
77 |
*/ |
78 |
public final class PinWatchUISupport { |
79 |
|
80 |
private static final PinWatchUISupport INSTANCE = new PinWatchUISupport(); |
81 |
private final Object valueProvidersLock = new Object(); |
82 |
private Map<String, DelegatingValueProvider> valueProviders; |
83 |
|
84 |
private PinWatchUISupport() { |
85 |
WatchAnnotationProvider.PIN_SUPPORT_ACCESS = new WatchAnnotationProvider.PinSupportedAccessor() { |
86 |
@Override |
87 |
public ValueProvider getValueProvider(EditorPin pin) { |
88 |
String id = pin.getVpId(); |
89 |
if (id == null) { |
90 |
return null; |
91 |
} |
92 |
synchronized (valueProvidersLock) { |
93 |
return getValueProviders().get(id); |
94 |
} |
95 |
} |
96 |
}; |
97 |
DebuggerManager.getDebuggerManager().addDebuggerListener( |
98 |
DebuggerManager.PROP_CURRENT_ENGINE, |
99 |
new DebuggerManagerAdapter() { |
100 |
@Override |
101 |
public void propertyChange(PropertyChangeEvent evt) { |
102 |
refreshValueProviders(); |
103 |
} |
104 |
} |
105 |
); |
106 |
} |
107 |
|
108 |
/** |
109 |
* Get the default instance of this class. |
110 |
* @return An instance of {@link PinWatchUISupport} class. |
111 |
*/ |
112 |
public static PinWatchUISupport getDefault() { |
113 |
return INSTANCE; |
114 |
} |
115 |
|
116 |
/** |
117 |
* Pin the watch into an editor. |
118 |
* The pinned watch need to return {@link EditorPin} from {@link Watch#getPin()} |
119 |
* and a {@link ValueProvider} needs to be registered for the provided |
120 |
* <code>valueProviderId</code>. |
121 |
* @param watch A watch to pin at the location determined by the {@link EditorPin}. |
122 |
* @param valueProviderId An id of a registered {@link ValueProvider}. |
123 |
* @throws IllegalArgumentException is thrown when {@link Watch#getPin()} |
124 |
* does not return an {@link EditorPin}, or |
125 |
* when we're not able to find the editor |
126 |
* where {@link EditorPin} points at. |
127 |
*/ |
128 |
public void pin(Watch watch, String valueProviderId) throws IllegalArgumentException { |
129 |
Pin wpin = watch.getPin(); |
130 |
if (!(wpin instanceof EditorPin)) { |
131 |
throw new IllegalArgumentException("Unsupported pin: "+wpin); |
132 |
} |
133 |
synchronized (valueProvidersLock) { |
134 |
if (!getValueProviders().containsKey(valueProviderId)) { |
135 |
valueProviders.put(valueProviderId, new DelegatingValueProvider(valueProviderId)); |
136 |
} |
137 |
} |
138 |
EditorPin pin = (EditorPin) wpin; |
139 |
pin.setVpId(valueProviderId); |
140 |
try { |
141 |
WatchAnnotationProvider.PIN_SUPPORT_ACCESS.pin(watch); |
142 |
} catch (DataObjectNotFoundException ex) { |
143 |
throw new IllegalArgumentException("Unable to find the pin's editor.", ex); |
144 |
} |
145 |
} |
146 |
|
147 |
private Map<String, DelegatingValueProvider> getValueProviders() { |
148 |
synchronized (valueProvidersLock) { |
149 |
if (valueProviders == null) { |
150 |
valueProviders = new HashMap<>(); |
151 |
refreshValueProviders(); |
152 |
} |
153 |
return valueProviders; |
154 |
} |
155 |
} |
156 |
|
157 |
private void refreshValueProviders() { |
158 |
DebuggerManager dm = DebuggerManager.getDebuggerManager (); |
159 |
DebuggerEngine e = dm.getCurrentEngine (); |
160 |
List<? extends ValueProvider> providers; |
161 |
if (e == null) { |
162 |
providers = dm.lookup (null, ValueProvider.class); |
163 |
} else { |
164 |
providers = DebuggerManager.join(e, dm).lookup (null, ValueProvider.class); |
165 |
} |
166 |
if (!providers.isEmpty()) { |
167 |
synchronized (valueProvidersLock) { |
168 |
if (valueProviders == null) { |
169 |
valueProviders = new HashMap<>(); |
170 |
} |
171 |
Set<String> existingProviderIds = new HashSet<>(); |
172 |
for (ValueProvider provider : providers) { |
173 |
String id = provider.getId(); |
174 |
existingProviderIds.add(id); |
175 |
DelegatingValueProvider dvp = valueProviders.get(id); |
176 |
if (dvp == null) { |
177 |
dvp = new DelegatingValueProvider(id); |
178 |
valueProviders.put(id, dvp); |
179 |
} |
180 |
dvp.setDelegate(provider); |
181 |
} |
182 |
Set<String> staleProviderIds = new HashSet<>(valueProviders.keySet()); |
183 |
staleProviderIds.removeAll(existingProviderIds); |
184 |
for (String staleId : staleProviderIds) { |
185 |
valueProviders.get(staleId).setDelegate(null); |
186 |
} |
187 |
} |
188 |
} |
189 |
} |
190 |
|
191 |
private static final class DelegatingValueProvider implements ValueProvider { |
192 |
|
193 |
private final String id; |
194 |
private volatile ValueProvider delegate; |
195 |
private final Map<Watch, ValueChangeListener> listeners = new HashMap<>(); |
196 |
|
197 |
DelegatingValueProvider(String id) { |
198 |
this.id = id; |
199 |
} |
200 |
|
201 |
@Override |
202 |
public String getId() { |
203 |
return id; |
204 |
} |
205 |
|
206 |
@Override |
207 |
public String getValue(Watch watch) { |
208 |
ValueProvider vp = delegate; |
209 |
if (vp != null) { |
210 |
return vp.getValue(watch); |
211 |
} else { |
212 |
return null; |
213 |
} |
214 |
} |
215 |
|
216 |
@Override |
217 |
public String getEvaluatingText() { |
218 |
ValueProvider vp = delegate; |
219 |
if (vp != null) { |
220 |
return vp.getEvaluatingText(); |
221 |
} else { |
222 |
return ValueProvider.super.getEvaluatingText(); |
223 |
} |
224 |
} |
225 |
|
226 |
@Override |
227 |
public String getEditableValue(Watch watch) { |
228 |
ValueProvider vp = delegate; |
229 |
if (vp != null) { |
230 |
return vp.getEditableValue(watch); |
231 |
} else { |
232 |
return ValueProvider.super.getEditableValue(watch); |
233 |
} |
234 |
} |
235 |
|
236 |
@Override |
237 |
public boolean setValue(Watch watch, String value) { |
238 |
ValueProvider vp = delegate; |
239 |
if (vp != null) { |
240 |
return vp.setValue(watch, value); |
241 |
} else { |
242 |
return ValueProvider.super.setValue(watch, value); |
243 |
} |
244 |
} |
245 |
|
246 |
@Override |
247 |
public synchronized void setChangeListener(Watch watch, ValueChangeListener chl) { |
248 |
ValueProvider vp = delegate; |
249 |
if (vp != null) { |
250 |
vp.setChangeListener(watch, chl); |
251 |
} |
252 |
listeners.put(watch, chl); |
253 |
} |
254 |
|
255 |
@Override |
256 |
public synchronized void unsetChangeListener(Watch watch) { |
257 |
ValueProvider vp = delegate; |
258 |
if (vp != null) { |
259 |
vp.unsetChangeListener(watch); |
260 |
} |
261 |
listeners.remove(watch); |
262 |
} |
263 |
|
264 |
synchronized void setDelegate(ValueProvider delegate) { |
265 |
this.delegate = delegate; |
266 |
if (delegate == null) { |
267 |
for (Map.Entry<Watch, ValueChangeListener> wvl : listeners.entrySet()) { |
268 |
wvl.getValue().valueChanged(wvl.getKey()); |
269 |
} |
270 |
} else { |
271 |
for (Map.Entry<Watch, ValueChangeListener> wvl : listeners.entrySet()) { |
272 |
delegate.setChangeListener(wvl.getKey(), wvl.getValue()); |
273 |
} |
274 |
} |
275 |
} |
276 |
|
277 |
} |
278 |
|
279 |
/** |
280 |
* Provider of pinned watch value. |
281 |
* Register an implementation of this class via {@link DebuggerServiceRegistration} |
282 |
* for the corresponding debugger session ID path. |
283 |
*/ |
284 |
public static interface ValueProvider { |
285 |
|
286 |
/** |
287 |
* Get a unique ID of this value provider. |
288 |
* Use this ID when pinning a watch via {@link #pin(org.netbeans.api.debugger.Watch, java.lang.String)}. |
289 |
* @return An ID of this value provider. |
290 |
*/ |
291 |
String getId(); |
292 |
|
293 |
/** |
294 |
* Get current value of pinned watch. This method must not block, |
295 |
* it's called synchronously in the EQ thread. This method should return |
296 |
* most recent value of the watch, or the same instance which |
297 |
* {@link #getEvaluatingText()} returns when the watch value is being computed, |
298 |
* or <code>null</code> when the watch can not be resolved. |
299 |
* @param watch the watch whose value is to be returned. |
300 |
* @return The current value of the watch, or {@link #getEvaluatingText()}, |
301 |
* or <code>null</code>. |
302 |
*/ |
303 |
String getValue(Watch watch); |
304 |
|
305 |
/** |
306 |
* Get a localized text to be displayed when the watch is being evaluated. |
307 |
* The pin watch UI highlights changed values. It uses this string to |
308 |
* distinguish real watch values. Return the same instance from |
309 |
* {@link #getValue(org.netbeans.api.debugger.Watch)} when the watch is |
310 |
* being computed. |
311 |
* @return A localized text displayed while the watch is evaluating. |
312 |
*/ |
313 |
@NbBundle.Messages("WATCH_EVALUATING=Evaluating...") |
314 |
default String getEvaluatingText() { |
315 |
return Bundle.WATCH_EVALUATING(); |
316 |
} |
317 |
|
318 |
/** |
319 |
* Determine whether the current watch value is editable and if yes, |
320 |
* return the value to edit. |
321 |
* The value returned by {@link #getValue(org.netbeans.api.debugger.Watch)} |
322 |
* might not be the proper value to edit. It may contain type description |
323 |
* or more descriptive information about the value. This method should |
324 |
* provide a raw textual representation of the value to edit, |
325 |
* or <code>null</code> when the value is not editable.<br> |
326 |
* The default implementation return <code>null</code>. |
327 |
* @param watch The watch to return the editable value for |
328 |
* @return The string representation of the editable value, |
329 |
* or <code>null</code> when the value is not editable. |
330 |
*/ |
331 |
default String getEditableValue(Watch watch) { |
332 |
return null; |
333 |
} |
334 |
|
335 |
/** |
336 |
* Set a watch value as a response to finished editing. |
337 |
* This method is called only when a prior call to |
338 |
* {@link #getEditableValue(org.netbeans.api.debugger.Watch)} returns |
339 |
* a non-null value.<br> |
340 |
* The default implementation throws {@link UnsupportedOperationException}. |
341 |
* @param watch The watch to set the value for. |
342 |
* @param value The new watch value. |
343 |
* @return <code>true</code> when the value was set successfully and |
344 |
* the UI should get updated from {@link #getValue(org.netbeans.api.debugger.Watch)}, |
345 |
* <code>false</code> when the set fails and the last watch value |
346 |
* should stay. The implementation is responsible for any error |
347 |
* reporting when the set fails. |
348 |
*/ |
349 |
default boolean setValue(Watch watch, String value) { |
350 |
throw new UnsupportedOperationException("Watch not editable."); |
351 |
} |
352 |
|
353 |
/** |
354 |
* Allows to set a value change listener for a specific watch. |
355 |
* @param watch The watch to listen for changes |
356 |
* @param chl The value change listener. |
357 |
*/ |
358 |
void setChangeListener(Watch watch, ValueChangeListener chl); |
359 |
|
360 |
/** |
361 |
* Unset a value change listener for a specific watch. |
362 |
* Use this method to unregister the listeners and free up associated resources. |
363 |
* @param watch The watch to unset the listener from. |
364 |
*/ |
365 |
void unsetChangeListener(Watch watch); |
366 |
|
367 |
/** |
368 |
* Listener for watch value changes. |
369 |
*/ |
370 |
public static interface ValueChangeListener { |
371 |
|
372 |
/** |
373 |
* Notify that a watch value has changed. |
374 |
* {@link ValueProvider#getValue(org.netbeans.api.debugger.Watch)} |
375 |
* then returns the new value. |
376 |
* @param watch The watch whose value has changed. |
377 |
*/ |
378 |
void valueChanged(Watch watch); |
379 |
} |
380 |
} |
381 |
} |