1 | package UtilsPlugin.JosmLint;
|
---|
2 |
|
---|
3 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
4 |
|
---|
5 | import org.openstreetmap.josm.Main;
|
---|
6 | import org.openstreetmap.josm.plugins.Plugin;
|
---|
7 | import org.openstreetmap.josm.gui.MapFrame;
|
---|
8 | import org.openstreetmap.josm.gui.IconToggleButton;
|
---|
9 | import org.openstreetmap.josm.gui.dialogs.ToggleDialog;
|
---|
10 | import java.awt.event.KeyEvent;
|
---|
11 | import javax.swing.JToolBar;
|
---|
12 | import java.awt.BorderLayout;
|
---|
13 | import javax.swing.BoxLayout;
|
---|
14 | import java.awt.GridLayout;
|
---|
15 | import javax.swing.DefaultListModel;
|
---|
16 | import javax.swing.JButton;
|
---|
17 | import javax.swing.JList;
|
---|
18 | import javax.swing.JPanel;
|
---|
19 | import javax.swing.JScrollPane;
|
---|
20 | import javax.swing.ListSelectionModel;
|
---|
21 | import java.awt.event.MouseAdapter;
|
---|
22 | import java.awt.event.MouseEvent;
|
---|
23 |
|
---|
24 | import org.openstreetmap.josm.data.SelectionChangedListener;
|
---|
25 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
26 |
|
---|
27 | import java.util.List;
|
---|
28 | import java.util.ArrayList;
|
---|
29 | import java.util.Map;
|
---|
30 | import java.util.TreeMap;
|
---|
31 | import java.util.Collection;
|
---|
32 |
|
---|
33 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
34 |
|
---|
35 | public class JosmLint extends ToggleDialog implements SelectionChangedListener {
|
---|
36 | /**
|
---|
37 | * The selection's list data.
|
---|
38 | */
|
---|
39 | private final DefaultListModel list = new DefaultListModel();
|
---|
40 | private final Map<OsmPrimitive,JosmLintTestResult> results = new TreeMap<OsmPrimitive,JosmLintTestResult>();
|
---|
41 | /**
|
---|
42 | * The display list.
|
---|
43 | */
|
---|
44 | private JList displaylist = new JList(list);
|
---|
45 | List<JosmLintTest> tests = new ArrayList<JosmLintTest>();
|
---|
46 |
|
---|
47 | private class JosmLintWorker implements Runnable
|
---|
48 | {
|
---|
49 | public boolean stop;
|
---|
50 | private JosmLint parent;
|
---|
51 |
|
---|
52 | public JosmLintWorker(JosmLint p)
|
---|
53 | {
|
---|
54 | parent = p;
|
---|
55 | stop = false;
|
---|
56 | }
|
---|
57 |
|
---|
58 | public void run()
|
---|
59 | {
|
---|
60 | /* Background loop, checks 200 objects per second... */
|
---|
61 | while( !stop )
|
---|
62 | {
|
---|
63 | Collection<OsmPrimitive> ds = Main.ds.allNonDeletedPrimitives();
|
---|
64 | for( OsmPrimitive o : ds )
|
---|
65 | {
|
---|
66 | if( stop )
|
---|
67 | break;
|
---|
68 | parent.checkObject(o);
|
---|
69 | simpleSleep(5);
|
---|
70 | }
|
---|
71 | simpleSleep(10000);
|
---|
72 | }
|
---|
73 | }
|
---|
74 | }
|
---|
75 |
|
---|
76 | private static void simpleSleep( int millis )
|
---|
77 | {
|
---|
78 | try {
|
---|
79 | Thread.sleep(millis);
|
---|
80 | }
|
---|
81 | catch(InterruptedException e) {}
|
---|
82 | }
|
---|
83 | private void checkObject( OsmPrimitive o )
|
---|
84 | {
|
---|
85 | JosmLintTestResult res = results.get(o);
|
---|
86 |
|
---|
87 | if( res != null )
|
---|
88 | {
|
---|
89 | if( !res.recheck() )
|
---|
90 | {
|
---|
91 | results.remove(o);
|
---|
92 | list.removeElement(res);
|
---|
93 | res = null;
|
---|
94 | }
|
---|
95 | }
|
---|
96 | if( res != null )
|
---|
97 | return;
|
---|
98 |
|
---|
99 | for( JosmLintTest test : tests )
|
---|
100 | {
|
---|
101 | res = test.runTest(o);
|
---|
102 | if( res != null )
|
---|
103 | {
|
---|
104 | System.out.println( "Got test failure: "+res );
|
---|
105 |
|
---|
106 | results.put(o,res);
|
---|
107 | list.addElement(res);
|
---|
108 | break;
|
---|
109 | }
|
---|
110 | }
|
---|
111 | }
|
---|
112 |
|
---|
113 | private JosmLintWorker worker;
|
---|
114 | private static JosmLint lint;
|
---|
115 |
|
---|
116 | public JosmLint()
|
---|
117 | {
|
---|
118 | super( tr("JosmLint"), "josmlint", tr("Scans the current data for problems"), KeyEvent.VK_J, 150 );
|
---|
119 | add(new JScrollPane(displaylist), BorderLayout.CENTER);
|
---|
120 | displaylist.addMouseListener(new MouseAdapter(){
|
---|
121 | @Override public void mouseClicked(MouseEvent e) {
|
---|
122 | if (e.getClickCount() < 2)
|
---|
123 | return;
|
---|
124 | selectObject();
|
---|
125 | }
|
---|
126 | });
|
---|
127 |
|
---|
128 | tests.add( new ConsistancyTest() );
|
---|
129 | tests.add( new WayCheckTest() );
|
---|
130 | }
|
---|
131 |
|
---|
132 | @Override public void setVisible(boolean b) {
|
---|
133 | if (b) {
|
---|
134 | try { Main.ds.addSelectionChangedListener(this); }
|
---|
135 | catch( NoSuchMethodError e )
|
---|
136 | {
|
---|
137 | try {
|
---|
138 | java.lang.reflect.Field f = DataSet.class.getDeclaredField("listeners");
|
---|
139 | ((Collection<SelectionChangedListener>)f.get(Main.ds)).add(this);
|
---|
140 | // Main.ds.listeners.add(this);
|
---|
141 | } catch (Exception x) { System.out.println( e ); }
|
---|
142 | }
|
---|
143 | selectionChanged(Main.ds.getSelected());
|
---|
144 | worker = new JosmLintWorker(this);
|
---|
145 | new Thread(worker).start();
|
---|
146 | } else {
|
---|
147 | try { Main.ds.removeSelectionChangedListener(this); }
|
---|
148 | catch( NoSuchMethodError e )
|
---|
149 | {
|
---|
150 | try {
|
---|
151 | java.lang.reflect.Field f = DataSet.class.getDeclaredField("listeners");
|
---|
152 | ((Collection<SelectionChangedListener>)f.get(Main.ds)).remove(this);
|
---|
153 | // Main.ds.listeners.remove(this);
|
---|
154 | } catch (Exception x) { System.out.println( e ); }
|
---|
155 | }
|
---|
156 | if( worker != null )
|
---|
157 | {
|
---|
158 | worker.stop = true;
|
---|
159 | // worker.join();
|
---|
160 | worker = null;
|
---|
161 | }
|
---|
162 | }
|
---|
163 | super.setVisible(b);
|
---|
164 | }
|
---|
165 | public void selectionChanged(Collection<? extends OsmPrimitive> newSelection) {
|
---|
166 | if (list == null)
|
---|
167 | return; // selection changed may be received in base class constructor before init
|
---|
168 | for( OsmPrimitive o : newSelection )
|
---|
169 | checkObject(o);
|
---|
170 | }
|
---|
171 | /* When user doubleclicks an item, select that object */
|
---|
172 | public void selectObject()
|
---|
173 | {
|
---|
174 | JosmLintTestResult res = (JosmLintTestResult)displaylist.getSelectedValue();
|
---|
175 | OsmPrimitive obj = res.getSelection();
|
---|
176 | Main.ds.setSelected(obj);
|
---|
177 | }
|
---|
178 |
|
---|
179 |
|
---|
180 | public static void setupPlugin()
|
---|
181 | {
|
---|
182 | JPanel toggleDialogs = null;
|
---|
183 | JToolBar toolBarActions = Main.map.toolBarActions;
|
---|
184 |
|
---|
185 | // Find the toggleDialogs
|
---|
186 | for( final java.awt.Component c : Main.map.getComponents() )
|
---|
187 | {
|
---|
188 | if( c.getClass() != JPanel.class )
|
---|
189 | continue;
|
---|
190 | JPanel c2 = (JPanel)c;
|
---|
191 |
|
---|
192 | if( c2.getLayout().getClass() != BoxLayout.class )
|
---|
193 | continue;
|
---|
194 | // System.out.println( "Found: "+ c2.getComponent(1).getClass() );
|
---|
195 | toggleDialogs = c2;
|
---|
196 | break;
|
---|
197 | }
|
---|
198 |
|
---|
199 | if( toggleDialogs == null )
|
---|
200 | {
|
---|
201 | System.out.println( "Failed to insert dialog" );
|
---|
202 | return;
|
---|
203 | }
|
---|
204 | lint = new JosmLint();
|
---|
205 | lint.addIconToggle( toggleDialogs, toolBarActions );
|
---|
206 | }
|
---|
207 | private void addIconToggle(JPanel toggleDialogs, JToolBar toolBarActions) {
|
---|
208 | IconToggleButton button = new IconToggleButton(this.action);
|
---|
209 | this.action.button = button;
|
---|
210 | this.parent = toggleDialogs;
|
---|
211 | toolBarActions.add(button);
|
---|
212 | toggleDialogs.add(this);
|
---|
213 | }
|
---|
214 | public static void stopPlugin()
|
---|
215 | {
|
---|
216 | if( lint.worker != null )
|
---|
217 | lint.worker.stop = true;
|
---|
218 | }
|
---|
219 | }
|
---|