Skip to content

Commit 4a2a844

Browse files
committed
Refactor actions to use correct selection
Previously selectionChanged was ignored and the selected elements where determined manually by getting the selection from the ui part the action is used for. However, this makes it impossible to programmatically call this action and provide a specific selection. This drastically simplifies the actions, since they mostly care about the selection and the selection is provided by eclipse automatically. Also fixes PMD issues and invalid charset handling while removing review comments.
1 parent a29549d commit 4a2a844

File tree

7 files changed

+90
-248
lines changed

7 files changed

+90
-248
lines changed

net.sourceforge.pmd.eclipse.plugin/src/main/java/net/sourceforge/pmd/eclipse/ui/actions/AbstractUIAction.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
*/
2222
public abstract class AbstractUIAction implements IObjectActionDelegate {
2323

24+
private ISelection selection;
2425
private IWorkbenchPart targetPart;
2526

2627
protected AbstractUIAction() {
@@ -59,7 +60,12 @@ protected IWorkbenchPartSite targetPartSite() {
5960
}
6061

6162
protected ISelection targetSelection() {
62-
return targetPartSite().getSelectionProvider().getSelection();
63+
return selection;
64+
}
65+
66+
@Override
67+
public void selectionChanged(IAction action, ISelection selection) {
68+
this.selection = selection;
6369
}
6470

6571
/**

net.sourceforge.pmd.eclipse.plugin/src/main/java/net/sourceforge/pmd/eclipse/ui/actions/CPDCheckProjectAction.java

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,10 @@ public class CPDCheckProjectAction extends AbstractUIAction {
4747
private static final String SIMPLE_KEY = "Simple Text";
4848
private static final String CSV_KEY = "CSV";
4949

50-
/*
51-
* @see org.eclipse.ui.IActionDelegate#run(IAction)
52-
*/
50+
@Override
5351
public void run(final IAction action) { // NOPMD:UnusedFormalParameter
5452
final IWorkbenchPartSite site = targetPartSite();
55-
final ISelection sel = site.getSelectionProvider().getSelection();
53+
final ISelection sel = targetSelection();
5654
final Shell shell = site.getShell();
5755
final String[] languages = LanguageFactory.supportedLanguages;
5856

@@ -64,7 +62,7 @@ public void run(final IAction action) { // NOPMD:UnusedFormalParameter
6462

6563
final CPDCheckDialog dialog = new CPDCheckDialog(shell, languages, formats);
6664

67-
if (dialog.open() == Dialog.OK && sel instanceof IStructuredSelection) {
65+
if (dialog.open() == Dialog.OK && sel instanceof IStructuredSelection) {
6866
final StructuredSelection ss = (StructuredSelection) sel;
6967
final Iterator<?> i = ss.iterator();
7068
while (i.hasNext()) {
@@ -83,17 +81,10 @@ public void run(final IAction action) { // NOPMD:UnusedFormalParameter
8381
LOG.warn("The selected object is not adaptable");
8482
LOG.debug(" -> selected object : " + obj);
8583
}
86-
}
84+
}
8785
}
8886
}
8987

90-
91-
/*
92-
* @see org.eclipse.ui.IActionDelegate#selectionChanged(IAction, ISelection)
93-
*/
94-
public void selectionChanged(final IAction action, final ISelection selection) { // NOPMD:UnusedFormalParameter
95-
}
96-
9788
/**
9889
* Run the DetectCutAndPaste command against the selected project
9990
* and creates optionally the report file.
@@ -173,5 +164,4 @@ private String createFileName(String rendererKey) {
173164
}
174165
return fileName;
175166
}
176-
177167
}

net.sourceforge.pmd.eclipse.plugin/src/main/java/net/sourceforge/pmd/eclipse/ui/actions/ClearReviewsAction.java

Lines changed: 41 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@
66

77
import java.io.BufferedReader;
88
import java.io.ByteArrayInputStream;
9-
import java.io.ByteArrayOutputStream;
109
import java.io.IOException;
1110
import java.io.InputStreamReader;
1211
import java.io.PrintWriter;
12+
import java.io.StringWriter;
1313
import java.lang.reflect.InvocationTargetException;
1414
import java.util.Iterator;
15+
import java.util.Locale;
1516

1617
import org.eclipse.core.resources.IFile;
1718
import org.eclipse.core.resources.IFolder;
@@ -28,9 +29,6 @@
2829
import org.eclipse.jface.viewers.ISelection;
2930
import org.eclipse.jface.viewers.IStructuredSelection;
3031
import org.eclipse.swt.widgets.Display;
31-
import org.eclipse.ui.IEditorInput;
32-
import org.eclipse.ui.IEditorPart;
33-
import org.eclipse.ui.IFileEditorInput;
3432
import org.eclipse.ui.IViewActionDelegate;
3533
import org.eclipse.ui.IViewPart;
3634
import org.slf4j.Logger;
@@ -39,7 +37,6 @@
3937
import net.sourceforge.pmd.eclipse.runtime.PMDRuntimeConstants;
4038
import net.sourceforge.pmd.eclipse.runtime.cmd.AbstractDefaultCommand;
4139
import net.sourceforge.pmd.eclipse.ui.nls.StringKeys;
42-
import net.sourceforge.pmd.eclipse.util.IOUtil;
4340

4441
/**
4542
* Implements the clear reviews action
@@ -52,21 +49,18 @@ public class ClearReviewsAction extends AbstractUIAction implements IResourceVis
5249
private static final Logger LOG = LoggerFactory.getLogger(ClearReviewsAction.class);
5350
private IProgressMonitor monitor;
5451

55-
/**
56-
* @see org.eclipse.ui.IViewActionDelegate#init(org.eclipse.ui.IViewPart)
57-
*/
52+
@Override
5853
public void init(IViewPart view) {
5954
setActivePart(null, view.getSite().getPage().getActivePart());
6055
}
6156

62-
/**
63-
* @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
64-
*/
57+
@Override
6558
public void run(IAction action) {
6659
LOG.info("Remove violation reviews requested.");
6760
ProgressMonitorDialog monitorDialog = new ProgressMonitorDialog(Display.getCurrent().getActiveShell());
6861
try {
6962
monitorDialog.run(false, false, new IRunnableWithProgress() {
63+
@Override
7064
public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
7165
setMonitor(monitor);
7266
clearReviews();
@@ -80,13 +74,6 @@ public void run(IProgressMonitor monitor) throws InvocationTargetException, Inte
8074
}
8175
}
8276

83-
/**
84-
* @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction,
85-
* org.eclipse.jface.viewers.ISelection)
86-
*/
87-
public void selectionChanged(IAction action, ISelection selection) {
88-
}
89-
9077
/**
9178
* Get the monitor
9279
*
@@ -131,56 +118,36 @@ protected void monitorSubTask(String message) {
131118
protected void clearReviews() {
132119

133120
try {
134-
// If action is started from a view, the process all selected
135-
// resource
136-
if (isViewPart()) {
137-
ISelection selection = targetSelection();
138-
139-
if (selection != null && selection instanceof IStructuredSelection) {
140-
IStructuredSelection structuredSelection = (IStructuredSelection) selection;
141-
if (getMonitor() != null) {
142-
getMonitor().beginTask(getString(StringKeys.MONITOR_REMOVE_REVIEWS), IProgressMonitor.UNKNOWN);
143-
144-
Iterator<?> i = structuredSelection.iterator();
145-
while (i.hasNext()) {
146-
Object object = i.next();
147-
IResource resource = null;
148-
149-
if (object instanceof IMarker) {
150-
resource = ((IMarker) object).getResource();
151-
} else if (object instanceof IAdaptable) {
152-
IAdaptable adaptable = (IAdaptable) object;
153-
resource = (IResource) adaptable.getAdapter(IResource.class);
154-
} else {
155-
LOG.warn("The selected object is not adaptable");
156-
LOG.debug(" -> selected object = " + object);
157-
}
158-
159-
if (resource != null) {
160-
resource.accept(this);
161-
} else {
162-
LOG.warn("The selected object cannot adapt to a resource.");
163-
LOG.debug(" -> selected object" + object);
164-
}
121+
ISelection selection = targetSelection();
122+
123+
if (selection instanceof IStructuredSelection) {
124+
IStructuredSelection structuredSelection = (IStructuredSelection) selection;
125+
if (getMonitor() != null) {
126+
getMonitor().beginTask(getString(StringKeys.MONITOR_REMOVE_REVIEWS), IProgressMonitor.UNKNOWN);
127+
128+
Iterator<?> i = structuredSelection.iterator();
129+
while (i.hasNext()) {
130+
Object object = i.next();
131+
IResource resource = null;
132+
133+
if (object instanceof IMarker) {
134+
resource = ((IMarker) object).getResource();
135+
} else if (object instanceof IAdaptable) {
136+
IAdaptable adaptable = (IAdaptable) object;
137+
resource = (IResource) adaptable.getAdapter(IResource.class);
138+
} else {
139+
LOG.warn("The selected object is not adaptable");
140+
LOG.debug(" -> selected object = " + object);
165141
}
166-
}
167-
}
168-
}
169142

170-
// If action is started from an editor, process the file currently
171-
// edited
172-
if (isEditorPart()) {
173-
IEditorInput editorInput = ((IEditorPart) this.targetPart()).getEditorInput();
174-
if (editorInput instanceof IFileEditorInput) {
175-
((IFileEditorInput) editorInput).getFile().accept(this);
176-
} else {
177-
LOG.debug("The kind of editor input is not supported. The editor input if of type: "
178-
+ editorInput.getClass().getName());
143+
if (resource != null) {
144+
resource.accept(this);
145+
} else {
146+
LOG.warn("The selected object cannot adapt to a resource.");
147+
LOG.debug(" -> selected object" + object);
148+
}
149+
}
179150
}
180-
} else {
181-
// else this is not supported
182-
LOG.debug("This action is not supported on this kind of part. This part type is: "
183-
+ targetPartClassName());
184151
}
185152
} catch (CoreException e) {
186153
logError("Core Exception when clearing violations reviews", e);
@@ -208,7 +175,7 @@ private static boolean isReviewable(IFile file) {
208175
if (AbstractDefaultCommand.isJavaFile(file)) {
209176
return true;
210177
}
211-
return file.getName().toLowerCase().endsWith(".jsp");
178+
return file.getName().toLowerCase(Locale.ROOT).endsWith(".jsp");
212179
}
213180

214181
/**
@@ -223,13 +190,11 @@ private String removeReviews(IFile file) {
223190
return null;
224191
}
225192

226-
ByteArrayOutputStream baos = new ByteArrayOutputStream();
227-
PrintWriter out = null;
193+
StringWriter modified = new StringWriter();
228194
boolean noChange = true;
229-
try {
195+
try (BufferedReader reader = new BufferedReader(new InputStreamReader(file.getContents(), file.getCharset()));
196+
PrintWriter out = new PrintWriter(modified)) {
230197
boolean comment = false;
231-
BufferedReader reader = new BufferedReader(new InputStreamReader(file.getContents()));
232-
out = new PrintWriter(baos);
233198

234199
while (reader.ready()) {
235200
String origLine = reader.readLine();
@@ -265,12 +230,9 @@ private String removeReviews(IFile file) {
265230
logError(StringKeys.ERROR_CORE_EXCEPTION, e);
266231
} catch (IOException e) {
267232
logError(StringKeys.ERROR_IO_EXCEPTION, e);
268-
} finally {
269-
IOUtil.closeQuietly(baos);
270-
IOUtil.closeQuietly(out);
271233
}
272234

273-
return noChange ? null : baos.toString();
235+
return noChange ? null : modified.toString();
274236
}
275237

276238
/**
@@ -281,15 +243,15 @@ private String removeReviews(IFile file) {
281243
*/
282244
private void saveNewContent(IFile file, String newContent) {
283245
try {
284-
file.setContents(new ByteArrayInputStream(newContent.getBytes()), false, true, getMonitor());
246+
file.setContents(new ByteArrayInputStream(newContent.getBytes(file.getCharset())), false, true, getMonitor());
285247
} catch (CoreException e) {
286248
logError(StringKeys.ERROR_CORE_EXCEPTION, e);
249+
} catch (IOException e) {
250+
logError(StringKeys.ERROR_IO_EXCEPTION, e);
287251
}
288252
}
289253

290-
/**
291-
* @see org.eclipse.core.resources.IResourceVisitor#visit(org.eclipse.core.resources.IResource)
292-
*/
254+
@Override
293255
public boolean visit(IResource resource) throws CoreException {
294256
if (resource instanceof IFile) {
295257
clearReviews((IFile) resource);

net.sourceforge.pmd.eclipse.plugin/src/main/java/net/sourceforge/pmd/eclipse/ui/actions/GenerateReportAction.java

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import java.util.List;
88

9+
import org.apache.commons.lang3.StringUtils;
910
import org.eclipse.core.resources.IProject;
1011
import org.eclipse.core.resources.IResource;
1112
import org.eclipse.core.runtime.IAdaptable;
@@ -20,7 +21,6 @@
2021
import net.sourceforge.pmd.eclipse.ui.nls.StringKeys;
2122
import net.sourceforge.pmd.eclipse.ui.reports.ReportManager;
2223
import net.sourceforge.pmd.renderers.Renderer;
23-
import net.sourceforge.pmd.util.StringUtil;
2424

2525
/**
2626
* Process GenerateReport action menu. Generate a HTML report on the current
@@ -57,7 +57,7 @@ private boolean checkRenderers() {
5757

5858
for (Renderer renderer : renderers) {
5959
String issue = renderer.dysfunctionReason();
60-
if (StringUtil.isNotEmpty(issue)) {
60+
if (StringUtils.isNotBlank(issue)) {
6161
errors.append(renderer.getName()).append(": ");
6262
errors.append(issue).append("\n");
6363
}
@@ -71,9 +71,7 @@ private boolean checkRenderers() {
7171
return false;
7272
}
7373

74-
/**
75-
* @see org.eclipse.ui.IActionDelegate#run(IAction)
76-
*/
74+
@Override
7775
public final void run(final IAction action) {
7876
LOG.info("Generation Report action requested");
7977
final ISelection sel = targetSelection();
@@ -98,23 +96,15 @@ public final void run(final IAction action) {
9896
}
9997
}
10098

101-
/**
102-
* @see org.eclipse.ui.IActionDelegate#selectionChanged(IAction, ISelection)
103-
*/
104-
public final void selectionChanged(IAction action, ISelection selection) {
105-
// nothing to do
106-
}
107-
10899
/**
109100
* Get a project from a selection
110101
*
111102
* @param selection
112103
* @return
113104
*/
114105
private static IProject getProject(IStructuredSelection selection) {
115-
116106
Object object = selection.getFirstElement();
117-
if (object != null && object instanceof IAdaptable) {
107+
if (object instanceof IAdaptable) {
118108
final IResource resource = (IResource) ((IAdaptable) object).getAdapter(IResource.class);
119109
if (resource != null) {
120110
return resource.getProject();

0 commit comments

Comments
 (0)