source: josm/trunk/src/org/openstreetmap/josm/gui/dialogs/ValidatorListManagementDialog.java@ 14939

Last change on this file since 14939 was 14939, checked in by Don-vip, 5 years ago

see #17268, see #17516 - add missing semicolons (issue reported by javadoc 13)

File size: 8.3 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.dialogs;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.Dimension;
7import java.awt.GridBagLayout;
8import java.awt.Rectangle;
9import java.awt.event.ActionEvent;
10import java.awt.event.KeyEvent;
11import java.awt.event.KeyListener;
12import java.awt.event.MouseAdapter;
13import java.awt.event.MouseEvent;
14import java.util.List;
15import java.util.Locale;
16import java.util.Map;
17
18import javax.swing.AbstractAction;
19import javax.swing.ImageIcon;
20import javax.swing.JMenuItem;
21import javax.swing.JOptionPane;
22import javax.swing.JPanel;
23import javax.swing.JPopupMenu;
24import javax.swing.JScrollPane;
25import javax.swing.JTree;
26import javax.swing.tree.DefaultMutableTreeNode;
27import javax.swing.tree.TreePath;
28
29import org.openstreetmap.josm.actions.ValidateAction;
30import org.openstreetmap.josm.data.validation.OsmValidator;
31import org.openstreetmap.josm.data.validation.TestError;
32import org.openstreetmap.josm.gui.ConditionalOptionPaneUtil;
33import org.openstreetmap.josm.gui.ExtendedDialog;
34import org.openstreetmap.josm.gui.MainApplication;
35import org.openstreetmap.josm.gui.MapFrame;
36import org.openstreetmap.josm.gui.util.GuiHelper;
37import org.openstreetmap.josm.tools.GBC;
38import org.openstreetmap.josm.tools.ImageProvider;
39import org.openstreetmap.josm.tools.Logging;
40
41
42/**
43 * A management window for the validator's ignorelist
44 * @author Taylor Smock
45 * @since xxx
46 */
47public class ValidatorListManagementDialog extends ExtendedDialog {
48 enum BUTTONS {
49 OK(0, tr("OK"), new ImageProvider("ok")),
50 CANCEL(1, tr("Cancel"), new ImageProvider("cancel"));
51
52 private int index;
53 private String name;
54 private ImageIcon icon;
55
56 BUTTONS(int index, String name, ImageProvider image) {
57 this.index = index;
58 this.name = name;
59 Dimension dim = new Dimension();
60 ImageIcon sizeto = new ImageProvider("ok").getResource().getImageIcon();
61 dim.setSize(-1, sizeto.getIconHeight());
62 this.icon = image.getResource().getImageIcon(dim);
63 }
64
65 public ImageIcon getImageIcon() {
66 return icon;
67 }
68
69 public int getIndex() {
70 return index;
71 }
72
73 public String getName() {
74 return name;
75 }
76 }
77
78 private static final String[] BUTTON_TEXTS = {BUTTONS.OK.getName(), BUTTONS.CANCEL.getName()};
79
80 private static final ImageIcon[] BUTTON_IMAGES = {BUTTONS.OK.getImageIcon(), BUTTONS.CANCEL.getImageIcon()};
81
82 private final JPanel panel = new JPanel(new GridBagLayout());
83
84 private final JTree ignoreErrors;
85
86 private final String type;
87
88 /**
89 * Create a new {@link ValidatorListManagementDialog}
90 * @param type The type of list to create (first letter may or may not be
91 * capitalized, it is put into all lowercase after building the title)
92 */
93 public ValidatorListManagementDialog(String type) {
94 super(MainApplication.getMainFrame(), tr("Validator {0} List Management", type), BUTTON_TEXTS, false);
95 this.type = type.toLowerCase(Locale.ENGLISH);
96 setButtonIcons(BUTTON_IMAGES);
97
98 ignoreErrors = buildList();
99 JScrollPane scroll = GuiHelper.embedInVerticalScrollPane(ignoreErrors);
100
101 panel.add(scroll, GBC.eol().fill(GBC.BOTH).anchor(GBC.CENTER));
102 setContent(panel);
103 setDefaultButton(1);
104 setupDialog();
105 setModal(true);
106 showDialog();
107 }
108
109 @Override
110 public void buttonAction(int buttonIndex, ActionEvent evt) {
111 // Currently OK/Cancel buttons do nothing
112 final int answer;
113 if (buttonIndex == BUTTONS.OK.getIndex()) {
114 Map<String, String> errors = OsmValidator.getIgnoredErrors();
115 Map<String, String> tree = OsmValidator.buildIgnore(ignoreErrors);
116 if (!errors.equals(tree)) {
117 answer = rerunValidatorPrompt();
118 if (answer == JOptionPane.YES_OPTION || answer == JOptionPane.NO_OPTION) {
119 OsmValidator.resetErrorList();
120 tree.forEach(OsmValidator::addIgnoredError);
121 OsmValidator.saveIgnoredErrors();
122 OsmValidator.initialize();
123 }
124 }
125 dispose();
126 } else {
127 super.buttonAction(buttonIndex, evt);
128 }
129 }
130
131 /**
132 * Build a JTree with a list
133 * @return &lt;type&gt;list as a {@code JTree}
134 */
135 public JTree buildList() {
136 JTree tree;
137
138 if ("ignore".equals(type)) {
139 tree = OsmValidator.buildJTreeList();
140 } else {
141 Logging.error(tr("Cannot understand the following type: {0}", type));
142 return null;
143 }
144 tree.setRootVisible(false);
145 tree.setShowsRootHandles(true);
146 tree.addMouseListener(new MouseAdapter() {
147 @Override
148 public void mousePressed(MouseEvent e) {
149 process(e);
150 }
151
152 @Override
153 public void mouseReleased(MouseEvent e) {
154 process(e);
155 }
156
157 private void process(MouseEvent e) {
158 if (e.isPopupTrigger()) {
159 TreePath[] paths = tree.getSelectionPaths();
160 if (paths == null) return;
161 Rectangle bounds = tree.getUI().getPathBounds(tree, paths[0]);
162 if (bounds != null) {
163 JPopupMenu menu = new JPopupMenu();
164 JMenuItem delete = new JMenuItem(new AbstractAction(tr("Don''t ignore")) {
165 @Override
166 public void actionPerformed(ActionEvent e1) {
167 deleteAction(tree, paths);
168 }
169 });
170 menu.add(delete);
171 menu.show(e.getComponent(), e.getX(), e.getY());
172 }
173 }
174 }
175 });
176
177 tree.addKeyListener(new KeyListener() {
178
179 @Override
180 public void keyTyped(KeyEvent e) {
181 // Do nothing
182 }
183
184 @Override
185 public void keyPressed(KeyEvent e) {
186 // Do nothing
187 }
188
189 @Override
190 public void keyReleased(KeyEvent e) {
191 TreePath[] paths = tree.getSelectionPaths();
192 if (e.getKeyCode() == KeyEvent.VK_DELETE && paths != null) {
193 deleteAction(tree, paths);
194 }
195 }
196 });
197 return tree;
198 }
199
200 private static void deleteAction(JTree tree, TreePath[] paths) {
201 for (TreePath path : paths) {
202 tree.clearSelection();
203 tree.addSelectionPath(path);
204 DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
205 DefaultMutableTreeNode parent = (DefaultMutableTreeNode) node.getParent();
206 node.removeAllChildren();
207 while (node.getChildCount() == 0) {
208 node.removeFromParent();
209 node = parent;
210 if (parent == null || parent.isRoot()) break;
211 parent = (DefaultMutableTreeNode) node.getParent();
212 }
213 }
214 tree.updateUI();
215 }
216
217
218 /**
219 * Prompt to rerun the validator when the ignore list changes
220 * @return {@code JOptionPane.YES_OPTION}, {@code JOptionPane.NO_OPTION},
221 * or {@code JOptionPane.CANCEL_OPTION}
222 */
223 public int rerunValidatorPrompt() {
224 MapFrame map = MainApplication.getMap();
225 List<TestError> errors = map.validatorDialog.tree.getErrors();
226 ValidateAction validateAction = ValidatorDialog.validateAction;
227 if (!validateAction.isEnabled() || errors == null || errors.isEmpty()) return JOptionPane.NO_OPTION;
228 final int answer = ConditionalOptionPaneUtil.showOptionDialog(
229 "rerun_validation_when_ignorelist_changed",
230 MainApplication.getMainFrame(),
231 tr("{0}Should the validation be rerun?{1}", "<hmtl><h3>", "</h3></html>"),
232 tr("Ignored error filter changed"),
233 JOptionPane.YES_NO_CANCEL_OPTION,
234 JOptionPane.QUESTION_MESSAGE,
235 null,
236 null);
237 if (answer == JOptionPane.YES_OPTION) {
238 validateAction.doValidate(true);
239 }
240 return answer;
241 }
242}
Note: See TracBrowser for help on using the repository browser.