|
| 1 | +/** |
| 2 | + * BSD-style license; for more info see http://pmd.sourceforge.net/license.html |
| 3 | + */ |
| 4 | + |
| 5 | +package net.sourceforge.pmd.eclipse.ui.properties; |
| 6 | + |
| 7 | +import java.net.MalformedURLException; |
| 8 | +import java.net.URL; |
| 9 | + |
| 10 | +import org.eclipse.core.resources.IMarker; |
| 11 | +import org.eclipse.core.runtime.CoreException; |
| 12 | +import org.eclipse.swt.SWT; |
| 13 | +import org.eclipse.swt.events.SelectionEvent; |
| 14 | +import org.eclipse.swt.events.SelectionListener; |
| 15 | +import org.eclipse.swt.layout.GridData; |
| 16 | +import org.eclipse.swt.layout.GridLayout; |
| 17 | +import org.eclipse.swt.widgets.Composite; |
| 18 | +import org.eclipse.swt.widgets.Control; |
| 19 | +import org.eclipse.swt.widgets.Label; |
| 20 | +import org.eclipse.swt.widgets.Link; |
| 21 | +import org.eclipse.swt.widgets.Text; |
| 22 | +import org.eclipse.ui.PartInitException; |
| 23 | +import org.eclipse.ui.PlatformUI; |
| 24 | +import org.eclipse.ui.browser.IWebBrowser; |
| 25 | +import org.eclipse.ui.dialogs.PropertyPage; |
| 26 | + |
| 27 | +import net.sourceforge.pmd.Rule; |
| 28 | +import net.sourceforge.pmd.eclipse.plugin.PMDPlugin; |
| 29 | +import net.sourceforge.pmd.eclipse.runtime.PMDRuntimeConstants; |
| 30 | +import net.sourceforge.pmd.eclipse.runtime.builder.MarkerUtil; |
| 31 | + |
| 32 | +public class PMDMarkerPropertyPage extends PropertyPage { |
| 33 | + |
| 34 | + @Override |
| 35 | + protected Control createContents(Composite parent) { |
| 36 | + noDefaultAndApplyButton(); |
| 37 | + |
| 38 | + Composite composite = new Composite(parent, SWT.NULL); |
| 39 | + composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); |
| 40 | + GridLayout layout = new GridLayout(); |
| 41 | + layout.numColumns = 2; |
| 42 | + composite.setLayout(layout); |
| 43 | + |
| 44 | + |
| 45 | + IMarker marker = (IMarker) getElement(); |
| 46 | + Rule rule = PMDPlugin.getDefault().getPreferencesManager().getRuleSet() |
| 47 | + .getRuleByName(MarkerUtil.ruleNameFor(marker)); |
| 48 | + |
| 49 | + try { |
| 50 | + addLabel(composite, "Rule:"); |
| 51 | + addText(composite, rule.getName()); |
| 52 | + |
| 53 | + addLabel(composite, "Category:"); |
| 54 | + addText(composite, rule.getRuleSetName()); |
| 55 | + |
| 56 | + addLabel(composite, "Priority:"); |
| 57 | + addText(composite, rule.getPriority().name()); |
| 58 | + |
| 59 | + addLabel(composite, "Message:"); |
| 60 | + addText(composite, getViolationMessage(marker)); |
| 61 | + |
| 62 | + addLabel(composite, "Description:", 2); |
| 63 | + addDescription(composite, rule); |
| 64 | + |
| 65 | + addLabel(composite, "External URL Info:"); |
| 66 | + addLink(composite, rule); |
| 67 | + } catch (CoreException e) { |
| 68 | + PMDPlugin.getDefault().logError(e.getMessage(), e); |
| 69 | + } |
| 70 | + |
| 71 | + return composite; |
| 72 | + } |
| 73 | + |
| 74 | + private String getViolationMessage(IMarker marker) throws CoreException { |
| 75 | + String defaultMessage = marker.getAttribute(IMarker.MESSAGE, ""); |
| 76 | + return marker.getAttribute(PMDRuntimeConstants.KEY_MARKERATT_MESSAGE, defaultMessage); |
| 77 | + } |
| 78 | + |
| 79 | + private void addDescription(Composite composite, Rule rule) { |
| 80 | + Text descriptionText = new Text(composite, SWT.MULTI | SWT.WRAP | SWT.READ_ONLY); |
| 81 | + GridData gridData = new GridData(GridData.FILL_BOTH); |
| 82 | + gridData.horizontalSpan = 2; |
| 83 | + gridData.heightHint = 50; |
| 84 | + descriptionText.setLayoutData(gridData); |
| 85 | + descriptionText.setText(rule.getDescription()); |
| 86 | + } |
| 87 | + |
| 88 | + private void addLink(Composite composite, Rule rule) { |
| 89 | + Link link = new Link(composite, SWT.NONE); |
| 90 | + link.setText("<a href=\"" + rule.getExternalInfoUrl() + "\">" + rule.getExternalInfoUrl() + "</a>"); |
| 91 | + link.addSelectionListener(new LinkClickListener()); |
| 92 | + } |
| 93 | + |
| 94 | + private void addText(Composite parent, String value) { |
| 95 | + Text text = new Text(parent, SWT.READ_ONLY | SWT.SINGLE); |
| 96 | + text.setBackground(parent.getBackground()); |
| 97 | + GridData gridData = new GridData(GridData.FILL_HORIZONTAL); |
| 98 | + text.setLayoutData(gridData); |
| 99 | + text.setText(value); |
| 100 | + } |
| 101 | + |
| 102 | + private void addLabel(Composite parent, String label) { |
| 103 | + addLabel(parent, label, 1); |
| 104 | + } |
| 105 | + |
| 106 | + private void addLabel(Composite parent, String text, int columnSpan) { |
| 107 | + Label label = new Label(parent, SWT.NONE); |
| 108 | + if (columnSpan > 1) { |
| 109 | + GridData gridData = new GridData(); |
| 110 | + gridData.horizontalSpan = columnSpan; |
| 111 | + label.setLayoutData(gridData); |
| 112 | + } |
| 113 | + label.setText(text); |
| 114 | + } |
| 115 | + |
| 116 | + private static final class LinkClickListener implements SelectionListener { |
| 117 | + @Override |
| 118 | + public void widgetSelected(SelectionEvent e) { |
| 119 | + try { |
| 120 | + URL url = new URL(e.text); |
| 121 | + IWebBrowser browser = PlatformUI.getWorkbench().getBrowserSupport().getExternalBrowser(); |
| 122 | + browser.openURL(url); |
| 123 | + } catch (MalformedURLException | PartInitException e1) { |
| 124 | + PMDPlugin.getDefault().logError(e1.getMessage(), e1); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + @Override |
| 129 | + public void widgetDefaultSelected(SelectionEvent e) { |
| 130 | + widgetSelected(e); |
| 131 | + } |
| 132 | + } |
| 133 | +} |
0 commit comments