source: josm/trunk/src/org/openstreetmap/josm/gui/layer/ValidatorLayer.java@ 3671

Last change on this file since 3671 was 3671, checked in by bastiK, 14 years ago

adapt coding style (to some degree); there shouldn't be any semantic changes in this commit

  • Property svn:eol-style set to native
File size: 4.8 KB
Line 
1// License: GPL. See LICENSE file for details.
2package org.openstreetmap.josm.gui.layer;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.Graphics2D;
7import java.util.Enumeration;
8import java.util.List;
9
10import javax.swing.Action;
11import javax.swing.Icon;
12import javax.swing.tree.DefaultMutableTreeNode;
13
14import org.openstreetmap.josm.Main;
15import org.openstreetmap.josm.actions.RenameLayerAction;
16import org.openstreetmap.josm.data.Bounds;
17import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor;
18import org.openstreetmap.josm.data.validation.OsmValidator;
19import org.openstreetmap.josm.data.validation.Severity;
20import org.openstreetmap.josm.data.validation.TestError;
21import org.openstreetmap.josm.data.validation.util.Bag;
22import org.openstreetmap.josm.gui.MapView;
23import org.openstreetmap.josm.gui.MapView.LayerChangeListener;
24import org.openstreetmap.josm.gui.dialogs.LayerListDialog;
25import org.openstreetmap.josm.gui.dialogs.LayerListPopup;
26import org.openstreetmap.josm.tools.ImageProvider;
27
28/**
29 * A layer showing error messages.
30 *
31 * @author frsantos
32 */
33public class ValidatorLayer extends Layer implements LayerChangeListener {
34
35 private int updateCount = -1;
36
37 public ValidatorLayer() {
38 super(tr("Validation errors"));
39 MapView.addLayerChangeListener(this);
40 }
41
42 /**
43 * Return a static icon.
44 */
45 @Override
46 public Icon getIcon() {
47 return ImageProvider.get("layer", "validator_small");
48 }
49
50 /**
51 * Draw all primitives in this layer but do not draw modified ones (they
52 * are drawn by the edit layer).
53 * Draw nodes last to overlap the ways they belong to.
54 */
55 @SuppressWarnings("unchecked")
56 @Override
57 public void paint(final Graphics2D g, final MapView mv, Bounds bounds) {
58 updateCount = Main.map.validatorDialog.tree.getUpdateCount();
59 DefaultMutableTreeNode root = Main.map.validatorDialog.tree.getRoot();
60 if (root == null || root.getChildCount() == 0)
61 return;
62
63 DefaultMutableTreeNode severity = (DefaultMutableTreeNode) root.getLastChild();
64 while (severity != null) {
65 Enumeration<DefaultMutableTreeNode> errorMessages = severity.breadthFirstEnumeration();
66 while (errorMessages.hasMoreElements()) {
67 Object tn = errorMessages.nextElement().getUserObject();
68 if (tn instanceof TestError) {
69 ((TestError) tn).paint(g, mv);
70 }
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
93 if (b.length() == 0)
94 return "<html>" + tr("No validation errors") + "</html>";
95 else
96 return "<html>" + tr("Validation errors") + ":<br>" + b + "</html>";
97 }
98
99 @Override
100 public void mergeFrom(Layer from) {
101 }
102
103 @Override
104 public boolean isMergable(Layer other) {
105 return false;
106 }
107
108 @Override
109 public boolean isChanged() {
110 return updateCount != Main.map.validatorDialog.tree.getUpdateCount();
111 }
112
113 @Override
114 public void visitBoundingBox(BoundingXYVisitor v) {
115 }
116
117 @Override
118 public Object getInfoComponent() {
119 return getToolTipText();
120 }
121
122 @Override
123 public Action[] getMenuEntries() {
124 return new Action[] {
125 LayerListDialog.getInstance().createShowHideLayerAction(),
126 LayerListDialog.getInstance().createDeleteLayerAction(),
127 SeparatorLayerAction.INSTANCE,
128 new RenameLayerAction(null, this),
129 SeparatorLayerAction.INSTANCE,
130 new LayerListPopup.InfoAction(this) };
131 }
132
133 @Override
134 public void destroy() {
135 }
136
137 @Override
138 public void activeLayerChange(Layer oldLayer, Layer newLayer) {
139 }
140
141 @Override
142 public void layerAdded(Layer newLayer) {
143 }
144
145 /**
146 * If layer is the OSM Data layer, remove all errors
147 */
148 @Override
149 public void layerRemoved(Layer oldLayer) {
150 if (oldLayer instanceof OsmDataLayer && Main.map.mapView.getEditLayer() == null) {
151 Main.map.mapView.removeLayer(this);
152 } else if (oldLayer == this) {
153 MapView.removeLayerChangeListener(this);
154 OsmValidator.errorLayer = null;
155 }
156 }
157}
Note: See TracBrowser for help on using the repository browser.