Ticket #14278: 14278_--_Print_patch.patch

File 14278_--_Print_patch.patch, 20.3 KB (added by hiddewie, 4 years ago)

Patch 2

  • plugins/print/src/org/openstreetmap/josm/plugins/print/PrintableLayerManager.java

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
     
    11// License: GPL. For details, see LICENSE file.
    22package org.openstreetmap.josm.plugins.print;
    33
    4 import java.util.ArrayList;
    5 import java.util.Collections;
    6 import java.util.Comparator;
    7 import java.util.List;
    8 
    94import org.openstreetmap.josm.data.osm.DataSet;
    10 import org.openstreetmap.josm.gui.MainApplication;
    115import org.openstreetmap.josm.gui.layer.Layer;
    126import org.openstreetmap.josm.gui.layer.MainLayerManager;
    137import org.openstreetmap.josm.gui.layer.OsmDataLayer;
    148
     9import java.util.Comparator;
     10import java.util.List;
     11import java.util.stream.Collectors;
     12
    1513public class PrintableLayerManager extends MainLayerManager {
    1614
    17     private static final MainLayerManager layerManager = MainApplication.getLayerManager();
     15    private final MainLayerManager layerManager;
     16
     17    PrintableLayerManager(MainLayerManager delegate) {
     18        this.layerManager = delegate;
     19    }
    1820
    1921    @Override
    2022    public synchronized void removeActiveLayerChangeListener(ActiveLayerChangeListener listener) {
     
    4345
    4446    @Override
    4547    public synchronized List<Layer> getVisibleLayersInZOrder() {
    46         ArrayList<Layer> layers = new ArrayList<>();
    47         for (Layer l: layerManager.getLayers()) {
    48             if (l.isVisible()) {
    49                 layers.add(l);
    50             }
    51         }
    52         Collections.sort(
    53             layers,
    54             new Comparator<Layer>() {
    55                 @Override
    56                 public int compare(Layer l2, Layer l1) { // l1 and l2 swapped!
    57                     if (l1 instanceof OsmDataLayer && l2 instanceof OsmDataLayer) {
    58                         if (l1 == layerManager.getActiveLayer()) return -1;
    59                         if (l2 == layerManager.getActiveLayer()) return 1;
    60                         return Integer.valueOf(layerManager.getLayers().indexOf(l1)).
    61                                      compareTo(layerManager.getLayers().indexOf(l2));
    62                     } else
    63                         return Integer.valueOf(layerManager.getLayers().indexOf(l1)).
    64                                      compareTo(layerManager.getLayers().indexOf(l2));
    65                 }
     48        final Comparator<Layer> layerComparator = (l2, l1) -> { // l1 and l2 swapped!
     49            if (l1 instanceof OsmDataLayer && l2 instanceof OsmDataLayer) {
     50                if (l1 == layerManager.getActiveLayer()) return -1;
     51                if (l2 == layerManager.getActiveLayer()) return 1;
    6652            }
    67         );
    68         return layers;
     53            return Integer.compare(layerManager.getLayers().indexOf(l1), layerManager.getLayers().indexOf(l2));
     54        };
     55
     56        return layerManager.getLayers().stream()
     57                .filter(Layer::isVisible)
     58                .sorted(layerComparator)
     59                .collect(Collectors.toList());
    6960    }
    7061
    7162    @Override
  • plugins/print/src/org/openstreetmap/josm/plugins/print/PrintableMapView.java

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
     
    1111import java.awt.Graphics;
    1212import java.awt.Graphics2D;
    1313import java.awt.Shape;
    14 import java.awt.event.ComponentListener;
    1514import java.awt.font.FontRenderContext;
    1615import java.awt.font.GlyphVector;
    1716import java.awt.geom.AffineTransform;
    1817import java.awt.geom.Rectangle2D;
    1918import java.awt.print.PageFormat;
    2019import java.awt.print.Printable;
    21 import java.awt.print.PrinterException;
     20import java.util.Arrays;
    2221
    2322import org.openstreetmap.gui.jmapviewer.tilesources.AbstractOsmTileSource;
    2423import org.openstreetmap.josm.data.Bounds;
     
    3433/**
    3534 * The PrintableMapView class implements a "Printable" perspective on
    3635 * the main MapView.
     36 *
    3737 * @author Kai Pastor
    3838 */
    3939public class PrintableMapView extends MapView implements Printable {
     
    5858     */
    5959    public PrintableMapView() {
    6060        /* Initialize MapView with a dummy parent */
    61         super(new PrintableLayerManager(), null);
     61        super(new PrintableLayerManager(MainApplication.getLayerManager()), null);
    6262
    6363        /* Disable MapView's ComponentLister,
    6464         * as it will interfere with the main MapView. */
    65         ComponentListener[] listeners = getComponentListeners();
    66         for (int i = 0; i < listeners.length; i++) {
    67             removeComponentListener(listeners[i]);
    68         }
     65        Arrays.stream(getComponentListeners())
     66                .forEach(this::removeComponentListener);
    6967    }
    7068
    7169    /**
     
    8078
    8179    /**
    8280     * Unset the fixed map scale
    83      *
     81     * <p>
    8482     * The map scaling will be chosen automatically such that the
    8583     * main windows map view fits on the page format.
    8684     */
     
    9391     * Get the map scale that will be used for rendering
    9492     * @return the map scale that will be used for rendering
    9593     */
    96     public int getMapScale() {
     94    public double getMapScale() {
    9795        if (fixedMapScale > 0 || g2dFactor == 0.0) {
    9896            return fixedMapScale;
    9997        }
    10098
    10199        double dist100px = getDist100Pixel() / g2dFactor;
    102         int mapScale = (int) (dist100px * 72.0 / 2.54);
     100        double mapScale = (dist100px * 72.0 / 2.54);
    103101        return mapScale;
    104102    }
    105103
     
    111109     */
    112110    public void initialize(PageFormat pageFormat) {
    113111        int resolution = Config.getPref().getInt("print.resolution.dpi", PrintPlugin.DEF_RESOLUTION_DPI);
    114         g2dFactor = 72.0/resolution;
    115         setSize((int) (pageFormat.getImageableWidth()/g2dFactor), (int) (pageFormat.getImageableHeight()/g2dFactor));
     112        g2dFactor = 72.0 / resolution;
     113        setSize((int) (pageFormat.getImageableWidth() / g2dFactor), (int) (pageFormat.getImageableHeight() / g2dFactor));
    116114    }
    117115
    118116    /**
     
    123121        Dimension dim = getSize();
    124122        if (dim.width != width || dim.height != height) {
    125123            super.setSize(width, height);
    126             zoomTo(MainApplication.getMap().mapView.getRealBounds());
     124            zoomTo(MainApplication.getMap().mapView.getCenter());
    127125            rezoomToFixedScale();
    128126        }
    129127    }
     
    136134        Dimension dim = getSize();
    137135        if (dim.width != newSize.width || dim.height != newSize.height) {
    138136            super.setSize(newSize);
    139             zoomTo(MainApplication.getMap().mapView.getRealBounds());
     137            zoomTo(MainApplication.getMap().mapView.getCenter());
    140138            rezoomToFixedScale();
    141139        }
    142140    }
     
    155153
    156154    /**
    157155     * Render a page for the printer
    158      *
     156     * <p>
    159157     * Implements java.awt.print.Printable.
    160158     *
    161      * @param g the context into which the page is drawn
     159     * @param g          the context into which the page is drawn
    162160     * @param pageFormat the size and orientation of the page being drawn
    163      * @param page the zero based index of the page to be drawn
    164      *
     161     * @param page       the zero based index of the page to be drawn
    165162     * @return {@code PAGE_EXISTS} for {@code page=0} or {@code NO_SUCH_PAGE} for {@code page>0}
    166      *
    167      * @throws PrinterException thrown when the print job is terminated
    168      *
    169163     */
    170164    @Override
    171     public int print(Graphics g, PageFormat pageFormat, int page) throws
    172                                                     PrinterException {
     165    public int print(Graphics g, PageFormat pageFormat, int page) {
    173166        if (page > 0) { /* stop after first page */
    174167            return NO_SUCH_PAGE;
    175168        }
     
    178171
    179172        Graphics2D g2d = (Graphics2D) g;
    180173        g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
    181         paintMap(g2d, pageFormat);
    182         paintMapScale(g2d, pageFormat);
     174        paintMap(g2d);
     175        paintMapScale(g2d);
    183176        paintMapAttribution(g2d, pageFormat);
    184177        return PAGE_EXISTS;
    185178    }
    186179
    187180    /**
    188181     * Paint the map
    189      *
     182     * <p>
    190183     * This implementation is derived from MapView's paint and
    191184     * from other JOSM core components.
    192185     *
    193186     * @param g2d the graphics context to use for painting
    194      * @param pageFormat the size and orientation of the page being drawn
    195187     */
    196     public void paintMap(Graphics2D g2d, PageFormat pageFormat) {
    197         AffineTransform at = g2d.getTransform();
     188    public void paintMap(Graphics2D g2d) {
     189        AffineTransform originalTransform = g2d.getTransform();
    198190        g2d.scale(g2dFactor, g2dFactor);
     191        g2d.translate(getWidth() / 2, getHeight() / 2);
    199192
    200         Bounds box = getRealBounds();
     193        Bounds box = MainApplication.getMap().mapView.getRealBounds();
    201194        for (Layer l : getLayerManager().getVisibleLayersInZOrder()) {
    202195            if (l.getOpacity() < 1) {
    203196                g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, (float) l.getOpacity()));
     
    206199            g2d.setPaintMode();
    207200        }
    208201
    209         g2d.setTransform(at);
     202        g2d.setTransform(originalTransform);
    210203    }
    211204
    212205    /**
    213206     * Paint a linear scale and a lexical scale
    214      *
     207     * <p>
    215208     * This implementation is derived from JOSM's MapScaler,
    216209     * NavigatableComponent and SystemOfMeasurement.
    217210     *
    218211     * @param g2d the graphics context to use for painting
    219      * @param pageFormat the size and orientation of the page being drawn
    220212     */
    221     public void paintMapScale(Graphics2D g2d, PageFormat pageFormat) {
     213    public void paintMapScale(Graphics2D g2d) {
    222214        SystemOfMeasurement som = SystemOfMeasurement.getSystemOfMeasurement();
    223215        double dist100px = getDist100Pixel() / g2dFactor;
    224216        double dist = dist100px / som.aValue;
     
    244236
    245237        /* offset from the left paper border to the left end of the bar */
    246238        Rectangle2D bound = g2d.getFontMetrics().getStringBounds("0", g2d);
    247         int xLeft = (int) (bound.getWidth()/2);
     239        int xLeft = (int) (bound.getWidth() / 2);
    248240
    249241        /* offset from the left paper border to the right label */
    250242        String rightLabel = som.getDistText(dist100px * distScale);
    251243        bound = g2d.getFontMetrics().getStringBounds(rightLabel, g2d);
    252         int xRight = xLeft+(int) Math.max(0.95*x, x-bound.getWidth()/2);
     244        int xRight = xLeft + (int) Math.max(0.95 * x, x - bound.getWidth() / 2);
    253245
    254246        // CHECKSTYLE.OFF: SingleSpaceSeparator
    255         int h        = FONT_SIZE / 2; // raster, height of the bar
    256         int yLexical = 3 * h;         // baseline of the lexical scale
    257         int yBar     = 4 * h;         // top of the bar
    258         int yLabel   = 8 * h;         // baseline of the labels
    259         int w  = (int) (distScale * 100.0); // length of the bar
    260         int ws = (int) (distScale * 20.0);   // length of a segment
     247        int h = FONT_SIZE / 2;            // raster, height of the bar
     248        int yLexical = 3 * h;              // baseline of the lexical scale
     249        int yBar = 4 * h;                  // top of the bar
     250        int yLabel = 8 * h;                // baseline of the labels
     251        int w = (int) (distScale * 100.0); // length of the bar
     252        int ws = (int) (distScale * 20.0); // length of a segment
    261253        // CHECKSTYLE.ON: SingleSpaceSeparator
    262254
    263255        /* white background */
    264256        g2d.setColor(Color.WHITE);
    265         g2d.fillRect(xLeft-1, yBar-1, w+2, h+2);
     257        g2d.fillRect(xLeft - 1, yBar - 1, w + 2, h + 2);
    266258
    267259        /* black foreground */
    268260        g2d.setColor(Color.BLACK);
    269261        g2d.drawRect(xLeft, yBar, w, h);
    270262        g2d.fillRect(xLeft, yBar, ws, h);
    271         g2d.fillRect(xLeft+(int) (distScale * 40.0), yBar, ws, h);
    272         g2d.fillRect(xLeft+w-ws, yBar, ws, h);
     263        g2d.fillRect(xLeft + (int) (distScale * 40.0), yBar, ws, h);
     264        g2d.fillRect(xLeft + w - ws, yBar, ws, h);
    273265        g2d.setFont(labelFont);
    274266        paintText(g2d, "0", 0, yLabel);
    275267        paintText(g2d, rightLabel, xRight, yLabel);
    276268
    277269        /* lexical scale */
    278         int mapScale = getMapScale();
     270        int mapScale = (int) getMapScale();
    279271        String lexicalScale = tr("Scale") + " 1 : " + mapScale;
    280272
    281273        Font scaleFront = new Font("Arial", Font.BOLD, FONT_SIZE);
     
    288280    /**
    289281     * Paint an attribution text
    290282     *
    291      * @param g2d the graphics context to use for painting
     283     * @param g2d        the graphics context to use for painting
    292284     * @param pageFormat the size and orientation of the page being drawn
    293285     */
    294286    public void paintMapAttribution(Graphics2D g2d, PageFormat pageFormat) {
     
    309301            String line = text.substring(from, to);
    310302
    311303            Rectangle2D bound = g2d.getFontMetrics().getStringBounds(line, g2d);
    312             int x = (int) ((pageFormat.getImageableWidth() - bound.getWidth()) - FONT_SIZE/2);
     304            int x = (int) ((pageFormat.getImageableWidth() - bound.getWidth()) - FONT_SIZE / 2);
    313305
    314306            paintText(g2d, line, x, y);
    315307
     
    321313
    322314    /**
    323315     * Paint a text.
    324      *
    325      * This method will not only draw the letters but also a background which improves redability.
     316     * <p>
     317     * This method will not only draw the letters but also a background which improves readability.
    326318     *
    327      * @param g2d the graphics context to use for painting
     319     * @param g2d  the graphics context to use for painting
    328320     * @param text the text to be drawn
    329      * @param x the x coordinate
    330      * @param y the y coordinate
     321     * @param x    the x coordinate
     322     * @param y    the y coordinate
    331323     */
    332324    public void paintText(Graphics2D g2d, String text, int x, int y) {
    333325        AffineTransform ax = g2d.getTransform();
  • plugins/print/src/org/openstreetmap/josm/plugins/print/PrintDialog.java

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
     
    223223        mapView.setFixedMapScale(mapScale);
    224224        scaleModel = new SpinnerNumberModel(mapScale, 250, 5000000, 250);
    225225        final JSpinner scaleField = new JSpinner(scaleModel);
    226         scaleField.addChangeListener(new ChangeListener() {
    227             @Override
    228             public void stateChanged(ChangeEvent evt) {
    229                 SwingUtilities.invokeLater(new Runnable() {
    230                     @Override
    231                     public void run() {
    232                         try {
    233                             scaleField.commitEdit();
    234                             Config.getPref().put("print.map-scale", scaleModel.getNumber().toString());
    235                             mapView.setFixedMapScale(scaleModel.getNumber().intValue());
    236                             printPreview.repaint();
    237                         } catch (ParseException e) {
    238                             Logging.error(e);
    239                         }
    240                     }
    241                 });
     226        scaleField.addChangeListener(evt -> SwingUtilities.invokeLater(() -> {
     227            try {
     228                scaleField.commitEdit();
     229                Config.getPref().put("print.map-scale", scaleModel.getNumber().toString());
     230                mapView.setFixedMapScale(scaleModel.getNumber().intValue());
     231                printPreview.repaint();
     232            } catch (ParseException e) {
     233                Logging.error(e);
    242234            }
    243         });
     235        }));
    244236        add(scaleField, std.grid(GBC.RELATIVE, row));
    245237
    246238        row++;
     
    252244          Config.getPref().getInt("print.resolution.dpi", PrintPlugin.DEF_RESOLUTION_DPI),
    253245          30, 1200, 10);
    254246        final JSpinner resolutionField = new JSpinner(resolutionModel);
    255         resolutionField.addChangeListener(new ChangeListener() {
    256             @Override
    257             public void stateChanged(ChangeEvent evt) {
    258                 SwingUtilities.invokeLater(new Runnable() {
    259                     @Override
    260                     public void run() {
    261                         try {
    262                             resolutionField.commitEdit();
    263                             Config.getPref().put("print.resolution.dpi", resolutionModel.getNumber().toString());
    264                             printPreview.repaint();
    265                         } catch (ParseException e) {
    266                             Logging.error(e);
    267                         }
    268                     }
    269                 });
     247        resolutionField.addChangeListener(evt -> SwingUtilities.invokeLater(() -> {
     248            try {
     249                resolutionField.commitEdit();
     250                Config.getPref().put("print.resolution.dpi", resolutionModel.getNumber().toString());
     251                printPreview.repaint();
     252            } catch (ParseException e) {
     253                Logging.error(e);
    270254            }
    271         });
     255        }));
    272256        add(resolutionField, std.grid(GBC.RELATIVE, row));
    273257
    274258        row++;
     
    283267        attributionText.getDocument().addDocumentListener(new DocumentListener() {
    284268            @Override
    285269            public void insertUpdate(DocumentEvent evt) {
    286                 SwingUtilities.invokeLater(new Runnable() {
    287                     @Override
    288                     public void run() {
    289                         Config.getPref().put("print.attribution", attributionText.getText());
    290                         printPreview.repaint();
    291                     }
     270                SwingUtilities.invokeLater(() -> {
     271                    Config.getPref().put("print.attribution", attributionText.getText());
     272                    printPreview.repaint();
    292273                });
    293274            }
    294275
     
    455436        }
    456437    }
    457438
     439//    @Override
     440//    public void dispose() {
     441//        mapView.destroy();
     442//        super.dispose();
     443//    }
     444
    458445    protected void savePrintSettings() {
    459446        // Save only one printer service attribute: printer name
    460447        PrintService service = job.getPrintService();
     
    469456        }
    470457
    471458        // Save all request attributes
    472         List<String> ignoredAttributes = Arrays.asList("media-printable-area");
     459        List<String> ignoredAttributes = Collections.singletonList("media-printable-area");
    473460        List<List<String>> requestAttributes = new ArrayList<>();
    474461        for (Attribute a : attrs.toArray()) {
    475462            List<String> setting = null;
  • plugins/print/src/org/openstreetmap/josm/plugins/print/PrintPlugin.java

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
     
    4545        int pos = fileMenu.getItemCount();
    4646        do {
    4747            pos--;
    48         } while (fileMenu != null && pos > 2 && fileMenu.getItem(pos) != null);
     48        } while (pos > 2 && fileMenu.getItem(pos) != null);
    4949
    5050        if (pos > 0) {
    5151            PrintAction printAction = new PrintAction();
  • plugins/print/src/org/openstreetmap/josm/plugins/print/PrintPreview.java

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
     
    258258        AffineTransform at = g2d.getTransform();
    259259
    260260        Dimension out = getZoomedPageDimension();
    261         double scale = Math.min(
    262           out.getHeight()/format.getHeight(), out.getWidth()/format.getWidth());
     261        double scale = Math.min(out.getHeight()/format.getHeight(), out.getWidth()/format.getWidth());
    263262        double left = 0.5 * (getWidth() - scale * format.getWidth());
    264263        double top = 0.5 * (getHeight() - scale * format.getHeight());
    265264