/*
* Geotoolkit - An Open Source Java GIS Toolkit
* http://www.geotoolkit.org
*
* (C) 2007 - 2008, Open Source Geospatial Foundation (OSGeo)
* (C) 2008 - 2009, Johann Sorel
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation;
* version 2.1 of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*/
package org.geotoolkit.gui.swing.render2d.decoration;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.geom.NoninvertibleTransformException;
import java.awt.geom.Point2D;
import java.awt.image.BufferedImage;
import java.text.NumberFormat;
import java.util.logging.Level;
import javax.measure.Unit;
import javax.swing.JComponent;
import org.apache.sis.measure.Units;
import org.geotoolkit.display.PortrayalException;
import org.geotoolkit.display2d.ext.scalebar.DefaultScaleBarTemplate;
import org.geotoolkit.display2d.ext.scalebar.J2DScaleBarUtilities;
import org.geotoolkit.display2d.ext.scalebar.ScaleBarTemplate;
import org.geotoolkit.gui.swing.render2d.JMap2D;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import org.opengis.referencing.operation.TransformException;
import org.apache.sis.util.logging.Logging;
/**
* Decoration displaying a scale bar
*
* @author Johann Sorel (Geomatys)
* @module
*/
public class JScaleBarDecoration extends JComponent implements MapDecoration{
private JMap2D map = null;
private ScaleBarTemplate template = new DefaultScaleBarTemplate(null,
new Dimension(300,40),10,
false, 5, NumberFormat.getNumberInstance(),
Color.BLACK, Color.BLACK, Color.WHITE,
3,true,false, new Font("Serial", Font.PLAIN, 12),true,
Units.KILOMETRE);
private final Dimension scaleDimension = new Dimension(500, 40);
private final BufferedImage buffer = new BufferedImage(scaleDimension.width, scaleDimension.height, BufferedImage.TYPE_INT_ARGB);
private CoordinateReferenceSystem lastObjCRS = null;
private CoordinateReferenceSystem lastDisplayCRS = null;
private Point2D lastCenter = null;
private int margin = 10;
private int roundSize = 12;
private int interMargin = 10;
private boolean mustUpdate = false;
public JScaleBarDecoration(){
}
/**
* {@inheritDoc }
*/
@Override
public void refresh() {
}
/**
* {@inheritDoc }
*/
@Override
public void dispose() {
}
/**
* {@inheritDoc }
*/
@Override
public void setMap2D(final JMap2D map) {
this.map = map;
}
/**
* {@inheritDoc }
*/
@Override
public JMap2D getMap2D() {
return map;
}
/**
* {@inheritDoc }
*/
@Override
public JComponent getComponent() {
return this;
}
public void setUnit(final Unit unit) {
template = new DefaultScaleBarTemplate(null,
new Dimension(300,40),10,
false, 5, NumberFormat.getNumberInstance(),
Color.BLACK, Color.BLACK, Color.WHITE,
3,true,false, new Font("Serial", Font.PLAIN, 12),true,
unit);
mustUpdate = true;
repaint(0,0,getWidth(),getHeight());
}
public Unit getUnit(){
return template.getUnit();
}
@Override
protected void paintComponent(final Graphics g) {
if(map == null) return;
final Rectangle all = getBounds();
final Rectangle scaleArea = new Rectangle(margin,
all.y+all.height-margin-scaleDimension.height,
scaleDimension.width,
scaleDimension.height);
Rectangle clip = g.getClipBounds();
if(clip != null && !(clip.intersects(scaleArea))){
return;
}
final Graphics2D g2d = (Graphics2D) g;
final double[] center;
final Point2D centerPoint;
final CoordinateReferenceSystem objCRS;
final CoordinateReferenceSystem dispCRS;
try {
center = map.getCanvas().getObjectiveCenter().getCoordinate();
centerPoint = new Point2D.Double(center[0], center[1]);
objCRS = map.getCanvas().getObjectiveCRS();
dispCRS = map.getCanvas().getDisplayCRS();
} catch (NoninvertibleTransformException | TransformException ex) {
Logging.getLogger("org.geotoolkit.gui.swing.render2d.decoration").log(Level.WARNING, null, ex);
return;
}
if(mustUpdate ||!centerPoint.equals(lastCenter) || !dispCRS.equals(lastDisplayCRS) || !objCRS.equals(lastObjCRS) ){
Graphics2D bufferG = buffer.createGraphics();
bufferG.setBackground(new Color(0f,0f,0f,0f));
bufferG.clearRect(0, 0, buffer.getWidth(), buffer.getHeight());
bufferG.setRenderingHints(g2d.getRenderingHints());
bufferG.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
bufferG.setStroke(new BasicStroke(1));
bufferG.setColor(new Color(1f, 1f, 1f, 0.85f));
bufferG.fillRoundRect(0,0, scaleArea.width-1, scaleArea.height-1, roundSize, roundSize);
bufferG.setColor(Color.GRAY);
bufferG.drawRoundRect(0,0, scaleArea.width-1, scaleArea.height-1, roundSize, roundSize);
scaleArea.x = interMargin;
scaleArea.y = interMargin;
scaleArea.width -= 2*interMargin;
scaleArea.height -= 2*interMargin;
try {
J2DScaleBarUtilities.paint(objCRS, dispCRS, centerPoint, bufferG, scaleArea.x, scaleArea.y, template);
} catch (PortrayalException ex) {
ex.printStackTrace();
}
scaleArea.x = margin;
scaleArea.y = all.y+all.height-margin-scaleDimension.height;
lastCenter = centerPoint;
lastObjCRS = objCRS;
lastDisplayCRS = dispCRS;
mustUpdate = false;
}
g2d.drawImage(buffer, scaleArea.x, scaleArea.y, null);
}
}