1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.dialogs;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.Dimension;
|
---|
7 | import java.awt.GridBagLayout;
|
---|
8 | import java.awt.Rectangle;
|
---|
9 | import java.awt.event.ActionEvent;
|
---|
10 | import java.awt.event.KeyEvent;
|
---|
11 | import java.awt.event.KeyListener;
|
---|
12 | import java.awt.event.MouseAdapter;
|
---|
13 | import java.awt.event.MouseEvent;
|
---|
14 | import java.util.List;
|
---|
15 | import java.util.Locale;
|
---|
16 | import java.util.Map;
|
---|
17 |
|
---|
18 | import javax.swing.AbstractAction;
|
---|
19 | import javax.swing.ImageIcon;
|
---|
20 | import javax.swing.JMenuItem;
|
---|
21 | import javax.swing.JOptionPane;
|
---|
22 | import javax.swing.JPanel;
|
---|
23 | import javax.swing.JPopupMenu;
|
---|
24 | import javax.swing.JScrollPane;
|
---|
25 | import javax.swing.JTree;
|
---|
26 | import javax.swing.tree.DefaultMutableTreeNode;
|
---|
27 | import javax.swing.tree.TreePath;
|
---|
28 |
|
---|
29 | import org.openstreetmap.josm.actions.ValidateAction;
|
---|
30 | import org.openstreetmap.josm.data.validation.OsmValidator;
|
---|
31 | import org.openstreetmap.josm.data.validation.TestError;
|
---|
32 | import org.openstreetmap.josm.gui.ConditionalOptionPaneUtil;
|
---|
33 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
---|
34 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
35 | import org.openstreetmap.josm.gui.MapFrame;
|
---|
36 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
---|
37 | import org.openstreetmap.josm.tools.GBC;
|
---|
38 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
39 | import 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 | */
|
---|
47 | public 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 <type>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 | }
|
---|