[3669] | 1 | // License: GPL. See LICENSE file for details.
|
---|
| 2 | package org.openstreetmap.josm.gui.layer;
|
---|
| 3 |
|
---|
| 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
| 5 |
|
---|
| 6 | import java.awt.Graphics2D;
|
---|
| 7 | import java.util.Enumeration;
|
---|
| 8 | import java.util.List;
|
---|
| 9 |
|
---|
| 10 | import javax.swing.Action;
|
---|
| 11 | import javax.swing.Icon;
|
---|
| 12 | import javax.swing.tree.DefaultMutableTreeNode;
|
---|
| 13 |
|
---|
| 14 | import org.openstreetmap.josm.Main;
|
---|
| 15 | import org.openstreetmap.josm.actions.RenameLayerAction;
|
---|
| 16 | import org.openstreetmap.josm.data.Bounds;
|
---|
| 17 | import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor;
|
---|
| 18 | import org.openstreetmap.josm.data.validation.OsmValidator;
|
---|
[5671] | 19 | import org.openstreetmap.josm.data.validation.PaintVisitor;
|
---|
[3669] | 20 | import org.openstreetmap.josm.data.validation.Severity;
|
---|
| 21 | import org.openstreetmap.josm.data.validation.TestError;
|
---|
| 22 | import org.openstreetmap.josm.gui.MapView;
|
---|
| 23 | import org.openstreetmap.josm.gui.MapView.LayerChangeListener;
|
---|
| 24 | import org.openstreetmap.josm.gui.dialogs.LayerListDialog;
|
---|
| 25 | import org.openstreetmap.josm.gui.dialogs.LayerListPopup;
|
---|
| 26 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
[3674] | 27 | import org.openstreetmap.josm.tools.MultiMap;
|
---|
[3669] | 28 |
|
---|
| 29 | /**
|
---|
| 30 | * A layer showing error messages.
|
---|
| 31 | *
|
---|
| 32 | * @author frsantos
|
---|
| 33 | */
|
---|
| 34 | public class ValidatorLayer extends Layer implements LayerChangeListener {
|
---|
| 35 |
|
---|
| 36 | private int updateCount = -1;
|
---|
| 37 |
|
---|
[5671] | 38 | /**
|
---|
| 39 | * Constructs a new Validator layer
|
---|
| 40 | */
|
---|
[3669] | 41 | public ValidatorLayer() {
|
---|
| 42 | super(tr("Validation errors"));
|
---|
| 43 | MapView.addLayerChangeListener(this);
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | /**
|
---|
| 47 | * Return a static icon.
|
---|
| 48 | */
|
---|
| 49 | @Override
|
---|
| 50 | public Icon getIcon() {
|
---|
| 51 | return ImageProvider.get("layer", "validator_small");
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | /**
|
---|
| 55 | * Draw all primitives in this layer but do not draw modified ones (they
|
---|
| 56 | * are drawn by the edit layer).
|
---|
| 57 | * Draw nodes last to overlap the ways they belong to.
|
---|
| 58 | */
|
---|
| 59 | @SuppressWarnings("unchecked")
|
---|
| 60 | @Override
|
---|
| 61 | public void paint(final Graphics2D g, final MapView mv, Bounds bounds) {
|
---|
| 62 | updateCount = Main.map.validatorDialog.tree.getUpdateCount();
|
---|
| 63 | DefaultMutableTreeNode root = Main.map.validatorDialog.tree.getRoot();
|
---|
| 64 | if (root == null || root.getChildCount() == 0)
|
---|
| 65 | return;
|
---|
[6070] | 66 |
|
---|
[5671] | 67 | PaintVisitor paintVisitor = new PaintVisitor(g, mv);
|
---|
[3669] | 68 |
|
---|
| 69 | DefaultMutableTreeNode severity = (DefaultMutableTreeNode) root.getLastChild();
|
---|
| 70 | while (severity != null) {
|
---|
| 71 | Enumeration<DefaultMutableTreeNode> errorMessages = severity.breadthFirstEnumeration();
|
---|
| 72 | while (errorMessages.hasMoreElements()) {
|
---|
| 73 | Object tn = errorMessages.nextElement().getUserObject();
|
---|
[3671] | 74 | if (tn instanceof TestError) {
|
---|
[5671] | 75 | paintVisitor.visit(((TestError) tn));
|
---|
[3671] | 76 | }
|
---|
[3669] | 77 | }
|
---|
| 78 |
|
---|
| 79 | // Severities in inverse order
|
---|
| 80 | severity = severity.getPreviousSibling();
|
---|
| 81 | }
|
---|
[6070] | 82 |
|
---|
[5671] | 83 | paintVisitor.clearPaintedObjects();
|
---|
[3669] | 84 | }
|
---|
| 85 |
|
---|
| 86 | @Override
|
---|
| 87 | public String getToolTipText() {
|
---|
[3674] | 88 | MultiMap<Severity, TestError> errorTree = new MultiMap<Severity, TestError>();
|
---|
[3669] | 89 | List<TestError> errors = Main.map.validatorDialog.tree.getErrors();
|
---|
| 90 | for (TestError e : errors) {
|
---|
[3674] | 91 | errorTree.put(e.getSeverity(), e);
|
---|
[3669] | 92 | }
|
---|
| 93 |
|
---|
| 94 | StringBuilder b = new StringBuilder();
|
---|
| 95 | for (Severity s : Severity.values()) {
|
---|
[3671] | 96 | if (errorTree.containsKey(s)) {
|
---|
[3669] | 97 | b.append(tr(s.toString())).append(": ").append(errorTree.get(s).size()).append("<br>");
|
---|
[3671] | 98 | }
|
---|
[3669] | 99 | }
|
---|
| 100 |
|
---|
| 101 | if (b.length() == 0)
|
---|
| 102 | return "<html>" + tr("No validation errors") + "</html>";
|
---|
| 103 | else
|
---|
| 104 | return "<html>" + tr("Validation errors") + ":<br>" + b + "</html>";
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | @Override
|
---|
| 108 | public void mergeFrom(Layer from) {
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | @Override
|
---|
| 112 | public boolean isMergable(Layer other) {
|
---|
| 113 | return false;
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | @Override
|
---|
| 117 | public boolean isChanged() {
|
---|
| 118 | return updateCount != Main.map.validatorDialog.tree.getUpdateCount();
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | @Override
|
---|
| 122 | public void visitBoundingBox(BoundingXYVisitor v) {
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | @Override
|
---|
| 126 | public Object getInfoComponent() {
|
---|
| 127 | return getToolTipText();
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | @Override
|
---|
| 131 | public Action[] getMenuEntries() {
|
---|
| 132 | return new Action[] {
|
---|
| 133 | LayerListDialog.getInstance().createShowHideLayerAction(),
|
---|
| 134 | LayerListDialog.getInstance().createDeleteLayerAction(),
|
---|
| 135 | SeparatorLayerAction.INSTANCE,
|
---|
| 136 | new RenameLayerAction(null, this),
|
---|
| 137 | SeparatorLayerAction.INSTANCE,
|
---|
| 138 | new LayerListPopup.InfoAction(this) };
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 | @Override
|
---|
| 142 | public void destroy() {
|
---|
| 143 | }
|
---|
| 144 |
|
---|
[3671] | 145 | @Override
|
---|
[3669] | 146 | public void activeLayerChange(Layer oldLayer, Layer newLayer) {
|
---|
| 147 | }
|
---|
| 148 |
|
---|
[3671] | 149 | @Override
|
---|
[3669] | 150 | public void layerAdded(Layer newLayer) {
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 | /**
|
---|
| 154 | * If layer is the OSM Data layer, remove all errors
|
---|
| 155 | */
|
---|
[3671] | 156 | @Override
|
---|
[3669] | 157 | public void layerRemoved(Layer oldLayer) {
|
---|
[6336] | 158 | if (oldLayer instanceof OsmDataLayer && Main.isDisplayingMapView() && !Main.main.hasEditLayer()) {
|
---|
[5897] | 159 | Main.main.removeLayer(this);
|
---|
[3669] | 160 | } else if (oldLayer == this) {
|
---|
| 161 | MapView.removeLayerChangeListener(this);
|
---|
| 162 | OsmValidator.errorLayer = null;
|
---|
| 163 | }
|
---|
| 164 | }
|
---|
| 165 | }
|
---|