source: osm/applications/editors/josm/plugins/utilsplugin/UtilsPlugin/JosmLint/JosmLint.java@ 5075

Last change on this file since 5075 was 5075, checked in by gabriel, 18 years ago

Import the latest upstream version of utilsplugin.

File size: 8.0 KB
Line 
1package UtilsPlugin.JosmLint;
2
3import static org.openstreetmap.josm.tools.I18n.tr;
4
5import org.openstreetmap.josm.Main;
6import org.openstreetmap.josm.plugins.Plugin;
7import org.openstreetmap.josm.gui.MapFrame;
8import org.openstreetmap.josm.gui.IconToggleButton;
9import org.openstreetmap.josm.gui.dialogs.ToggleDialog;
10import java.awt.event.KeyEvent;
11import javax.swing.JToolBar;
12import java.awt.BorderLayout;
13import javax.swing.BoxLayout;
14import java.awt.GridLayout;
15import javax.swing.DefaultListModel;
16import javax.swing.JButton;
17import javax.swing.JList;
18import javax.swing.JPanel;
19import javax.swing.JScrollPane;
20import javax.swing.ListSelectionModel;
21import java.awt.event.MouseAdapter;
22import java.awt.event.MouseEvent;
23
24import org.openstreetmap.josm.data.SelectionChangedListener;
25import org.openstreetmap.josm.data.osm.DataSet;
26
27import java.util.List;
28import java.util.ArrayList;
29import java.util.Map;
30import java.util.TreeMap;
31import java.util.Collection;
32
33import org.openstreetmap.josm.data.osm.OsmPrimitive;
34
35public 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}
Note: See TracBrowser for help on using the repository browser.