Changeset 9334 in josm for trunk/src/org


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/src/org/openstreetmap/josm/gui
Files:
6 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                        }
Note: See TracChangeset for help on using the changeset viewer.