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