/** * Adds an edit layer change listener * * @param listener the listener. Ignored if null or already registered. * @param initialFire fire an edit-layer-changed-event right after adding the listener in case * there is an edit layer present */ public static void addEditLayerChangeListener( EditLayerChangeListener listener, boolean initialFire) { addEditLayerChangeListener(listener); if (initialFire && Main.isDisplayingMapView() && Main.map.mapView.getEditLayer() != null) { listener.editLayerChanged(null, Main.map.mapView.getEditLayer()); } }
/** * This function handles all work related to updating the cursor and highlights * * @param e * @param modifiers */ private void updateCursor(MouseEvent e, int modifiers) { if (!Main.isDisplayingMapView()) return; if (!Main.map.mapView.isActiveLayerVisible() || e == null) return; DeleteParameters parameters = getDeleteParameters(e, modifiers); Main.map.mapView.setNewCursor(parameters.mode.cursor(), this); }
public boolean doSave() { if (Main.isDisplayingMapView()) { Layer layer = Main.map.mapView.getActiveLayer(); if (layer != null && layer.isSavable()) { return doSave(layer); } } return false; }
/** * Returns the list of recent relations on active layer. * * @return the list of recent relations on active layer */ public static List<Relation> getRecentRelationsOnActiveLayer() { if (!Main.isDisplayingMapView()) return Collections.emptyList(); Layer activeLayer = Main.main.getActiveLayer(); if (!(activeLayer instanceof OsmDataLayer)) { return Collections.emptyList(); } else { return ((OsmDataLayer) activeLayer).getRecentRelations(); } }
/** If layer is the OSM Data layer, remove all errors */ @Override public void layerRemoved(Layer oldLayer) { if (oldLayer instanceof OsmDataLayer && Main.isDisplayingMapView() && !Main.main.hasEditLayer()) { Main.main.removeLayer(this); } else if (oldLayer == this) { MapView.removeLayerChangeListener(this); OsmValidator.errorLayer = null; } }
protected OsmDataLayer getFirstDataLayer() { if (!Main.isDisplayingMapView()) { return null; } Collection<Layer> layers = Main.map.mapView.getAllLayersAsList(); for (Layer layer : layers) { if (layer instanceof OsmDataLayer) { return (OsmDataLayer) layer; } } return null; }
@Override protected int getBestZoom() { if (!Main.isDisplayingMapView()) return 1; for (int i = getMinZoomLvl() + 1; i <= getMaxZoomLvl(); i++) { double ret = getTileToScreenRatio(i); if (ret < 1) { return i - 1; } } return getMaxZoomLvl(); }
protected int getNumDataLayers() { int count = 0; if (!Main.isDisplayingMapView()) { return 0; } Collection<Layer> layers = Main.map.mapView.getAllLayers(); for (Layer layer : layers) { if (layer instanceof OsmDataLayer) { count++; } } return count; }
/** Refreshes the enabled state */ @Override protected void updateEnabledState() { if (Main.applet) { setEnabled(false); return; } boolean check = Main.isDisplayingMapView() && Main.map.mapView.getActiveLayer() != null; if (!check) { setEnabled(false); return; } Layer layer = Main.map.mapView.getActiveLayer(); setEnabled(layer != null && layer.isSavable()); }
public void executeFilters() { DataSet ds = Main.main.getCurrentDataSet(); boolean changed = false; if (ds == null) { disabledAndHiddenCount = 0; disabledCount = 0; changed = true; } else { final Collection<OsmPrimitive> deselect = new HashSet<OsmPrimitive>(); ds.beginUpdate(); try { final Collection<OsmPrimitive> all = ds.allNonDeletedCompletePrimitives(); changed = FilterWorker.executeFilters(all, filterMatcher); disabledCount = 0; disabledAndHiddenCount = 0; // collect disabled and selected the primitives for (OsmPrimitive osm : all) { if (osm.isDisabled()) { disabledCount++; if (osm.isSelected()) { deselect.add(osm); } if (osm.isDisabledAndHidden()) { disabledAndHiddenCount++; } } } disabledCount -= disabledAndHiddenCount; } finally { ds.endUpdate(); } if (!deselect.isEmpty()) { ds.clearSelection(deselect); } } if (Main.isDisplayingMapView() && changed) { Main.map.mapView.repaint(); Main.map.filterDialog.updateDialogHeader(); } }
@Override protected void finish() { if (isFailed() || isCanceled()) { return; } if (dataSet == null) { return; // user canceled download or error occurred } if (dataSet.allPrimitives().isEmpty()) { rememberErrorMessage(tr("No data found in this area.")); // need to synthesize a download bounds lest the visual indication of downloaded // area doesn't work dataSet.dataSources.add( new DataSource( currentBounds != null ? currentBounds : new Bounds(new LatLon(0, 0)), "OpenStreetMap server")); } rememberDownloadedData(dataSet); int numDataLayers = getNumDataLayers(); if (newLayer || numDataLayers == 0 || (numDataLayers > 1 && getEditLayer() == null)) { // the user explicitly wants a new layer, we don't have any layer at all // or it is not clear which layer to merge to // targetLayer = createNewLayer(newLayerName); final boolean isDisplayingMapView = Main.isDisplayingMapView(); Main.main.addLayer(targetLayer); // If the mapView is not there yet, we cannot calculate the bounds (see constructor of // MapView). // Otherwise jump to the current download. if (isDisplayingMapView) { computeBboxAndCenterScale(); } } else { targetLayer = getEditLayer(); if (targetLayer == null) { targetLayer = getFirstDataLayer(); } targetLayer.mergeFrom(dataSet); computeBboxAndCenterScale(); targetLayer.onPostDownloadFromServer(); } }
/** Restores the previous settings in the download dialog. */ public void restoreSettings() { cbDownloadOsmData.setSelected(Main.pref.getBoolean("download.osm", true)); cbDownloadGpxData.setSelected(Main.pref.getBoolean("download.gps", false)); cbNewLayer.setSelected(Main.pref.getBoolean("download.newlayer", false)); cbStartup.setSelected(isAutorunEnabled()); int idx = Main.pref.getInteger("download.tab", 0); if (idx < 0 || idx > tpDownloadAreaSelectors.getTabCount()) { idx = 0; } tpDownloadAreaSelectors.setSelectedIndex(idx); if (Main.isDisplayingMapView()) { MapView mv = Main.map.mapView; currentBounds = new Bounds(mv.getLatLon(0, mv.getHeight()), mv.getLatLon(mv.getWidth(), 0)); boundingBoxChanged(currentBounds, null); } else { Bounds bounds = getSavedDownloadBounds(); if (bounds != null) { currentBounds = bounds; boundingBoxChanged(currentBounds, null); } } }
protected OsmDataLayer getEditLayer() { if (!Main.isDisplayingMapView()) { return null; } return Main.main.getEditLayer(); }
@Override protected void updateEnabledState() { setEnabled(Main.isDisplayingMapView() && Main.map.mapView.isActiveLayerDrawable()); }
/** Draw the component. */ @Override public void paint(Graphics g) { if (!prepareToDraw()) { return; } List<Layer> visibleLayers = getVisibleLayersInZOrder(); int nonChangedLayersCount = 0; for (Layer l : visibleLayers) { if (l.isChanged() || l == changedLayer) { break; } else { nonChangedLayersCount++; } } boolean canUseBuffer; synchronized (this) { canUseBuffer = !paintPreferencesChanged; paintPreferencesChanged = false; } canUseBuffer = canUseBuffer && nonChangedLayers.size() <= nonChangedLayersCount && lastViewID == getViewID() && lastClipBounds.contains(g.getClipBounds()); if (canUseBuffer) { for (int i = 0; i < nonChangedLayers.size(); i++) { if (visibleLayers.get(i) != nonChangedLayers.get(i)) { canUseBuffer = false; break; } } } if (null == offscreenBuffer || offscreenBuffer.getWidth() != getWidth() || offscreenBuffer.getHeight() != getHeight()) { offscreenBuffer = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_3BYTE_BGR); } Graphics2D tempG = offscreenBuffer.createGraphics(); tempG.setClip(g.getClip()); Bounds box = getLatLonBounds(g.getClipBounds()); if (!canUseBuffer || nonChangedLayersBuffer == null) { if (null == nonChangedLayersBuffer || nonChangedLayersBuffer.getWidth() != getWidth() || nonChangedLayersBuffer.getHeight() != getHeight()) { nonChangedLayersBuffer = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_3BYTE_BGR); } Graphics2D g2 = nonChangedLayersBuffer.createGraphics(); g2.setClip(g.getClip()); g2.setColor(PaintColors.getBackgroundColor()); g2.fillRect(0, 0, getWidth(), getHeight()); for (int i = 0; i < nonChangedLayersCount; i++) { paintLayer(visibleLayers.get(i), g2, box); } } else { // Maybe there were more unchanged layers then last time - draw them to buffer if (nonChangedLayers.size() != nonChangedLayersCount) { Graphics2D g2 = nonChangedLayersBuffer.createGraphics(); g2.setClip(g.getClip()); for (int i = nonChangedLayers.size(); i < nonChangedLayersCount; i++) { paintLayer(visibleLayers.get(i), g2, box); } } } nonChangedLayers.clear(); changedLayer = null; for (int i = 0; i < nonChangedLayersCount; i++) { nonChangedLayers.add(visibleLayers.get(i)); } lastViewID = getViewID(); lastClipBounds = g.getClipBounds(); tempG.drawImage(nonChangedLayersBuffer, 0, 0, null); for (int i = nonChangedLayersCount; i < visibleLayers.size(); i++) { paintLayer(visibleLayers.get(i), tempG, box); } synchronized (temporaryLayers) { for (MapViewPaintable mvp : temporaryLayers) { mvp.paint(tempG, this, box); } } // draw world borders tempG.setColor(Color.WHITE); Bounds b = getProjection().getWorldBoundsLatLon(); double lat = b.getMinLat(); double lon = b.getMinLon(); Point p = getPoint(b.getMin()); GeneralPath path = new GeneralPath(); path.moveTo(p.x, p.y); double max = b.getMax().lat(); for (; lat <= max; lat += 1.0) { p = getPoint(new LatLon(lat >= max ? max : lat, lon)); path.lineTo(p.x, p.y); } lat = max; max = b.getMax().lon(); for (; lon <= max; lon += 1.0) { p = getPoint(new LatLon(lat, lon >= max ? max : lon)); path.lineTo(p.x, p.y); } lon = max; max = b.getMinLat(); for (; lat >= max; lat -= 1.0) { p = getPoint(new LatLon(lat <= max ? max : lat, lon)); path.lineTo(p.x, p.y); } lat = max; max = b.getMinLon(); for (; lon >= max; lon -= 1.0) { p = getPoint(new LatLon(lat, lon <= max ? max : lon)); path.lineTo(p.x, p.y); } int w = getWidth(); int h = getHeight(); // Work around OpenJDK having problems when drawing out of bounds final Area border = new Area(path); // Make the viewport 1px larger in every direction to prevent an // additional 1px border when zooming in final Area viewport = new Area(new Rectangle(-1, -1, w + 2, h + 2)); border.intersect(viewport); tempG.draw(border); if (Main.isDisplayingMapView() && Main.map.filterDialog != null) { Main.map.filterDialog.drawOSDText(tempG); } if (playHeadMarker != null) { playHeadMarker.paint(tempG, this); } g.drawImage(offscreenBuffer, 0, 0, null); super.paint(g); }
/** * Adds a layer change listener * * @param listener the listener. Ignored if null or already registered. * @param initialFire fire an active-layer-changed-event right after adding the listener in case * there is a layer present (should be) */ public static void addLayerChangeListener(LayerChangeListener listener, boolean initialFire) { addLayerChangeListener(listener); if (initialFire && Main.isDisplayingMapView()) { listener.activeLayerChange(null, Main.map.mapView.getActiveLayer()); } }