Changeset 33062 in osm for applications/editors/josm/plugins/print
- Timestamp:
- 2016-11-16T00:46:26+01:00 (8 years ago)
- Location:
- applications/editors/josm/plugins/print
- Files:
-
- 2 added
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/print/.project
r32167 r33062 16 16 </arguments> 17 17 </buildCommand> 18 <buildCommand> 19 <name>net.sf.eclipsecs.core.CheckstyleBuilder</name> 20 <arguments> 21 </arguments> 22 </buildCommand> 18 23 </buildSpec> 19 24 <natures> 20 25 <nature>org.eclipse.jdt.core.javanature</nature> 26 <nature>net.sf.eclipsecs.core.CheckstyleNature</nature> 21 27 </natures> 22 28 </projectDescription> -
applications/editors/josm/plugins/print/src/org/openstreetmap/josm/plugins/print/PrintAction.java
r30707 r33062 1 /* 2 * PrintAction.java 3 * 4 * Copyright 2011 Kai Pastor 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation; either version 2 of the License, or 9 * (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 19 * MA 02110-1301, USA. 20 * 21 * 22 */ 1 // License: GPL. For details, see LICENSE file. 23 2 package org.openstreetmap.josm.plugins.print; 24 3 … … 37 16 * The PrintAction controls basic printing of the MapView 38 17 * and takes care of reasonable temporary adjustments to the preferences. 18 * @author Kai Pastor 39 19 */ 40 20 public class PrintAction extends JosmAction implements Runnable { … … 44 24 */ 45 25 public PrintAction() { 46 super(tr("Print..."), (String) null, tr("Print the map"),26 super(tr("Print..."), (String) null, tr("Print the map"), 47 27 Shortcut.registerShortcut("system:print", tr("File: {0}", tr("Print...")), 48 28 KeyEvent.VK_P, Shortcut.CTRL), true, "print/print", true); … … 66 46 */ 67 47 @Override 68 public void run 48 public void run() { 69 49 PrintPlugin.adjustPrefs(); 70 50 PrintDialog window = new PrintDialog(Main.parent); -
applications/editors/josm/plugins/print/src/org/openstreetmap/josm/plugins/print/PrintDialog.java
r32172 r33062 1 /* 2 * PrintDialog.java 3 * 4 * Copyright 2011 Kai Pastor 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation; either version 2 of the License, or 9 * (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 19 * MA 02110-1301, USA. 20 * 21 * 22 */ 23 1 // License: GPL. For details, see LICENSE file. 24 2 package org.openstreetmap.josm.plugins.print; 25 3 … … 84 62 /** 85 63 * A print dialog with preview 64 * @author Kai Pastor 86 65 */ 87 66 public class PrintDialog extends JDialog implements ActionListener { … … 91 70 */ 92 71 protected JTextField printerField; 93 72 94 73 /** 95 74 * The media format name 96 75 */ 97 76 protected JTextField paperField; 98 77 99 78 /** 100 79 * The media orientation … … 106 85 */ 107 86 protected JCheckBox previewCheckBox; 108 87 109 88 /** 110 89 * The resolution in dpi for printing/preview 111 90 */ 112 91 protected SpinnerNumberModel resolutionModel; 113 92 114 93 /** 115 94 * The map scale 116 95 */ 117 96 protected SpinnerNumberModel scaleModel; 118 97 119 98 /** 120 99 * The page preview 121 100 */ 122 101 protected PrintPreview printPreview; 123 102 124 103 /** 125 104 * The map view for preview an printing 126 105 */ 127 106 protected PrintableMapView mapView; 128 107 129 108 /** 130 109 * The printer job 131 110 */ 132 111 protected transient PrinterJob job; 133 112 134 113 /** 135 114 * The custom printer job attributes 136 115 */ 137 116 transient PrintRequestAttributeSet attrs = new HashPrintRequestAttributeSet(); 138 139 /** 117 118 /** 140 119 * Create a new print dialog 141 * 120 * 142 121 * @param parent the parent component 143 122 */ … … 154 133 setMaximumSize(Toolkit.getDefaultToolkit().getScreenSize()); 155 134 } 156 135 157 136 /** 158 137 * Show or hide the dialog 159 * 138 * 160 139 * Set the dialog size to reasonable values. 161 * 140 * 162 141 * @param visible a flag indication the visibility of the dialog 163 142 */ … … 181 160 } else if (isShowing()) { // Avoid IllegalComponentStateException like in #8775 182 161 new WindowGeometry(this).remember(getClass().getName() + ".geometry"); 183 Main.pref.put("print.preview.enabled", previewCheckBox.isSelected());162 Main.pref.put("print.preview.enabled", previewCheckBox.isSelected()); 184 163 } 185 164 super.setVisible(visible); … … 191 170 public void build() { 192 171 setLayout(new GridBagLayout()); 193 final GBC std = GBC.std().insets(0, 5,5,0);172 final GBC std = GBC.std().insets(0, 5, 5, 0); 194 173 std.fill = GBC.HORIZONTAL; 195 final GBC twoColumns = GBC.std().insets(0,5,5,0).span(2);174 final GBC twoColumns = GBC.std().insets(0, 5, 5, 0).span(2); 196 175 twoColumns.fill = GBC.HORIZONTAL; 197 final GBC threeColumns = GBC.std().insets(0, 5,5,0).span(3);176 final GBC threeColumns = GBC.std().insets(0, 5, 5, 0).span(3); 198 177 threeColumns.fill = GBC.HORIZONTAL; 199 178 200 179 JLabel caption; 201 180 202 181 int row = 0; 203 182 caption = new JLabel(tr("Printer")+":"); … … 228 207 229 208 row++; 230 add(GBC.glue(5, 10), GBC.std(1,row).fill(GBC.VERTICAL));231 209 add(GBC.glue(5, 10), GBC.std(1, row).fill(GBC.VERTICAL)); 210 232 211 row++; 233 212 caption = new JLabel(tr("Scale")+":"); … … 247 226 try { 248 227 scaleField.commitEdit(); 249 Main.pref.put("print.map-scale", scaleModel.getNumber().toString());228 Main.pref.put("print.map-scale", scaleModel.getNumber().toString()); 250 229 mapView.setFixedMapScale(scaleModel.getNumber().intValue()); 251 230 printPreview.repaint(); … … 266 245 resolutionModel = new SpinnerNumberModel( 267 246 Main.pref.getInteger("print.resolution.dpi", PrintPlugin.DEF_RESOLUTION_DPI), 268 30, 1200, 10 247 30, 1200, 10); 269 248 final JSpinner resolutionField = new JSpinner(resolutionModel); 270 249 resolutionField.addChangeListener(new ChangeListener() { … … 276 255 try { 277 256 resolutionField.commitEdit(); 278 Main.pref.put("print.resolution.dpi", resolutionModel.getNumber().toString());257 Main.pref.put("print.resolution.dpi", resolutionModel.getNumber().toString()); 279 258 printPreview.repaint(); 280 259 } catch (ParseException pe) { … … 286 265 }); 287 266 add(resolutionField, std.grid(GBC.RELATIVE, row)); 288 267 289 268 row++; 290 269 caption = new JLabel(tr("Map information")+":"); … … 307 286 }); 308 287 } 288 309 289 @Override 310 290 public void removeUpdate(DocumentEvent evt) { 311 291 this.insertUpdate(evt); 312 292 } 293 313 294 @Override 314 295 public void changedUpdate(DocumentEvent evt) { … … 317 298 }); 318 299 JScrollPane attributionPane = new JScrollPane(attributionText); 319 add(attributionPane, GBC.std().insets(0, 5,5,0).span(3).fill(GBC.BOTH).weight(0.0,1.0).grid(2, row));320 321 row++; 322 add(GBC.glue(5, 10), GBC.std(1,row).fill(GBC.VERTICAL));323 300 add(attributionPane, GBC.std().insets(0, 5, 5, 0).span(3).fill(GBC.BOTH).weight(0.0, 1.0).grid(2, row)); 301 302 row++; 303 add(GBC.glue(5, 10), GBC.std(1, row).fill(GBC.VERTICAL)); 304 324 305 row++; 325 306 previewCheckBox = new JCheckBox(tr("Map Preview")); 326 previewCheckBox.setSelected(Main.pref.getBoolean("print.preview.enabled", false));307 previewCheckBox.setSelected(Main.pref.getBoolean("print.preview.enabled", false)); 327 308 previewCheckBox.setActionCommand("toggle-preview"); 328 309 previewCheckBox.addActionListener(this); … … 340 321 zoomOutButton.addActionListener(this); 341 322 add(zoomOutButton, threeColumns.grid(2, row)); 342 323 343 324 row++; 344 325 JButton zoomToPageButton = new JButton(tr("Zoom To Page")); … … 346 327 zoomToPageButton.addActionListener(this); 347 328 add(zoomToPageButton, threeColumns.grid(2, row)); 348 329 349 330 row++; 350 331 JButton zoomToActualSize = new JButton(tr("Zoom To Actual Size")); … … 352 333 zoomToActualSize.addActionListener(this); 353 334 add(zoomToActualSize, threeColumns.grid(2, row)); 354 335 355 336 printPreview = new PrintPreview(); 356 337 if (previewCheckBox.isSelected()) { 357 338 printPreview.setPrintable(mapView); 358 339 } 359 JScrollPane previewPane = new JScrollPane(printPreview, 340 JScrollPane previewPane = new JScrollPane(printPreview, 360 341 JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); 361 previewPane.setPreferredSize(Main.main != null ? Main.map.mapView.getSize() : new Dimension(210, 297));362 add(previewPane, GBC.std(0, 0).span(1, GBC.RELATIVE).fill().weight(5.0,5.0));342 previewPane.setPreferredSize(Main.main != null ? Main.map.mapView.getSize() : new Dimension(210, 297)); 343 add(previewPane, GBC.std(0, 0).span(1, GBC.RELATIVE).fill().weight(5.0, 5.0)); 363 344 364 345 row++; … … 372 353 cancelButton.addActionListener(this); 373 354 actionPanel.add(cancelButton); 374 add(actionPanel, GBC.std(0, row).insets(5,5,5,5).span(GBC.REMAINDER).fill(GBC.HORIZONTAL));375 } 376 355 add(actionPanel, GBC.std(0, row).insets(5, 5, 5, 5).span(GBC.REMAINDER).fill(GBC.HORIZONTAL)); 356 } 357 377 358 /** 378 359 * Update the dialog fields from the underlying model … … 386 367 } else { 387 368 printerField.setText(service.getName()); 388 if (! 369 if (!attrs.containsKey(Media.class)) { 389 370 attrs.add((Attribute) service.getDefaultAttributeValue(Media.class)); 390 } 371 } 391 372 if (attrs.containsKey(Media.class)) { 392 373 paperField.setText(attrs.get(Media.class).toString()); 393 374 } 394 375 395 if (! 376 if (!attrs.containsKey(OrientationRequested.class)) { 396 377 attrs.add((Attribute) service.getDefaultAttributeValue(OrientationRequested.class)); 397 378 } … … 400 381 } 401 382 402 if (! 383 if (!attrs.containsKey(MediaPrintableArea.class)) { 403 384 PageFormat pf = job.defaultPage(); 404 385 attrs.add(new MediaPrintableArea( 405 (float) pf.getImageableX()/72f,406 (float) pf.getImageableY()/72f,407 (float) pf.getImageableWidth()/72f,408 (float) pf.getImageableHeight()/72f,409 MediaPrintableArea.INCH) 410 } 411 386 (float) pf.getImageableX()/72f, 387 (float) pf.getImageableY()/72f, 388 (float) pf.getImageableWidth()/72f, 389 (float) pf.getImageableHeight()/72f, 390 MediaPrintableArea.INCH)); 391 } 392 412 393 PageFormat pf = job.getPageFormat(attrs); 413 394 printPreview.setPageFormat(pf); 414 395 } 415 396 } 416 397 417 398 /** 418 399 * Handle user input 419 * 400 * 420 401 * @param e an ActionEvent with one of the known commands 421 402 */ … … 470 451 471 452 protected void savePrintSettings() { 472 // Save only one printer service attribute: printer name 453 // Save only one printer service attribute: printer name 473 454 PrintService service = job.getPrintService(); 474 455 if (service != null) { … … 476 457 for (Attribute a : service.getAttributes().toArray()) { 477 458 if ("printer-name".equals(a.getName()) && a instanceof TextSyntax) { 478 serviceAttributes.add(marshallPrintSetting(a, TextSyntax.class, ((TextSyntax) a).getValue()));459 serviceAttributes.add(marshallPrintSetting(a, TextSyntax.class, ((TextSyntax) a).getValue())); 479 460 } 480 461 } 481 462 Main.pref.putArray("print.settings.service-attributes", serviceAttributes); 482 463 } 483 464 484 465 // Save all request attributes 485 466 Collection<String> ignoredAttributes = Arrays.asList("media-printable-area"); … … 488 469 Collection<String> setting = null; 489 470 if (a instanceof TextSyntax) { 490 setting = marshallPrintSetting(a, TextSyntax.class, ((TextSyntax) a).getValue());471 setting = marshallPrintSetting(a, TextSyntax.class, ((TextSyntax) a).getValue()); 491 472 } else if (a instanceof EnumSyntax) { 492 setting = marshallPrintSetting(a, EnumSyntax.class, Integer.toString(((EnumSyntax) a).getValue()));473 setting = marshallPrintSetting(a, EnumSyntax.class, Integer.toString(((EnumSyntax) a).getValue())); 493 474 } else if (a instanceof IntegerSyntax) { 494 setting = marshallPrintSetting(a, IntegerSyntax.class, Integer.toString(((IntegerSyntax) a).getValue()));475 setting = marshallPrintSetting(a, IntegerSyntax.class, Integer.toString(((IntegerSyntax) a).getValue())); 495 476 } else if (!ignoredAttributes.contains(a.getName())) { 496 477 // TODO: Add support for DateTimeSyntax, SetOfIntegerSyntax, ResolutionSyntax if needed … … 503 484 Main.pref.putArray("print.settings.request-attributes", requestAttributes); 504 485 } 505 486 506 487 protected Collection<String> marshallPrintSetting(Attribute a, Class<?> syntaxClass, String value) { 507 488 return new ArrayList<>(Arrays.asList(a.getCategory().getName(), a.getClass().getName(), syntaxClass.getName(), value)); 508 489 } 509 490 510 491 @SuppressWarnings("unchecked") 511 protected Attribute unmarshallPrintSetting(Collection<String> setting) throws 492 protected Attribute unmarshallPrintSetting(Collection<String> setting) throws 512 493 ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException { 513 494 514 495 if (setting == null || setting.size() != 4) { 515 496 throw new IllegalArgumentException("Invalid setting: "+setting); -
applications/editors/josm/plugins/print/src/org/openstreetmap/josm/plugins/print/PrintPlugin.java
r30173 r33062 1 /* 2 * PrintPlugin.java 3 * 4 * Copyright 2011 Kai Pastor 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation; either version 2 of the License, or 9 * (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 19 * MA 02110-1301, USA. 20 * 21 * 22 */ 23 1 // License: GPL. For details, see LICENSE file. 24 2 package org.openstreetmap.josm.plugins.print; 25 3 … … 34 12 35 13 /** 36 * The PrintPlugin class implements the interface JOSM needs to 14 * The PrintPlugin class implements the interface JOSM needs to 37 15 * load extension. 16 * @author Kai Pastor 38 17 */ 39 18 public class PrintPlugin extends Plugin { … … 84 63 85 64 /** 86 * Enables/disables the print action in the GUI 65 * Enables/disables the print action in the GUI 87 66 * when a MapFrame gets shown or removed. 88 67 * … … 98 77 /** 99 78 * Temporary adjust the preferences for map printing 100 * 101 * This will adjust some preferences such that elements which are 102 * not useful on paper will not be printed. This includes the 103 * following markup: downloaded area; node markers for connection, 79 * 80 * This will adjust some preferences such that elements which are 81 * not useful on paper will not be printed. This includes the 82 * following markup: downloaded area; node markers for connection, 104 83 * selected, unselected, tagged state. 105 * 84 * 106 85 * Changes will be undone by restorePrefs(). 107 86 */ … … 117 96 } 118 97 } 119 98 120 99 /** 121 100 * Adjust a single preference. 122 * 101 * 123 102 * Saves the existing value for later restorePref. 124 * 103 * 125 104 * @param key the preference key 126 105 * @param the temporary new int value … … 132 111 Main.pref.putInteger(key, value); 133 112 } 134 113 135 114 /** 136 115 * Adjust a single preference. 137 * 116 * 138 117 * Saves the existing value for later restorePref. 139 * 118 * 140 119 * @param key the preference key 141 120 * @param the temporary new boolean value … … 147 126 Main.pref.put(key, value); 148 127 } 149 128 150 129 /** 151 130 * Adjust a single preference. 152 * 131 * 153 132 * Saves the existing value for later restorePref. 154 * 133 * 155 134 * @param key the preference key 156 135 * @param the temporary new String value … … 162 141 Main.pref.put(key, value); 163 142 } 164 143 165 144 /** 166 145 * Undo temporary adjustments to the preferences made by adjustPrefs(). … … 178 157 } 179 158 } 180 159 181 160 /** 182 161 * Restore a single preference previously saved by adjustPref() 183 * 162 * 184 163 * @param key the preference key to be restored 185 164 */ -
applications/editors/josm/plugins/print/src/org/openstreetmap/josm/plugins/print/PrintPreview.java
r32169 r33062 1 /* 2 * PrintPreview.java 3 * 4 * Copyright 2011 Kai Pastor 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation; either version 2 of the License, or 9 * (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 19 * MA 02110-1301, USA. 20 * 21 * 22 */ 23 1 // License: GPL. For details, see LICENSE file. 24 2 package org.openstreetmap.josm.plugins.print; 25 3 … … 37 15 import java.awt.print.PrinterException; 38 16 17 import javax.swing.JPanel; 39 18 import javax.swing.JViewport; 40 import javax.swing.JPanel;41 19 import javax.swing.RepaintManager; 42 20 … … 45 23 /** 46 24 * A generic print preview component 47 * 48 * Given a Printable, a PageFormat and a zoom factor, this component 49 * will render a scrollable and zoomable print preview. If no Printable 50 * is defined, it will fill the printable area of the page format in 25 * 26 * Given a Printable, a PageFormat and a zoom factor, this component 27 * will render a scrollable and zoomable print preview. If no Printable 28 * is defined, it will fill the printable area of the page format in 51 29 * plain gray. 52 * 30 * 53 31 * There is a special zoom-to-page condition where the component will 54 32 * automatically adjust the zoom level such that the whole page preview 55 33 * will just fit into the size of the component. 34 * @author Kai Pastor 56 35 */ 57 36 class PrintPreview extends JPanel { 58 37 59 38 /** 60 39 * The PageFormat chosen for printing (and preview) 61 40 */ 62 41 protected transient PageFormat format; 63 42 64 43 /** 65 44 * The current zoom factor for the preview 66 45 */ 67 46 protected double zoom = 1.0; 68 69 /** 70 * If true, the preview is always zoomed such that 47 48 /** 49 * If true, the preview is always zoomed such that 71 50 * the whole page fits into the preview area. 72 51 */ 73 52 protected boolean zoomToPage = true; 74 53 75 54 /** 76 55 * When this flag is true, no painting operations will be performed. 77 56 */ 78 57 protected boolean paintingDisabled; 79 58 80 59 /** 81 60 * the printable object for rendering preview contents 82 61 */ 83 62 protected transient Printable printable; 84 85 /** 86 * Constructs a new preview component 87 */ 88 publicPrintPreview() {63 64 /** 65 * Constructs a new preview component 66 */ 67 PrintPreview() { 89 68 super(); 90 69 } … … 92 71 /** 93 72 * Constructs a new preview component for a printable 94 * 73 * 95 74 * @param p the printable object for rendering preview contents, or null 96 75 */ 97 publicPrintPreview(Printable p) {76 PrintPreview(Printable p) { 98 77 super(); 99 78 printable = p; … … 102 81 /** 103 82 * Sets (and unsets) the printable object 104 * 83 * 105 84 * @param p a printable object, or null 106 85 */ … … 109 88 repaint(); 110 89 } 111 90 112 91 /** 113 92 * Sets the page format 114 * 93 * 115 94 * @param format the PageFormat chosen for printing (and preview) 116 95 */ … … 120 99 repaint(); 121 100 } 122 101 123 102 /** 124 103 * Sets the preview zoom relativ to the actual size 125 104 * 126 * @param zoom the zoom factor 105 * @param zoom the zoom factor 127 106 */ 128 107 public void setZoom(double zoom) { 129 108 setZoom(zoom, false); 130 109 } 131 110 132 111 /** 133 112 * Zoom into the preview 134 * 113 * 135 114 * Doubles the current zoom factor, if smaller than 5.0. 136 115 */ … … 141 120 } 142 121 } 143 122 144 123 /** 145 124 * Zoom out of the preview 146 * 125 * 147 126 * Set the zoom factor to half its current value, if bigger than 0.1. 148 127 */ … … 156 135 /** 157 136 * Zoom to fit the page size 158 * 137 * 159 138 * Set the zoom factor such that the whole page fits into the preview area. 160 139 */ … … 178 157 * Sets the preview zoom relativ to the actual size 179 158 * 180 * @param zoom the zoom factor 181 * @param zoomToPage when set to true, the zoom factor will be 159 * @param zoom the zoom factor 160 * @param zoomToPage when set to true, the zoom factor will be 182 161 * adjusted on resize operations such that the whole page always 183 162 * fits to the preview area. … … 196 175 this.zoomToPage = zoomToPage; 197 176 revalidate(); // Make JScrollPane aware of the change in size 198 RepaintManager.currentManager(this).validateInvalidComponents(); 177 RepaintManager.currentManager(this).validateInvalidComponents(); 199 178 200 179 Dimension dim = getZoomedPageDimension(); 201 180 Rectangle view = getVisibleRect(); 202 181 203 view.x = (int)((0.5*oldView.width+oldView.x)/Math.max(oldView.width,oldDim.width)*Math.max(view.width,dim.width)-0.5*view.width); 204 view.y = (int)((0.5*oldView.height+oldView.y)/Math.max(oldView.height,oldDim.height)*Math.max(view.height,dim.height)-0.5*view.height); 182 view.x = (int) ((0.5*oldView.width+oldView.x) / 183 Math.max(oldView.width, oldDim.width)*Math.max(view.width, dim.width)-0.5*view.width); 184 view.y = (int) ((0.5*oldView.height+oldView.y) / 185 Math.max(oldView.height, oldDim.height)*Math.max(view.height, dim.height)-0.5*view.height); 205 186 206 187 scrollRectToVisible(view); … … 211 192 /** 212 193 * Returns the actual current zoom 213 * 194 * 214 195 * This factor might be different from the last explicitly set factor 215 196 * when zoomToPage is true. 216 * 217 * @return the zoom factor 197 * 198 * @return the zoom factor 218 199 */ 219 200 public double getZoom() { 220 201 if (format != null && (zoomToPage || zoom < 0.01)) { 221 // actually this is zoom-to-page 202 // actually this is zoom-to-page 222 203 Dimension dim = getParent().getSize(); 223 204 int resolution = Toolkit.getDefaultToolkit().getScreenResolution(); … … 228 209 return zoom; 229 210 } 230 211 231 212 /** 232 213 * Get the current dimension of the page preview 233 * 214 * 234 215 * @return dimension in pixels 235 216 */ … … 237 218 int resolution = Toolkit.getDefaultToolkit().getScreenResolution(); 238 219 double z = getZoom(); 239 int width = (int) (z * resolution * format.getWidth() / 72.0);240 int height = (int) (z * resolution * format.getHeight() / 72.0);220 int width = (int) (z * resolution * format.getWidth() / 72.0); 221 int height = (int) (z * resolution * format.getHeight() / 72.0); 241 222 return new Dimension(width, height); 242 223 } … … 244 225 /** 245 226 * Get the current preferred size of the component 246 * 247 * @return indicates a zero size when zoomToPage is active, 227 * 228 * @return indicates a zero size when zoomToPage is active, 248 229 * the zoomed page dimension otherwise 249 230 */ … … 251 232 public Dimension getPreferredSize() { 252 233 if (format == null || zoomToPage || zoom < 0.01) { 253 return new Dimension(0, 0);234 return new Dimension(0, 0); 254 235 } 255 236 return getZoomedPageDimension(); … … 258 239 /** 259 240 * Paints the component 260 * 261 * This will do nothing, not even call super.paintComponent(g), if 241 * 242 * This will do nothing, not even call super.paintComponent(g), if 262 243 * painting is disabled in this component. 263 * 244 * 264 245 * @param g a Graphics to draw to 265 246 */ … … 279 260 Dimension out = getZoomedPageDimension(); 280 261 double scale = Math.min( 281 out.getHeight()/format.getHeight() , out.getWidth()/format.getWidth());282 double left = 0.5 * ( (double)getWidth() - scale * format.getWidth());283 double top = 0.5 * ((double)getHeight() - scale * format.getHeight());284 262 out.getHeight()/format.getHeight(), out.getWidth()/format.getWidth()); 263 double left = 0.5 * (getWidth() - scale * format.getWidth()); 264 double top = 0.5 * (getHeight() - scale * format.getHeight()); 265 285 266 g2d.translate(left, top); 286 267 g2d.setColor(Color.black); … … 304 285 } else { 305 286 g2d.setColor(Color.gray); 306 g2d.fillRect(0, 0, (int) format.getWidth(), (int)format.getHeight());287 g2d.fillRect(0, 0, (int) format.getWidth(), (int) format.getHeight()); 307 288 } 308 289 -
applications/editors/josm/plugins/print/src/org/openstreetmap/josm/plugins/print/PrintableLayerManager.java
r32965 r33062 1 // License: GPL. For details, see LICENSE file. 1 2 package org.openstreetmap.josm.plugins.print; 2 3 -
applications/editors/josm/plugins/print/src/org/openstreetmap/josm/plugins/print/PrintableMapView.java
r32191 r33062 1 /* 2 * PrintableMapView.java 3 * 4 * Copyright 2011 Kai Pastor 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation; either version 2 of the License, or 9 * (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 19 * MA 02110-1301, USA. 20 * 21 * 22 */ 23 1 // License: GPL. For details, see LICENSE file. 24 2 package org.openstreetmap.josm.plugins.print; 25 3 … … 55 33 * The PrintableMapView class implements a "Printable" perspective on 56 34 * the main MapView. 57 * 35 * @author Kai Pastor 58 36 */ 59 37 public class PrintableMapView extends MapView implements Printable { 60 38 61 39 /** 62 40 * A fixed map scale if greater than zero. 63 41 */ 64 42 protected int fixedMapScale = 0; 65 43 66 44 /** 67 45 * The factor for scaling the printing graphics to the desired … … 69 47 */ 70 48 protected double g2dFactor; 71 49 72 50 /** 73 51 * The font size for text added by PrintableMapView … … 81 59 /* Initialize MapView with a dummy parent */ 82 60 super(new PrintableLayerManager(), new JPanel(), null); 83 84 /* Disable MapView's ComponentLister, 61 62 /* Disable MapView's ComponentLister, 85 63 * as it will interfere with the main MapView. */ 86 ComponentListener listeners[]= getComponentListeners();87 for (int i =0; i<listeners.length; i++) {64 ComponentListener[] listeners = getComponentListeners(); 65 for (int i = 0; i < listeners.length; i++) { 88 66 removeComponentListener(listeners[i]); 89 67 } … … 92 70 /** 93 71 * Set a fixed map scale 1 : "scale" 94 * 72 * 95 73 * @param scale the fixed map scale 96 74 */ … … 102 80 /** 103 81 * Unset the fixed map scale 104 * 82 * 105 83 * The map scaling will be chosen automatically such that the 106 84 * main windows map view fits on the page format. … … 111 89 } 112 90 113 /** 91 /** 114 92 * Get the map scale that will be used for rendering 115 */ 93 */ 116 94 public int getMapScale() { 117 95 if (fixedMapScale > 0 || g2dFactor == 0.0) { … … 127 105 * Initialize the PrintableMapView for a particular combination of 128 106 * main MapView, PageFormat and target resolution 129 * 107 * 130 108 * @param pageformat the size and orientation of the page being drawn 131 109 */ … … 133 111 int resolution = Main.pref.getInteger("print.resolution.dpi", PrintPlugin.DEF_RESOLUTION_DPI); 134 112 g2dFactor = 72.0/resolution; 135 setSize((int) (pageFormat.getImageableWidth()/g2dFactor),(int)(pageFormat.getImageableHeight()/g2dFactor));136 } 137 138 /** 139 * Resizes this component. 113 setSize((int) (pageFormat.getImageableWidth()/g2dFactor), (int) (pageFormat.getImageableHeight()/g2dFactor)); 114 } 115 116 /** 117 * Resizes this component. 140 118 */ 141 119 @Override … … 148 126 } 149 127 } 150 151 /** 152 * Resizes this component. 128 129 /** 130 * Resizes this component. 153 131 */ 154 132 @Override … … 176 154 /** 177 155 * Render a page for the printer 178 * 156 * 179 157 * Implements java.awt.print.Printable. 180 158 * 181 159 * @param g the context into which the page is drawn 182 160 * @param pageFormat the size and orientation of the page being drawn 183 * @param page the zero based index of the page to be drawn 184 * 161 * @param page the zero based index of the page to be drawn 162 * 185 163 * @return PAGE_EXISTS for page==0 or NO_SUCH_PAGE for page>0 186 * 164 * 187 165 * @throws PrinterException thrown when the print job is terminated 188 * 166 * 189 167 */ 190 168 @Override … … 197 175 initialize(pageFormat); 198 176 199 Graphics2D g2d = (Graphics2D) g;177 Graphics2D g2d = (Graphics2D) g; 200 178 g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY()); 201 179 paintMap(g2d, pageFormat); … … 204 182 return PAGE_EXISTS; 205 183 } 206 207 /** 184 185 /** 208 186 * Paint the map 209 * 187 * 210 188 * This implementation is derived from MapView's paint and 211 189 * from other JOSM core components. 212 * 190 * 213 191 * @param g2d the graphics context to use for painting 214 192 * @param pageFormat the size and orientation of the page being drawn … … 217 195 AffineTransform at = g2d.getTransform(); 218 196 g2d.scale(g2dFactor, g2dFactor); 219 197 220 198 Bounds box = getRealBounds(); 221 199 for (Layer l : getLayerManager().getVisibleLayersInZOrder()) { 222 200 if (l.getOpacity() < 1) { 223 g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, (float)l.getOpacity()));201 g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, (float) l.getOpacity())); 224 202 } 225 203 l.paint(g2d, this, box); … … 230 208 } 231 209 232 /** 210 /** 233 211 * Paint a linear scale and a lexical scale 234 * 235 * This implementation is derived from JOSM's MapScaler, 212 * 213 * This implementation is derived from JOSM's MapScaler, 236 214 * NavigatableComponent and SystemOfMeasurement. 237 215 * … … 253 231 if (distMantissa <= 2.5) { 254 232 distScale = 2.5 / distMantissa; 255 } 256 else if (distMantissa <= 4.0) { 233 } else if (distMantissa <= 4.0) { 257 234 distScale = 5.0 / distMantissa; 258 } 259 else { 235 } else { 260 236 distScale = 10.0 / distMantissa; 261 237 } … … 269 245 /* offset from the left paper border to the left end of the bar */ 270 246 Rectangle2D bound = g2d.getFontMetrics().getStringBounds("0", g2d); 271 int xLeft = (int) (bound.getWidth()/2);272 247 int xLeft = (int) (bound.getWidth()/2); 248 273 249 /* offset from the left paper border to the right label */ 274 250 String rightLabel = som.getDistText(dist100px * distScale); 275 251 bound = g2d.getFontMetrics().getStringBounds(rightLabel, g2d); 276 252 int xRight = xLeft+(int) Math.max(0.95*x, x-bound.getWidth()/2); 277 253 254 // CHECKSTYLE.OFF: SingleSpaceSeparator 278 255 int h = FONT_SIZE / 2; // raster, height of the bar 279 256 int yLexical = 3 * h; // baseline of the lexical scale 280 257 int yBar = 4 * h; // top of the bar 281 258 int yLabel = 8 * h; // baseline of the labels 282 int w = (int)(distScale * 100.0); // length of the bar 283 int ws = (int)(distScale * 20.0); // length of a segment 284 259 int w = (int) (distScale * 100.0); // length of the bar 260 int ws = (int) (distScale * 20.0); // length of a segment 261 // CHECKSTYLE.ON: SingleSpaceSeparator 262 285 263 /* white background */ 286 264 g2d.setColor(Color.WHITE); 287 265 g2d.fillRect(xLeft-1, yBar-1, w+2, h+2); 288 266 289 267 /* black foreground */ 290 268 g2d.setColor(Color.BLACK); 291 269 g2d.drawRect(xLeft, yBar, w, h); 292 270 g2d.fillRect(xLeft, yBar, ws, h); 293 g2d.fillRect(xLeft+(int) (distScale * 40.0), yBar, ws, h);271 g2d.fillRect(xLeft+(int) (distScale * 40.0), yBar, ws, h); 294 272 g2d.fillRect(xLeft+w-ws, yBar, ws, h); 295 273 g2d.setFont(labelFont); 296 274 paintText(g2d, "0", 0, yLabel); 297 275 paintText(g2d, rightLabel, xRight, yLabel); 298 276 299 277 /* lexical scale */ 300 278 int mapScale = getMapScale(); … … 304 282 g2d.setFont(scaleFront); 305 283 bound = g2d.getFontMetrics().getStringBounds(lexicalScale, g2d); 306 int xLexical = Math.max(0, xLeft + (w - (int) bound.getWidth()) / 2);284 int xLexical = Math.max(0, xLeft + (w - (int) bound.getWidth()) / 2); 307 285 paintText(g2d, lexicalScale, xLexical, yLexical); 308 286 } 309 310 /** 287 288 /** 311 289 * Paint an attribution text 312 * 290 * 313 291 * @param g2d the graphics context to use for painting 314 292 * @param pageFormat the size and orientation of the page being drawn … … 316 294 public void paintMapAttribution(Graphics2D g2d, PageFormat pageFormat) { 317 295 String text = Main.pref.get("print.attribution", AbstractOsmTileSource.DEFAULT_OSM_ATTRIBUTION); 318 296 319 297 if (text == null) { 320 298 return; 321 299 } 322 300 323 301 Font attributionFont = new Font("Arial", Font.PLAIN, FONT_SIZE * 8 / 10); 324 302 g2d.setFont(attributionFont); … … 327 305 int y = FONT_SIZE * 3 / 2; 328 306 int from = 0; 329 int to = text.indexOf( "\n", from);307 int to = text.indexOf('\n', from); 330 308 while (to >= from) { 331 309 String line = text.substring(from, to); 332 310 333 311 Rectangle2D bound = g2d.getFontMetrics().getStringBounds(line, g2d); 334 int x = (int) ((pageFormat.getImageableWidth() - bound.getWidth()) - FONT_SIZE/2);335 312 int x = (int) ((pageFormat.getImageableWidth() - bound.getWidth()) - FONT_SIZE/2); 313 336 314 paintText(g2d, line, x, y); 337 315 338 y 316 y += FONT_SIZE * 5 / 4; 339 317 from = to + 1; 340 to = text.indexOf("\n", from);341 } 342 } 343 318 to = text.indexOf('\n', from); 319 } 320 } 321 344 322 /** 345 323 * Paint a text. 346 * 324 * 347 325 * This method will not only draw the letters but also a background which improves redability. 348 * 326 * 349 327 * @param g2d the graphics context to use for painting 350 328 * @param text the text to be drawn
Note:
See TracChangeset
for help on using the changeset viewer.