Skip to content

Commit 4ba26c1

Browse files
Phillip KrallPhillip Krall
authored andcommitted
Refresh views when you update a marker
1 parent 27f36f2 commit 4ba26c1

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

net.sourceforge.pmd.eclipse.plugin/src/main/java/net/sourceforge/pmd/eclipse/plugin/PMDPlugin.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,10 @@
4545
import org.eclipse.ui.IDecoratorManager;
4646
import org.eclipse.ui.IEditorPart;
4747
import org.eclipse.ui.IEditorReference;
48+
import org.eclipse.ui.IViewPart;
49+
import org.eclipse.ui.IViewReference;
4850
import org.eclipse.ui.IWorkbenchPart;
51+
import org.eclipse.ui.PartInitException;
4952
import org.eclipse.ui.PlatformUI;
5053
import org.eclipse.ui.plugin.AbstractUIPlugin;
5154
import org.osgi.framework.Bundle;
@@ -92,6 +95,8 @@ public class PMDPlugin extends AbstractUIPlugin {
9295
private Map<RGB, Color> coloursByRGB = new HashMap<RGB, Color>();
9396

9497
public static final String PLUGIN_ID = "net.sourceforge.pmd.eclipse.plugin";
98+
public static final String VIOLATIONS_OVERVIEW_ID = "net.sourceforge.pmd.eclipse.ui.views.violationOverview";
99+
public static final String VIOLATIONS_OUTLINE_ID = "net.sourceforge.pmd.eclipse.ui.views.violationOutline";
95100

96101
private static Map<IProject, IJavaProject> javaProjectsByIProject = new HashMap<IProject, IJavaProject>();
97102

@@ -281,6 +286,55 @@ public void fileChangeListenerEnabled(boolean flag) {
281286
}
282287
}
283288
}
289+
290+
/**
291+
* Get a view from the view id.
292+
* @param id id of the view
293+
* @return view
294+
*/
295+
public static IViewPart getView(String id) {
296+
IViewReference[] viewReferences = PlatformUI.getWorkbench()
297+
.getActiveWorkbenchWindow().getActivePage().getViewReferences();
298+
for (int i = 0; i < viewReferences.length; i++) {
299+
if (id.equals(viewReferences[i].getId())) {
300+
return viewReferences[i].getView(false);
301+
}
302+
}
303+
return null;
304+
}
305+
306+
/**
307+
* refresh a view to the id passed in.
308+
*
309+
* @param viewId id of the view
310+
*/
311+
public void refreshView(final String viewId) {
312+
Display.getDefault().asyncExec(new Runnable() {
313+
@Override
314+
public void run() {
315+
try {
316+
IViewPart view = getView(viewId);
317+
if (view == null) {
318+
return;
319+
}
320+
boolean found = false;
321+
IViewPart[] views = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getViews();
322+
for (IViewPart activeView: views) {
323+
if (activeView.getTitle().equals(view.getTitle())) {
324+
found = true;
325+
}
326+
}
327+
if (!found) {
328+
return;
329+
}
330+
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().hideView(view);
331+
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().showView(viewId);
332+
} catch (PartInitException e) {
333+
LOG.error(e);
334+
}
335+
}
336+
});
337+
}
284338

285339
/*
286340
* (non-Javadoc)

net.sourceforge.pmd.eclipse.plugin/src/main/java/net/sourceforge/pmd/eclipse/ui/preferences/GeneralPreferencesPage.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import org.eclipse.swt.widgets.Spinner;
4343
import org.eclipse.swt.widgets.Table;
4444
import org.eclipse.swt.widgets.Text;
45+
import org.eclipse.ui.IPageLayout;
4546
import org.eclipse.ui.IWorkbench;
4647
import org.eclipse.ui.IWorkbenchPreferencePage;
4748
import org.eclipse.ui.dialogs.PreferencesUtil;
@@ -782,6 +783,13 @@ private void updateMarkerIcons() {
782783
RootRecord root = new RootRecord(ResourcesPlugin.getWorkspace().getRoot());
783784
Set<IFile> files = MarkerUtil.allMarkedFiles(root);
784785
PMDPlugin.getDefault().changedFiles(files);
786+
787+
/* Refresh the views to pick up the marker change */
788+
PMDPlugin.getDefault().refreshView(PMDPlugin.VIOLATIONS_OVERVIEW_ID);
789+
PMDPlugin.getDefault().refreshView(PMDPlugin.VIOLATIONS_OUTLINE_ID);
790+
PMDPlugin.getDefault().refreshView(IPageLayout.ID_PROJECT_EXPLORER);
791+
PMDPlugin.getDefault().refreshView(IPageLayout.ID_OUTLINE);
792+
785793
}
786794

787795
public boolean performCancel() {

0 commit comments

Comments
 (0)