-
Notifications
You must be signed in to change notification settings - Fork 86
/
ExternalizeStringsAction.java
547 lines (485 loc) · 17.3 KB
/
ExternalizeStringsAction.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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
/*******************************************************************************
* Copyright (c) 2000, 2023 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
*******************************************************************************/
package org.eclipse.jdt.ui.actions;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.ITextSelection;
import org.eclipse.ui.IWorkbenchSite;
import org.eclipse.ui.IWorkingSet;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.dialogs.ListDialog;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.compiler.InvalidInputException;
import org.eclipse.jdt.internal.core.manipulation.util.BasicElementLabels;
import org.eclipse.jdt.internal.corext.refactoring.RefactoringAvailabilityTester;
import org.eclipse.jdt.internal.corext.refactoring.nls.NLSElement;
import org.eclipse.jdt.internal.corext.refactoring.nls.NLSLine;
import org.eclipse.jdt.internal.corext.refactoring.nls.NLSScanner;
import org.eclipse.jdt.internal.corext.refactoring.reorg.ReorgUtilsCore;
import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
import org.eclipse.jdt.internal.corext.util.Messages;
import org.eclipse.jdt.ui.JavaElementLabelProvider;
import org.eclipse.jdt.ui.JavaElementLabels;
import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
import org.eclipse.jdt.internal.ui.JavaPlugin;
import org.eclipse.jdt.internal.ui.actions.ActionMessages;
import org.eclipse.jdt.internal.ui.actions.ActionUtil;
import org.eclipse.jdt.internal.ui.actions.SelectionConverter;
import org.eclipse.jdt.internal.ui.javaeditor.CompilationUnitEditor;
import org.eclipse.jdt.internal.ui.refactoring.nls.ExternalizeWizard;
import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
import org.eclipse.jdt.internal.ui.util.Progress;
/**
* Externalizes the strings of a compilation unit or find all strings
* in a package or project that are not externalized yet. Opens a wizard that
* gathers additional information to externalize the strings.
* <p>
* The action is applicable to structured selections containing elements
* of type <code>ICompilationUnit</code>, <code>IType</code>, <code>IJavaProject</code>,
* <code>IPackageFragment</code>, and <code>IPackageFragmentRoot</code>
*
* <p>
* This class may be instantiated; it is not intended to be subclassed.
* </p>
*
* @since 2.0
*
* @noextend This class is not intended to be subclassed by clients.
*/
public class ExternalizeStringsAction extends SelectionDispatchAction {
private CompilationUnitEditor fEditor;
private NonNLSElement[] fElements;
/**
* Creates a new <code>ExternalizeStringsAction</code>. The action requires
* that the selection provided by the site's selection provider is of type <code>
* org.eclipse.jface.viewers.IStructuredSelection</code>.
*
* @param site the site providing context information for this action
*/
public ExternalizeStringsAction(IWorkbenchSite site) {
super(site);
setText(ActionMessages.ExternalizeStringsAction_label);
PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.EXTERNALIZE_STRINGS_ACTION);
}
/**
* Note: This constructor is for internal use only. Clients should not call this constructor.
* @param editor the compilation unit editor
*
* @noreference This constructor is not intended to be referenced by clients.
*/
public ExternalizeStringsAction(CompilationUnitEditor editor) {
this(editor.getEditorSite());
fEditor= editor;
setEnabled(fEditor != null && SelectionConverter.canOperateOn(fEditor));
}
@Override
public void selectionChanged(ITextSelection selection) {
}
@Override
public void selectionChanged(IStructuredSelection selection) {
try {
setEnabled(RefactoringAvailabilityTester.isExternalizeStringsAvailable(selection));
} catch (JavaModelException e) {
if (JavaModelUtil.isExceptionToBeLogged(e))
JavaPlugin.log(e);
setEnabled(false);//no UI - happens on selection changes
}
}
@Override
public void run(ITextSelection selection) {
IJavaElement element= SelectionConverter.getInput(fEditor);
if (!(element instanceof ICompilationUnit))
return;
run((ICompilationUnit)element);
}
@Override
public void run(IStructuredSelection selection) {
ICompilationUnit unit= getCompilationUnit(selection);
if (unit != null) {//run on cu
run(unit);
} else {
//run on multiple
try {
PlatformUI.getWorkbench().getProgressService().run(true, true, createRunnable(selection));
} catch(InvocationTargetException e) {
ExceptionHandler.handle(e, getShell(),
ActionMessages.ExternalizeStringsAction_dialog_title,
ActionMessages.FindStringsToExternalizeAction_error_message);
return;
} catch(InterruptedException e) {
// OK
return;
}
showResults();
}
}
/**
* Note: this method is for internal use only. Clients should not call this method.
* @param unit the compilation unit
*
* @noreference This method is not intended to be referenced by clients.
*/
public void run(ICompilationUnit unit) {
if (!ActionUtil.isEditable(fEditor, getShell(), unit))
return;
ExternalizeWizard.open(unit, getShell());
}
private static ICompilationUnit getCompilationUnit(IStructuredSelection selection) {
if (selection.isEmpty() || selection.size() != 1)
return null;
Object first= selection.getFirstElement();
if (first instanceof ICompilationUnit)
return (ICompilationUnit) first;
if (first instanceof IType)
return ((IType) first).getCompilationUnit();
return null;
}
private IRunnableWithProgress createRunnable(final IStructuredSelection selection) {
return pm -> {
try {
fElements= doRun(selection, pm);
} catch (CoreException e) {
throw new InvocationTargetException(e);
}
};
}
private NonNLSElement[] doRun(IStructuredSelection selection, IProgressMonitor pm) throws CoreException {
List<?> elements= getSelectedElementList(selection);
if (elements == null || elements.isEmpty())
return new NonNLSElement[0];
pm.beginTask(ActionMessages.FindStringsToExternalizeAction_find_strings, elements.size());
try{
List<NonNLSElement> result= new ArrayList<>();
for (Object obj : elements) {
result.addAll(analyze(obj, pm));
}
return result.toArray(new NonNLSElement[result.size()]);
} finally{
pm.done();
}
}
private List<NonNLSElement> analyze(Object obj, IProgressMonitor pm) throws CoreException, JavaModelException {
if (obj instanceof IJavaElement) {
IJavaElement element= (IJavaElement) obj;
int elementType= element.getElementType();
if (elementType == IJavaElement.PACKAGE_FRAGMENT) {
return analyze((IPackageFragment) element, Progress.subMonitor(pm, 1));
} else if (elementType == IJavaElement.PACKAGE_FRAGMENT_ROOT) {
IPackageFragmentRoot root= (IPackageFragmentRoot)element;
if (!root.isExternal() && !ReorgUtilsCore.isClassFolder(root)) {
return analyze((IPackageFragmentRoot) element, Progress.subMonitor(pm, 1));
} else {
pm.worked(1);
}
} else if (elementType == IJavaElement.JAVA_PROJECT) {
return analyze((IJavaProject) element, Progress.subMonitor(pm, 1));
} else if (elementType == IJavaElement.COMPILATION_UNIT) {
ICompilationUnit cu= (ICompilationUnit)element;
if (cu.exists()) {
NonNLSElement nlsElement= analyze(cu);
if (nlsElement != null) {
pm.worked(1);
ArrayList<NonNLSElement> result= new ArrayList<>();
result.add(nlsElement);
return result;
}
}
pm.worked(1);
} else if (elementType == IJavaElement.TYPE) {
IType type= (IType)element;
ICompilationUnit cu= type.getCompilationUnit();
if (cu != null && cu.exists()) {
NonNLSElement nlsElement= analyze(cu);
if (nlsElement != null) {
pm.worked(1);
ArrayList<NonNLSElement> result= new ArrayList<>();
result.add(nlsElement);
return result;
}
}
pm.worked(1);
} else {
pm.worked(1);
}
} else if (obj instanceof IWorkingSet) {
List<NonNLSElement> result= new ArrayList<>();
IWorkingSet workingSet= (IWorkingSet) obj;
for (IAdaptable element : workingSet.getElements()) {
result.addAll(analyze(element, new NullProgressMonitor()));
}
pm.worked(1);
return result;
} else {
pm.worked(1);
}
return Collections.emptyList();
}
private void showResults() {
if (noStrings())
MessageDialog.openInformation(getShell(), ActionMessages.ExternalizeStringsAction_dialog_title, ActionMessages.FindStringsToExternalizeAction_noStrings);
else
new NonNLSListDialog(getShell(), fElements, countStrings()).open();
}
private boolean noStrings() {
if (fElements != null) {
for (NonNLSElement fElement : fElements) {
if (fElement.count != 0) {
return false;
}
}
}
return true;
}
/*
* returns List of Strings
*/
private List<NonNLSElement> analyze(IPackageFragment pack, IProgressMonitor pm) throws CoreException {
try{
if (pack == null)
return new ArrayList<>(0);
ICompilationUnit[] cus= pack.getCompilationUnits();
pm.beginTask("", cus.length); //$NON-NLS-1$
pm.setTaskName(pack.getElementName());
List<NonNLSElement> l= new ArrayList<>(cus.length);
for (ICompilationUnit cu : cus) {
pm.subTask(BasicElementLabels.getFileName(cu));
NonNLSElement element= analyze(cu);
if (element != null)
l.add(element);
pm.worked(1);
if (pm.isCanceled())
throw new OperationCanceledException();
}
return l;
} finally {
pm.done();
}
}
/*
* returns List of Strings
*/
private List<NonNLSElement> analyze(IPackageFragmentRoot sourceFolder, IProgressMonitor pm) throws CoreException {
try{
IJavaElement[] children= sourceFolder.getChildren();
pm.beginTask("", children.length); //$NON-NLS-1$
pm.setTaskName(JavaElementLabels.getElementLabel(sourceFolder, JavaElementLabels.ALL_DEFAULT));
List<NonNLSElement> result= new ArrayList<>();
for (IJavaElement iJavaElement : children) {
if (iJavaElement.getElementType() == IJavaElement.PACKAGE_FRAGMENT){
IPackageFragment pack= (IPackageFragment)iJavaElement;
if (! pack.isReadOnly())
result.addAll(analyze(pack, Progress.subMonitor(pm, 1)));
else
pm.worked(1);
} else
pm.worked(1);
}
return result;
} finally{
pm.done();
}
}
/*
* returns List of Strings
*/
private List<NonNLSElement> analyze(IJavaProject project, IProgressMonitor pm) throws CoreException {
try{
IPackageFragment[] packs= project.getPackageFragments();
pm.beginTask("", packs.length); //$NON-NLS-1$
List<NonNLSElement> result= new ArrayList<>();
for (IPackageFragment pack : packs) {
if (!pack.isReadOnly()) {
result.addAll(analyze(pack, Progress.subMonitor(pm, 1)));
} else {
pm.worked(1);
}
}
return result;
} finally{
pm.done();
}
}
private int countStrings() {
int found= 0;
if (fElements != null) {
for (NonNLSElement fElement : fElements) {
found+= fElement.count;
}
}
return found;
}
private NonNLSElement analyze(ICompilationUnit cu) throws CoreException {
int count= countNonExternalizedStrings(cu);
if (count == 0)
return null;
else
return new NonNLSElement(cu, count);
}
private int countNonExternalizedStrings(ICompilationUnit cu) throws CoreException {
try{
int result= 0;
for (NLSLine line : NLSScanner.scan(cu)) {
result+= countNonExternalizedStrings(line);
}
return result;
} catch (InvalidInputException | BadLocationException e) {
throw new CoreException(new Status(IStatus.ERROR, JavaPlugin.getPluginId(), IStatus.ERROR, Messages.format(ActionMessages.FindStringsToExternalizeAction_error_cannotBeParsed,
BasicElementLabels.getFileName(cu)), e));
}
}
private int countNonExternalizedStrings(NLSLine line){
int result= 0;
for (NLSElement element : line.getElements()) {
if (!element.hasTag()) {
result++;
}
}
return result;
}
/**
* returns <code>List</code> of <code>IPackageFragments</code>, <code>IPackageFragmentRoots</code> or
* <code>IJavaProjects</code> (all entries are of the same kind)
* @param selection the selection
* @return the selected elements
*/
private static List<?> getSelectedElementList(IStructuredSelection selection) {
if (selection == null)
return null;
return selection.toList();
}
//-------private classes --------------
private static class NonNLSListDialog extends ListDialog {
private static final int OPEN_BUTTON_ID= IDialogConstants.CLIENT_ID + 1;
private Button fOpenButton;
NonNLSListDialog(Shell parent, NonNLSElement[] input, int count) {
super(parent);
setAddCancelButton(false);
setInput(Arrays.asList(input));
setTitle(ActionMessages.ExternalizeStringsAction_dialog_title);
String message= count == 1 ? ActionMessages.FindStringsToExternalizeAction_non_externalized_singular : Messages.format(
ActionMessages.FindStringsToExternalizeAction_non_externalized_plural, new Object[] { Integer.valueOf(count) });
setMessage(message);
setContentProvider(ArrayContentProvider.getInstance());
setLabelProvider(createLabelProvider());
}
@Override
protected Point getInitialSize() {
return getShell().computeSize(SWT.DEFAULT, SWT.DEFAULT, true);
}
@Override
protected Control createDialogArea(Composite parent) {
Composite result= (Composite)super.createDialogArea(parent);
getTableViewer().addSelectionChangedListener(event -> {
if (fOpenButton != null){
fOpenButton.setEnabled(! getTableViewer().getSelection().isEmpty());
}
});
getTableViewer().getTable().addSelectionListener(new SelectionAdapter(){
@Override
public void widgetDefaultSelected(SelectionEvent e) {
NonNLSElement element= (NonNLSElement)e.item.getData();
ExternalizeWizard.open(element.cu, getShell());
}
});
getTableViewer().getTable().setFocus();
applyDialogFont(result);
return result;
}
@Override
protected void createButtonsForButtonBar(Composite parent) {
fOpenButton= createButton(parent, OPEN_BUTTON_ID, ActionMessages.FindStringsToExternalizeAction_button_label, true);
fOpenButton.setEnabled(false);
//looks like a 'close' but it a 'cancel'
createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CLOSE_LABEL, false);
}
@Override
protected void buttonPressed(int buttonId) {
if (buttonId != OPEN_BUTTON_ID){
super.buttonPressed(buttonId);
return;
}
ISelection s= getTableViewer().getSelection();
if (s instanceof IStructuredSelection){
IStructuredSelection ss= (IStructuredSelection)s;
if (ss.getFirstElement() instanceof NonNLSElement)
ExternalizeWizard.open(((NonNLSElement) ss.getFirstElement()).cu, getShell());
}
}
private static LabelProvider createLabelProvider() {
return new JavaElementLabelProvider(JavaElementLabelProvider.SHOW_DEFAULT){
@Override
public String getText(Object element) {
NonNLSElement nlsel= (NonNLSElement)element;
String elementName= BasicElementLabels.getPathLabel(nlsel.cu.getResource().getFullPath(), false);
return Messages.format(
ActionMessages.FindStringsToExternalizeAction_foundStrings,
new Object[] {Integer.valueOf(nlsel.count), elementName} );
}
@Override
public Image getImage(Object element) {
return super.getImage(((NonNLSElement)element).cu);
}
};
}
/*
* @see org.eclipse.jface.window.Window#configureShell(Shell)
*/
@Override
protected void configureShell(Shell newShell) {
super.configureShell(newShell);
PlatformUI.getWorkbench().getHelpSystem().setHelp(newShell, IJavaHelpContextIds.NONNLS_DIALOG);
}
}
private static class NonNLSElement{
ICompilationUnit cu;
int count;
NonNLSElement(ICompilationUnit cu, int count){
this.cu= cu;
this.count= count;
}
}
}