This is an automated email from the git hooks/post-receive script. New commit to branch feature/6944 in repository observe. See http://git.codelutin.com/observe.git commit 52347840205301005a3727a66ab10f7390de4269 Author: Sylvain Bavencoff <bavencoff@codelutin.com> Date: Tue Apr 21 16:18:29 2015 +0200 move scale in map and add compass --- .../observe/ui/util/tripMap/ObserveMapPane.java | 221 +++++++++++++++++++++ .../ird/observe/ui/util/tripMap/TripMapScale.java | 133 ------------- .../fr/ird/observe/ui/util/tripMap/TripMapUI.jaxx | 19 +- .../observe/ui/util/tripMap/TripMapUIHandler.java | 1 - .../resources/i18n/observe-swing_en_GB.properties | 5 + .../resources/i18n/observe-swing_es_ES.properties | 5 + .../resources/i18n/observe-swing_fr_FR.properties | 5 + 7 files changed, 238 insertions(+), 151 deletions(-) diff --git a/observe-swing/src/main/java/fr/ird/observe/ui/util/tripMap/ObserveMapPane.java b/observe-swing/src/main/java/fr/ird/observe/ui/util/tripMap/ObserveMapPane.java new file mode 100644 index 0000000..62578c4 --- /dev/null +++ b/observe-swing/src/main/java/fr/ird/observe/ui/util/tripMap/ObserveMapPane.java @@ -0,0 +1,221 @@ +package fr.ird.observe.ui.util.tripMap; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.geotools.geometry.jts.ReferencedEnvelope; +import org.geotools.renderer.lite.RendererUtilities; +import org.geotools.swing.JMapPane; +import org.geotools.swing.event.MapPaneAdapter; +import org.geotools.swing.event.MapPaneEvent; + +import java.awt.Color; +import java.awt.FontMetrics; +import java.awt.Graphics; +import java.awt.Point; +import java.awt.geom.Point2D; +import java.awt.geom.Rectangle2D; + +import static org.nuiton.i18n.I18n.t; + +/** + * @author Sylvain Bavencoff - bavencoff@codelutin.com + */ +public class ObserveMapPane extends JMapPane { + + private static final Log log = LogFactory.getLog(ObserveMapPane.class); + + protected static final int MARGIN = 10; + + protected static final int SCALE_HEIGHT = 15; + protected static final int SCALE_WIDTH_MAX = 200; + + protected static final int METERS_BY_MILES = 1852; + + protected int scaleWidth; + + protected String labelScaleUp; + + protected double rotation; + + public ObserveMapPane() { + labelScaleUp = "0 m"; + scaleWidth = 100; + rotation = 0; + addMapPaneListener(new MapPaneAdapter() { + @Override + public void onDisplayAreaChanged(MapPaneEvent ev) { + updateScale(); + } + }); + + + } + + + protected void updateScale() { + ReferencedEnvelope displayArea = getDisplayArea(); + double dpi = 2.54 / 100; // pour avoir l'echélle en metre/pixel + + try { + double meterPerPixel = RendererUtilities.calculateScale(displayArea, getWidth(), getHeight(), dpi); + + double maxWidthMeter = SCALE_WIDTH_MAX * meterPerPixel; + + double maxWidthMiles = maxWidthMeter / METERS_BY_MILES; + + int nbDigit = (int) Math.floor(Math.log10(maxWidthMiles)); + + int firstDigit = (int) Math.floor(maxWidthMiles / Math.pow(10, nbDigit)); // le premier chiffre significatif + + int useFirstDigit; + + if (firstDigit >= 5) { + useFirstDigit = 5; + } else if (firstDigit >= 2) { + useFirstDigit = 2; + } else { + useFirstDigit = 1; + } + + long scaleInMiles = useFirstDigit * (long) Math.pow(10, nbDigit); + + scaleWidth = (int) Math.round(scaleInMiles * METERS_BY_MILES / meterPerPixel); + + labelScaleUp = String.format("%,d " + t("observe.map.miles"), scaleInMiles); + + } catch (Exception e) { + if (log.isErrorEnabled()) { + log.error("error", e); + } + } + } + + protected void paintScale(Graphics graphics) { + graphics.setColor(Color.BLACK); + + FontMetrics fm = graphics.getFontMetrics(); + + Rectangle2D textArea = fm.getStringBounds(labelScaleUp, graphics); + + int labelLeft = getWidth() - MARGIN * 2 - scaleWidth - (int) textArea.getWidth(); + + graphics.drawString(labelScaleUp, labelLeft, getHeight() - MARGIN); + + int scalesEndX = getWidth() - MARGIN; + + int scaleStartX = scalesEndX - scaleWidth; + + int scalesEndY = getHeight() - MARGIN; + + int scaleStartY = scalesEndY - SCALE_HEIGHT; + + graphics.drawLine(scaleStartX, scaleStartY, scaleStartX, scalesEndY); + graphics.drawLine(scaleStartX, scalesEndY, scalesEndX, scalesEndY); + graphics.drawLine(scalesEndX, scalesEndY, scalesEndX, scaleStartY); + + } + + protected static int AXIS_LENGHT = 30; + protected static int CIRCLE_RADIUS = 20; + protected static int CENTER_MARGIN = 60; + protected static int INTER_AXIS_TEXT = 3; + + + protected void paintCompass(Graphics graphics) { + + Point center = new Point(getWidth() - CENTER_MARGIN, CENTER_MARGIN); + + graphics.drawOval( + center.x - CIRCLE_RADIUS, + center.y - CIRCLE_RADIUS, + CIRCLE_RADIUS * 2, + CIRCLE_RADIUS * 2); + + FontMetrics fm = graphics.getFontMetrics(); + + for (CardinalPoint cardinalPoint : CardinalPoint.values()) { + + Point2D direction = cardinalPoint.getDirection(rotation, AXIS_LENGHT, center); + + graphics.drawLine(center.x, center.y, (int)direction.getX(), (int) direction.getY()); + + Rectangle2D textArea = fm.getStringBounds(cardinalPoint.getLabel(), graphics); + + // on cherche la ditance entre le centre du text et sa bordure dans le direction donné + double l = cardinalPoint.distanceCenterBorder(rotation, textArea); + + Point2D textCenter = cardinalPoint.getDirection(rotation, AXIS_LENGHT + INTER_AXIS_TEXT + l, center); + + graphics.drawString( + cardinalPoint.getLabel(), + (int) (textCenter.getX() - textArea.getWidth() / 2), + (int) (textCenter.getY() + textArea.getHeight() / 2)); + } + + } + + + + @Override + public void paint(Graphics graphics) { + + super.paint(graphics); + + paintScale(graphics); + + paintCompass(graphics); + + } + + protected enum CardinalPoint { + NORTH(-1, 0, 0, -1, t("observe.map.north")), + SOUTH( 1, 0, 0, 1, t("observe.map.south")), + WEST ( 0, -1, 1, 0, t("observe.map.west")), + EST ( 0, 1, -1, 0, t("observe.map.east")); + + protected int matrix00; + protected int matrix01; + protected int matrix10; + protected int matrix11; + protected String label; + + CardinalPoint(int matrix00, int matrix01, int matrix10, int matrix11, String label) { + this.matrix00 = matrix00; + this.matrix01 = matrix01; + this.matrix10 = matrix10; + this.matrix11 = matrix11; + this.label = label; + } + + public Point2D.Double getDirection(double angle, double length, Point center) { + + double x = Math.sin(angle) * length; + double y = Math.cos(angle) * length; + + double deltaX = matrix00 * x + matrix01 * y; + double deltaY = matrix10 * x + matrix11 * y; + Point2D.Double result = new Point2D.Double(center.getX() + deltaX, center.getY() + deltaY); + + return result; + } + + // on cherche la ditance entre le centre du text et sa bordure dans le direction donné + public double distanceCenterBorder(double angle, Rectangle2D textArea) { + + double x = Math.sin(angle); + double y = Math.cos(angle); + + double deltaW = Math.abs(textArea.getWidth() / 2 / (matrix00 * x + matrix01 * y)); + double deltaH = Math.abs(textArea.getHeight() / 2 / (matrix10 * x + matrix11 * y)); + + double delta = Math.min(deltaH, deltaW); + + return delta; + } + + public String getLabel() { + return label; + } + } + +} diff --git a/observe-swing/src/main/java/fr/ird/observe/ui/util/tripMap/TripMapScale.java b/observe-swing/src/main/java/fr/ird/observe/ui/util/tripMap/TripMapScale.java deleted file mode 100644 index 14db68f..0000000 --- a/observe-swing/src/main/java/fr/ird/observe/ui/util/tripMap/TripMapScale.java +++ /dev/null @@ -1,133 +0,0 @@ -package fr.ird.observe.ui.util.tripMap; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.geotools.geometry.jts.ReferencedEnvelope; -import org.geotools.renderer.lite.RendererUtilities; -import org.geotools.swing.JMapPane; -import org.geotools.swing.event.MapPaneEvent; -import org.geotools.swing.event.MapPaneListener; -import org.opengis.referencing.crs.CoordinateReferenceSystem; - -import javax.swing.JPanel; -import java.awt.Color; -import java.awt.Dimension; -import java.awt.FontMetrics; -import java.awt.Graphics; -import java.awt.geom.Rectangle2D; - -/** - * @author Sylvain Bavencoff - bavencoff@codelutin.com - */ -public class TripMapScale extends JPanel implements MapPaneListener{ - - private static final Log log = LogFactory.getLog(TripMapScale.class); - - protected static final int MARGIN_HORIZONTAL_MIN = 10; - - protected static final int SCALE_HEIGHT = 15; - - protected int scaleWidth; - - protected String labelScaleUp; - - public TripMapScale() { - labelScaleUp = "0 m"; - scaleWidth = 100; - Dimension size = new Dimension(200, 50); - setMinimumSize(size); - setPreferredSize(size); - setBackground(Color.WHITE); - } - - @Override - public void onNewMapContent(MapPaneEvent ev) { - } - - @Override - public void onDisplayAreaChanged(MapPaneEvent ev) { - JMapPane jMapPane = (JMapPane) ev.getSource(); - ReferencedEnvelope displayArea = jMapPane.getDisplayArea(); - CoordinateReferenceSystem crs = jMapPane.getMapContent().getCoordinateReferenceSystem(); - int width = jMapPane.getWidth(); - int height = jMapPane.getHeight(); - double dpi = 2.54 / 100; // pour avoir l'echélle en metre/pixel - - try { - double meterPerPixel = RendererUtilities.calculateScale(displayArea, width, height, dpi); - - double maxWidthMeter = (getWidth() - 2 * MARGIN_HORIZONTAL_MIN) * meterPerPixel; - - int nbDigit = (int) Math.floor(Math.log10(maxWidthMeter)); - - int firstDigit = (int) Math.floor(maxWidthMeter / Math.pow(10, nbDigit)); // le premier chiffre significatif - - int useFirstDigit; - - if (firstDigit >= 5) { - useFirstDigit = 5; - } else if (firstDigit >= 2) { - useFirstDigit = 2; - } else { - useFirstDigit = 1; - } - - long scalUpInMeter = useFirstDigit * (long) Math.pow(10, nbDigit); - - scaleWidth = (int) Math.round(scalUpInMeter / meterPerPixel); - - if (nbDigit >= 3) { - labelScaleUp = String.format("%,d km", scalUpInMeter / 1000); - } else { - labelScaleUp = String.format("%,d m", scalUpInMeter); - } - - repaint(); - - } catch (Exception e) { - if (log.isErrorEnabled()) { - log.error("error", e); - } - } - - } - - @Override - public void onRenderingStarted(MapPaneEvent ev) { - } - - @Override - public void onRenderingStopped(MapPaneEvent ev) { - } - - - - @Override - public void paint(Graphics graphics) { - - super.paint(graphics); - - graphics.setColor(Color.BLACK); - - FontMetrics fm = graphics.getFontMetrics(); - - Rectangle2D textArea = fm.getStringBounds(labelScaleUp, graphics); - - int interLine = (getHeight() - (int)textArea.getHeight() - SCALE_HEIGHT) / 3; - - int labelScaleMargin = (getWidth() - (int) textArea.getWidth()) / 2; - - graphics.drawString(labelScaleUp, labelScaleMargin, interLine + (int)textArea.getHeight()); - - int scaleMargin = (getWidth() - scaleWidth) / 2; - - int scaleTop = getHeight() - interLine - SCALE_HEIGHT; - - graphics.drawLine(scaleMargin, scaleTop, scaleMargin, scaleTop + SCALE_HEIGHT); - - graphics.drawLine(scaleMargin, scaleTop + SCALE_HEIGHT, scaleMargin + scaleWidth, scaleTop + SCALE_HEIGHT); - - graphics.drawLine(scaleMargin + scaleWidth, scaleTop + SCALE_HEIGHT, scaleMargin + scaleWidth, scaleTop); - - } -} diff --git a/observe-swing/src/main/java/fr/ird/observe/ui/util/tripMap/TripMapUI.jaxx b/observe-swing/src/main/java/fr/ird/observe/ui/util/tripMap/TripMapUI.jaxx index 7c55000..73ddb67 100644 --- a/observe-swing/src/main/java/fr/ird/observe/ui/util/tripMap/TripMapUI.jaxx +++ b/observe-swing/src/main/java/fr/ird/observe/ui/util/tripMap/TripMapUI.jaxx @@ -7,8 +7,7 @@ fr.ird.observe.ObserveConfig fr.ird.observe.ObserveContext - org.geotools.swing.JMapPane - fr.ird.observe.ui.util.tripMap.TripMapScale + fr.ird.observe.ui.util.tripMap.ObserveMapPane </import> <TripMapUIHandler id='handler' initializer='TripMapUIHandler.newHandler(this)'/> @@ -22,23 +21,9 @@ void $afterCompleteSetup() { ]]> </script> - <Table constraints='BorderLayout.WEST' - id='legendTable'> - <row> - <cell columns="2"> - <JPanel/> - </cell> - </row> - <row> - <cell columns="2"> - <TripMapScale id="mapScale"/> - </cell> - </row> - </Table> - - <JMapPane id='map' + <ObserveMapPane id='map' constraints="BorderLayout.CENTER"/> diff --git a/observe-swing/src/main/java/fr/ird/observe/ui/util/tripMap/TripMapUIHandler.java b/observe-swing/src/main/java/fr/ird/observe/ui/util/tripMap/TripMapUIHandler.java index 03d236b..7fa37d0 100644 --- a/observe-swing/src/main/java/fr/ird/observe/ui/util/tripMap/TripMapUIHandler.java +++ b/observe-swing/src/main/java/fr/ird/observe/ui/util/tripMap/TripMapUIHandler.java @@ -98,7 +98,6 @@ public class TripMapUIHandler { mapPane.addMouseMotionListener(mouseMapListener); mapPane.addMouseListener(mouseMapListener); mapPane.addMapPaneListener(new TripMapListener()); - mapPane.addMapPaneListener(view.getMapScale()); rendererRunning = false; } diff --git a/observe-swing/src/main/resources/i18n/observe-swing_en_GB.properties b/observe-swing/src/main/resources/i18n/observe-swing_en_GB.properties index 9fa197f..dfd012c 100644 --- a/observe-swing/src/main/resources/i18n/observe-swing_en_GB.properties +++ b/observe-swing/src/main/resources/i18n/observe-swing_en_GB.properties @@ -1264,6 +1264,11 @@ observe.longlineGlobalComposition.tab.hooksComposition=Hooks observe.longlineGlobalComposition.tab.mitigationType=Mitigation observe.longlineGlobalComposition.title=Global composition observe.measurement.delete.message= +observe.map.east= +observe.map.miles= +observe.map.north= +observe.map.south= +observe.map.west= observe.menu.actions=Actions observe.menu.application.locale=Application observe.menu.configuration=Configuration diff --git a/observe-swing/src/main/resources/i18n/observe-swing_es_ES.properties b/observe-swing/src/main/resources/i18n/observe-swing_es_ES.properties index 4a9dc4b..6d65e9d 100644 --- a/observe-swing/src/main/resources/i18n/observe-swing_es_ES.properties +++ b/observe-swing/src/main/resources/i18n/observe-swing_es_ES.properties @@ -1266,6 +1266,11 @@ observe.longlineGlobalComposition.tab.hooksComposition= observe.longlineGlobalComposition.tab.mitigationType= observe.longlineGlobalComposition.title= observe.measurement.delete.message= +observe.map.east= +observe.map.miles= +observe.map.north= +observe.map.south= +observe.map.west= observe.menu.actions=Acciones observe.menu.application.locale=Aplicación observe.menu.configuration=Configuración diff --git a/observe-swing/src/main/resources/i18n/observe-swing_fr_FR.properties b/observe-swing/src/main/resources/i18n/observe-swing_fr_FR.properties index 793d2bf..181aef6 100644 --- a/observe-swing/src/main/resources/i18n/observe-swing_fr_FR.properties +++ b/observe-swing/src/main/resources/i18n/observe-swing_fr_FR.properties @@ -1264,6 +1264,11 @@ observe.longlineGlobalComposition.tab.hooksComposition=Hameçons observe.longlineGlobalComposition.tab.mitigationType=Atténuations observe.longlineGlobalComposition.title=Composition globale de la palangre observe.measurement.delete.message=Supprimer la caractéristique sélectionnée +observe.map.east=Est +observe.map.miles=milles +observe.map.north=Nord +observe.map.south=Sud +observe.map.west=Ouest observe.menu.actions=Actions observe.menu.application.locale=Application observe.menu.configuration=Configuration -- To stop receiving notification emails like this one, please contact codelutin.com SCM administrator <admin+scm@list.forge.codelutin.com>.