Changeset 34398 in osm
- Timestamp:
- 2018-07-06T15:34:04+02:00 (7 years ago)
- Location:
- applications/editors/josm/plugins/pdfimport/src/pdfimport
- Files:
-
- 10 added
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/pdfimport/src/pdfimport/FilePlacement.java
r32542 r34398 14 14 15 15 public class FilePlacement { 16 public Projection projection = null; 17 public double minX = 0; 18 public double maxX = 1; 19 public double minY = 0; 20 public double maxY = 1; 21 22 public double minEast = 0; 23 public double maxEast = 10000; 24 public double minNorth = 0; 25 public double maxNorth = 10000; 16 /* 17 * provide data and services to place a PDF-File to world coordinates 18 * enhanced by FilePlacement18 but kept for compatibilty to existing code 19 */ 20 protected Projection projection = null; 21 protected double minX = 0; 22 protected double maxX = 1; 23 protected double minY = 0; 24 protected double maxY = 1; 25 26 protected double minEast = 0; 27 protected double maxEast = 10000; 28 protected double minNorth = 0; 29 protected double maxNorth = 10000; 26 30 27 31 private AffineTransform transform; … … 41 45 } 42 46 43 p ublicProperties toProperties() {47 protected Properties toProperties() { 44 48 Properties p = new Properties(); 45 49 if (projection != null) { … … 59 63 } 60 64 61 p ublicvoid fromProperties(Properties p) {65 protected void fromProperties(Properties p) { 62 66 String projectionCode = p.getProperty("Projection", null); 63 67 if (projectionCode != null) { 64 projection = ProjectionInfo.getProjectionByCode(projectionCode); 68 projection = ProjectionInfo.getProjectionByCode(projectionCode); // TODO: Handle non-core Projections 65 69 } else { 66 70 projection = null; … … 78 82 } 79 83 80 pr ivatedouble parseProperty(Properties p, String name, double defaultValue) {84 protected double parseProperty(Properties p, String name, double defaultValue) { 81 85 if (!p.containsKey(name)) { 82 86 return defaultValue; … … 92 96 } 93 97 94 p ublicString prepareTransform() {98 protected String prepareTransform() { 95 99 if (this.minX > this.maxX) { 96 100 return tr("Transform error: Min X must be smaller than max"); … … 181 185 Point2D src = new Point2D.Double(); 182 186 183 p ublicBounds getWorldBounds(PathOptimizer data) {187 protected Bounds getWorldBounds(PathOptimizer data) { 184 188 LatLon min = this.tranformCoords(new Point2D.Double(data.bounds.getMinX(), data.bounds.getMinY())); 185 189 LatLon max = this.tranformCoords(new Point2D.Double(data.bounds.getMaxX(), data.bounds.getMaxY())); … … 187 191 } 188 192 189 p ublicLatLon tranformCoords(Point2D pt) {193 protected LatLon tranformCoords(Point2D pt) { 190 194 191 195 if (this.projection == null) { … … 199 203 } 200 204 201 p ublicEastNorth reverseTransform(LatLon coor) {205 protected EastNorth reverseTransform(LatLon coor) { 202 206 if (this.projection == null) { 203 207 return new EastNorth(coor.lon() * 1000, coor.lat() * 1000); -
applications/editors/josm/plugins/pdfimport/src/pdfimport/LoadPdfDialog.java
r34369 r34398 4 4 import static org.openstreetmap.josm.tools.I18n.tr; 5 5 6 import java.awt.BorderLayout;7 6 import java.awt.Color; 8 7 import java.awt.Component; … … 12 11 import java.awt.GridBagConstraints; 13 12 import java.awt.GridBagLayout; 13 import java.awt.Insets; 14 14 import java.awt.event.ActionEvent; 15 15 import java.awt.event.ActionListener; 16 import java.awt.event.FocusEvent;17 import java.awt.event.FocusListener;18 import java.awt.event.KeyEvent;19 16 import java.awt.event.WindowAdapter; 20 17 import java.awt.event.WindowEvent; 21 18 import java.io.File; 22 import java.io.FileInputStream;23 19 import java.io.FileNotFoundException; 24 import java.io.FileOutputStream;25 20 import java.io.IOException; 26 import java.util.Collection; 27 import java.util.Properties; 28 29 import javax.swing.AbstractAction; 21 30 22 import javax.swing.BorderFactory; 31 import javax.swing.BoxLayout;32 23 import javax.swing.JButton; 33 import javax.swing.JCheckBox;34 import javax.swing.JComboBox;35 24 import javax.swing.JComponent; 36 import javax.swing.JDialog;37 25 import javax.swing.JFileChooser; 38 26 import javax.swing.JFrame; … … 42 30 import javax.swing.JProgressBar; 43 31 import javax.swing.JTextField; 44 import javax.swing. KeyStroke;32 import javax.swing.SwingConstants; 45 33 import javax.swing.SwingUtilities; 46 import javax.swing.border.Border;47 34 import javax.swing.filechooser.FileFilter; 48 35 49 36 import org.openstreetmap.josm.Main; 50 import org.openstreetmap.josm.data.coor.EastNorth;51 import org.openstreetmap.josm.data.coor.LatLon;52 37 import org.openstreetmap.josm.data.osm.DataSet; 53 import org.openstreetmap.josm.data.osm.Node;54 import org.openstreetmap.josm.data.osm.OsmPrimitive;55 38 import org.openstreetmap.josm.data.osm.UploadPolicy; 56 39 import org.openstreetmap.josm.gui.MainApplication; 57 import org.openstreetmap.josm.gui.SideButton;58 40 import org.openstreetmap.josm.gui.io.importexport.OsmExporter; 59 41 import org.openstreetmap.josm.gui.layer.OsmDataLayer; 60 import org.openstreetmap.josm.gui.preferences.projection.ProjectionChoice;61 import org.openstreetmap.josm.gui.preferences.projection.ProjectionPreference;62 42 import org.openstreetmap.josm.gui.progress.ProgressMonitor; 63 43 import org.openstreetmap.josm.gui.progress.ProgressRenderer; 64 44 import org.openstreetmap.josm.gui.progress.swing.SwingRenderingProgressMonitor; 65 import org.openstreetmap.josm.gui.util.WindowGeometry;66 import org.openstreetmap.josm.tools.ImageProvider;67 45 import org.openstreetmap.josm.tools.Logging; 68 46 … … 71 49 public class LoadPdfDialog extends JFrame { 72 50 73 static class LoadProgressRenderer implements ProgressRenderer { 74 private final JProgressBar pBar; 75 private String title = ""; 76 77 LoadProgressRenderer(JProgressBar pb) { 78 this.pBar = pb; 79 this.pBar.setMinimum(0); 80 this.pBar.setValue(0); 81 this.pBar.setMaximum(1); 82 this.pBar.setString(""); 83 this.pBar.setStringPainted(true); 84 85 } 86 87 @Override 88 public void setCustomText(String message) { 89 this.pBar.setString(this.title + message); 90 } 91 92 @Override 93 public void setIndeterminate(boolean indeterminate) { 94 this.pBar.setIndeterminate(indeterminate); 95 } 96 97 @Override 98 public void setMaximum(int maximum) { 99 this.pBar.setMaximum(maximum); 100 } 101 102 @Override 103 public void setTaskTitle(String taskTitle) { 104 this.title = taskTitle; 105 this.pBar.setString(this.title); 106 } 107 108 @Override 109 public void setValue(int value) { 110 this.pBar.setValue(value); 111 } 112 113 public void finish() { 114 this.pBar.setString(tr("Finished")); 115 this.pBar.setValue(this.pBar.getMaximum()); 116 } 117 118 } 119 120 private File pdfFile; 121 private PathOptimizer data; 122 private OsmDataLayer layer; 123 124 /** 125 * Combobox with all projections available 126 */ 127 private JComboBox<ProjectionChoice> projectionCombo; 128 private JButton projectionPreferencesButton; 129 private JTextField minXField; 130 private JTextField minYField; 131 private JTextField minEastField; 132 private JTextField minNorthField; 133 private JButton getMinButton; 134 private JButton okButton; 135 private JButton cancelButton; 136 private JButton getMaxButton; 137 private JTextField maxNorthField; 138 private JTextField maxEastField; 139 private JTextField maxYField; 140 private JTextField maxXField; 141 private JButton loadFileButton; 142 private JButton showButton; 143 private JButton saveButton; 144 private JCheckBox debugModeCheck; 145 private JCheckBox mergeCloseNodesCheck; 146 private JTextField mergeCloseNodesTolerance; 147 private JCheckBox removeSmallObjectsCheck; 148 private JTextField removeSmallObjectsSize; 149 private JTextField colorFilterColor; 150 private JCheckBox colorFilterCheck; 151 private JCheckBox removeParallelSegmentsCheck; 152 private JTextField removeParallelSegmentsTolerance; 153 private JCheckBox removeLargeObjectsCheck; 154 private JTextField removeLargeObjectsSize; 155 private JProgressBar loadProgress; 156 protected OsmDataLayer newLayer; 157 158 private LoadProgressRenderer progressRenderer; 159 private JCheckBox limitPathCountCheck; 160 private JTextField limitPathCount; 161 private JCheckBox splitOnColorChangeCheck; 162 private JCheckBox splitOnShapeClosedCheck; 163 private JCheckBox splitOnSingleSegmentCheck; 164 private JCheckBox splitOnOrthogonalCheck; 165 private Border defaulfBorder = (new JTextField()).getBorder(); 166 167 public LoadPdfDialog() { 168 Logging.debug("PdfImport:LoadPdfDialog"); 169 this.buildGUI(); 170 FilePlacement pl = new FilePlacement(); 171 this.setPlacement(pl); 172 this.addListeners(); 173 this.removeLayer(); 174 if (PdfImportPlugin.Preferences.guiMode == PdfImportPlugin.GuiMode.Simple) this.loadFilePressed();; 175 } 176 177 private class CheckDouble implements FocusListener { 51 public static class MainButtons { 52 public JButton okButton; 53 public JButton cancelButton; 54 public JButton showButton; 55 public JButton saveButton; 56 public JPanel panel; 57 58 public MainButtons() { 59 } 60 61 void build(LoadPdfDialog loadPdfDialog) { 62 /* 63 * build the dialog Window from components 64 */ 65 okButton = new JButton(tr("Import")); 66 okButton.addActionListener(new ActionListener() { 67 @Override 68 public void actionPerformed(ActionEvent e) { 69 loadPdfDialog.importAction(); 70 } 71 }); 72 saveButton = new JButton(tr("Save")); 73 saveButton.addActionListener(new ActionListener() { 74 @Override 75 public void actionPerformed(ActionEvent e) { 76 loadPdfDialog.saveAction(); 77 } 78 }); 79 80 showButton = new JButton(tr("Show target")); 81 showButton.addActionListener(new ActionListener() { 82 @Override 83 public void actionPerformed(ActionEvent e) { 84 loadPdfDialog.showAction(); 85 } 86 }); 87 88 cancelButton = new JButton(tr("Cancel")); 89 cancelButton.addActionListener(new ActionListener() { 90 @Override 91 public void actionPerformed(ActionEvent e) { 92 loadPdfDialog.cancelAction(); 93 } 94 }); 95 96 panel = new JPanel(new FlowLayout()); 97 panel.add(cancelButton); 98 panel.add(showButton); 99 panel.add(okButton); 100 panel.add(saveButton); 101 showButton.setVisible(Preferences.isLegacyActions()); 102 saveButton.setVisible(Preferences.isLegacyActions()); 103 } 104 } 105 106 107 private static class Config { 108 /* 109 * encapsulate options for Path optimizer 110 * provide GUI 111 */ 112 public GuiFieldBool debugModeCheck; 113 public GuiFieldBool mergeCloseNodesCheck; 114 public GuiFieldDouble mergeCloseNodesTolerance; 115 public GuiFieldBool removeSmallObjectsCheck; 116 public GuiFieldDouble removeSmallObjectsSize; 117 public JTextField colorFilterColor; 118 public GuiFieldBool colorFilterCheck; 119 public GuiFieldBool removeParallelSegmentsCheck; 120 public GuiFieldDouble removeParallelSegmentsTolerance; 121 public GuiFieldBool removeLargeObjectsCheck; 122 public GuiFieldDouble removeLargeObjectsSize; 123 public GuiFieldBool limitPathCountCheck; 124 public GuiFieldInteger limitPathCount; 125 public GuiFieldBool splitOnColorChangeCheck; 126 public GuiFieldBool splitOnShapeClosedCheck; 127 public GuiFieldBool splitOnSingleSegmentCheck; 128 public GuiFieldBool splitOnOrthogonalCheck; 129 private JPanel panel; 130 131 public Config() { 132 build(); 133 } 134 135 public JComponent getComponent() { 136 return panel; 137 } 138 139 private void build() { 140 141 142 debugModeCheck = new GuiFieldBool(tr("Debug info"), Preferences.isDebugTags()); 143 144 mergeCloseNodesTolerance = new GuiFieldDouble(Preferences.getMergeNodesValue()); 145 mergeCloseNodesCheck = new GuiFieldBool(tr("Merge close nodes"), Preferences.isMergeNodes()); 146 mergeCloseNodesCheck.setCompanion(mergeCloseNodesTolerance); 147 148 removeSmallObjectsSize = new GuiFieldDouble(Preferences.getRemoveSmallValue()); 149 removeSmallObjectsCheck = new GuiFieldBool(tr("Remove objects smaller than"),Preferences.isRemoveSmall()); 150 removeSmallObjectsCheck.setCompanion(removeSmallObjectsSize); 151 152 removeLargeObjectsSize = new GuiFieldDouble((Preferences.getRemoveLargeValue())); 153 removeLargeObjectsCheck = new GuiFieldBool(tr("Remove objects larger than"),Preferences.isRemoveLarge()); 154 removeLargeObjectsCheck.setCompanion(removeLargeObjectsSize); 155 156 colorFilterColor = new GuiFieldHex(Preferences.getLimitColorValue()); 157 colorFilterCheck = new GuiFieldBool(tr("Only this color"), Preferences.isLimitColor()); 158 colorFilterCheck.setCompanion(colorFilterColor); 159 160 removeParallelSegmentsTolerance = new GuiFieldDouble((Preferences.getRemoveParallelValue())); 161 removeParallelSegmentsCheck = new GuiFieldBool(tr("Remove parallel lines"),Preferences.isRemoveParallel()); 162 removeParallelSegmentsCheck.setCompanion(removeParallelSegmentsTolerance); 163 164 limitPathCount = new GuiFieldInteger((Preferences.getLimitPathValue())); 165 limitPathCountCheck = new GuiFieldBool(tr("Take only first X paths"),Preferences.isLimitPath()); 166 limitPathCountCheck.setCompanion(limitPathCount); 167 168 splitOnColorChangeCheck = new GuiFieldBool(tr("Color/width change"),Preferences.isLayerAttribChange()); 169 splitOnShapeClosedCheck = new GuiFieldBool(tr("Shape closed"), Preferences.isLayerClosed()); 170 171 splitOnSingleSegmentCheck = new GuiFieldBool(tr("Single segments", Preferences.isLayerSegment())); 172 splitOnOrthogonalCheck = new GuiFieldBool(tr("Orthogonal shapes", Preferences.isLayerOrtho())); 173 174 panel = new JPanel(new GridBagLayout()); 175 panel.setBorder(BorderFactory.createTitledBorder(tr("Import settings"))); 176 177 GridBagConstraints cBasic = new GridBagConstraints(); 178 cBasic.gridx = GridBagConstraints.RELATIVE; 179 cBasic.gridy = GridBagConstraints.RELATIVE; 180 cBasic.insets = new Insets(0, 0, 0, 4); 181 cBasic.anchor = GridBagConstraints.LINE_START; 182 cBasic.fill = GridBagConstraints.HORIZONTAL; 183 cBasic.gridheight = 1; 184 cBasic.gridwidth = 1; 185 cBasic.ipadx = 0; 186 cBasic.ipady = 0; 187 cBasic.weightx = 0.0; 188 cBasic.weighty = 0.0; 189 190 GridBagConstraints cLeft = (GridBagConstraints) cBasic.clone(); 191 cLeft.gridx = 0; 192 193 GridBagConstraints cMiddle = (GridBagConstraints) cBasic.clone(); 194 cMiddle.gridx = 1; 195 cMiddle.anchor = GridBagConstraints.LINE_END; 196 197 GridBagConstraints cRight = (GridBagConstraints) cBasic.clone(); 198 cRight.gridx = 2; 199 200 panel.add(mergeCloseNodesCheck, cLeft); 201 panel.add(new JLabel(tr("Tolerance:"),SwingConstants.RIGHT), cMiddle); 202 panel.add(mergeCloseNodesTolerance, cRight); 203 204 panel.add(removeSmallObjectsCheck, cLeft); 205 panel.add(new JLabel(tr("Tolerance:"),SwingConstants.RIGHT), cMiddle); 206 panel.add(removeSmallObjectsSize, cRight); 207 208 panel.add(removeLargeObjectsCheck, cLeft); 209 panel.add(new JLabel(tr("Tolerance:"),SwingConstants.RIGHT), cMiddle); 210 panel.add(removeLargeObjectsSize, cRight); 211 212 panel.add(removeParallelSegmentsCheck, cLeft); 213 panel.add(new JLabel(tr("Max distance:"),SwingConstants.RIGHT), cMiddle); 214 panel.add(removeParallelSegmentsTolerance, cRight); 215 216 panel.add(limitPathCountCheck, cLeft); 217 panel.add(limitPathCount, cRight); 218 219 panel.add(colorFilterCheck, cLeft); 220 panel.add(colorFilterColor, cRight); 221 222 panel.add(debugModeCheck, cLeft); 223 224 cLeft.gridy = 8; panel.add(new JLabel(tr("Introduce separate layers for:")), cLeft); 225 cMiddle.gridy = 8; panel.add(splitOnShapeClosedCheck, cMiddle); 226 cRight.gridy = 8; panel.add(splitOnSingleSegmentCheck, cRight); 227 cMiddle.gridy = 9; panel.add(splitOnColorChangeCheck, cMiddle); 228 cRight.gridy = 9;panel.add(splitOnOrthogonalCheck, cRight); 229 } 230 } 231 232 static class LoadProgressRenderer implements ProgressRenderer { 233 private final JProgressBar pBar; 234 private String title = ""; 235 236 LoadProgressRenderer(JProgressBar pb) { 237 this.pBar = pb; 238 this.pBar.setMinimum(0); 239 this.pBar.setValue(0); 240 this.pBar.setMaximum(1); 241 this.pBar.setString(""); 242 this.pBar.setStringPainted(true); 243 244 } 178 245 179 246 @Override 180 public void focusGained(FocusEvent e) { 247 public void setCustomText(String message) { 248 this.pBar.setString(this.title + message); 181 249 } 182 250 183 251 @Override 184 public void focusLost(FocusEvent event) { 185 check((JTextField) event.getSource()); 186 } 187 188 public void check(JTextField t) { 252 public void setIndeterminate(boolean indeterminate) { 253 this.pBar.setIndeterminate(indeterminate); 254 } 255 256 @Override 257 public void setMaximum(int maximum) { 258 this.pBar.setMaximum(maximum); 259 } 260 261 @Override 262 public void setTaskTitle(String taskTitle) { 263 this.title = taskTitle; 264 this.pBar.setString(this.title); 265 } 266 267 @Override 268 public void setValue(int value) { 269 this.pBar.setValue(value); 270 } 271 272 public void finish() { 273 this.pBar.setString(tr("Finished")); 274 this.pBar.setValue(this.pBar.getMaximum()); 275 } 276 277 } 278 279 private File pdfFile; 280 private final FilePlacement18 placement = new FilePlacement18(); 281 282 private PathOptimizer pdfData; 283 private OsmDataLayer dataLayer; 284 285 private final JButton loadFileButton = new JButton(tr("Load preview ...")); 286 287 private final JProgressBar loadProgress = new JProgressBar(); 288 ; 289 private OsmDataLayer newLayer; 290 291 private LoadProgressRenderer progressRenderer; 292 293 public LoadPdfDialog() { 294 buildGUI(); 295 removeLayer(); 296 if (Preferences.getGuiMode() == Preferences.GuiMode.Simple) { 297 loadFileButton.setVisible(false); 298 configPanel.panel.setVisible(false); 299 actionPanel.saveButton.setVisible(false); 300 actionPanel.showButton.setVisible(false); 301 setSize(new Dimension(380, 350)); 302 loadAction(); 303 } else { 304 setSize(new Dimension(450, 600)); 305 // setSize(450, 600); 306 } 307 } 308 309 Component placementPanel = placement.getGui(); 310 MainButtons actionPanel = new MainButtons(); 311 Config configPanel = new Config(); 312 313 private void buildGUI() { 314 /* 315 * build the GUI from Components 316 */ 317 GridBagConstraints c = new GridBagConstraints(); 318 c.gridheight = 1; 319 c.gridwidth = 1; 320 c.gridx = 0; 321 c.gridy = GridBagConstraints.RELATIVE; 322 c.fill = GridBagConstraints.BOTH; 323 c.insets = new java.awt.Insets(0, 0, 0, 0); 324 325 actionPanel.build(this); 326 327 loadFileButton.addActionListener(new ActionListener() { 328 @Override 329 public void actionPerformed(ActionEvent e) { 330 loadAction(); 331 } 332 }); 333 334 progressRenderer = new LoadProgressRenderer(loadProgress); 335 336 JPanel panel = new JPanel(new GridBagLayout()); 337 338 panel.add(configPanel.getComponent(), c); 339 c.fill = GridBagConstraints.HORIZONTAL; 340 panel.add(loadFileButton, c); 341 c.fill = GridBagConstraints.BOTH; 342 panel.add(placementPanel, c); 343 panel.add(actionPanel.panel, c); 344 c.fill = GridBagConstraints.HORIZONTAL; 345 panel.add(this.loadProgress, c); 346 347 setContentPane(panel); 348 addWindowListener(new WindowAdapter() { 349 @Override 350 public void windowClosing(WindowEvent e) { 351 cancelAction(); 352 } 353 }); 354 placement.setDependsOnValid(actionPanel.okButton); 355 356 /* 357 * TODO: Make okButton to default Button of Dialog, make cancelButton to react on ESC-Key 358 */ 359 // SwingUtilities.getRootPane(panel).setDefaultButton(actionPanel.okButton); 360 } 361 362 private void loadAction() { 363 /* 364 * perform load PDF file to preview 365 */ 366 final File newFileName = this.chooseFile(); 367 368 if (newFileName == null) { 369 return; 370 } 371 Logging.debug("PdfImport: Load Preview"); 372 this.removeLayer(); 373 374 this.loadFileButton.setEnabled(false); 375 376 this.runAsBackgroundTask(new Runnable() { 377 @Override 378 public void run() { 379 // async part 380 LoadPdfDialog.this.loadProgress.setVisible(true); 381 SwingRenderingProgressMonitor monitor = new SwingRenderingProgressMonitor(progressRenderer); 382 monitor.beginTask("Loading file", 1000); 383 pdfData = loadPDF(newFileName, monitor.createSubTaskMonitor(500, false)); 384 OsmBuilder.Mode mode = LoadPdfDialog.this.configPanel.debugModeCheck.getValue() 385 ? OsmBuilder.Mode.Debug 386 : OsmBuilder.Mode.Draft; 387 388 if (pdfData != null) { 389 LoadPdfDialog.this.newLayer = LoadPdfDialog.this.makeLayer( 390 tr("PDF preview: ") + newFileName.getName(), new FilePlacement(), mode, 391 monitor.createSubTaskMonitor(500, false)); 392 } 393 394 monitor.finishTask(); 395 progressRenderer.finish(); 396 } 397 }, new ActionListener() { 398 399 @Override 400 public void actionPerformed(ActionEvent event) { 401 // sync part 402 LoadPdfDialog.this.pdfFile = newFileName; 403 if (pdfData != null) { 404 LoadPdfDialog.this.placeLayer(newLayer, new FilePlacement()); 405 try { 406 LoadPdfDialog.this.placement.load(newFileName); 407 } catch (IOException e) { 408 // Dont care 409 } finally { 410 LoadPdfDialog.this.placement.verify(); 411 } 412 LoadPdfDialog.this.newLayer = null; 413 LoadPdfDialog.this.loadFileButton.setEnabled(true); 414 LoadPdfDialog.this.placementPanel.setEnabled(true); 415 LoadPdfDialog.this.actionPanel.panel.setEnabled(true); 416 LoadPdfDialog.this.actionPanel.showButton.setEnabled(true); 417 LoadPdfDialog.this.actionPanel.saveButton.setEnabled(true); 418 LoadPdfDialog.this.actionPanel.okButton.setEnabled(true); 419 LoadPdfDialog.this.loadProgress.setVisible(false); 420 } 421 } 422 }); 423 } 424 425 private void importAction() { 426 427 if (!placement.isValid()) return; 428 429 Logging.debug("PdfImport: Import"); 430 this.removeLayer(); 431 432 this.runAsBackgroundTask(new Runnable() { 433 @Override 434 public void run() { 435 // async part 436 LoadPdfDialog.this.loadProgress.setVisible(true); 437 SwingRenderingProgressMonitor monitor = new SwingRenderingProgressMonitor(progressRenderer); 438 LoadPdfDialog.this.newLayer = LoadPdfDialog.this.makeLayer(tr("PDF: ") + pdfFile.getName(), placement, 439 OsmBuilder.Mode.Final, monitor); 440 progressRenderer.finish(); 441 } 442 }, new ActionListener() { 443 444 @Override 445 public void actionPerformed(ActionEvent e) { 446 // sync part 447 // rebuild layer with latest projection 448 LoadPdfDialog.this.placeLayer(newLayer, placement); 449 LoadPdfDialog.this.setVisible(false); 450 } 451 }); 452 } 453 454 private void saveAction() { 455 /* 456 * perform save preview layer to file 457 * TODO: is this action valueable? Can be easily performed from main menu 458 */ 459 460 if (!placement.isValid()) return; 461 462 final java.io.File file = this.chooseSaveFile(); 463 464 if (file == null) { 465 return; 466 } 467 468 this.removeLayer(); 469 470 this.runAsBackgroundTask(new Runnable() { 471 @Override 472 public void run() { 473 // async part 474 SwingRenderingProgressMonitor monitor = new SwingRenderingProgressMonitor(progressRenderer); 475 LoadPdfDialog.this.saveLayer(file, placement, monitor); 476 progressRenderer.finish(); 477 } 478 }, new ActionListener() { 479 480 @Override 481 public void actionPerformed(ActionEvent e) { 482 // sync part 483 LoadPdfDialog.this.setVisible(false); 484 } 485 }); 486 } 487 488 private void showAction() { 489 /* 490 * perform show action 491 * TODO: is this action valuable? User can do it easy from OSM Main Menu 492 */ 493 if (!placement.isValid()) return; 494 495 // zoom to new location 496 MainApplication.getMap().mapView.zoomTo(placement.getWorldBounds(pdfData)); 497 MainApplication.getMap().repaint(); 498 } 499 500 private void cancelAction() { 501 /* 502 * perform cancel action 503 */ 504 removeLayer(); 505 setVisible(false); 506 } 507 508 // Implementation methods 509 510 private static JFileChooser loadChooser = null; 511 512 private java.io.File chooseFile() { 513 // get PDF file to load 514 if (loadChooser == null) { 515 loadChooser = new JFileChooser(Preferences.getLoadDir()); 516 loadChooser.setAcceptAllFileFilterUsed(false); 517 loadChooser.setMultiSelectionEnabled(false); 518 loadChooser.setFileFilter(new FileFilter() { 519 @Override 520 public boolean accept(java.io.File pathname) { 521 return pathname.isDirectory() || pathname.getName().endsWith(".pdf"); 522 } 523 524 @Override 525 public String getDescription() { 526 return tr("PDF files"); 527 } 528 }); 529 } else { 530 loadChooser.rescanCurrentDirectory(); 531 } 532 int result = loadChooser.showDialog(this, tr("Import PDF")); 533 if (result != JFileChooser.APPROVE_OPTION) { 534 return null; 535 } else { 536 Preferences.setLoadDir(loadChooser.getSelectedFile().getParentFile().getAbsolutePath()); 537 return loadChooser.getSelectedFile(); 538 } 539 } 540 541 private java.io.File chooseSaveFile() { 542 // get file name 543 JFileChooser fc = new JFileChooser(); 544 fc.setAcceptAllFileFilterUsed(true); 545 fc.setMultiSelectionEnabled(false); 546 fc.setFileFilter(new FileFilter() { 547 @Override 548 public boolean accept(java.io.File pathname) { 549 return pathname.isDirectory() || pathname.getName().endsWith(".osm"); 550 } 551 552 @Override 553 public String getDescription() { 554 return tr("OSM files"); 555 } 556 }); 557 int result = fc.showOpenDialog(Main.parent); 558 559 if (result != JFileChooser.APPROVE_OPTION) { 560 return null; 561 } else { 562 return fc.getSelectedFile(); 563 } 564 } 565 566 private void runAsBackgroundTask(final Runnable task, final ActionListener after) { 567 /* 568 * run @task in background (asychronosly , run @after when @task has finished 569 */ 570 this.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); 571 Thread t = new Thread(new Runnable() { 572 @Override 573 public void run() { 574 task.run(); 575 576 SwingUtilities.invokeLater(new Runnable() { 577 @Override 578 public void run() { 579 setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); 580 after.actionPerformed(null); 581 } 582 }); 583 } 584 }); 585 t.start(); 586 } 587 588 private PathOptimizer loadPDF(File fileName, ProgressMonitor monitor) { 589 /* 590 * postprocess load PDF-file according to options 591 */ 592 593 monitor.beginTask("", 100); 594 monitor.setTicks(0); 595 monitor.setCustomText(tr("Preparing")); 596 597 double nodesTolerance = 0.0; 598 Color color = null; 599 int maxPaths = Integer.MAX_VALUE; 600 601 if (configPanel.mergeCloseNodesCheck.getValue() && configPanel.mergeCloseNodesTolerance.isDataValid()) { 602 nodesTolerance = configPanel.mergeCloseNodesTolerance.getValue(); 603 } 604 605 if (configPanel.colorFilterCheck.getValue()) { 189 606 try { 190 Double.valueOf(t.getText()); 191 t.setBorder((LoadPdfDialog.this.defaulfBorder)); 192 } catch (NumberFormatException e) { 193 t.setBorder(BorderFactory.createLineBorder(Color.red)); 194 } 195 } 196 } 197 198 private CheckDouble doublelistener = new CheckDouble(); 199 200 private void addListeners() { 201 202 this.maxEastField.addFocusListener(doublelistener); 203 this.maxEastField.addFocusListener(new FocusListener() { 204 @Override 205 public void focusLost(FocusEvent e) { 206 checkCoords(LoadPdfDialog.this.maxEastField, LoadPdfDialog.this.maxNorthField); 207 } 208 209 @Override 210 public void focusGained(FocusEvent e) { 211 } 212 }); 213 this.maxNorthField.addFocusListener(doublelistener); 214 215 this.minEastField.addFocusListener(doublelistener); 216 this.minEastField.addFocusListener(new FocusListener() { 217 @Override 218 public void focusLost(FocusEvent e) { 219 checkCoords(LoadPdfDialog.this.minEastField, LoadPdfDialog.this.minNorthField); 220 } 221 222 @Override 223 public void focusGained(FocusEvent e) { 224 } 225 }); 226 227 this.minNorthField.addFocusListener(doublelistener); 228 229 this.minXField.addFocusListener(doublelistener); 230 this.minXField.addFocusListener(doublelistener); 231 this.minXField.addFocusListener(new FocusListener() { 232 @Override 233 public void focusLost(FocusEvent e) { 234 checkCoords(LoadPdfDialog.this.minXField, LoadPdfDialog.this.minYField); 235 } 236 237 @Override 238 public void focusGained(FocusEvent e) { 239 } 240 }); 241 242 this.minYField.addFocusListener(doublelistener); 243 244 this.maxXField.addFocusListener(doublelistener); 245 this.maxXField.addFocusListener(new FocusListener() { 246 @Override 247 public void focusLost(FocusEvent e) { 248 checkCoords(LoadPdfDialog.this.maxXField, LoadPdfDialog.this.maxYField); 249 } 250 251 @Override 252 public void focusGained(FocusEvent e) { 253 } 254 }); 255 256 this.maxYField.addFocusListener(doublelistener); 257 258 this.projectionCombo.addActionListener(new ActionListener() { 259 @Override 260 public void actionPerformed(ActionEvent e) { 261 updateProjectionPrefButton(); 262 } 263 264 }); 265 this.projectionPreferencesButton.addActionListener(new ActionListener() { 266 @Override 267 public void actionPerformed(ActionEvent e) { 268 showProjectionPreferences(); 269 } 270 }); 271 272 this.loadFileButton.addActionListener(new ActionListener() { 273 @Override 274 public void actionPerformed(ActionEvent e) { 275 loadFilePressed(); 276 } 277 }); 278 279 this.okButton.addActionListener(new ActionListener() { 280 @Override 281 public void actionPerformed(ActionEvent e) { 282 okPressed(); 283 } 284 }); 285 286 this.saveButton.addActionListener(new ActionListener() { 287 @Override 288 public void actionPerformed(ActionEvent e) { 289 savePressed(); 290 } 291 }); 292 293 this.showButton.addActionListener(new ActionListener() { 294 @Override 295 public void actionPerformed(ActionEvent e) { 296 showPressed(); 297 } 298 }); 299 300 this.cancelButton.addActionListener(new ActionListener() { 301 @Override 302 public void actionPerformed(ActionEvent e) { 303 cancelPressed(); 304 } 305 }); 306 307 this.addWindowListener(new WindowAdapter() { 308 @Override 309 public void windowClosing(WindowEvent e) { 310 cancelPressed(); 311 } 312 }); 313 314 this.getMinButton.addActionListener(new ActionListener() { 315 @Override 316 public void actionPerformed(ActionEvent e) { 317 getMinPressed(); 318 } 319 }); 320 321 this.getMaxButton.addActionListener(new ActionListener() { 322 @Override 323 public void actionPerformed(ActionEvent e) { 324 getMaxPressed(); 325 } 326 }); 327 } 328 329 JPanel projectionPanel = null; 330 JPanel okCancelPanel = null; 331 332 private void buildGUI() { 333 GridBagConstraints c = new GridBagConstraints(); 334 c.gridheight = 1; c.gridwidth = 1; c.weightx = 1; c.weighty = 1; c.fill = GridBagConstraints.BOTH; 335 c.insets = new java.awt.Insets(0,0,0,0); 336 // c.ipadx = 1; c.ipady = 1; 337 338 this.projectionCombo = new JComboBox<>(); 339 for (ProjectionChoice p: ProjectionPreference.getProjectionChoices()) { 340 this.projectionCombo.addItem(p); 341 } 342 343 this.projectionPreferencesButton = new JButton(tr("Prefs")); 344 updateProjectionPrefButton(); 345 346 this.loadFileButton = new JButton(tr("Load preview ...")); 347 this.okButton = new JButton(tr("Import")); 348 this.saveButton = new JButton(tr("Save")); 349 this.showButton = new JButton(tr("Show target")); 350 this.cancelButton = new JButton(tr("Cancel")); 351 this.loadProgress = new JProgressBar(); 352 this.progressRenderer = new LoadProgressRenderer(this.loadProgress); 353 354 this.minXField = new JTextField(""); 355 this.minYField = new JTextField(""); 356 this.minEastField = new JTextField(""); 357 this.minNorthField = new JTextField(""); 358 this.getMinButton = new JButton(tr("Take X and Y from selected node")); 359 360 this.maxXField = new JTextField(""); 361 this.maxYField = new JTextField(""); 362 this.maxEastField = new JTextField(""); 363 this.maxNorthField = new JTextField(""); 364 this.getMaxButton = new JButton(tr("Take X and Y from selected node")); 365 366 this.debugModeCheck = new JCheckBox(tr("Debug info"),PdfImportPlugin.Preferences.DebugTags); 367 this.mergeCloseNodesCheck = new JCheckBox(tr("Merge close nodes"),PdfImportPlugin.Preferences.MergeNodes); 368 this.mergeCloseNodesTolerance = new JTextField(Double.toString(PdfImportPlugin.Preferences.MergeNodesValue)); 369 370 this.removeSmallObjectsCheck = new JCheckBox(tr("Remove objects smaller than"),PdfImportPlugin.Preferences.RemoveSmall); 371 this.removeSmallObjectsSize = new JTextField(Double.toString(PdfImportPlugin.Preferences.RemoveSmallValue)); 372 373 this.removeLargeObjectsCheck = new JCheckBox(tr("Remove objects larger than"),PdfImportPlugin.Preferences.RemoveLarge); 374 this.removeLargeObjectsSize = new JTextField(Double.toString(PdfImportPlugin.Preferences.RemoveLargeValue)); 375 376 377 this.colorFilterCheck = new JCheckBox(tr("Only this color"),PdfImportPlugin.Preferences.LimitColor); 378 this.colorFilterColor = new JTextField(PdfImportPlugin.Preferences.LimitColorValue); 379 380 this.removeParallelSegmentsCheck = new JCheckBox(tr("Remove parallel lines"),PdfImportPlugin.Preferences.RemoveParallel); 381 this.removeParallelSegmentsTolerance = new JTextField(Double.toString(PdfImportPlugin.Preferences.RemoveParallelValue)); 382 383 this.limitPathCountCheck = new JCheckBox(tr("Take only first X paths"),PdfImportPlugin.Preferences.LimitPath); 384 this.limitPathCount = new JTextField(Integer.toString(PdfImportPlugin.Preferences.LimitPathValue)); 385 386 this.splitOnColorChangeCheck = new JCheckBox(tr("Color/width change"),PdfImportPlugin.Preferences.LayerAttribChange); 387 this.splitOnShapeClosedCheck = new JCheckBox(tr("Shape closed"),PdfImportPlugin.Preferences.LayerClosed); 388 this.splitOnSingleSegmentCheck = new JCheckBox(tr("Single segments",PdfImportPlugin.Preferences.LayerSegment)); 389 this.splitOnOrthogonalCheck = new JCheckBox(tr("Orthogonal shapes",PdfImportPlugin.Preferences.LayerOrtho)); 390 391 JPanel configPanel = new JPanel(new GridBagLayout()); 392 configPanel.setBorder(BorderFactory.createTitledBorder(tr("Import settings"))); 393 c.gridx = 0; c.gridy = 0; c.gridwidth = 1; 394 configPanel.add(this.mergeCloseNodesCheck, c); 395 c.gridx = 1; c.gridy = 0; c.gridwidth = 1; c.anchor = GridBagConstraints.NORTHEAST; 396 configPanel.add(new JLabel("Tolerance :"), c); 397 c.gridx = 2; c.gridy = 0; c.gridwidth = 1; c.anchor = GridBagConstraints.NORTHWEST; 398 configPanel.add(this.mergeCloseNodesTolerance, c); 399 400 c.gridx = 0; c.gridy = 1; c.gridwidth = 1; 401 configPanel.add(this.removeSmallObjectsCheck, c); 402 c.gridx = 1; c.gridy = 1; c.gridwidth = 1; c.anchor = GridBagConstraints.NORTHEAST; 403 configPanel.add(new JLabel("Tolerance :"), c); 404 c.gridx = 2; c.gridy = 1; c.gridwidth = 1; c.anchor = GridBagConstraints.NORTHWEST; 405 configPanel.add(this.removeSmallObjectsSize, c); 406 407 c.gridx = 0; c.gridy = 2; c.gridwidth = 1; 408 configPanel.add(this.removeLargeObjectsCheck, c); 409 c.gridx = 1; c.gridy = 2; c.gridwidth = 1; c.anchor = GridBagConstraints.NORTHEAST; 410 configPanel.add(new JLabel("Tolerance :"), c); 411 c.gridx = 2; c.gridy = 2; c.gridwidth = 1; c.anchor = GridBagConstraints.NORTHWEST; 412 configPanel.add(this.removeLargeObjectsSize, c); 413 414 c.gridx = 0; c.gridy = 3; c.gridwidth = 1; 415 configPanel.add(this.removeParallelSegmentsCheck, c); 416 c.gridx = 1; c.gridy = 3; c.gridwidth = 1; c.anchor = GridBagConstraints.NORTHEAST; 417 configPanel.add(new JLabel("Max distance :"), c); 418 c.gridx = 2; c.gridy = 3; c.gridwidth = 1; c.anchor = GridBagConstraints.NORTHWEST; 419 configPanel.add(this.removeParallelSegmentsTolerance, c); 420 421 422 c.gridx = 0; c.gridy = 4; c.gridwidth = 2; 423 configPanel.add(this.limitPathCountCheck, c); 424 c.gridx = 2; c.gridy = 4; c.gridwidth = 1; 425 configPanel.add(this.limitPathCount, c); 426 427 c.gridx = 0; c.gridy = 5; c.gridwidth = 1; 428 configPanel.add(this.colorFilterCheck, c); 429 c.gridx = 2; c.gridy = 5; c.gridwidth = 1; 430 configPanel.add(this.colorFilterColor, c); 431 432 c.gridx = 0; c.gridy = 6; c.gridwidth = 2; 433 configPanel.add(this.debugModeCheck, c); 434 435 436 c.gridx = 0; c.gridy = 7; c.gridwidth = 1; 437 configPanel.add(new JLabel(tr("Introduce separate layers for:")), c); 438 c.gridx = 1; c.gridy = 7; c.gridwidth = 1; 439 configPanel.add(this.splitOnShapeClosedCheck, c); 440 c.gridx = 2; c.gridy = 7; c.gridwidth = 1; 441 configPanel.add(this.splitOnSingleSegmentCheck, c); 442 c.gridx = 1; c.gridy = 8; c.gridwidth = 1; 443 configPanel.add(this.splitOnColorChangeCheck, c); 444 c.gridx = 2; c.gridy = 8; c.gridwidth = 1; 445 configPanel.add(this.splitOnOrthogonalCheck, c); 446 if (PdfImportPlugin.Preferences.guiMode == PdfImportPlugin.GuiMode.Simple) configPanel.setVisible(false); 447 448 projectionPanel = new JPanel(new GridBagLayout()); 449 projectionPanel.setBorder(BorderFactory.createTitledBorder(tr("Bind to coordinates"))); 450 // projectionPanel.setVisible(false); 451 452 JPanel projectionSubPanel = new JPanel(); 453 projectionSubPanel.setLayout(new BoxLayout(projectionSubPanel, BoxLayout.X_AXIS)); 454 455 projectionSubPanel.add(new JLabel(tr("Projection:"))); 456 projectionSubPanel.add(this.projectionCombo); 457 projectionSubPanel.add(this.projectionPreferencesButton); 458 this.projectionPreferencesButton.setEnabled(false); // ToDo: disabled do avoid bugs 459 460 c.gridx = 0; c.gridy = 0; c.gridwidth = 3; 461 projectionPanel.add(projectionSubPanel, c); 462 463 c.gridx = 0; c.gridy = 1; c.gridwidth = 2; 464 projectionPanel.add(new JLabel(tr("Bottom left (min) corner:")), c); 465 c.gridx = 0; c.gridy = 2; c.gridwidth = 1; 466 projectionPanel.add(new JLabel(tr("PDF X and Y")), c); 467 c.gridx = 1; c.gridy = 2; c.gridwidth = 1; 468 projectionPanel.add(new JLabel(tr("East and North")), c); 469 c.gridx = 0; c.gridy = 3; c.gridwidth = 1; 470 projectionPanel.add(this.minXField, c); 471 c.gridx = 0; c.gridy = 4; c.gridwidth = 1; 472 projectionPanel.add(this.minYField, c); 473 c.gridx = 1; c.gridy = 3; c.gridwidth = 1; 474 projectionPanel.add(this.minEastField, c); 475 c.gridx = 1; c.gridy = 4; c.gridwidth = 1; 476 projectionPanel.add(this.minNorthField, c); 477 c.gridx = 0; c.gridy = 5; c.gridwidth = 1; 478 projectionPanel.add(this.getMinButton, c); 479 480 481 c.gridx = 0; c.gridy = 6; c.gridwidth = 2; 482 projectionPanel.add(new JLabel(tr("Top right (max) corner:")), c); 483 c.gridx = 0; c.gridy = 7; c.gridwidth = 1; 484 projectionPanel.add(new JLabel(tr("PDF X and Y")), c); 485 c.gridx = 1; c.gridy = 7; c.gridwidth = 1; 486 projectionPanel.add(new JLabel(tr("East and North")), c); 487 c.gridx = 0; c.gridy = 8; c.gridwidth = 1; 488 projectionPanel.add(this.maxXField, c); 489 c.gridx = 0; c.gridy = 9; c.gridwidth = 1; 490 projectionPanel.add(this.maxYField, c); 491 c.gridx = 1; c.gridy = 8; c.gridwidth = 1; 492 projectionPanel.add(this.maxEastField, c); 493 c.gridx = 1; c.gridy = 9; c.gridwidth = 1; 494 projectionPanel.add(this.maxNorthField, c); 495 c.gridx = 0; c.gridy = 10; c.gridwidth = 1; 496 projectionPanel.add(this.getMaxButton, c); 497 498 okCancelPanel = new JPanel(new FlowLayout()); 499 // okCancelPanel.setVisible(false); 500 okCancelPanel.add(this.cancelButton); 501 okCancelPanel.add(this.showButton); 502 okCancelPanel.add(this.okButton); 503 okCancelPanel.add(this.saveButton); 504 505 JPanel panel = new JPanel(new GridBagLayout()); 506 c.gridx = 0; c.gridy = 0; c.gridwidth = 1; 507 panel.add(configPanel, c); 508 c.gridx = 0; c.gridy = 1; c.gridwidth = 1; c.fill = GridBagConstraints.HORIZONTAL; 509 panel.add(loadFileButton, c); 510 c.gridx = 0; c.gridy = 2; c.gridwidth = 1; 511 panel.add(projectionPanel, c); 512 c.gridx = 0; c.gridy = 3; c.gridwidth = 1; 513 panel.add(okCancelPanel, c); 514 c.gridx = 0; c.gridy = 4; c.gridwidth = 1; c.anchor = GridBagConstraints.NORTH; c.fill = GridBagConstraints.HORIZONTAL; 515 panel.add(this.loadProgress, c); 516 517 this.setSize(450, 550); 518 this.setContentPane(panel); 519 if (PdfImportPlugin.Preferences.guiMode == PdfImportPlugin.GuiMode.Simple) { 520 loadFileButton.setVisible(false); 521 configPanel.setVisible(false); 522 saveButton.setVisible(false); 523 showButton.setVisible(false); 524 } 525 } 526 527 private class ProjectionSubPrefsDialog extends JDialog { 528 private final ProjectionChoice projPref; 529 private OKAction actOK; 530 private CancelAction actCancel; 531 private JPanel projPrefPanel; 532 533 ProjectionSubPrefsDialog(Component parent, ProjectionChoice pr) { 534 super(JOptionPane.getFrameForComponent(parent), ModalityType.DOCUMENT_MODAL); 535 536 projPref = pr; 537 538 setTitle(tr("Projection Preferences")); 539 setDefaultCloseOperation(DISPOSE_ON_CLOSE); 540 541 build(); 542 } 543 544 protected void makeButtonRespondToEnter(SideButton btn) { 545 btn.setFocusable(true); 546 btn.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "enter"); 547 btn.getActionMap().put("enter", btn.getAction()); 548 } 549 550 protected JPanel buildInputForm() { 551 return projPref.getPreferencePanel(null); 552 } 553 554 protected JPanel buildButtonRow() { 555 JPanel pnl = new JPanel(new FlowLayout()); 556 557 actOK = new OKAction(); 558 actCancel = new CancelAction(); 559 560 SideButton btn; 561 pnl.add(btn = new SideButton(actOK)); 562 makeButtonRespondToEnter(btn); 563 pnl.add(btn = new SideButton(actCancel)); 564 makeButtonRespondToEnter(btn); 565 return pnl; 566 } 567 568 protected void build() { 569 projPrefPanel = buildInputForm(); 570 getContentPane().setLayout(new BorderLayout()); 571 getContentPane().add(projPrefPanel, BorderLayout.CENTER); 572 getContentPane().add(buildButtonRow(), BorderLayout.SOUTH); 573 pack(); 574 575 // make dialog respond to ESCAPE 576 getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put( 577 KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "escape"); 578 getRootPane().getActionMap().put("escape", actCancel); 579 } 580 581 class OKAction extends AbstractAction { 582 OKAction() { 583 putValue(NAME, tr("OK")); 584 putValue(SHORT_DESCRIPTION, tr("Close the dialog and apply projection preferences")); 585 putValue(SMALL_ICON, ImageProvider.get("ok")); 586 } 587 588 @Override 589 public void actionPerformed(ActionEvent e) { 590 projPref.setPreferences(projPref.getPreferences(projPrefPanel)); 591 setVisible(false); 592 } 593 } 594 595 class CancelAction extends AbstractAction { 596 CancelAction() { 597 putValue(NAME, tr("Cancel")); 598 putValue(SHORT_DESCRIPTION, tr("Close the dialog, discard projection preference changes")); 599 putValue(SMALL_ICON, ImageProvider.get("cancel")); 600 } 601 602 @Override 603 public void actionPerformed(ActionEvent e) { 604 setVisible(false); 605 } 606 } 607 608 @Override 609 public void setVisible(boolean visible) { 610 if (visible) { 611 new WindowGeometry( 612 getClass().getName() + ".geometry", 613 WindowGeometry.centerOnScreen(new Dimension(400, 300))).applySafe(this); 614 } else if (isShowing()) { // Avoid IllegalComponentStateException like in #8775 615 new WindowGeometry(this).remember(getClass().getName() + ".geometry"); 616 } 617 super.setVisible(visible); 618 } 619 } 620 621 private void updateProjectionPrefButton() { 622 // ProjectionChoice proj = (ProjectionChoice) projectionCombo.getSelectedItem(); 623 624 //TODO 625 // Enable/disable pref button 626 // if(!(proj instanceof ProjectionSubPrefs)) { 627 // projectionPreferencesButton.setEnabled(false); 628 // } else { 629 projectionPreferencesButton.setEnabled(true); 630 // } 631 } 632 633 private void checkCoords(JTextField x, JTextField y) { 634 int splitpos = 0; 635 String eastVal = x.getText().trim(); 636 if ((splitpos = eastVal.indexOf(';')) >= 0) { 637 // Split a coordinate into its parts for easy of data entry 638 y.setText(eastVal.substring(splitpos + 1).trim()); 639 x.setText(eastVal.substring(0, splitpos).trim()); 640 } 641 doublelistener.check(x); 642 doublelistener.check(y); 643 } 644 645 private void showProjectionPreferences() { 646 ProjectionChoice proj = (ProjectionChoice) projectionCombo.getSelectedItem(); 647 648 ProjectionSubPrefsDialog dlg = new ProjectionSubPrefsDialog(this, proj); 649 dlg.setVisible(true); 650 651 } 652 653 private void loadFilePressed() { 654 final File newFileName = this.chooseFile(); 655 656 if (newFileName == null) { 657 return; 658 } 659 Logging.debug("PdfImport: Load Preview"); 660 this.removeLayer(); 661 662 this.loadFileButton.setEnabled(false); 663 this.loadFileButton.setText(tr("Loading...")); 664 665 this.runAsBackgroundTask( 666 new Runnable() { 667 @Override 668 public void run() { 669 //async part 670 LoadPdfDialog.this.loadProgress.setVisible(true); 671 SwingRenderingProgressMonitor monitor = new SwingRenderingProgressMonitor(progressRenderer); 672 monitor.beginTask("Loading file", 1000); 673 data = loadPDF(newFileName, monitor.createSubTaskMonitor(500, false)); 674 OsmBuilder.Mode mode = LoadPdfDialog.this.debugModeCheck.isSelected() ? OsmBuilder.Mode.Debug : OsmBuilder.Mode.Draft; 675 // OsmBuilder.Mode mode = Logging.isDebugEnabled() ? OsmBuilder.Mode.Debug : OsmBuilder.Mode.Draft; 676 677 if (data != null) { 678 LoadPdfDialog.this.newLayer = LoadPdfDialog.this.makeLayer( 679 tr("PDF preview: ") + newFileName.getName(), new FilePlacement(), mode, monitor.createSubTaskMonitor(500, false)); 680 } 681 682 monitor.finishTask(); 683 progressRenderer.finish(); 684 } 685 }, 686 new ActionListener() { 687 688 @Override 689 public void actionPerformed(ActionEvent e) { 690 //sync part 691 if (data != null) { 692 LoadPdfDialog.this.placeLayer(newLayer, new FilePlacement()); 693 pdfFile = newFileName; 694 newLayer = null; 695 LoadPdfDialog.this.loadFileButton.setText(tr("Preview") + ": " + LoadPdfDialog.this.pdfFile.getName()); 696 LoadPdfDialog.this.loadFileButton.setEnabled(true); 697 FilePlacement placement = LoadPdfDialog.this.loadPlacement(pdfFile); 698 LoadPdfDialog.this.setPlacement(placement); 699 } 700 } 701 }); 702 } 703 704 private FilePlacement preparePlacement() { 705 FilePlacement placement = this.parsePlacement(); 706 if (placement == null) { 707 return null; 708 } 709 710 String transformError = placement.prepareTransform(); 711 if (transformError != null) { 712 JOptionPane.showMessageDialog(Main.parent, transformError); 713 return null; 714 } 715 716 this.savePlacement(placement); 717 718 return placement; 719 } 720 721 private void okPressed() { 722 723 final FilePlacement placement = preparePlacement(); 724 if (placement == null) { 725 return; 726 } 727 Logging.debug("PdfImport: Import"); 728 this.removeLayer(); 729 730 this.runAsBackgroundTask( 731 new Runnable() { 732 @Override 733 public void run() { 734 //async part 735 LoadPdfDialog.this.loadProgress.setVisible(true); 736 SwingRenderingProgressMonitor monitor = new SwingRenderingProgressMonitor(progressRenderer); 737 LoadPdfDialog.this.newLayer = LoadPdfDialog.this.makeLayer( 738 tr("PDF: ") + pdfFile.getName(), placement, OsmBuilder.Mode.Final, monitor); 739 progressRenderer.finish(); 740 } 741 }, 742 new ActionListener() { 743 744 @Override 745 public void actionPerformed(ActionEvent e) { 746 //sync part 747 //rebuild layer with latest projection 748 LoadPdfDialog.this.placeLayer(newLayer, placement); 749 LoadPdfDialog.this.setVisible(false); 750 } 751 }); 752 } 753 754 private void savePressed() { 755 756 final FilePlacement placement = preparePlacement(); 757 if (placement == null) { 758 return; 759 } 760 761 final java.io.File file = this.chooseSaveFile(); 762 763 if (file == null) { 764 return; 765 } 766 767 this.removeLayer(); 768 769 this.runAsBackgroundTask( 770 new Runnable() { 771 @Override 772 public void run() { 773 //async part 774 SwingRenderingProgressMonitor monitor = new SwingRenderingProgressMonitor(progressRenderer); 775 LoadPdfDialog.this.saveLayer(file, placement, monitor); 776 progressRenderer.finish(); 777 } 778 }, 779 new ActionListener() { 780 781 @Override 782 public void actionPerformed(ActionEvent e) { 783 //sync part 784 LoadPdfDialog.this.setVisible(false); 785 } 786 }); 787 } 788 789 private void showPressed() { 790 791 FilePlacement placement = preparePlacement(); 792 if (placement == null) { 793 return; 794 } 795 796 //zoom to new location 797 MainApplication.getMap().mapView.zoomTo(placement.getWorldBounds(this.data)); 798 MainApplication.getMap().repaint(); 799 } 800 801 private void cancelPressed() { 802 this.removeLayer(); 803 this.setVisible(false); 804 } 805 806 private void getMinPressed() { 807 EastNorth en = this.getSelectedCoor(); 808 809 if (en != null) { 810 this.minXField.setText(Double.toString(en.east())); 811 this.minYField.setText(Double.toString(en.north())); 812 } 813 } 814 815 private void getMaxPressed() { 816 EastNorth en = this.getSelectedCoor(); 817 818 if (en != null) { 819 this.maxXField.setText(Double.toString(en.east())); 820 this.maxYField.setText(Double.toString(en.north())); 821 } 822 } 823 824 // Implementation methods 825 826 private EastNorth getSelectedCoor() { 827 Collection<OsmPrimitive> selected = MainApplication.getLayerManager().getEditDataSet().getSelected(); 828 829 if (selected.size() != 1 || !(selected.iterator().next() instanceof Node)) { 830 JOptionPane.showMessageDialog(Main.parent, tr("Please select exactly one node.")); 831 return null; 832 } 833 834 LatLon ll = ((Node) selected.iterator().next()).getCoor(); 835 FilePlacement pl = new FilePlacement(); 836 return pl.reverseTransform(ll); 837 } 838 839 private static JFileChooser loadChooser = null; 840 841 private java.io.File chooseFile() { 842 //get PDF file to load 843 if (loadChooser == null) { 844 loadChooser = new JFileChooser(PdfImportPlugin.Preferences.LoadDir); 845 loadChooser.setAcceptAllFileFilterUsed(false); 846 loadChooser.setMultiSelectionEnabled(false); 847 loadChooser.setFileFilter(new FileFilter() { 848 @Override 849 public boolean accept(java.io.File pathname) { 850 return pathname.isDirectory() || pathname.getName().endsWith(".pdf"); 851 } 852 853 @Override 854 public String getDescription() { 855 return tr("PDF files"); 856 } 857 }); 858 } else { 859 loadChooser.rescanCurrentDirectory(); 860 } 861 int result = loadChooser.showDialog(this, tr("Preview")); 862 if (result != JFileChooser.APPROVE_OPTION) { 863 return null; 864 } else { 865 return loadChooser.getSelectedFile(); 866 } 867 } 868 869 private java.io.File chooseSaveFile() { 870 //get file name 871 JFileChooser fc = new JFileChooser(); 872 fc.setAcceptAllFileFilterUsed(true); 873 fc.setMultiSelectionEnabled(false); 874 fc.setFileFilter(new FileFilter() { 875 @Override 876 public boolean accept(java.io.File pathname) { 877 return pathname.isDirectory() || pathname.getName().endsWith(".osm"); 878 } 879 880 @Override 881 public String getDescription() { 882 return tr("OSM files"); 883 } 884 }); 885 int result = fc.showOpenDialog(Main.parent); 886 887 if (result != JFileChooser.APPROVE_OPTION) { 888 return null; 889 } else { 890 return fc.getSelectedFile(); 891 } 892 } 893 894 private void runAsBackgroundTask(final Runnable task, final ActionListener after) { 895 this.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); 896 Thread t = new Thread(new Runnable() { 897 @Override 898 public void run() { 899 task.run(); 900 901 SwingUtilities.invokeLater(new Runnable() { 902 @Override 903 public void run() { 904 setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); 905 after.actionPerformed(null); 906 } 907 }); 908 } 909 }); 910 t.start(); 911 } 912 913 private PathOptimizer loadPDF(File fileName, ProgressMonitor monitor) { 914 915 monitor.beginTask("", 100); 916 monitor.setTicks(0); 917 monitor.setCustomText(tr("Preparing")); 918 919 double nodesTolerance = 0.0; 920 Color color = null; 921 int maxPaths = Integer.MAX_VALUE; 922 923 if (this.mergeCloseNodesCheck.isSelected()) { 924 try { 925 nodesTolerance = Double.parseDouble(this.mergeCloseNodesTolerance.getText()); 926 } catch (Exception e) { 927 JOptionPane 928 .showMessageDialog( 929 Main.parent, 930 tr("Tolerance is not a number")); 931 return null; 932 } 933 } 934 935 if (this.colorFilterCheck.isSelected()) { 936 try { 937 String colString = this.colorFilterColor.getText().replace("#", ""); 938 color = new Color(Integer.parseInt(colString, 16)); 939 } catch (Exception e) { 940 JOptionPane 941 .showMessageDialog( 942 Main.parent, 943 tr("Could not parse color")); 944 return null; 945 } 946 } 947 948 if (this.limitPathCountCheck.isSelected()) { 949 try { 950 maxPaths = Integer.parseInt(this.limitPathCount.getText()); 951 } catch (Exception e) { 952 JOptionPane 953 .showMessageDialog( 954 Main.parent, 955 tr("Could not parse max path count")); 956 return null; 957 } 958 } 959 960 961 monitor.setTicks(10); 962 monitor.setCustomText(tr("Parsing file")); 963 964 PathOptimizer data = new PathOptimizer(nodesTolerance, color, this.splitOnColorChangeCheck.isSelected()); 965 966 try { 967 PdfBoxParser parser = new PdfBoxParser(data); 968 parser.parse(fileName, maxPaths, monitor.createSubTaskMonitor(80, false)); 969 970 } catch (FileNotFoundException e1) { 971 JOptionPane 972 .showMessageDialog( 973 Main.parent, 974 tr("File not found.")); 975 return null; 976 } catch (Exception e) { 977 e.printStackTrace(); 978 JOptionPane 979 .showMessageDialog( 980 Main.parent, 981 tr("Error while parsing: {0}", e.getMessage())); 982 return null; 983 } 984 985 monitor.setTicks(80); 986 987 if (this.removeParallelSegmentsCheck.isSelected()) { 988 try { 989 double tolerance = Double.parseDouble(this.removeParallelSegmentsTolerance.getText()); 990 monitor.setCustomText(tr("Removing parallel segments")); 991 data.removeParallelLines(tolerance); 992 } catch (Exception e) { 993 JOptionPane 994 .showMessageDialog( 995 Main.parent, 996 tr("Max distance is not a number")); 997 return null; 998 } 999 } 1000 1001 if (nodesTolerance > 0.0) { 1002 monitor.setTicks(83); 1003 monitor.setCustomText(tr("Joining nodes")); 1004 data.mergeNodes(); 1005 } 1006 1007 monitor.setTicks(85); 1008 monitor.setCustomText(tr("Joining adjacent segments")); 1009 data.mergeSegments(); 1010 1011 if (this.removeSmallObjectsCheck.isSelected()) { 1012 try { 1013 double tolerance = Double.parseDouble(this.removeSmallObjectsSize.getText()); 1014 monitor.setTicks(90); 1015 monitor.setCustomText(tr("Removing small objects")); 1016 1017 data.removeSmallObjects(tolerance); 1018 } catch (Exception e) { 1019 JOptionPane 1020 .showMessageDialog( 1021 Main.parent, 1022 tr("Tolerance is not a number")); 1023 return null; 1024 } 1025 } 1026 1027 if (this.removeLargeObjectsCheck.isSelected()) { 1028 try { 1029 double tolerance = Double.parseDouble(this.removeLargeObjectsSize.getText()); 1030 monitor.setTicks(90); 1031 monitor.setCustomText(tr("Removing large objects")); 1032 data.removeLargeObjects(tolerance); 1033 } catch (Exception e) { 1034 JOptionPane 1035 .showMessageDialog( 1036 Main.parent, 1037 tr("Tolerance is not a number")); 1038 return null; 1039 } 1040 } 1041 1042 monitor.setTicks(95); 1043 monitor.setCustomText(tr("Finalizing layers")); 1044 data.splitLayersByPathKind( 1045 this.splitOnShapeClosedCheck.isSelected(), 1046 this.splitOnSingleSegmentCheck.isSelected(), 1047 this.splitOnOrthogonalCheck.isSelected()); 1048 data.finish(); 1049 1050 monitor.finishTask(); 1051 return data; 1052 } 1053 1054 private FilePlacement parsePlacement() { 1055 ProjectionChoice selectedProjection = (ProjectionChoice) this.projectionCombo.getSelectedItem(); 1056 1057 if (selectedProjection == null) { 1058 JOptionPane.showMessageDialog(Main.parent, tr("Please set a projection.")); 1059 return null; 1060 } 1061 1062 FilePlacement placement = new FilePlacement(); 1063 1064 placement.projection = selectedProjection.getProjection(); 1065 1066 try { 1067 placement.setPdfBounds( 1068 Double.parseDouble(this.minXField.getText()), 1069 Double.parseDouble(this.minYField.getText()), 1070 Double.parseDouble(this.maxXField.getText()), 1071 Double.parseDouble(this.maxYField.getText())); 1072 placement.setEastNorthBounds( 1073 Double.parseDouble(this.minEastField.getText()), 1074 Double.parseDouble(this.minNorthField.getText()), 1075 Double.parseDouble(this.maxEastField.getText()), 1076 Double.parseDouble(this.maxNorthField.getText())); 1077 } catch (Exception e) { 1078 JOptionPane.showMessageDialog(Main.parent, tr("Could not parse numbers. Please check.")); 1079 return null; 1080 } 1081 1082 return placement; 1083 } 1084 1085 private void setPlacement(FilePlacement placement) { 1086 1087 if (placement == null) { 1088 //use default values. 1089 placement = new FilePlacement(); 1090 } 1091 1092 if (placement.projection != null) { 1093 String projectionCode = placement.projection.toCode(); 1094 BIG_LOOP: 1095 for (ProjectionChoice projectionChoice: ProjectionPreference.getProjectionChoices()) { 1096 for (String code: projectionChoice.allCodes()) { 1097 if (code.equals(projectionCode)) { 1098 projectionChoice.getPreferencesFromCode(projectionCode); 1099 this.projectionCombo.setSelectedItem(projectionChoice); 1100 break BIG_LOOP; 1101 } 1102 } 1103 } 1104 } 1105 1106 this.minXField.setText(Double.toString(placement.minX)); 1107 this.maxXField.setText(Double.toString(placement.maxX)); 1108 this.minYField.setText(Double.toString(placement.minY)); 1109 this.maxYField.setText(Double.toString(placement.maxY)); 1110 this.minEastField.setText(Double.toString(placement.minEast)); 1111 this.maxEastField.setText(Double.toString(placement.maxEast)); 1112 this.minNorthField.setText(Double.toString(placement.minNorth)); 1113 this.maxNorthField.setText(Double.toString(placement.maxNorth)); 1114 projectionPanel.setEnabled(true); 1115 okCancelPanel.setEnabled(true);; 1116 this.showButton.setEnabled(true); 1117 this.saveButton.setEnabled(true); 1118 this.okButton.setEnabled(true); 1119 this.getMaxButton.setEnabled(true); 1120 this.getMinButton.setEnabled(true); 1121 } 1122 1123 private FilePlacement loadPlacement(File BaseFile) { 1124 FilePlacement pl = null; 1125 //load saved transformation 1126 File propertiesFile = new File(BaseFile.getAbsoluteFile() + ".placement"); 1127 try { 1128 1129 if (propertiesFile.exists()) { 1130 pl = new FilePlacement(); 1131 Properties p = new Properties(); 1132 p.load(new FileInputStream(propertiesFile)); 1133 pl.fromProperties(p); 1134 } 1135 } catch (Exception e) { 1136 pl = null; 1137 e.printStackTrace(); 1138 } 1139 1140 return pl; 1141 } 1142 1143 private void savePlacement(FilePlacement pl) { 1144 //load saved transformation 1145 File propertiesFile = new File(pdfFile.getAbsoluteFile()+ ".placement"); 1146 try { 1147 Properties p = pl.toProperties(); 1148 p.store(new FileOutputStream(propertiesFile), "PDF file placement on OSM"); 1149 } catch (Exception e) { 1150 e.printStackTrace(); 1151 } 1152 } 1153 1154 private OsmDataLayer makeLayer(String name, FilePlacement placement, OsmBuilder.Mode mode, ProgressMonitor monitor) { 1155 monitor.beginTask(tr("Building JOSM layer"), 100); 1156 OsmBuilder builder = new OsmBuilder(placement); 1157 DataSet data = builder.build(this.data.getLayers(), mode, monitor.createSubTaskMonitor(50, false)); 1158 monitor.setTicks(50); 1159 monitor.setCustomText(tr("Postprocessing layer")); 1160 OsmDataLayer result = new OsmDataLayer(data, name, null); 1161 data.setUploadPolicy(UploadPolicy.BLOCKED); 1162 result.setUploadDiscouraged(true); 1163 result.setBackgroundLayer(true); 1164 result.onPostLoadFromFile(); 1165 1166 monitor.finishTask(); 1167 return result; 1168 } 1169 1170 private void placeLayer(OsmDataLayer _layer, FilePlacement placement) { 1171 this.removeLayer(); 1172 this.layer = _layer; 1173 MainApplication.getLayerManager().addLayer(this.layer); 1174 MainApplication.getMap().mapView.zoomTo(placement.getWorldBounds(this.data)); 1175 } 1176 1177 private void removeLayer() { 1178 if (this.layer != null) { 1179 MainApplication.getLayerManager().removeLayer(this.layer); 1180 this.layer.data.clear(); //saves memory 1181 this.layer = null; 1182 } 1183 // projectionPanel.setVisible(false); 1184 // okCancelPanel.setVisible(false); 1185 // loadProgress.setVisible(false); 1186 this.showButton.setEnabled(false); 1187 this.saveButton.setEnabled(false); 1188 this.okButton.setEnabled(false); 1189 this.getMaxButton.setEnabled(false); 1190 this.getMinButton.setEnabled(false); 1191 1192 } 1193 1194 private void saveLayer(java.io.File file, FilePlacement placement, ProgressMonitor monitor) { 1195 monitor.beginTask(tr("Saving to file."), 1000); 1196 1197 OsmBuilder builder = new OsmBuilder(placement); 1198 DataSet data = builder.build(this.data.getLayers(), OsmBuilder.Mode.Final, monitor.createSubTaskMonitor(500, false)); 1199 OsmDataLayer layer = new OsmDataLayer(data, file.getName(), file); 1200 1201 monitor.setCustomText(tr(" Writing to file")); 1202 monitor.setTicks(500); 1203 1204 OsmExporter exporter = new OsmExporter(); 1205 1206 try { 1207 exporter.exportData(file, layer); 1208 } catch (IOException e) { 1209 Logging.error(e); 1210 } 1211 1212 monitor.finishTask(); 1213 } 607 String colString = this.configPanel.colorFilterColor.getText().replace("#", ""); 608 color = new Color(Integer.parseInt(colString, 16)); 609 } catch (Exception e) { 610 JOptionPane.showMessageDialog(Main.parent, tr("Could not parse color")); 611 return null; 612 } 613 } 614 615 if (configPanel.limitPathCountCheck.getValue() && configPanel.limitPathCount.isDataValid()) { 616 maxPaths = configPanel.limitPathCount.getValue(); 617 } 618 619 monitor.setTicks(10); 620 monitor.setCustomText(tr("Parsing file")); 621 622 PathOptimizer data = new PathOptimizer(nodesTolerance, color, configPanel.splitOnColorChangeCheck.getValue()); 623 624 try { 625 PdfBoxParser parser = new PdfBoxParser(data); 626 parser.parse(fileName, maxPaths, monitor.createSubTaskMonitor(80, false)); 627 628 } catch (FileNotFoundException e1) { 629 JOptionPane.showMessageDialog(Main.parent, tr("File not found.")); 630 return null; 631 } catch (Exception e) { 632 e.printStackTrace(); 633 JOptionPane.showMessageDialog(Main.parent, tr("Error while parsing: {0}", e.getMessage())); 634 return null; 635 } 636 637 monitor.setTicks(80); 638 639 if (configPanel.removeParallelSegmentsCheck.getValue() && configPanel.removeParallelSegmentsTolerance.isDataValid()) { 640 double tolerance = configPanel.removeParallelSegmentsTolerance.getValue(); 641 monitor.setCustomText(tr("Removing parallel segments")); 642 } 643 644 if (nodesTolerance > 0.0) { 645 monitor.setTicks(83); 646 monitor.setCustomText(tr("Joining nodes")); 647 data.mergeNodes(); 648 } 649 650 monitor.setTicks(85); 651 monitor.setCustomText(tr("Joining adjacent segments")); 652 data.mergeSegments(); 653 654 if (configPanel.removeSmallObjectsCheck.getValue() && configPanel.removeSmallObjectsSize.isDataValid()) { 655 double tolerance = configPanel.removeSmallObjectsSize.getValue(); 656 monitor.setTicks(90); 657 monitor.setCustomText(tr("Removing small objects")); 658 659 data.removeSmallObjects(tolerance); 660 661 } 662 663 if (configPanel.removeLargeObjectsCheck.getValue() && configPanel.removeLargeObjectsSize.isDataValid()) { 664 double tolerance = configPanel.removeLargeObjectsSize.getValue(); 665 monitor.setTicks(90); 666 monitor.setCustomText(tr("Removing large objects")); 667 data.removeLargeObjects(tolerance); 668 } 669 670 monitor.setTicks(95); 671 monitor.setCustomText(tr("Finalizing layers")); 672 data.splitLayersByPathKind(configPanel.splitOnShapeClosedCheck.getValue(), 673 configPanel.splitOnSingleSegmentCheck.getValue(), 674 configPanel.splitOnOrthogonalCheck.getValue()); 675 data.finish(); 676 677 monitor.finishTask(); 678 return data; 679 } 680 681 private OsmDataLayer makeLayer(String name, FilePlacement placement, OsmBuilder.Mode mode, 682 ProgressMonitor monitor) { 683 /* 684 * create a layer from data 685 */ 686 monitor.beginTask(tr("Building JOSM layer"), 100); 687 OsmBuilder builder = new OsmBuilder(placement); 688 DataSet data = builder.build(pdfData.getLayers(), mode, monitor.createSubTaskMonitor(50, false)); 689 data.setUploadPolicy(UploadPolicy.BLOCKED); 690 monitor.setTicks(50); 691 monitor.setCustomText(tr("Postprocessing layer")); 692 OsmDataLayer result = new OsmDataLayer(data, name, null); 693 result.setUploadDiscouraged(true); 694 result.setBackgroundLayer(true); 695 result.onPostLoadFromFile(); 696 697 monitor.finishTask(); 698 return result; 699 } 700 701 private void placeLayer(OsmDataLayer _layer, FilePlacement placement) { 702 /* 703 * 704 */ 705 removeLayer(); 706 dataLayer = _layer; 707 MainApplication.getLayerManager().addLayer(dataLayer); 708 MainApplication.getMap().mapView.zoomTo(placement.getWorldBounds(pdfData)); 709 } 710 711 private void removeLayer() { 712 /* 713 * remove preview layer 714 */ 715 if (dataLayer != null) { 716 MainApplication.getLayerManager().removeLayer(dataLayer); 717 dataLayer.data.clear(); // saves memory 718 dataLayer = null; 719 } 720 // No layer ==> no actions 721 actionPanel.showButton.setEnabled(false); 722 actionPanel.saveButton.setEnabled(false); 723 actionPanel.okButton.setEnabled(false); 724 placementPanel.setEnabled(false); 725 726 } 727 728 private void saveLayer(java.io.File file, FilePlacement placement, ProgressMonitor monitor) { 729 /* 730 * save layer to file 731 * TODO: is this methode valuable? Functionality can easily performed from Main-Menu 732 */ 733 monitor.beginTask(tr("Saving to file."), 1000); 734 735 OsmBuilder builder = new OsmBuilder(placement); 736 DataSet data = builder.build(this.pdfData.getLayers(), OsmBuilder.Mode.Final, 737 monitor.createSubTaskMonitor(500, false)); 738 OsmDataLayer layer = new OsmDataLayer(data, file.getName(), file); 739 740 monitor.setCustomText(tr(" Writing to file")); 741 monitor.setTicks(500); 742 743 OsmExporter exporter = new OsmExporter(); 744 745 try { 746 exporter.exportData(file, layer); 747 } catch (IOException e) { 748 Logging.error(e); 749 } 750 751 monitor.finishTask(); 752 } 1214 753 1215 754 } -
applications/editors/josm/plugins/pdfimport/src/pdfimport/PdfImportAction.java
r32542 r34398 33 33 34 34 //show dialog asking to select coordinate axes and input coordinates and projection. 35 /* 36 * TODO: make dialog reusable 37 */ 35 38 LoadPdfDialog dialog = new LoadPdfDialog(); 36 39 dialog.setAlwaysOnTop(true); -
applications/editors/josm/plugins/pdfimport/src/pdfimport/PdfImportPlugin.java
r34369 r34398 4 4 import org.openstreetmap.josm.gui.MainApplication; 5 5 import org.openstreetmap.josm.gui.MainMenu; 6 import org.openstreetmap.josm.gui.preferences.PreferenceSetting; 7 import org.openstreetmap.josm.gui.preferences.PreferenceTabbedPane; 8 import org.openstreetmap.josm.gui.preferences.SubPreferenceSetting; 9 import org.openstreetmap.josm.gui.preferences.TabPreferenceSetting; 6 10 import org.openstreetmap.josm.plugins.Plugin; 7 11 import org.openstreetmap.josm.plugins.PluginInformation; 8 import org.openstreetmap.josm.spi.preferences.Config;9 12 10 13 /** … … 14 17 public class PdfImportPlugin extends Plugin { 15 18 16 public enum GuiMode {17 Auto, Expert, Simple18 };19 20 public static class Preferences {21 22 public static String LoadDir;23 public static GuiMode guiMode;24 public static boolean MergeNodes;25 public static double MergeNodesValue;26 public static boolean RemoveSmall;27 public static double RemoveSmallValue;28 public static boolean RemoveLarge;29 public static double RemoveLargeValue;30 public static boolean RemoveParallel;31 public static double RemoveParallelValue;32 public static boolean LimitPath;33 public static int LimitPathValue;34 public static boolean LimitColor;35 public static String LimitColorValue;36 public static boolean DebugTags;37 public static boolean LayerClosed;38 public static boolean LayerSegment;39 public static boolean LayerAttribChange;40 public static boolean LayerOrtho;41 42 protected static int GuiCode;43 44 private static String PreFix;45 46 private Preferences() {47 return;48 }49 50 public static void reload(String Name) {51 Preferences.PreFix = Name + ".";52 Preferences.LoadDir = Config.getPref().get(Preferences.PreFix + "loadDir");53 Preferences.GuiCode = Config.getPref().getInt(Preferences.PreFix + "GuiCode",0 );54 switch (Preferences.GuiCode) {55 case 1:56 Preferences.guiMode = GuiMode.Expert;57 break;58 case 2:59 Preferences.guiMode = GuiMode.Simple;60 break;61 default:62 Preferences.guiMode = GuiMode.Expert;63 }64 Preferences.MergeNodes = Config.getPref().getBoolean(Preferences.PreFix + "MergeNodes");65 Preferences.MergeNodesValue = Config.getPref().getDouble(Preferences.PreFix + "MergeNodes.Value", 1e-3);66 Preferences.RemoveSmall = Config.getPref().getBoolean(Preferences.PreFix + "RemoveSmall");67 Preferences.RemoveSmallValue = Config.getPref().getDouble(Preferences.PreFix + "RemoveSmall.Value", 1);68 Preferences.RemoveLarge = Config.getPref().getBoolean(Preferences.PreFix + "RemoveLarge");69 Preferences.RemoveLargeValue = Config.getPref().getDouble(Preferences.PreFix + "RemoveLarge.Value", 10);70 Preferences.RemoveParallel = Config.getPref().getBoolean(Preferences.PreFix + "RemoveParallel");71 Preferences.RemoveParallelValue = Config.getPref().getDouble(Preferences.PreFix + "RemoveParalle.Value", 3);72 Preferences.LimitPath = Config.getPref().getBoolean(Preferences.PreFix + "LimitPath");73 Preferences.LimitPathValue = Config.getPref().getInt(Preferences.PreFix + "LimitPath.Value", 10000);74 Preferences.LimitColor = Config.getPref().getBoolean(Preferences.PreFix + "LimitColor");75 Preferences.LimitColorValue = Config.getPref().get(Preferences.PreFix + "LimitColor.Value","#000000");76 Preferences.DebugTags = Config.getPref().getBoolean(Preferences.PreFix + "DebugTags");77 Preferences.LayerClosed = Config.getPref().getBoolean(Preferences.PreFix + "LayerClosed");78 Preferences.LayerSegment = Config.getPref().getBoolean(Preferences.PreFix + "LayerSegment");79 Preferences.LayerAttribChange = Config.getPref().getBoolean(Preferences.PreFix + "LayerAttribChanges");80 Preferences.LayerOrtho = Config.getPref().getBoolean(Preferences.PreFix + "LayerOrtho");81 }82 }83 19 84 20 public PdfImportPlugin(PluginInformation info) { 85 21 super(info); 86 22 MainMenu.add(MainApplication.getMenu().imagerySubMenu, new PdfImportAction()); 87 Preferences .reload(this.getPluginInformation().name);23 new Preferences(getPluginInformation().name); 88 24 } 89 25 26 public class pdfimportPrefs implements SubPreferenceSetting 27 { 28 @Override 29 public TabPreferenceSetting getTabPreferenceSetting(PreferenceTabbedPane gui) 30 { 31 return null; 32 } 33 34 @Override 35 public void addGui(PreferenceTabbedPane gui) { 36 return; 37 } 38 39 @Override 40 public boolean ok() { 41 return false; 42 } 43 44 @Override 45 public boolean isExpert() { 46 return false; 47 } 48 } 49 50 @Override 51 public PreferenceSetting getPreferenceSetting() { 52 return new pdfimportPrefs(); 53 } 54 90 55 } -
applications/editors/josm/plugins/pdfimport/src/pdfimport/ProjectionInfo.java
r32542 r34398 2 2 package pdfimport; 3 3 4 import java.util.Collection;5 4 import java.util.HashMap; 6 5 import java.util.Map; … … 9 8 import org.openstreetmap.josm.gui.preferences.projection.ProjectionChoice; 10 9 import org.openstreetmap.josm.gui.preferences.projection.ProjectionPreference; 10 import org.openstreetmap.josm.gui.preferences.projection.SingleProjectionChoice; 11 11 12 12 public final class ProjectionInfo { … … 27 27 28 28 public static Projection getProjectionByCode(String code) { 29 Projection p = allCodes.get(code); 30 if (p != null) return p; 31 ProjectionChoice pc = allCodesPC.get(code); 32 if (pc == null) return null; 33 Collection<String> pref = pc.getPreferencesFromCode(code); 34 pc.setPreferences(pref); 35 p = pc.getProjection(); 36 allCodes.put(code, p); 37 return p; 29 return new SingleProjectionChoice(code.toString(), code.toString(), code).getProjection() ; 30 31 // Projection p = allCodes.get(code); 32 // if (p != null) return p; 33 // ProjectionChoice pc = allCodesPC.get(code); 34 // if (pc == null) return null; 35 // Collection<String> pref = pc.getPreferencesFromCode(code); 36 // pc.setPreferences(pref); 37 // p = pc.getProjection(); 38 // allCodes.put(code, p); 39 // return p; 38 40 } 39 41 }
Note:
See TracChangeset
for help on using the changeset viewer.