This is an automated email from the git hooks/post-receive script. New commit to branch develop in repository observe. See http://git.codelutin.com/observe.git commit 6ca2e47cefc96c9bc2aae13d328396760d659f5b Author: Sylvain Bavencoff <bavencoff@codelutin.com> Date: Tue Apr 21 10:20:21 2015 +0200 add scale mpa and creat jaxx file for map --- .../ui/content/open/impl/seine/TripSeineUI.jaxx | 5 +- .../open/impl/seine/TripSeineUIHandler.java | 17 ++- .../ird/observe/ui/util/tripMap/TripMapScale.java | 133 +++++++++++++++++++++ .../fr/ird/observe/ui/util/tripMap/TripMapUI.jaxx | 45 +++++++ .../tripMap/TripMapUIHandler.java} | 89 +++++++++----- observe-swing/src/main/resources/log4j.properties | 2 +- 6 files changed, 250 insertions(+), 41 deletions(-) diff --git a/observe-swing/src/main/java/fr/ird/observe/ui/content/open/impl/seine/TripSeineUI.jaxx b/observe-swing/src/main/java/fr/ird/observe/ui/content/open/impl/seine/TripSeineUI.jaxx index d63894c..b380f98 100644 --- a/observe-swing/src/main/java/fr/ird/observe/ui/content/open/impl/seine/TripSeineUI.jaxx +++ b/observe-swing/src/main/java/fr/ird/observe/ui/content/open/impl/seine/TripSeineUI.jaxx @@ -32,6 +32,7 @@ fr.ird.observe.entities.referentiel.Vessels fr.ird.observe.entities.referentiel.Person fr.ird.observe.entities.referentiel.Ocean + fr.ird.observe.ui.util.tripMap.TripMapUI org.jdesktop.swingx.JXDatePicker @@ -45,8 +46,6 @@ static fr.ird.observe.ui.UIHelper.getStringValue static org.apache.commons.lang3.StringUtils.isEmpty static org.nuiton.i18n.I18n.n - - org.geotools.swing.JMapPane </import> <!-- handler --> @@ -239,7 +238,7 @@ protected boolean canEditOcean(java.util.List<Route> routes) { </Table> </tab> <tab id="mapTab"> - <JMapPane id='tripMap'/> + <TripMapUI id="tripMap"/> </tab> </JTabbedPane> </JPanel> diff --git a/observe-swing/src/main/java/fr/ird/observe/ui/content/open/impl/seine/TripSeineUIHandler.java b/observe-swing/src/main/java/fr/ird/observe/ui/content/open/impl/seine/TripSeineUIHandler.java index 180233f..ad8d6c7 100644 --- a/observe-swing/src/main/java/fr/ird/observe/ui/content/open/impl/seine/TripSeineUIHandler.java +++ b/observe-swing/src/main/java/fr/ird/observe/ui/content/open/impl/seine/TripSeineUIHandler.java @@ -34,9 +34,9 @@ import fr.ird.observe.entities.seine.TripSeine; import fr.ird.observe.ui.content.ContentMode; import fr.ird.observe.ui.content.open.ContentOpenableUIHandler; import fr.ird.observe.ui.content.open.ContentOpenableUIModel; +import fr.ird.observe.ui.util.tripMap.TripMapUI; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.geotools.swing.JMapPane; import org.nuiton.topia.TopiaContext; import org.nuiton.topia.TopiaException; import org.nuiton.topia.persistence.TopiaDAO; @@ -61,8 +61,6 @@ public class TripSeineUIHandler extends ContentOpenableUIHandler<TripSeine> { /** Logger */ static private final Log log = LogFactory.getLog(TripSeineUIHandler.class); - protected TripMapHandler tripMapHandler; - public TripSeineUIHandler(TripSeineUI ui) { super(ui, DataContextType.Program, @@ -142,21 +140,20 @@ public class TripSeineUIHandler extends ContentOpenableUIHandler<TripSeine> { } - JMapPane tripMap = getUi().getTripMap(); + TripSeineUI ui = getUi(); + TripMapUI tripMap = ui.getTripMap(); ObserveConfig config = ui.getContextValue(ObserveConfig.class); - tripMapHandler = new TripMapHandler(tripMap, config); - - - + tripMap.getHandler().setConfig(config); getUi().getTripSeineTabPane().addChangeListener(new ChangeListener() { @Override public void stateChanged(ChangeEvent e) { JTabbedPane tripSeineTabPane = (JTabbedPane) e.getSource(); TripSeineUI ui = getUi(); - if (tripSeineTabPane.getSelectedComponent() == ui.getTripMap()) { - tripMapHandler.doOpenMap(ui.getDataService(), ui.getDataSource(), tripId); + TripMapUI tripMap = ui.getTripMap(); + if (tripSeineTabPane.getSelectedComponent() == tripMap) { + tripMap.getHandler().doOpenMap(ui.getDataSource(), ui.getDataService(), tripId); } } }); 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 new file mode 100644 index 0000000..14db68f --- /dev/null +++ b/observe-swing/src/main/java/fr/ird/observe/ui/util/tripMap/TripMapScale.java @@ -0,0 +1,133 @@ +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 new file mode 100644 index 0000000..7c55000 --- /dev/null +++ b/observe-swing/src/main/java/fr/ird/observe/ui/util/tripMap/TripMapUI.jaxx @@ -0,0 +1,45 @@ +<JPanel id="tripMap" + layout="{new BorderLayout()}"> + + <import> + fr.ird.observe.DataService + fr.ird.observe.db.DataSource + fr.ird.observe.ObserveConfig + fr.ird.observe.ObserveContext + + org.geotools.swing.JMapPane + fr.ird.observe.ui.util.tripMap.TripMapScale + </import> + + <TripMapUIHandler id='handler' initializer='TripMapUIHandler.newHandler(this)'/> + + <script><![CDATA[ + +void $afterCompleteSetup() { + getHandler().initUI(); +} + +]]> + </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' + constraints="BorderLayout.CENTER"/> + + +</JPanel> \ No newline at end of file diff --git a/observe-swing/src/main/java/fr/ird/observe/ui/content/open/impl/seine/TripMapHandler.java b/observe-swing/src/main/java/fr/ird/observe/ui/util/tripMap/TripMapUIHandler.java similarity index 89% rename from observe-swing/src/main/java/fr/ird/observe/ui/content/open/impl/seine/TripMapHandler.java rename to observe-swing/src/main/java/fr/ird/observe/ui/util/tripMap/TripMapUIHandler.java index b879b57..03d236b 100644 --- a/observe-swing/src/main/java/fr/ird/observe/ui/content/open/impl/seine/TripMapHandler.java +++ b/observe-swing/src/main/java/fr/ird/observe/ui/util/tripMap/TripMapUIHandler.java @@ -1,4 +1,4 @@ -package fr.ird.observe.ui.content.open.impl.seine; +package fr.ird.observe.ui.util.tripMap; import com.google.common.base.Function; import com.google.common.base.Optional; @@ -64,52 +64,69 @@ import java.util.List; /** * @author Sylvain Bavencoff - bavencoff@codelutin.com */ -public class TripMapHandler { +public class TripMapUIHandler { public static final String TRIP_LINES_LAYER_NAME = "Trip lines"; public static final String TRIP_POINTS_LAYER_NAME = "Trip points"; public static final double ZOOM_STEP_RATIO = 0.1; + public static TripMapUIHandler newHandler(TripMapUI view) { + return new TripMapUIHandler(view); + } + private static final Log log = LogFactory.getLog(TripMapUIHandler.class); - private static final Log log = LogFactory.getLog(TripMapHandler.class); + protected TripMapUI view; - protected JMapPane tripMapUI; - protected ObserveConfig config; + private ObserveConfig config; protected MapContent mapContent; protected ReferencedEnvelope tripArea; protected List<TripMapPoint> tripMapPoints; protected boolean rendererRunning; - public TripMapHandler(JMapPane tripMapUI, ObserveConfig config) { - this.tripMapUI = tripMapUI; - this.config = config; + public TripMapUIHandler(TripMapUI view) { + this.view = view; + } + + public void initUI() { - tripMapUI.setBackground(config.getMapBackgroundColor()); + JMapPane mapPane = view.getMap(); MouseMapListener mouseMapListener = new MouseMapListener(); - tripMapUI.addMouseWheelListener(mouseMapListener); - tripMapUI.addMouseMotionListener(mouseMapListener); - tripMapUI.addMouseListener(mouseMapListener); - tripMapUI.addMapPaneListener(new TripMapListener()); + mapPane.addMouseWheelListener(mouseMapListener); + mapPane.addMouseMotionListener(mouseMapListener); + mapPane.addMouseListener(mouseMapListener); + mapPane.addMapPaneListener(new TripMapListener()); + mapPane.addMapPaneListener(view.getMapScale()); rendererRunning = false; } - public void doOpenMap(DataService dataService, DataSource source, String tripId) { + public void setConfig(ObserveConfig config) { + this.config = config; + + JMapPane mapPane = view.getMap(); + mapPane.setBackground(config.getMapBackgroundColor()); + } + + - getTipMapPoints(dataService, source, tripId); + public void doOpenMap(DataSource dataSource, DataService dataService, String tripId) { - tripMapUI.setMapContent(getMapContent()); - tripMapUI.setDisplayArea(tripArea); + getTipMapPoints(dataSource, dataService, tripId); + + JMapPane mapPane = view.getMap(); + mapPane.setMapContent(getMapContent()); + mapPane.setDisplayArea(tripArea); } - protected List<TripMapPoint> getTipMapPoints(DataService dataService, DataSource source, String tripId) { + protected List<TripMapPoint> getTipMapPoints(DataSource dataSource, DataService dataService, String tripId) { if (tripMapPoints == null) { + try { - tripMapPoints = dataService.loadTripMapActivityPoints(source, tripId); + tripMapPoints = dataService.loadTripMapActivityPoints(dataSource, tripId); } catch (DataSourceException e) { if (log.isErrorEnabled()) { log.error("error in loadTripMapActivityPoints", e); @@ -128,7 +145,6 @@ public class TripMapHandler { StyledLayerDescriptor styledLayerDescriptor = importStyledLayerDescriptor(); - for (File layerFile : config.getMapLayerFiles()) { FileDataStore store = FileDataStoreFinder.getDataStore(layerFile); SimpleFeatureSource featureSource = store.getFeatureSource(); @@ -203,6 +219,7 @@ public class TripMapHandler { Style styleLines = findStyle(styledLayerDescriptor, TRIP_LINES_LAYER_NAME, null); Layer layerLines = new FeatureLayer(linesFeatures, styleLines); + mapContent.addLayer(layerLines); @@ -263,7 +280,9 @@ public class TripMapHandler { } protected StyledLayerDescriptor importStyledLayerDescriptor() throws FileNotFoundException { + File styleFile = config.getMapStyleFile(); + StyleFactory styleFactory = CommonFactoryFinder.getStyleFactory(null); SLDParser stylereader = new SLDParser(styleFactory, styleFile); @@ -337,15 +356,18 @@ public class TripMapHandler { protected void zoomApply() { if (zoomRatio != 1 && ! rendererRunning) { - ReferencedEnvelope displayArea = tripMapUI.getDisplayArea(); + + JMapPane mapPane = view.getMap(); + + ReferencedEnvelope displayArea = mapPane.getDisplayArea(); double deltaWidth = displayArea.getWidth() * (zoomRatio -1); double deltaHeight = displayArea.getHeight() * (zoomRatio - 1); - double ratioLeft = zoomCenter.getX() * 1d / tripMapUI.getWidth(); + double ratioLeft = zoomCenter.getX() * 1d / mapPane.getWidth(); // l'axe de Y est inversé entre le référentiel du composant swing et le référentiel géographique - double ratioTop = 1 - (zoomCenter.getY() * 1d / tripMapUI.getHeight()); + double ratioTop = 1 - (zoomCenter.getY() * 1d / mapPane.getHeight()); double deltaLeft = deltaWidth * ratioLeft; double deltaRight = deltaLeft - deltaWidth; @@ -366,7 +388,7 @@ public class TripMapHandler { displayArea.getCoordinateReferenceSystem() ); - tripMapUI.setDisplayArea(newDisplayArea); + mapPane.setDisplayArea(newDisplayArea); zoomRatio = 1; @@ -429,22 +451,35 @@ public class TripMapHandler { } protected void startMove(Point2D startPointInScreen) { - startDisplayArea = tripMapUI.getDisplayArea(); - startScreenToWorldTransform = tripMapUI.getScreenToWorldTransform(); + JMapPane mapPane = view.getMap(); + + startDisplayArea = mapPane.getDisplayArea(); + + startScreenToWorldTransform = mapPane.getScreenToWorldTransform(); + startPointInWorld = new Point2D.Double(); + startScreenToWorldTransform.transform(startPointInScreen, startPointInWorld); + } protected void endMove(Point2D endPointInScreen) { + Point2D endPointInWorld = new Point2D.Double(); + startScreenToWorldTransform.transform(endPointInScreen, endPointInWorld); + double transX = startPointInWorld.getX() - endPointInWorld.getX(); double transY = startPointInWorld.getY() - endPointInWorld.getY(); ReferencedEnvelope endDisplayArea = new ReferencedEnvelope(startDisplayArea); + endDisplayArea.translate(transX, transY); - tripMapUI.setDisplayArea(endDisplayArea); + JMapPane mapPane = view.getMap(); + + mapPane.setDisplayArea(endDisplayArea); + if (log.isDebugEnabled()) { log.debug(String.format("Translate (x : %s, y : %s)", transX, transY)); } diff --git a/observe-swing/src/main/resources/log4j.properties b/observe-swing/src/main/resources/log4j.properties index 8a5bc1e..5542ef3 100644 --- a/observe-swing/src/main/resources/log4j.properties +++ b/observe-swing/src/main/resources/log4j.properties @@ -51,4 +51,4 @@ log4j.logger.org.nuiton.topia.migration=INFO log4j.logger.jaxx.runtime.validator.swing.SwingValidator=ERROR -log4j.logger.fr.ird.observe.ui.content.open.impl.seine.TripMapHandler=DEBUG +log4j.logger.fr.ird.observe.ui.util.tripMap.TripMapHandler=DEBUG -- To stop receiving notification emails like this one, please contact codelutin.com SCM administrator <admin+scm@list.forge.codelutin.com>.