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

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

global use of Utils.isEmpty/isBlank

File size: 8.4 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;
40import org.openstreetmap.josm.tools.Utils;
41
42/**
43 * A management window for the validator's ignorelist
44 * @author Taylor Smock
45 * @since 14828
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 final int index;
53 private final String name;
54 @SuppressWarnings("ImmutableEnumChecker")
55 private final ImageIcon icon;
56
57 BUTTONS(int index, String name, ImageProvider image) {
58 this.index = index;
59 this.name = name;
60 Dimension dim = new Dimension();
61 ImageIcon sizeto = new ImageProvider("ok").getResource().getImageIcon();
62 dim.setSize(-1, sizeto.getIconHeight());
63 this.icon = image.getResource().getImageIcon(dim);
64 }
65
66 public ImageIcon getImageIcon() {
67 return icon;
68 }
69
70 public int getIndex() {
71 return index;
72 }
73
74 public String getName() {
75 return name;
76 }
77 }
78
79 private static final String[] BUTTON_TEXTS = {BUTTONS.OK.getName(), BUTTONS.CANCEL.getName()};
80
81 private static final ImageIcon[] BUTTON_IMAGES = {BUTTONS.OK.getImageIcon(), BUTTONS.CANCEL.getImageIcon()};
82
83 private final JPanel panel = new JPanel(new GridBagLayout());
84
85 private final JTree ignoreErrors;
86
87 private final String type;
88
89 /**
90 * Create a new {@link ValidatorListManagementDialog}
91 * @param type The type of list to create (first letter may or may not be
92 * capitalized, it is put into all lowercase after building the title)
93 */
94 public ValidatorListManagementDialog(String type) {
95 super(MainApplication.getMainFrame(), tr("Validator {0} List Management", type), BUTTON_TEXTS, false);
96 this.type = type.toLowerCase(Locale.ENGLISH);
97 setButtonIcons(BUTTON_IMAGES);
98
99 ignoreErrors = buildList();
100 JScrollPane scroll = GuiHelper.embedInVerticalScrollPane(ignoreErrors);
101
102 panel.add(scroll, GBC.eol().fill(GBC.BOTH).anchor(GBC.CENTER));
103 setContent(panel);
104 setDefaultButton(1);
105 setupDialog();
106 setModal(true);
107 showDialog();
108 }
109
110 @Override
111 public void buttonAction(int buttonIndex, ActionEvent evt) {
112 // Currently OK/Cancel buttons do nothing
113 final int answer;
114 if (buttonIndex == BUTTONS.OK.getIndex()) {
115 Map<String, String> errors = OsmValidator.getIgnoredErrors();
116 Map<String, String> tree = OsmValidator.buildIgnore(ignoreErrors);
117 if (!errors.equals(tree)) {
118 answer = rerunValidatorPrompt();
119 if (answer == JOptionPane.YES_OPTION || answer == JOptionPane.NO_OPTION) {
120 OsmValidator.resetErrorList();
121 tree.forEach(OsmValidator::addIgnoredError);
122 OsmValidator.saveIgnoredErrors();
123 OsmValidator.initialize();
124 }
125 }
126 dispose();
127 } else {
128 super.buttonAction(buttonIndex, evt);
129 }
130 }
131
132 /**
133 * Build a JTree with a list
134 * @return &lt;type&gt;list as a {@code JTree}
135 */
136 public JTree buildList() {
137 JTree tree;
138
139 if ("ignore".equals(type)) {
140 tree = OsmValidator.buildJTreeList();
141 } else {
142 Logging.error(tr("Cannot understand the following type: {0}", type));
143 return null;
144 }
145 tree.setRootVisible(false);
146 tree.setShowsRootHandles(true);
147 tree.addMouseListener(new MouseAdapter() {
148 @Override
149 public void mousePressed(MouseEvent e) {
150 process(e);
151 }
152
153 @Override
154 public void mouseReleased(MouseEvent e) {
155 process(e);
156 }
157
158 private void process(MouseEvent e) {
159 if (e.isPopupTrigger()) {
160 TreePath[] paths = tree.getSelectionPaths();
161 if (paths == null) return;
162 Rectangle bounds = tree.getUI().getPathBounds(tree, paths[0]);
163 if (bounds != null) {
164 JPopupMenu menu = new JPopupMenu();
165 JMenuItem delete = new JMenuItem(new AbstractAction(tr("Don''t ignore")) {
166 @Override
167 public void actionPerformed(ActionEvent e1) {
168 deleteAction(tree, paths);
169 }
170 });
171 menu.add(delete);
172 menu.show(e.getComponent(), e.getX(), e.getY());
173 }
174 }
175 }
176 });
177
178 tree.addKeyListener(new KeyListener() {
179
180 @Override
181 public void keyTyped(KeyEvent e) {
182 // Do nothing
183 }
184
185 @Override
186 public void keyPressed(KeyEvent e) {
187 // Do nothing
188 }
189
190 @Override
191 public void keyReleased(KeyEvent e) {
192 TreePath[] paths = tree.getSelectionPaths();
193 if (e.getKeyCode() == KeyEvent.VK_DELETE && paths != null) {
194 deleteAction(tree, paths);
195 }
196 }
197 });
198 return tree;
199 }
200
201 private static void deleteAction(JTree tree, TreePath[] paths) {
202 for (TreePath path : paths) {
203 tree.clearSelection();
204 tree.addSelectionPath(path);
205 DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
206 DefaultMutableTreeNode parent = (DefaultMutableTreeNode) node.getParent();
207 node.removeAllChildren();
208 while (node.getChildCount() == 0) {
209 node.removeFromParent();
210 node = parent;
211 if (parent == null || parent.isRoot()) break;
212 parent = (DefaultMutableTreeNode) node.getParent();
213 }
214 }
215 tree.updateUI();
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() || Utils.isEmpty(errors)) 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.