Ticket #14278: 14278-print-1.patch
| File 14278-print-1.patch, 19.0 KB (added by , 6 years ago) |
|---|
-
src/org/openstreetmap/josm/plugins/print/PrintDialog.java
20 20 import java.lang.reflect.Method; 21 21 import java.lang.reflect.Modifier; 22 22 import java.text.ParseException; 23 import java.util.ArrayList; 24 import java.util.Arrays; 25 import java.util.Collection; 26 import java.util.Iterator; 27 import java.util.List; 28 import java.util.Locale; 23 import java.util.*; 29 24 30 25 import javax.print.PrintService; 31 26 import javax.print.PrintServiceLookup; … … 223 218 mapView.setFixedMapScale(mapScale); 224 219 scaleModel = new SpinnerNumberModel(mapScale, 250, 5000000, 250); 225 220 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 }); 221 scaleField.addChangeListener(evt -> SwingUtilities.invokeLater(() -> { 222 try { 223 scaleField.commitEdit(); 224 Config.getPref().put("print.map-scale", scaleModel.getNumber().toString()); 225 mapView.setFixedMapScale(scaleModel.getNumber().intValue()); 226 printPreview.repaint(); 227 } catch (ParseException e) { 228 Logging.error(e); 242 229 } 243 }) ;230 })); 244 231 add(scaleField, std.grid(GBC.RELATIVE, row)); 245 232 246 233 row++; … … 252 239 Config.getPref().getInt("print.resolution.dpi", PrintPlugin.DEF_RESOLUTION_DPI), 253 240 30, 1200, 10); 254 241 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 }); 242 resolutionField.addChangeListener(evt -> SwingUtilities.invokeLater(() -> { 243 try { 244 resolutionField.commitEdit(); 245 Config.getPref().put("print.resolution.dpi", resolutionModel.getNumber().toString()); 246 printPreview.repaint(); 247 } catch (ParseException e) { 248 Logging.error(e); 270 249 } 271 }) ;250 })); 272 251 add(resolutionField, std.grid(GBC.RELATIVE, row)); 273 252 274 253 row++; … … 283 262 attributionText.getDocument().addDocumentListener(new DocumentListener() { 284 263 @Override 285 264 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 } 265 SwingUtilities.invokeLater(() -> { 266 Config.getPref().put("print.attribution", attributionText.getText()); 267 printPreview.repaint(); 292 268 }); 293 269 } 294 270 … … 469 445 } 470 446 471 447 // Save all request attributes 472 List<String> ignoredAttributes = Arrays.asList("media-printable-area");448 List<String> ignoredAttributes = Collections.singletonList("media-printable-area"); 473 449 List<List<String>> requestAttributes = new ArrayList<>(); 474 450 for (Attribute a : attrs.toArray()) { 475 451 List<String> setting = null; -
src/org/openstreetmap/josm/plugins/print/PrintPlugin.java
45 45 int pos = fileMenu.getItemCount(); 46 46 do { 47 47 pos--; 48 } while ( fileMenu != null &&pos > 2 && fileMenu.getItem(pos) != null);48 } while (pos > 2 && fileMenu.getItem(pos) != null); 49 49 50 50 if (pos > 0) { 51 51 PrintAction printAction = new PrintAction(); … … 107 107 * Saves the existing value for later restorePref. 108 108 * 109 109 * @param key the preference key 110 * @param the temporary new int value110 * @param value the temporary new int value 111 111 */ 112 112 protected static void adjustPref(String key, int value) { 113 113 if (!Config.getPref().get(key).isEmpty()) { … … 122 122 * Saves the existing value for later restorePref. 123 123 * 124 124 * @param key the preference key 125 * @param the temporary new boolean value125 * @param value the temporary new boolean value 126 126 */ 127 127 protected static void adjustPref(String key, boolean value) { 128 128 if (!Config.getPref().get(key).isEmpty()) { … … 137 137 * Saves the existing value for later restorePref. 138 138 * 139 139 * @param key the preference key 140 * @param the temporary new String value140 * @param value the temporary new String value 141 141 */ 142 142 protected static void adjustPref(String key, String value) { 143 143 if (!Config.getPref().get(key).isEmpty()) { -
src/org/openstreetmap/josm/plugins/print/PrintableLayerManager.java
1 1 // License: GPL. For details, see LICENSE file. 2 2 package org.openstreetmap.josm.plugins.print; 3 3 4 import java.util.ArrayList;5 import java.util.Collections;6 4 import java.util.Comparator; 7 5 import java.util.List; 6 import java.util.stream.Collectors; 8 7 9 8 import org.openstreetmap.josm.data.osm.DataSet; 10 9 import org.openstreetmap.josm.gui.MainApplication; … … 14 13 15 14 public class PrintableLayerManager extends MainLayerManager { 16 15 17 private static final MainLayerManager layerManager = MainApplication.getLayerManager();16 private final MainLayerManager layerManager; 18 17 18 PrintableLayerManager(MainLayerManager delegate) { 19 this.layerManager = delegate; 20 } 21 19 22 @Override 20 23 public synchronized void removeActiveLayerChangeListener(ActiveLayerChangeListener listener) { 21 24 layerManager.removeActiveLayerChangeListener(listener); … … 43 46 44 47 @Override 45 48 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);49 final Comparator<Layer> layerComparator = (l2, l1) -> { // l1 and l2 swapped! 50 if (l1 instanceof OsmDataLayer && l2 instanceof OsmDataLayer) { 51 if (l1 == layerManager.getActiveLayer()) return -1; 52 if (l2 == layerManager.getActiveLayer()) return 1; 50 53 } 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 } 66 } 67 ); 68 return layers; 54 return Integer.compare(layerManager.getLayers().indexOf(l1), layerManager.getLayers().indexOf(l2)); 55 }; 56 57 return layerManager.getLayers().stream() 58 .filter(Layer::isVisible) 59 .sorted(layerComparator) 60 .collect(Collectors.toList()); 69 61 } 70 62 71 63 @Override -
src/org/openstreetmap/josm/plugins/print/PrintableMapView.java
11 11 import java.awt.Graphics; 12 12 import java.awt.Graphics2D; 13 13 import java.awt.Shape; 14 import java.awt.event.ComponentListener;15 14 import java.awt.font.FontRenderContext; 16 15 import java.awt.font.GlyphVector; 17 16 import java.awt.geom.AffineTransform; … … 18 17 import java.awt.geom.Rectangle2D; 19 18 import java.awt.print.PageFormat; 20 19 import java.awt.print.Printable; 21 import java. awt.print.PrinterException;20 import java.util.Arrays; 22 21 23 22 import org.openstreetmap.gui.jmapviewer.tilesources.AbstractOsmTileSource; 24 23 import org.openstreetmap.josm.data.Bounds; … … 30 29 import org.openstreetmap.josm.gui.layer.LayerManager.LayerOrderChangeEvent; 31 30 import org.openstreetmap.josm.gui.layer.LayerManager.LayerRemoveEvent; 32 31 import org.openstreetmap.josm.spi.preferences.Config; 32 import org.openstreetmap.josm.tools.Logging; 33 33 34 34 /** 35 35 * The PrintableMapView class implements a "Printable" perspective on 36 36 * the main MapView. 37 * 37 38 * @author Kai Pastor 38 39 */ 39 40 public class PrintableMapView extends MapView implements Printable { … … 58 59 */ 59 60 public PrintableMapView() { 60 61 /* Initialize MapView with a dummy parent */ 61 super(new PrintableLayerManager( ), null);62 super(new PrintableLayerManager(MainApplication.getLayerManager()), null); 62 63 63 64 /* Disable MapView's ComponentLister, 64 65 * 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 } 66 Arrays.stream(getComponentListeners()) 67 .forEach(this::removeComponentListener); 69 68 } 70 69 71 70 /** … … 80 79 81 80 /** 82 81 * Unset the fixed map scale 83 * 82 * <p> 84 83 * The map scaling will be chosen automatically such that the 85 84 * main windows map view fits on the page format. 86 85 */ … … 106 105 * Initialize the PrintableMapView for a particular combination of 107 106 * main MapView, PageFormat and target resolution 108 107 * 109 * @param page format the size and orientation of the page being drawn108 * @param pageFormat the size and orientation of the page being drawn 110 109 */ 111 110 public void initialize(PageFormat pageFormat) { 112 111 int resolution = Config.getPref().getInt("print.resolution.dpi", PrintPlugin.DEF_RESOLUTION_DPI); 113 g2dFactor = 72.0 /resolution;114 setSize((int) (pageFormat.getImageableWidth() /g2dFactor), (int) (pageFormat.getImageableHeight()/g2dFactor));112 g2dFactor = 72.0 / resolution; 113 setSize((int) (pageFormat.getImageableWidth() / g2dFactor), (int) (pageFormat.getImageableHeight() / g2dFactor)); 115 114 } 116 115 117 116 /** … … 154 153 155 154 /** 156 155 * Render a page for the printer 157 * 156 * <p> 158 157 * Implements java.awt.print.Printable. 159 158 * 160 * @param g the context into which the page is drawn159 * @param g the context into which the page is drawn 161 160 * @param pageFormat the size and orientation of the page being drawn 162 * @param page the zero based index of the page to be drawn 163 * 161 * @param page the zero based index of the page to be drawn 164 162 * @return PAGE_EXISTS for page==0 or NO_SUCH_PAGE for page>0 165 *166 * @throws PrinterException thrown when the print job is terminated167 *168 163 */ 169 164 @Override 170 public int print(Graphics g, PageFormat pageFormat, int page) throws 171 PrinterException { 165 public int print(Graphics g, PageFormat pageFormat, int page) { 172 166 if (page > 0) { /* stop after first page */ 173 167 return NO_SUCH_PAGE; 174 168 } 175 169 170 Logging.info("Page format " + pageFormat.getImageableX() + ", " + pageFormat.getImageableY() + ", width " + pageFormat.getWidth() +" height " + pageFormat.getHeight()); 171 176 172 initialize(pageFormat); 177 173 178 174 Graphics2D g2d = (Graphics2D) g; 179 175 g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY()); 180 paintMap(g2d , pageFormat);181 paintMapScale(g2d , pageFormat);176 paintMap(g2d); 177 paintMapScale(g2d); 182 178 paintMapAttribution(g2d, pageFormat); 183 179 return PAGE_EXISTS; 184 180 } … … 185 181 186 182 /** 187 183 * Paint the map 188 * 184 * <p> 189 185 * This implementation is derived from MapView's paint and 190 186 * from other JOSM core components. 191 187 * 192 188 * @param g2d the graphics context to use for painting 193 * @param pageFormat the size and orientation of the page being drawn194 189 */ 195 public void paintMap(Graphics2D g2d , PageFormat pageFormat) {196 AffineTransform at= g2d.getTransform();190 public void paintMap(Graphics2D g2d) { 191 AffineTransform originalTransform = g2d.getTransform(); 197 192 g2d.scale(g2dFactor, g2dFactor); 198 193 199 Bounds box = getRealBounds();194 Bounds box = MainApplication.getMap().mapView.getRealBounds(); 200 195 for (Layer l : getLayerManager().getVisibleLayersInZOrder()) { 196 paintLayer(l, g2d); 201 197 if (l.getOpacity() < 1) { 202 198 g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, (float) l.getOpacity())); 203 199 } … … 205 201 g2d.setPaintMode(); 206 202 } 207 203 208 g2d.setTransform( at);204 g2d.setTransform(originalTransform); 209 205 } 210 206 211 207 /** 212 208 * Paint a linear scale and a lexical scale 213 * 209 * <p> 214 210 * This implementation is derived from JOSM's MapScaler, 215 211 * NavigatableComponent and SystemOfMeasurement. 216 212 * 217 213 * @param g2d the graphics context to use for painting 218 * @param pageFormat the size and orientation of the page being drawn219 214 */ 220 public void paintMapScale(Graphics2D g2d , PageFormat pageFormat) {215 public void paintMapScale(Graphics2D g2d) { 221 216 SystemOfMeasurement som = SystemOfMeasurement.getSystemOfMeasurement(); 222 217 double dist100px = getDist100Pixel() / g2dFactor; 223 218 double dist = dist100px / som.aValue; … … 243 238 244 239 /* offset from the left paper border to the left end of the bar */ 245 240 Rectangle2D bound = g2d.getFontMetrics().getStringBounds("0", g2d); 246 int xLeft = (int) (bound.getWidth() /2);241 int xLeft = (int) (bound.getWidth() / 2); 247 242 248 243 /* offset from the left paper border to the right label */ 249 244 String rightLabel = som.getDistText(dist100px * distScale); 250 245 bound = g2d.getFontMetrics().getStringBounds(rightLabel, g2d); 251 int xRight = xLeft +(int) Math.max(0.95*x, x-bound.getWidth()/2);246 int xRight = xLeft + (int) Math.max(0.95 * x, x - bound.getWidth() / 2); 252 247 253 248 // CHECKSTYLE.OFF: SingleSpaceSeparator 254 int h = FONT_SIZE / 2;// raster, height of the bar255 int yLexical = 3 * h; // baseline of the lexical scale256 int yBar = 4 * h;// top of the bar257 int yLabel = 8 * h;// baseline of the labels258 int w = (int) (distScale * 100.0);// length of the bar259 int ws = (int) (distScale * 20.0); // length of a segment249 int h = FONT_SIZE / 2; // raster, height of the bar 250 int yLexical = 3 * h; // baseline of the lexical scale 251 int yBar = 4 * h; // top of the bar 252 int yLabel = 8 * h; // baseline of the labels 253 int w = (int) (distScale * 100.0); // length of the bar 254 int ws = (int) (distScale * 20.0); // length of a segment 260 255 // CHECKSTYLE.ON: SingleSpaceSeparator 261 256 262 257 /* white background */ 263 258 g2d.setColor(Color.WHITE); 264 g2d.fillRect(xLeft -1, yBar-1, w+2, h+2);259 g2d.fillRect(xLeft - 1, yBar - 1, w + 2, h + 2); 265 260 266 261 /* black foreground */ 267 262 g2d.setColor(Color.BLACK); 268 263 g2d.drawRect(xLeft, yBar, w, h); 269 264 g2d.fillRect(xLeft, yBar, ws, h); 270 g2d.fillRect(xLeft +(int) (distScale * 40.0), yBar, ws, h);271 g2d.fillRect(xLeft +w-ws, yBar, ws, h);265 g2d.fillRect(xLeft + (int) (distScale * 40.0), yBar, ws, h); 266 g2d.fillRect(xLeft + w - ws, yBar, ws, h); 272 267 g2d.setFont(labelFont); 273 268 paintText(g2d, "0", 0, yLabel); 274 269 paintText(g2d, rightLabel, xRight, yLabel); … … 287 282 /** 288 283 * Paint an attribution text 289 284 * 290 * @param g2d the graphics context to use for painting285 * @param g2d the graphics context to use for painting 291 286 * @param pageFormat the size and orientation of the page being drawn 292 287 */ 293 288 public void paintMapAttribution(Graphics2D g2d, PageFormat pageFormat) { … … 308 303 String line = text.substring(from, to); 309 304 310 305 Rectangle2D bound = g2d.getFontMetrics().getStringBounds(line, g2d); 311 int x = (int) ((pageFormat.getImageableWidth() - bound.getWidth()) - FONT_SIZE /2);306 int x = (int) ((pageFormat.getImageableWidth() - bound.getWidth()) - FONT_SIZE / 2); 312 307 313 308 paintText(g2d, line, x, y); 314 309 … … 320 315 321 316 /** 322 317 * Paint a text. 323 * 318 * <p> 324 319 * This method will not only draw the letters but also a background which improves redability. 325 320 * 326 * @param g2d the graphics context to use for painting321 * @param g2d the graphics context to use for painting 327 322 * @param text the text to be drawn 328 * @param x the x coordinate329 * @param y the y coordinate323 * @param x the x coordinate 324 * @param y the y coordinate 330 325 */ 331 326 public void paintText(Graphics2D g2d, String text, int x, int y) { 332 327 AffineTransform ax = g2d.getTransform();
