-
Notifications
You must be signed in to change notification settings - Fork 86
/
PropertyKeyHyperlink.java
530 lines (455 loc) · 17.7 KB
/
PropertyKeyHyperlink.java
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
/*******************************************************************************
* Copyright (c) 2000, 2016 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* IBM Corporation - initial API and implementation
* Terry Parker <tparker@google.com> (Google Inc.) - Bug 458852 - Speed up JDT text searches by supporting parallelism in its TextSearchRequestors
*******************************************************************************/
package org.eclipse.jdt.internal.ui.propertiesfileeditor;
import java.lang.reflect.InvocationTargetException;
import java.text.Collator;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Pattern;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.PlatformObject;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IStorage;
import org.eclipse.core.filebuffers.FileBuffers;
import org.eclipse.core.filebuffers.ITextFileBuffer;
import org.eclipse.core.filebuffers.ITextFileBufferManager;
import org.eclipse.core.filebuffers.LocationKind;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.window.Window;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.hyperlink.IHyperlink;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IFileEditorInput;
import org.eclipse.ui.IStorageEditorInput;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.dialogs.TwoPaneElementSelector;
import org.eclipse.ui.model.IWorkbenchAdapter;
import org.eclipse.ui.model.WorkbenchLabelProvider;
import org.eclipse.ui.texteditor.IEditorStatusLine;
import org.eclipse.ui.texteditor.ITextEditor;
import org.eclipse.search.core.text.TextSearchEngine;
import org.eclipse.search.core.text.TextSearchMatchAccess;
import org.eclipse.search.core.text.TextSearchRequestor;
import org.eclipse.search.core.text.TextSearchScope;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.search.IJavaSearchConstants;
import org.eclipse.jdt.core.search.SearchEngine;
import org.eclipse.jdt.core.search.SearchMatch;
import org.eclipse.jdt.core.search.SearchPattern;
import org.eclipse.jdt.core.search.SearchRequestor;
import org.eclipse.jdt.internal.core.manipulation.util.BasicElementLabels;
import org.eclipse.jdt.internal.corext.util.Messages;
import org.eclipse.jdt.internal.corext.util.SearchUtils;
import org.eclipse.jdt.ui.JavaElementLabels;
import org.eclipse.jdt.ui.JavaUI;
import org.eclipse.jdt.internal.ui.JavaPlugin;
import org.eclipse.jdt.internal.ui.javaeditor.EditorUtility;
import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
import org.eclipse.jdt.internal.ui.util.PatternConstructor;
import org.eclipse.jdt.internal.ui.util.Progress;
import org.eclipse.jdt.internal.ui.viewsupport.JavaElementImageProvider;
/**
* Properties key hyperlink.
*
* @since 3.1
*/
public class PropertyKeyHyperlink implements IHyperlink {
/**
* Note: this class KeyReference has a natural ordering that is inconsistent with equals
*/
private static class KeyReference extends PlatformObject implements IWorkbenchAdapter, Comparable<KeyReference> {
private static final Collator fgCollator= Collator.getInstance();
private IResource resource;
private IJavaElement element;
private int offset;
private int length;
private final boolean showLineNumber;
private KeyReference(IResource resource, IJavaElement element, int offset, int length, boolean showLineNumber) {
Assert.isNotNull(resource);
this.resource= resource;
this.offset= offset;
this.length= length;
this.element= element;
this.showLineNumber= showLineNumber;
}
@SuppressWarnings("unchecked")
@Override
public <T> T getAdapter(Class<T> adapter) {
if (adapter == IWorkbenchAdapter.class)
return (T) this;
else
return super.getAdapter(adapter);
}
/*
* @see org.eclipse.ui.model.IWorkbenchAdapter#getChildren(java.lang.Object)
*/
@Override
public Object[] getChildren(Object o) {
return null;
}
/*
* @see org.eclipse.ui.model.IWorkbenchAdapter#getImageDescriptor(java.lang.Object)
*/
@Override
public ImageDescriptor getImageDescriptor(Object object) {
IWorkbenchAdapter wbAdapter= resource.getAdapter(IWorkbenchAdapter.class);
if (wbAdapter != null)
return wbAdapter.getImageDescriptor(resource);
return null;
}
/*
* @see org.eclipse.ui.model.IWorkbenchAdapter#getLabel(java.lang.Object)
*/
@Override
public String getLabel(Object o) {
ITextFileBufferManager manager= FileBuffers.getTextFileBufferManager();
try {
manager.connect(resource.getFullPath(), LocationKind.NORMALIZE, null);
try {
ITextFileBuffer buffer= manager.getTextFileBuffer(resource.getFullPath(), LocationKind.NORMALIZE);
IDocument document= buffer.getDocument();
if (document != null) {
int line= document.getLineOfOffset(offset) + 1;
String pathLabel= BasicElementLabels.getPathLabel(resource.getFullPath(), false);
Object[] args= new Object[] { Integer.valueOf(line), pathLabel };
return showLineNumber ? Messages.format(PropertiesFileEditorMessages.OpenAction_SelectionDialog_elementLabel, args) : pathLabel;
}
} finally {
manager.disconnect(resource.getFullPath(), LocationKind.NORMALIZE, null);
}
} catch (CoreException e) {
JavaPlugin.log(e.getStatus());
} catch (BadLocationException e) {
JavaPlugin.log(e);
}
return BasicElementLabels.getPathLabel(resource.getFullPath(), false);
}
/*
* @see org.eclipse.ui.model.IWorkbenchAdapter#getParent(java.lang.Object)
*/
@Override
public Object getParent(Object o) {
return null;
}
@Override
public int compareTo(KeyReference otherRef) {
String thisPath= resource.getFullPath().toString();
String otherPath= otherRef.resource.getFullPath().toString();
int result= fgCollator.compare(thisPath, otherPath);
if (result != 0)
return result;
else
return offset - otherRef.offset;
}
}
private static class ResultCollector extends TextSearchRequestor {
private final List<KeyReference> fResult;
private final boolean fIsKeyDoubleQuoted;
public ResultCollector(List<KeyReference> result, boolean isKeyDoubleQuoted) {
fResult= result;
fIsKeyDoubleQuoted= isKeyDoubleQuoted;
}
@Override
public boolean canRunInParallel() {
return true;
}
@Override
public boolean acceptPatternMatch(TextSearchMatchAccess matchAccess) throws CoreException {
int start= matchAccess.getMatchOffset();
int length= matchAccess.getMatchLength();
if (fIsKeyDoubleQuoted) {
start= start + 1;
length= length - 2;
}
synchronized(fResult) {
fResult.add(new KeyReference(matchAccess.getFile(), null, start, length, true));
}
return true;
}
}
private IRegion fRegion;
private String fPropertiesKey;
private Shell fShell;
private IStorage fStorage;
private ITextEditor fEditor;
private boolean fIsFileEditorInput;
/**
* Creates a new properties key hyperlink.
*
* @param region the region
* @param key the properties key
* @param editor the text editor
*/
public PropertyKeyHyperlink(IRegion region, String key, ITextEditor editor) {
Assert.isNotNull(region);
Assert.isNotNull(key);
Assert.isNotNull(editor);
fRegion= region;
fPropertiesKey= key;
fEditor= editor;
fIsFileEditorInput= fEditor.getEditorInput() instanceof IFileEditorInput;
IStorageEditorInput storageEditorInput= (IStorageEditorInput)fEditor.getEditorInput();
fShell= fEditor.getEditorSite().getShell();
try {
fStorage= storageEditorInput.getStorage();
} catch (CoreException e) {
fStorage= null;
}
}
/*
* @see org.eclipse.jdt.internal.ui.javaeditor.IHyperlink#getHyperlinkRegion()
*/
@Override
public IRegion getHyperlinkRegion() {
return fRegion;
}
/*
* @see org.eclipse.jdt.internal.ui.javaeditor.IHyperlink#open()
*/
@Override
public void open() {
// Search the key
KeyReference[] references= search(fPropertiesKey);
if (references == null)
return; // canceled by the user
if (references.length == 0) {
String message= PropertiesFileEditorMessages.OpenAction_error_messageNoResult;
showErrorInStatusLine(message);
return;
}
open(references);
}
private void open(KeyReference[] keyReferences) {
Assert.isLegal(keyReferences != null && keyReferences.length > 0);
if (keyReferences.length == 1)
open(keyReferences[0]);
else
open(select(keyReferences));
}
/**
* Opens a dialog which allows to select a key reference.
*
* @param keyReferences the array of key references
* @return the selected key reference or <code>null</code> if canceled by the user
*/
private KeyReference select(final KeyReference[] keyReferences) {
Arrays.sort(keyReferences);
final int length= keyReferences.length;
ILabelProvider labelProvider= new WorkbenchLabelProvider() {
@Override
public String decorateText(String input, Object element) {
KeyReference keyRef= (KeyReference)element;
IJavaElement javaElement= keyRef.element;
IResource resource= keyRef.resource;
String name= javaElement != null ? JavaElementLabels.getElementLabel(javaElement, JavaElementLabels.ALL_DEFAULT | JavaElementLabels.ALL_FULLY_QUALIFIED
| JavaElementLabels.USE_RESOLVED | JavaElementLabels.P_COMPRESSED) : resource.getName();
if (name == null)
return input;
int count= 0;
for (int i= 0; i < length; i++) {
if (javaElement != null) {
if (keyReferences[i].element.equals(javaElement))
count++;
} else {
if (keyReferences[i].resource.equals(resource))
count++;
}
}
if (count > 1) {
Object[] args= new Object[] { BasicElementLabels.getResourceName(name), Integer.valueOf(count) };
name= Messages.format(PropertiesFileEditorMessages.OpenAction_SelectionDialog_elementLabelWithMatchCount, args);
}
return name;
}
@Override
protected ImageDescriptor decorateImage(ImageDescriptor input, Object element) {
KeyReference keyRef= (KeyReference)element;
IJavaElement javaElement= keyRef.element;
return javaElement != null ? new JavaElementImageProvider().getJavaImageDescriptor(javaElement, JavaElementImageProvider.SMALL_ICONS) : super.decorateImage(input, element);
}
};
TwoPaneElementSelector dialog= new TwoPaneElementSelector(fShell, labelProvider, new WorkbenchLabelProvider());
dialog.setLowerListLabel(PropertiesFileEditorMessages.OpenAction_SelectionDialog_details);
dialog.setLowerListComparator((o1, o2) -> 0);
dialog.setTitle(PropertiesFileEditorMessages.OpenAction_SelectionDialog_title);
dialog.setMessage(PropertiesFileEditorMessages.OpenAction_SelectionDialog_message);
dialog.setElements(keyReferences);
if (dialog.open() == Window.OK) {
Object[] result= dialog.getResult();
if (result != null && result.length == 1)
return (KeyReference)result[0];
}
return null;
}
private void open(KeyReference keyReference) {
if (keyReference == null)
return;
try {
IEditorPart part= keyReference.element != null ? EditorUtility.openInEditor(keyReference.element, true) : EditorUtility.openInEditor(keyReference.resource, true);
if (part != null)
EditorUtility.revealInEditor(part, keyReference.offset, keyReference.length);
} catch (PartInitException x) {
String message= null;
IWorkbenchAdapter wbAdapter= ((IAdaptable)keyReference).getAdapter(IWorkbenchAdapter.class);
if (wbAdapter != null)
message= Messages.format(PropertiesFileEditorMessages.OpenAction_error_messageArgs,
new String[] { wbAdapter.getLabel(keyReference), x.getLocalizedMessage() } );
if (message == null)
message= Messages.format(PropertiesFileEditorMessages.OpenAction_error_message, x.getLocalizedMessage());
MessageDialog.openError(fShell,
PropertiesFileEditorMessages.OpenAction_error_messageProblems,
message);
}
}
private String getErrorDialogTitle() {
return PropertiesFileEditorMessages.OpenAction_error_title;
}
private void showError(CoreException e) {
ExceptionHandler.handle(e, fShell, getErrorDialogTitle(), PropertiesFileEditorMessages.OpenAction_error_message);
}
private void showErrorInStatusLine(final String message) {
fShell.getDisplay().beep();
final IEditorStatusLine statusLine= fEditor.getAdapter(IEditorStatusLine.class);
if (statusLine != null) {
fShell.getDisplay().asyncExec(() -> statusLine.setMessage(true, message, null));
}
}
/**
* Returns whether we search the key in double-quotes or not.
* <p>
* XXX: This is a hack to improve the accuracy of matches, see https://bugs.eclipse.org/bugs/show_bug.cgi?id=81140
* </p>
*
* @return <code>true</code> if we search for double-quoted key
*/
private boolean useDoubleQuotedKey() {
if (fStorage == null)
return false;
String name= fStorage.getName();
return name != null && !"about.properties".equals(name) && !"feature.properties".equals(name) && !"plugin.properties".equals(name); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
}
/**
* Searches references to the given key in the given scope.
*
* @param key the properties key
* @return the references or <code>null</code> if the search has been canceled by the user
*/
private KeyReference[] search(final String key) {
if (key == null)
return new KeyReference[0];
final List<KeyReference> result= new ArrayList<>(5);
try {
fEditor.getEditorSite().getWorkbenchWindow().getWorkbench().getProgressService().busyCursorWhile(
monitor -> {
if (monitor == null)
monitor= new NullProgressMonitor();
monitor.beginTask("", 5); //$NON-NLS-1$
try {
// XXX: This is a hack to improve the accuracy of matches, see https://bugs.eclipse.org/bugs/show_bug.cgi?id=81140
boolean useDoubleQuotedKey= useDoubleQuotedKey();
if (useDoubleQuotedKey) {
SearchPattern pattern= SearchPattern.createPattern(key, IJavaSearchConstants.FIELD, IJavaSearchConstants.REFERENCES, SearchPattern.R_PATTERN_MATCH
| SearchPattern.R_CASE_SENSITIVE);
if (pattern == null)
return;
try {
new SearchEngine().search(pattern, SearchUtils.getDefaultSearchParticipants(), SearchEngine.createWorkspaceScope(), new SearchRequestor() {
@Override
public void acceptSearchMatch(SearchMatch match) throws CoreException {
IResource resource= match.getResource();
if (resource != null)
result.add(new KeyReference(resource, (IJavaElement) match.getElement(), match.getOffset(), match.getLength(), fIsFileEditorInput));
}
}, Progress.subMonitor(monitor, 1));
} catch (CoreException e) {
throw new InvocationTargetException(e);
}
}
if (result.isEmpty()) {
//maybe not an eclipse style NLS string
String searchString;
if (useDoubleQuotedKey) {
StringBuilder buf= new StringBuilder("\""); //$NON-NLS-1$
buf.append(key);
buf.append('"');
searchString= buf.toString();
} else
searchString= key;
ResultCollector collector= new ResultCollector(result, useDoubleQuotedKey);
TextSearchEngine engine= TextSearchEngine.create();
Pattern searchPattern= PatternConstructor.createPattern(searchString, true, false);
/* <p>
* XXX: This does not work for properties files coming from a JAR.
* For details see https://bugs.eclipse.org/bugs/show_bug.cgi?id=23341
* </p>
*/
if (fStorage instanceof IResource) {
engine.search(createScope(((IResource)fStorage).getProject()), collector, searchPattern, Progress.subMonitor(monitor, 4));
}
} else {
monitor.worked(1);
}
} finally {
monitor.done();
}
}
);
} catch (InvocationTargetException ex) {
String message= PropertiesFileEditorMessages.OpenAction_error_messageErrorSearchingKey;
showError(new CoreException(new Status(IStatus.ERROR, JavaUI.ID_PLUGIN, IStatus.OK, message, ex.getTargetException())));
} catch (InterruptedException ex) {
return null; // canceled
}
return result.toArray(new KeyReference[result.size()]);
}
private static TextSearchScope createScope(IResource scope) {
ArrayList<String> fileNamePatternStrings= new ArrayList<>();
// XXX: Should be configurable via preference, see https://bugs.eclipse.org/bugs/show_bug.cgi?id=81117
for (String javaExtension : JavaCore.getJavaLikeExtensions()) {
fileNamePatternStrings.add("*." + javaExtension); //$NON-NLS-1$
}
fileNamePatternStrings.add("*.xml"); //$NON-NLS-1$
fileNamePatternStrings.add("*.ini"); //$NON-NLS-1$
String[] allPatternStrings= fileNamePatternStrings.toArray(new String[fileNamePatternStrings.size()]);
Pattern fileNamePattern= PatternConstructor.createPattern(allPatternStrings, false, false);
return TextSearchScope.newSearchScope(new IResource[] { scope }, fileNamePattern, false);
}
/*
* @see org.eclipse.jdt.internal.ui.javaeditor.IHyperlink#getTypeLabel()
*/
@Override
public String getTypeLabel() {
return null;
}
/*
* @see org.eclipse.jdt.internal.ui.javaeditor.IHyperlink#getHyperlinkText()
*/
@Override
public String getHyperlinkText() {
return Messages.format(PropertiesFileEditorMessages.OpenAction_hyperlinkText, fPropertiesKey);
}
}