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