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