Changeset 9334 in josm for trunk


Ignore:
Timestamp:
2016-01-07T01:53:58+01:00 (9 years ago)
Author:
Don-vip
Message:

see #12282 - handle warnings for mappaint styles:

  • Log warnings that occur when loading style
  • Display them in Mappaint dialog (Info action)
  • Drop redundant error icon
  • Ignore XML styles in styles validation test
  • Make styles validation test fail if at least a warning occur
Location:
trunk
Files:
1 deleted
8 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/dialogs/MapPaintDialog.java

    r9280 r9334  
    22package org.openstreetmap.josm.gui.dialogs;
    33
     4import static org.openstreetmap.josm.tools.I18n.marktr;
    45import static org.openstreetmap.josm.tools.I18n.tr;
    56
     
    2627import java.util.ArrayList;
    2728import java.util.Arrays;
     29import java.util.Collection;
    2830import java.util.List;
    2931
     
    3133import javax.swing.DefaultButtonModel;
    3234import javax.swing.DefaultListSelectionModel;
     35import javax.swing.ImageIcon;
    3336import javax.swing.JCheckBox;
    3437import javax.swing.JFileChooser;
     
    544547
    545548        private boolean errorsTabLoaded;
     549        private boolean warningsTabLoaded;
    546550        private boolean sourceTabLoaded;
    547551
     
    573577            tabs.setTabComponentAt(0, lblInfo);
    574578
    575             final JPanel pErrors = new JPanel(new GridBagLayout());
    576             tabs.add("Errors", pErrors);
    577             JLabel lblErrors;
    578             if (s.getErrors().isEmpty()) {
    579                 lblErrors = new JLabel(tr("Errors"));
    580                 lblErrors.setFont(lblInfo.getFont().deriveFont(Font.PLAIN));
    581                 lblErrors.setEnabled(false);
    582                 tabs.setTabComponentAt(1, lblErrors);
    583                 tabs.setEnabledAt(1, false);
    584             } else {
    585                 lblErrors = new JLabel(tr("Errors"), ImageProvider.get("misc", "error"), JLabel.HORIZONTAL);
    586                 tabs.setTabComponentAt(1, lblErrors);
    587             }
     579            final JPanel pErrors = addErrorOrWarningTab(tabs, lblInfo,
     580                    s.getErrors(), marktr("Errors"), 1, ImageProvider.get("misc", "error"));
     581            final JPanel pWarnings = addErrorOrWarningTab(tabs, lblInfo,
     582                    s.getWarnings(), marktr("Warnings"), 2, ImageProvider.get("warning-small"));
    588583
    589584            final JPanel pSource = new JPanel(new GridBagLayout());
     
    591586            JLabel lblSource = new JLabel(tr("Source"));
    592587            lblSource.setFont(lblSource.getFont().deriveFont(Font.PLAIN));
    593             tabs.setTabComponentAt(2, lblSource);
     588            tabs.setTabComponentAt(3, lblSource);
    594589
    595590            tabs.getModel().addChangeListener(new ChangeListener() {
     
    598593                    if (!errorsTabLoaded && ((SingleSelectionModel) e.getSource()).getSelectedIndex() == 1) {
    599594                        errorsTabLoaded = true;
    600                         buildErrorsPanel(s, pErrors);
     595                        buildErrorsOrWarningPanel(s.getErrors(), pErrors);
    601596                    }
    602                     if (!sourceTabLoaded && ((SingleSelectionModel) e.getSource()).getSelectedIndex() == 2) {
     597                    if (!warningsTabLoaded && ((SingleSelectionModel) e.getSource()).getSelectedIndex() == 2) {
     598                        warningsTabLoaded = true;
     599                        buildErrorsOrWarningPanel(s.getWarnings(), pWarnings);
     600                    }
     601                    if (!sourceTabLoaded && ((SingleSelectionModel) e.getSource()).getSelectedIndex() == 3) {
    603602                        sourceTabLoaded = true;
    604603                        buildSourcePanel(s, pSource);
     
    608607            info.setContent(tabs, false);
    609608            info.showDialog();
     609        }
     610
     611        private JPanel addErrorOrWarningTab(final JTabbedPane tabs, JLabel lblInfo,
     612                Collection<?> items, String title, int pos, ImageIcon icon) {
     613            final JPanel pErrors = new JPanel(new GridBagLayout());
     614            tabs.add(title, pErrors);
     615            if (items.isEmpty()) {
     616                JLabel lblErrors = new JLabel(tr(title));
     617                lblErrors.setFont(lblInfo.getFont().deriveFont(Font.PLAIN));
     618                lblErrors.setEnabled(false);
     619                tabs.setTabComponentAt(pos, lblErrors);
     620                tabs.setEnabledAt(pos, false);
     621            } else {
     622                JLabel lblErrors = new JLabel(tr(title), icon, JLabel.HORIZONTAL);
     623                tabs.setTabComponentAt(pos, lblErrors);
     624            }
     625            return pErrors;
    610626        }
    611627
     
    658674        }
    659675
    660         private void buildErrorsPanel(StyleSource s, JPanel p) {
     676        private <T> void buildErrorsOrWarningPanel(Collection<T> items, JPanel p) {
    661677            JosmTextArea txtErrors = new JosmTextArea();
    662678            txtErrors.setFont(GuiHelper.getMonospacedFont(txtErrors));
    663679            txtErrors.setEditable(false);
    664680            p.add(new JScrollPane(txtErrors), GBC.std().fill());
    665             for (Throwable t : s.getErrors()) {
     681            for (T t : items) {
    666682                txtErrors.append(t + "\n");
    667683            }
  • trunk/src/org/openstreetmap/josm/gui/mappaint/MapPaintStyles.java

    r9299 r9334  
    1111import java.util.Arrays;
    1212import java.util.Collection;
    13 import java.util.Collections;
    1413import java.util.HashSet;
    1514import java.util.LinkedList;
     
    270269            }
    271270        }
    272         if (Main.isDebugEnabled() || !source.getErrors().isEmpty()) {
     271        if (Main.isDebugEnabled() || !source.isValid()) {
    273272            final long elapsedTime = System.currentTimeMillis() - startTime;
    274273            String message = "Initializing map style " + source.url + " completed in " + Utils.getDurationString(elapsedTime);
    275             if (!source.getErrors().isEmpty()) {
    276                 Main.warn(message + " (" + source.getErrors().size() + " errors)");
     274            if (!source.isValid()) {
     275                Main.warn(message + " (" + source.getErrors().size() + " errors, " + source.getWarnings().size() + " warnings)");
    277276            } else {
    278277                Main.debug(message);
     
    445444     * Add a new map paint style.
    446445     * @param entry map paint style
    447      * @return list of errors that occured during loading
    448      */
    449     public static Collection<Throwable> addStyle(SourceEntry entry) {
     446     * @return loaded style source, or {@code null}
     447     */
     448    public static StyleSource addStyle(SourceEntry entry) {
    450449        StyleSource source = fromSourceEntry(entry);
    451450        if (source != null) {
     
    458457                Main.map.mapView.repaint();
    459458            }
    460             return source.getErrors();
    461         }
    462         return Collections.emptyList();
     459        }
     460        return source;
    463461    }
    464462
  • trunk/src/org/openstreetmap/josm/gui/mappaint/StyleSource.java

    r9078 r9334  
    1414import java.util.List;
    1515import java.util.Map;
     16import java.util.Set;
     17import java.util.concurrent.CopyOnWriteArrayList;
     18import java.util.concurrent.CopyOnWriteArraySet;
    1619
    1720import javax.swing.ImageIcon;
     
    3336public abstract class StyleSource extends SourceEntry {
    3437
    35     private final List<Throwable> errors = new ArrayList<>();
     38    private final List<Throwable> errors = new CopyOnWriteArrayList<>();
     39    private final Set<String> warnings = new CopyOnWriteArraySet<>();
    3640    public File zipIcons;
    3741
     
    4246    private static ImageProvider defaultIconProvider;
    4347
    44     /******
    45      * The following fields is additional information found in the header
    46      * of the source file.
     48    /**
     49     * The following fields is additional information found in the header of the source file.
    4750     */
    4851    public String icon;
     
    121124    }
    122125
     126    /**
     127     * Log an error that occured with this style.
     128     * @param e error
     129     */
    123130    public void logError(Throwable e) {
    124131        errors.add(e);
    125132    }
    126133
     134    /**
     135     * Log a warning that occured with this style.
     136     * @param w warnings
     137     */
     138    public void logWarning(String w) {
     139        warnings.add(w);
     140    }
     141
     142    /**
     143     * Replies the collection of errors that occured with this style.
     144     * @return collection of errors
     145     */
    127146    public Collection<Throwable> getErrors() {
    128147        return Collections.unmodifiableCollection(errors);
     148    }
     149
     150    /**
     151     * Replies the collection of warnings that occured with this style.
     152     * @return collection of warnings
     153     */
     154    public Collection<String> getWarnings() {
     155        return Collections.unmodifiableCollection(warnings);
     156    }
     157
     158    /**
     159     * Determines if this style is valid (no error, no warning).
     160     * @return {@code true} if this style has 0 errors and 0 warnings
     161     */
     162    public boolean isValid() {
     163        return errors.isEmpty() && warnings.isEmpty();
    129164    }
    130165
     
    180215        ImageProvider i = getSourceIconProvider();
    181216        if (!getErrors().isEmpty()) {
    182             i = new ImageProvider(i).addOverlay(new ImageOverlay(new ImageProvider("dialogs/mappaint/error_small")));
     217            i = new ImageProvider(i).addOverlay(new ImageOverlay(new ImageProvider("misc", "error"), 0.5, 0.5, 1, 1));
     218        } else if (!getWarnings().isEmpty()) {
     219            i = new ImageProvider(i).addOverlay(new ImageOverlay(new ImageProvider("warning-small"), 0.5, 0.5, 1, 1));
    183220        }
    184221        return i;
     
    200237     */
    201238    public String getToolTipText() {
    202         if (errors.isEmpty())
     239        if (errors.isEmpty() && warnings.isEmpty())
    203240            return null;
    204         else
    205             return trn("There was an error when loading this style. Select ''Info'' from the right click menu for details.",
    206                     "There were {0} errors when loading this style. Select ''Info'' from the right click menu for details.",
    207                     errors.size(), errors.size());
     241        int n = errors.size() + warnings.size();
     242        return trn("There was an error when loading this style. Select ''Info'' from the right click menu for details.",
     243                "There were {0} errors when loading this style. Select ''Info'' from the right click menu for details.",
     244                n, n);
    208245    }
    209246
  • trunk/src/org/openstreetmap/josm/gui/mappaint/mapcss/MapCSSStyleSource.java

    r9278 r9334  
    535535            backgroundColorOverride = c.get("background-color", null, Color.class);
    536536            if (backgroundColorOverride != null) {
    537                 Main.warn(tr("Detected deprecated ''{0}'' in ''{1}'' which will be removed shortly. Use ''{2}'' instead.",
    538                         "canvas{background-color}", url, "fill-color"));
     537                String msg = tr("Detected deprecated ''{0}'' in ''{1}'' which will be removed shortly. Use ''{2}'' instead.",
     538                        "canvas{background-color}", url, "fill-color");
     539                logWarning(msg);
     540                Main.warn(msg);
    539541            }
    540542        }
  • trunk/src/org/openstreetmap/josm/gui/mappaint/styleelement/MapImage.java

    r9278 r9334  
    11// License: GPL. For details, see LICENSE file.
    22package org.openstreetmap.josm.gui.mappaint.styleelement;
     3
     4import static org.openstreetmap.josm.tools.I18n.tr;
    35
    46import java.awt.Graphics;
     
    102104                        synchronized (MapImage.this) {
    103105                            if (result == null) {
     106                                source.logWarning(tr("Failed to locate image ''{0}''", name));
    104107                                ImageIcon noIcon = MapPaintStyles.getNoIcon_Icon(source);
    105108                                img = noIcon == null ? null : (BufferedImage) noIcon.getImage();
  • trunk/src/org/openstreetmap/josm/gui/preferences/SourceEditor.java

    r9239 r9334  
    701701        public String link;
    702702        public String description;
     703        /** Style type: can only have one value: "xml". Used to filter out old XML styles. For MapCSS styles, the value is not set. */
     704        public String styleType;
    703705        public Integer minJosmVersion;
    704706
     
    14181420                                    }
    14191421                                }
     1422                            } else if ("style-type".equals(key)) {
     1423                                last.styleType = value;
    14201424                            }
    14211425                        }
  • trunk/test/unit/org/openstreetmap/josm/gui/layer/geoimage/ImageEntryTest.java

    r9329 r9334  
    55
    66import java.io.File;
    7 import java.io.IOException;
    87
    98import org.junit.Test;
    109import org.openstreetmap.josm.TestUtils;
    11 import org.openstreetmap.josm.io.IllegalDataException;
    12 import org.xml.sax.SAXException;
    1310
    1411/**
     
    1916    /**
    2017     * Non-regression test for ticket <a href="https://josm.openstreetmap.de/ticket/12255">#12255</a>.
    21      * @throws IllegalDataException if an error was found while parsing the data from the source
    22      * @throws IOException if any I/O error occurs
    23      * @throws SAXException if any XML error occurs
    2418     */
    2519    @Test
    26     public void testTicket12255() throws IllegalDataException, IOException, SAXException {
     20    public void testTicket12255() {
    2721        ImageEntry e = new ImageEntry(new File(TestUtils.getRegressionDataFile(12255, "G0016941.JPG")));
    2822        e.extractExif();
  • trunk/test/unit/org/openstreetmap/josm/gui/preferences/map/MapPaintPreferenceTest.java

    r9166 r9334  
    1414import org.openstreetmap.josm.JOSMFixture;
    1515import org.openstreetmap.josm.gui.mappaint.MapPaintStyles;
     16import org.openstreetmap.josm.gui.mappaint.StyleSource;
    1617import org.openstreetmap.josm.gui.mappaint.mapcss.parsergen.ParseException;
    1718import org.openstreetmap.josm.gui.preferences.SourceEditor.ExtendedSourceEntry;
     
    4142        assertFalse(sources.isEmpty());
    4243        Map<String, Collection<Throwable>> allErrors = new HashMap<>();
     44        Map<String, Collection<String>> allWarnings = new HashMap<>();
    4345        for (ExtendedSourceEntry source : sources) {
    44             System.out.println(source.url);
    45             Collection<Throwable> errors = MapPaintStyles.addStyle(source);
    46             System.out.println(errors.isEmpty() ? " => OK" : " => KO");
    47             if (!errors.isEmpty()) {
    48                 allErrors.put(source.url, errors);
     46            // Do not validate XML styles
     47            if (!"xml".equalsIgnoreCase(source.styleType)) {
     48                System.out.println(source.url);
     49                StyleSource style = MapPaintStyles.addStyle(source);
     50                System.out.println(style.isValid() ? " => OK" : " => KO");
     51                Collection<Throwable> errors = style.getErrors();
     52                Collection<String> warnings = style.getWarnings();
     53                if (!errors.isEmpty()) {
     54                    allErrors.put(source.url, errors);
     55                }
     56                if (!warnings.isEmpty()) {
     57                    allWarnings.put(source.url, warnings);
     58                }
    4959            }
    5060        }
    51         assertTrue(allErrors.toString(), allErrors.isEmpty());
     61        assertTrue(allErrors.toString()+"\n"+allWarnings.toString(), allErrors.isEmpty() && allWarnings.isEmpty());
    5262    }
    5363}
Note: See TracChangeset for help on using the changeset viewer.