|
| 1 | +/* |
| 2 | + * BSD-style license; for more info see http://pmd.sourceforge.net/license.html |
| 3 | + */ |
| 4 | + |
| 5 | +package net.sourceforge.pmd.eclipse.ui.dialogs; |
| 6 | + |
| 7 | +import java.net.MalformedURLException; |
| 8 | +import java.net.URL; |
| 9 | + |
| 10 | +import org.eclipse.core.resources.IMarker; |
| 11 | +import org.eclipse.jface.dialogs.DialogPage; |
| 12 | +import org.eclipse.jface.resource.JFaceResources; |
| 13 | +import org.eclipse.jface.widgets.WidgetFactory; |
| 14 | +import org.eclipse.swt.SWT; |
| 15 | +import org.eclipse.swt.events.SelectionEvent; |
| 16 | +import org.eclipse.swt.events.SelectionListener; |
| 17 | +import org.eclipse.swt.graphics.Font; |
| 18 | +import org.eclipse.swt.layout.GridData; |
| 19 | +import org.eclipse.swt.layout.GridLayout; |
| 20 | +import org.eclipse.swt.widgets.Composite; |
| 21 | +import org.eclipse.swt.widgets.Label; |
| 22 | +import org.eclipse.swt.widgets.Link; |
| 23 | +import org.eclipse.swt.widgets.Text; |
| 24 | +import org.eclipse.ui.PartInitException; |
| 25 | +import org.eclipse.ui.PlatformUI; |
| 26 | +import org.eclipse.ui.browser.IWebBrowser; |
| 27 | + |
| 28 | +import net.sourceforge.pmd.Rule; |
| 29 | +import net.sourceforge.pmd.eclipse.plugin.PMDPlugin; |
| 30 | +import net.sourceforge.pmd.eclipse.runtime.PMDRuntimeConstants; |
| 31 | +import net.sourceforge.pmd.eclipse.ui.nls.StringKeys; |
| 32 | +import net.sourceforge.pmd.eclipse.ui.nls.StringTable; |
| 33 | + |
| 34 | +public class ViolationDetailsDialogPage extends DialogPage { |
| 35 | + private final IMarker violation; |
| 36 | + private final Rule rule; |
| 37 | + |
| 38 | + public ViolationDetailsDialogPage(IMarker selectedViolation, Rule selectedRule) { |
| 39 | + this.violation = selectedViolation; |
| 40 | + this.rule = selectedRule; |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public void createControl(Composite parent) { |
| 45 | + StringTable messages = PMDPlugin.getDefault().getStringTable(); |
| 46 | + |
| 47 | + GridLayout layout = new GridLayout(2, false); |
| 48 | + Composite composite = WidgetFactory.composite(SWT.NONE).layout(layout).create(parent); |
| 49 | + |
| 50 | + createLabel(messages.getString(StringKeys.DIALOG_VIOLATION_DETAILS_MESSAGE), composite); |
| 51 | + createText(getViolationMessage(), composite); |
| 52 | + |
| 53 | + createLabel(messages.getString(StringKeys.DIALOG_VIOLATION_DETAILS_LOCATION), composite); |
| 54 | + createText(getLocation(), composite); |
| 55 | + |
| 56 | + createLabel(messages.getString(StringKeys.DIALOG_VIOLATION_DETAILS_RULENAME), composite); |
| 57 | + createText(rule.getName(), composite); |
| 58 | + |
| 59 | + createLabel(messages.getString(StringKeys.DIALOG_VIOLATION_DETAILS_CATEGORY), composite); |
| 60 | + createText(rule.getRuleSetName(), composite); |
| 61 | + |
| 62 | + createLabel(messages.getString(StringKeys.DIALOG_VIOLATION_DETAILS_PRIORITY), composite); |
| 63 | + createText(rule.getPriority().getName(), composite); |
| 64 | + |
| 65 | + createTwoColumnLabel(messages.getString(StringKeys.DIALOG_VIOLATION_DETAILS_DESCRIPTION), composite); |
| 66 | + createTwoColumnMultiText(rule.getDescription(), composite); |
| 67 | + |
| 68 | + createTwoColumnLabel(messages.getString(StringKeys.DIALOG_VIOLATION_DETAILS_EXAMPLES), composite); |
| 69 | + createTwoColumnMultiText(getExamples(), JFaceResources.getTextFont(), composite); |
| 70 | + |
| 71 | + createLabel(messages.getString(StringKeys.DIALOG_VIOLATION_DETAILS_INFOURL), composite); |
| 72 | + createLink(rule.getExternalInfoUrl(), composite); |
| 73 | + } |
| 74 | + |
| 75 | + private Label createLabel(String text, Composite parent) { |
| 76 | + return WidgetFactory.label(SWT.NONE).text(text).font(JFaceResources.getHeaderFont()) |
| 77 | + .layoutData(new GridData(GridData.VERTICAL_ALIGN_BEGINNING)).create(parent); |
| 78 | + } |
| 79 | + |
| 80 | + private Label createTwoColumnLabel(String text, Composite parent) { |
| 81 | + GridData gridData = new GridData(); |
| 82 | + gridData.verticalAlignment = SWT.BEGINNING; |
| 83 | + gridData.horizontalSpan = 2; |
| 84 | + return WidgetFactory.label(SWT.NONE).text(text).font(JFaceResources.getHeaderFont()) |
| 85 | + .layoutData(gridData).create(parent); |
| 86 | + } |
| 87 | + |
| 88 | + private Text createText(String text, Composite parent) { |
| 89 | + return WidgetFactory.text(SWT.READ_ONLY | SWT.SINGLE) |
| 90 | + .background(parent.getBackground()) |
| 91 | + .layoutData(new GridData(GridData.FILL_HORIZONTAL)) |
| 92 | + .text(text) |
| 93 | + .create(parent); |
| 94 | + } |
| 95 | + |
| 96 | + private Text createTwoColumnMultiText(String text, Composite parent) { |
| 97 | + return createTwoColumnMultiText(text, null, parent); |
| 98 | + } |
| 99 | + |
| 100 | + private Text createTwoColumnMultiText(String text, Font font, Composite parent) { |
| 101 | + GridData gridData = new GridData(GridData.FILL_BOTH); |
| 102 | + gridData.horizontalSpan = 2; |
| 103 | + return WidgetFactory.text(SWT.READ_ONLY | SWT.MULTI | SWT.WRAP) |
| 104 | + .background(parent.getBackground()) |
| 105 | + .layoutData(gridData) |
| 106 | + .text(text) |
| 107 | + .font(font) |
| 108 | + .create(parent); |
| 109 | + } |
| 110 | + |
| 111 | + private Link createLink(String url, Composite parent) { |
| 112 | + Link link = new Link(parent, SWT.NONE); |
| 113 | + link.setText(String.format("<a href=\"%s\">%s</a>", url, url)); |
| 114 | + link.addSelectionListener(new LinkClickListener()); |
| 115 | + link.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_BEGINNING)); |
| 116 | + return link; |
| 117 | + } |
| 118 | + |
| 119 | + private String getViolationMessage() { |
| 120 | + String defaultMessage = violation.getAttribute(IMarker.MESSAGE, ""); |
| 121 | + return violation.getAttribute(PMDRuntimeConstants.KEY_MARKERATT_MESSAGE, defaultMessage); |
| 122 | + } |
| 123 | + |
| 124 | + private String getLocation() { |
| 125 | + String projectName = violation.getResource().getProject().getName(); |
| 126 | + String path = violation.getResource().getProjectRelativePath().toString(); |
| 127 | + int lineNumber = violation.getAttribute(IMarker.LINE_NUMBER, -1); |
| 128 | + |
| 129 | + if (lineNumber != -1) { |
| 130 | + return String.format("%s: %s:%d", projectName, path, lineNumber); |
| 131 | + } |
| 132 | + return String.format("%s: %s", projectName, path); |
| 133 | + } |
| 134 | + |
| 135 | + private String getExamples() { |
| 136 | + StringBuilder result = new StringBuilder(); |
| 137 | + for (String example : rule.getExamples()) { |
| 138 | + result.append(example); |
| 139 | + result.append(System.lineSeparator()); |
| 140 | + result.append(System.lineSeparator()); |
| 141 | + } |
| 142 | + return result.toString(); |
| 143 | + } |
| 144 | + |
| 145 | + private static final class LinkClickListener implements SelectionListener { |
| 146 | + @Override |
| 147 | + public void widgetSelected(SelectionEvent e) { |
| 148 | + try { |
| 149 | + URL url = new URL(e.text); |
| 150 | + IWebBrowser browser = PlatformUI.getWorkbench().getBrowserSupport().getExternalBrowser(); |
| 151 | + browser.openURL(url); |
| 152 | + } catch (MalformedURLException | PartInitException e1) { |
| 153 | + PMDPlugin.getDefault().logError(e1.getMessage(), e1); |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + @Override |
| 158 | + public void widgetDefaultSelected(SelectionEvent e) { |
| 159 | + widgetSelected(e); |
| 160 | + } |
| 161 | + } |
| 162 | +} |
0 commit comments