diff --git a/openlayers3-parent/openlayers3-bootstrap/src/main/java/org/wicketstuff/openlayers3/api/overlay/Popover.java b/openlayers3-parent/openlayers3-bootstrap/src/main/java/org/wicketstuff/openlayers3/api/overlay/Popover.java index 2ebdcaa8b5..b1be23ebf3 100644 --- a/openlayers3-parent/openlayers3-bootstrap/src/main/java/org/wicketstuff/openlayers3/api/overlay/Popover.java +++ b/openlayers3-parent/openlayers3-bootstrap/src/main/java/org/wicketstuff/openlayers3/api/overlay/Popover.java @@ -1,273 +1,273 @@ -package org.wicketstuff.openlayers3.api.overlay; - -import org.apache.wicket.Component; -import org.apache.wicket.ajax.AjaxRequestTarget; -import org.apache.wicket.model.IModel; -import org.apache.wicket.util.string.Strings; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.wicketstuff.openlayers3.api.coordinate.LongLat; - -/** - * Provides an object that models an overlay containing a popover. - */ -public class Popover extends Overlay { - - private final static Logger logger = LoggerFactory.getLogger(Popover.class); - - /** - * The placement position of the popover relative to the overlay's position. - */ - private String placement; - - /** - * Flag indicating that the content to be displayed is HTML. - */ - private boolean html = true; - - /** - * Backing model for this popover's title. - */ - private IModel titleModel; - - /** - * Backing model for this popover. - */ - private IModel model; - - /** - * Creates a new instance. - * - * @param component - * Target component for the overlay - */ - public Popover(Component component, IModel titleModel, IModel model) { - super(component); - this.titleModel = titleModel; - this.model = model; - } - - /** - * Returns the placement of this popover. - * - * @return String with placement value - */ - public String getPlacement() { - return placement; - } - - /** - * Sets the placement of this popover. - * - * @param placement - * New value - */ - public void setPlacement(String placement) { - this.placement = placement; - } - - /** - * Sets the placement of this popover. - * - * @param placement - * New value - * @return This instance - */ - public Popover placement(String placement) { - setPlacement(placement); - return this; - } - - /** - * Returns the value of the HTML flag for this popover. - * - * @return Value of the HTML flag - */ - public boolean getHtml() { - return html; - } - - /** - * Sets the value of the HTML flag for this popover. - * - * @param html - * New value - */ - public void setHtml(boolean html) { - this.html = html; - } - - /** - * Sets the value of the HTML flag for this popover. - * - * @param html - * New value - * @return This instance - */ - public Popover html(boolean html) { - setHtml(html); - return this; - } - - /** - * Returns the model for this popover. - * - * @return Backing model for the popover - */ - public IModel getModel() { - return model; - } - - /** - * Sets the model for this popover. - * - * @param model - * New value - */ - public void setModel(IModel model) { - this.model = model; - } - - /** - * Sets the model for this popover. - * - * @param model - * New value - * @return This instance - */ - public Popover model(IModel model) { - setModel(model); - return this; - } - - /** - * Sets the position of the overlay. - * - * @param position - * Coordinate for the overlay's position - * @return This instance - */ - public Popover position(LongLat position) { - setPosition(position); - return this; - } - - /** - * Sets the positioning of this overlay. - * - * @param positioning - * New value - * @return This instance - */ - public Popover positioning(Positioning positioning) { - setPositioning(positioning); - return this; - } - - /** - * Sets the element tied to this popover. - * - * @param element - * Wicket component - * @return This instance - */ - public Popover element(Component element) { - setElement(element); - return this; - } - - /** - * Sets the stopEvent value. - * - * @param stopEvent - * New value - * @return this instance - */ - public Popover stopEvent(Boolean stopEvent) { - setStopEvent(stopEvent); - return this; - } - - /** - * Displays the popover. - * - * @param target - * Ajax request target - * @return This instance - */ - public Popover show(AjaxRequestTarget target) { - hide(target); - target.appendJavaScript(renderPopoverJs()); - return this; - } - - /** - * Hides this popover. - * - * @param target - * Ajax request target - * @return This instance - */ - public Popover hide(AjaxRequestTarget target) { - target.appendJavaScript("$(" + getJsId() + ".getElement()).popover('destroy');"); - return this; - } - - @Override - public String getJsId() { - return "popup_" + element.getMarkupId(); - } - - /** - * Renders the Javascript for the popover. - * - * @return String with the rendered Javascript - */ - private String renderPopoverJs() { - - StringBuilder builder = new StringBuilder(); - - // get a handle on our popup - builder.append("var popup = " + getJsId() + ";"); - builder.append("var element = popup.getElement();"); - - // set the new position of the popup - builder.append("popup.setPosition(" + position.renderJs() + ");"); - - // inital popup settings - builder.append("$(element).popover({"); - builder.append("'trigger': 'manual', 'animation': false,"); - if (titleModel != null) { - builder.append("'title': '" + escapeQuoteJs(titleModel.getObject()) + "',"); - } - if (placement != null) { - builder.append("'placement': '" + placement + "',"); - } - builder.append("'html': '" + html + "',"); - if (html) { - builder.append("'content': '" + escapeQuoteJs(model.getObject()) + "',"); - } else { - builder.append("'content': '" + Strings.escapeMarkup(model.getObject()) + "',"); - } - builder.append("});"); - - // update popup content - if(titleModel != null) { - builder.append("$(element).data('original-title', '"); - builder.append(escapeQuoteJs(titleModel.getObject())); - builder.append("');"); - } else { - builder.append("$(element).data('original-title', '');"); - } - if (html) { - builder.append("$(element).attr('data-content', '" + escapeQuoteJs(model.getObject()) + "');"); - } else { - builder.append("$(element).attr('data-content', '" + Strings.escapeMarkup(model.getObject()) + "');"); - } - - // show our popup - builder.append("$(element).popover('show');"); - - return builder.toString(); - } -} +package org.wicketstuff.openlayers3.api.overlay; + +import org.apache.wicket.Component; +import org.apache.wicket.ajax.AjaxRequestTarget; +import org.apache.wicket.model.IModel; +import org.apache.wicket.util.string.Strings; +import org.wicketstuff.openlayers3.api.coordinate.LongLat; + +/** + * Provides an object that models an overlay containing a popover. + */ +public class Popover extends Overlay { + + /** + * The placement position of the popover relative to the overlay's position. + */ + private String placement; + + /** + * Flag indicating that the content to be displayed is HTML. + */ + private boolean html = true; + + /** + * Backing model for this popover's title. + */ + private IModel titleModel; + + /** + * Backing model for this popover. + */ + private IModel model; + + /** + * Creates a new instance. + * + * @param component + * Target component for the overlay + */ + public Popover(Component component, IModel titleModel, IModel model) { + super(component); + this.titleModel = titleModel; + this.model = model; + } + + /** + * Returns the placement of this popover. + * + * @return String with placement value + */ + public String getPlacement() { + return placement; + } + + /** + * Sets the placement of this popover. + * + * @param placement + * New value + */ + public void setPlacement(String placement) { + this.placement = placement; + } + + /** + * Sets the placement of this popover. + * + * @param placement + * New value + * @return This instance + */ + public Popover placement(String placement) { + setPlacement(placement); + return this; + } + + /** + * Returns the value of the HTML flag for this popover. + * + * @return Value of the HTML flag + */ + public boolean getHtml() { + return html; + } + + /** + * Sets the value of the HTML flag for this popover. + * + * @param html + * New value + */ + public void setHtml(boolean html) { + this.html = html; + } + + /** + * Sets the value of the HTML flag for this popover. + * + * @param html + * New value + * @return This instance + */ + public Popover html(boolean html) { + setHtml(html); + return this; + } + + /** + * Returns the model for this popover. + * + * @return Backing model for the popover + */ + public IModel getModel() { + return model; + } + + /** + * Sets the model for this popover. + * + * @param model + * New value + */ + public void setModel(IModel model) { + this.model = model; + } + + /** + * Sets the model for this popover. + * + * @param model + * New value + * @return This instance + */ + public Popover model(IModel model) { + setModel(model); + return this; + } + + /** + * Sets the position of the overlay. + * + * @param position + * Coordinate for the overlay's position + * @return This instance + */ + @Override + public Popover position(LongLat position) { + setPosition(position); + return this; + } + + /** + * Sets the positioning of this overlay. + * + * @param positioning + * New value + * @return This instance + */ + @Override + public Popover positioning(Positioning positioning) { + setPositioning(positioning); + return this; + } + + /** + * Sets the element tied to this popover. + * + * @param element + * Wicket component + * @return This instance + */ + @Override + public Popover element(Component element) { + setElement(element); + return this; + } + + /** + * Sets the stopEvent value. + * + * @param stopEvent + * New value + * @return this instance + */ + @Override + public Popover stopEvent(Boolean stopEvent) { + setStopEvent(stopEvent); + return this; + } + + /** + * Displays the popover. + * + * @param target + * Ajax request target + * @return This instance + */ + public Popover show(AjaxRequestTarget target) { + hide(target); + target.appendJavaScript(renderPopoverJs()); + return this; + } + + /** + * Hides this popover. + * + * @param target + * Ajax request target + * @return This instance + */ + public Popover hide(AjaxRequestTarget target) { + target.appendJavaScript("$(" + getJsId() + ".getElement()).popover('destroy');"); + return this; + } + + @Override + public String getJsId() { + return "popup_" + element.getMarkupId(); + } + + /** + * Renders the Javascript for the popover. + * + * @return String with the rendered Javascript + */ + private String renderPopoverJs() { + + StringBuilder builder = new StringBuilder(); + + // get a handle on our popup + builder.append("var popup = " + getJsId() + ";"); + builder.append("var element = popup.getElement();"); + + // set the new position of the popup + builder.append("popup.setPosition(" + position.renderJs() + ");"); + + // inital popup settings + builder.append("$(element).popover({"); + builder.append("'trigger': 'manual', 'animation': false,"); + if (titleModel != null) { + builder.append("'title': '" + escapeQuoteJs(titleModel.getObject()) + "',"); + } + if (placement != null) { + builder.append("'placement': '" + placement + "',"); + } + builder.append("'html': '" + html + "',"); + if (html) { + builder.append("'content': '" + escapeQuoteJs(model.getObject()) + "',"); + } else { + builder.append("'content': '" + Strings.escapeMarkup(model.getObject()) + "',"); + } + builder.append("});"); + + // update popup content + if(titleModel != null) { + builder.append("$(element).data('original-title', '"); + builder.append(escapeQuoteJs(titleModel.getObject())); + builder.append("');"); + } else { + builder.append("$(element).data('original-title', '');"); + } + if (html) { + builder.append("$(element).attr('data-content', '" + escapeQuoteJs(model.getObject()) + "');"); + } else { + builder.append("$(element).attr('data-content', '" + Strings.escapeMarkup(model.getObject()) + "');"); + } + + // show our popup + builder.append("$(element).popover('show');"); + + return builder.toString(); + } +} diff --git a/openlayers3-parent/openlayers3-examples/pom.xml b/openlayers3-parent/openlayers3-examples/pom.xml index 72845cc938..2f02a5e24e 100644 --- a/openlayers3-parent/openlayers3-examples/pom.xml +++ b/openlayers3-parent/openlayers3-examples/pom.xml @@ -45,6 +45,11 @@ wicketstuff-annotation ${project.version} + + de.agilecoders.wicket + wicket-bootstrap-extensions + 0.10.0 + @@ -55,6 +60,10 @@ log4j log4j + + commons-logging + commons-logging + diff --git a/openlayers3-parent/openlayers3-examples/src/main/java/org/wicketstuff/openlayers3/examples/ClusterPage.java b/openlayers3-parent/openlayers3-examples/src/main/java/org/wicketstuff/openlayers3/examples/ClusterPage.java index a112fccb8c..0118ea262f 100644 --- a/openlayers3-parent/openlayers3-examples/src/main/java/org/wicketstuff/openlayers3/examples/ClusterPage.java +++ b/openlayers3-parent/openlayers3-examples/src/main/java/org/wicketstuff/openlayers3/examples/ClusterPage.java @@ -1,14 +1,10 @@ package org.wicketstuff.openlayers3.examples; -import com.google.gson.JsonArray; -import com.google.gson.JsonElement; -import com.google.gson.JsonNull; -import com.google.gson.JsonObject; +import java.util.Arrays; + import org.apache.wicket.ajax.AjaxRequestTarget; import org.apache.wicket.model.Model; import org.apache.wicket.util.string.Strings; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.wicketstuff.annotation.mount.MountPath; import org.wicketstuff.openlayers3.DefaultOpenLayersMap; import org.wicketstuff.openlayers3.OpenLayersMap; @@ -21,18 +17,25 @@ import org.wicketstuff.openlayers3.api.layer.Vector; import org.wicketstuff.openlayers3.api.overlay.Overlay; import org.wicketstuff.openlayers3.api.proj.Projection; -import org.wicketstuff.openlayers3.api.source.vector.Cluster; import org.wicketstuff.openlayers3.api.source.tile.Osm; +import org.wicketstuff.openlayers3.api.source.vector.Cluster; import org.wicketstuff.openlayers3.api.source.vector.VectorSource; import org.wicketstuff.openlayers3.api.source.vector.loader.DefaultGeoJsonLoader; -import org.wicketstuff.openlayers3.api.style.*; +import org.wicketstuff.openlayers3.api.style.Circle; +import org.wicketstuff.openlayers3.api.style.ClusterStyle; +import org.wicketstuff.openlayers3.api.style.Fill; +import org.wicketstuff.openlayers3.api.style.Stroke; +import org.wicketstuff.openlayers3.api.style.Text; import org.wicketstuff.openlayers3.api.util.Color; import org.wicketstuff.openlayers3.behavior.ClickFeatureHandler; import org.wicketstuff.openlayers3.component.MarkerPopover; import org.wicketstuff.openlayers3.component.PopoverPanel; import org.wicketstuff.openlayers3.examples.base.BasePage; -import java.util.Arrays; +import com.google.gson.JsonArray; +import com.google.gson.JsonElement; +import com.google.gson.JsonNull; +import com.google.gson.JsonObject; /** * Provides a page demonstrating clustered data. @@ -40,8 +43,6 @@ @MountPath("/cluster") public class ClusterPage extends BasePage { - private final static Logger logger = LoggerFactory.getLogger(ClusterPage.class); - /** * Popover for providing detail on markers. */ @@ -57,11 +58,6 @@ public class ClusterPage extends BasePage { */ private MarkerPopover markerPopover; - /** - * Layer with our clusters of features. - */ - private Vector cluster; - @Override protected void onInitialize() { super.onInitialize(); @@ -97,7 +93,7 @@ protected void onInitialize() { new Osm()), // add our vector layer for the clusters - cluster = new Vector( + new Vector( // our cluster data source new Cluster(new GeoJsonFormat(), new Projection("EPSG:3857", "degrees", "nue"), @@ -209,7 +205,6 @@ public void handleClick(AjaxRequestTarget target, String featureId, LongLat long // parse out constructed and architecture String constructed = parseField(featureThis, "CONSTRUCTI"); - String architecture = parseField(featureThis, "ARCHITECTU"); // assemble our description StringBuilder description = new StringBuilder(); diff --git a/openlayers3-parent/openlayers3-examples/src/main/java/org/wicketstuff/openlayers3/examples/MarkerPage.html b/openlayers3-parent/openlayers3-examples/src/main/java/org/wicketstuff/openlayers3/examples/MarkerPage.html index c2148c5a1a..73d17cc551 100644 --- a/openlayers3-parent/openlayers3-examples/src/main/java/org/wicketstuff/openlayers3/examples/MarkerPage.html +++ b/openlayers3-parent/openlayers3-examples/src/main/java/org/wicketstuff/openlayers3/examples/MarkerPage.html @@ -5,7 +5,16 @@

Below is a map of the world. This map centered on the location of Miles' office and provides a - helpful marker to ensure that you don't miss it.

+ helpful marker to ensure that you don't miss it. Use the layer drop-down to toggle between + the street layer and the satellite layer.

+ +
+
+
+ +
+
+
[MARKER]
diff --git a/openlayers3-parent/openlayers3-examples/src/main/java/org/wicketstuff/openlayers3/examples/MarkerPage.java b/openlayers3-parent/openlayers3-examples/src/main/java/org/wicketstuff/openlayers3/examples/MarkerPage.java index d7311d925f..a5d91342de 100644 --- a/openlayers3-parent/openlayers3-examples/src/main/java/org/wicketstuff/openlayers3/examples/MarkerPage.java +++ b/openlayers3-parent/openlayers3-examples/src/main/java/org/wicketstuff/openlayers3/examples/MarkerPage.java @@ -1,8 +1,16 @@ package org.wicketstuff.openlayers3.examples; +import java.util.Arrays; +import java.util.Collection; + +import org.apache.wicket.ajax.AjaxRequestTarget; +import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior; +import org.apache.wicket.markup.html.form.EnumChoiceRenderer; +import org.apache.wicket.markup.html.form.Form; +import org.apache.wicket.model.IModel; +import org.apache.wicket.model.LoadableDetachableModel; import org.apache.wicket.model.Model; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import org.apache.wicket.model.util.ListModel; import org.wicketstuff.annotation.mount.MountPath; import org.wicketstuff.openlayers3.DefaultOpenLayersMap; import org.wicketstuff.openlayers3.api.Map; @@ -12,11 +20,15 @@ import org.wicketstuff.openlayers3.api.layer.Tile; import org.wicketstuff.openlayers3.api.overlay.Overlay; import org.wicketstuff.openlayers3.api.source.tile.Osm; +import org.wicketstuff.openlayers3.api.source.tile.TileArcGISRest; +import org.wicketstuff.openlayers3.api.source.tile.XYZ; import org.wicketstuff.openlayers3.api.util.Color; import org.wicketstuff.openlayers3.component.Marker; import org.wicketstuff.openlayers3.examples.base.BasePage; -import java.util.Arrays; +import de.agilecoders.wicket.core.markup.html.bootstrap.form.BootstrapForm; +import de.agilecoders.wicket.core.markup.html.bootstrap.form.FormGroup; +import de.agilecoders.wicket.extensions.markup.html.bootstrap.form.select.BootstrapSelect; /** * Provides a page with a mpa that includes a marker. @@ -24,22 +36,44 @@ @MountPath("/marker") public class MarkerPage extends BasePage { - private final static Logger logger = LoggerFactory.getLogger(MarkerPage.class); + private static final String MA_ORTHO_URL = "http://tiles.arcgis.com/tiles/hGdibHYSPO59RG1h/arcgis/rest/services/USGS_Orthos_2013_2014/MapServer/tile/{z}/{y}/{x}"; + private static final String MA_OPEN_SPACE_URL = "http://gisprpxy.itd.state.ma.us/arcgisserver/rest/services/AGOL/OpenSpaceLevProt/MapServer"; + + private enum LayerOption { + STREET, + SATELLITE, + OPEN_SPACE; + } - /** - * Marker over Miles' office. - */ - private Marker marker; + private static class LayerSelectedModel extends LoadableDetachableModel { - @Override - protected void onInitialize() { - super.onInitialize(); + private IModel model; + private Collection layerOption; - // create and add our marker - add(marker = new Marker("marker", Model.of(new Color("#4169E1")))); + public LayerSelectedModel(IModel model, LayerOption... layerOption) { + this.model = model; + this.layerOption = Arrays.asList(layerOption); + } + + @Override + protected Boolean load() { + return layerOption.contains(model.getObject()); + } + + } + + public MarkerPage() { + super(); + + // model of the selected layer + final IModel model = Model.of(LayerOption.STREET); + + // create and add our marker over Miles' office + Marker marker = new Marker("marker", Model.of(new Color("#4169E1"))); + add(marker); // create and add our marker - add(new DefaultOpenLayersMap("map", + final DefaultOpenLayersMap map = new DefaultOpenLayersMap("map", // create the model for our map Model.of(new Map( @@ -47,11 +81,31 @@ protected void onInitialize() { // list of layers Arrays.asList( - // a new tile layer with the map of the world + // a new tile layer with the street map of Noho new Tile("Streets", // a new web map service tile layer - new Osm())), + new Osm(), + // visible when the layer selector is street + new LayerSelectedModel(model, LayerOption.STREET)), + + // a new tile layer with the satellite map of Noho + new Tile("Ortho", + + // MA ortho-imagery layer + new XYZ().setUrl(MA_ORTHO_URL), + // visible when the layer selector is satellite or open space + new LayerSelectedModel(model, LayerOption.OPEN_SPACE, LayerOption.SATELLITE)), + + // a new tile layer with the open space map of Noho + new Tile("Open Space", + + // MA open space layer + new TileArcGISRest().setUrl(MA_OPEN_SPACE_URL), + // visible when the layer selector is open space + new LayerSelectedModel(model, LayerOption.OPEN_SPACE)) + // fractional opacity so we can see the base map underneath + .setOpacityModel(Model.of(.5))), // list of overlays Arrays.asList( @@ -73,6 +127,28 @@ protected void onInitialize() { new LongLat(-72.638382, 42.313181, "EPSG:4326").transform(View.DEFAULT_PROJECTION), // zoom level for the view - 16))))); + 16)))); + add(map); + + // form for changing the layer + Form form = new BootstrapForm<>("form", model); + add(form); + + // layer selector -- refresh the map's layers on change + form.add(new FormGroup("layer") + .add(new BootstrapSelect<>("layerSelector", model, + new ListModel<>(Arrays.asList(LayerOption.values())), + new EnumChoiceRenderer()) + .setLabel(Model.of("Layer")) + .add(new AjaxFormComponentUpdatingBehavior("change") { + + @Override + protected void onUpdate(AjaxRequestTarget target) { + map.updateLayers(target); + } + + }))); + } + } diff --git a/openlayers3-parent/openlayers3-examples/src/main/java/org/wicketstuff/openlayers3/examples/ModifyPage.java b/openlayers3-parent/openlayers3-examples/src/main/java/org/wicketstuff/openlayers3/examples/ModifyPage.java index 38e5506d12..28db3a685e 100644 --- a/openlayers3-parent/openlayers3-examples/src/main/java/org/wicketstuff/openlayers3/examples/ModifyPage.java +++ b/openlayers3-parent/openlayers3-examples/src/main/java/org/wicketstuff/openlayers3/examples/ModifyPage.java @@ -1,13 +1,12 @@ package org.wicketstuff.openlayers3.examples; -import com.google.gson.JsonObject; +import java.util.Arrays; + import org.apache.wicket.ajax.AjaxRequestTarget; import org.apache.wicket.markup.html.basic.Label; import org.apache.wicket.model.IModel; import org.apache.wicket.model.LoadableDetachableModel; import org.apache.wicket.model.Model; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.wicketstuff.annotation.mount.MountPath; import org.wicketstuff.openlayers3.DefaultOpenLayersMap; import org.wicketstuff.openlayers3.OpenLayersMap; @@ -28,10 +27,9 @@ import org.wicketstuff.openlayers3.api.style.Style; import org.wicketstuff.openlayers3.api.util.CorsPolicy; import org.wicketstuff.openlayers3.behavior.FeatureChangeListener; -import org.wicketstuff.openlayers3.component.Marker; import org.wicketstuff.openlayers3.examples.base.BasePage; -import java.util.Arrays; +import com.google.gson.JsonObject; /** * Provides a page with a mpa that includes a marker. @@ -39,13 +37,6 @@ @MountPath("/modify") public class ModifyPage extends BasePage { - private final static Logger logger = LoggerFactory.getLogger(MarkerPage.class); - - /** - * Marker over Miles' office. - */ - private Marker marker; - /** * Map in view. */ diff --git a/openlayers3-parent/openlayers3-examples/src/main/java/org/wicketstuff/openlayers3/examples/SimplePage.java b/openlayers3-parent/openlayers3-examples/src/main/java/org/wicketstuff/openlayers3/examples/SimplePage.java index b3c3c75b19..e1cc1bf5a0 100644 --- a/openlayers3-parent/openlayers3-examples/src/main/java/org/wicketstuff/openlayers3/examples/SimplePage.java +++ b/openlayers3-parent/openlayers3-examples/src/main/java/org/wicketstuff/openlayers3/examples/SimplePage.java @@ -1,6 +1,7 @@ package org.wicketstuff.openlayers3.examples; -import com.google.common.collect.ImmutableMap; +import java.util.Arrays; + import org.apache.wicket.model.Model; import org.wicketstuff.annotation.mount.MountPath; import org.wicketstuff.openlayers3.DefaultOpenLayersMap; @@ -10,11 +11,8 @@ import org.wicketstuff.openlayers3.api.layer.Layer; import org.wicketstuff.openlayers3.api.layer.Tile; import org.wicketstuff.openlayers3.api.source.tile.Osm; -import org.wicketstuff.openlayers3.api.source.tile.TileWms; import org.wicketstuff.openlayers3.examples.base.BasePage; -import java.util.Arrays; - /** * Provides a simple map with one layer. */ diff --git a/openlayers3-parent/openlayers3-examples/src/main/java/org/wicketstuff/openlayers3/examples/WfsPage.java b/openlayers3-parent/openlayers3-examples/src/main/java/org/wicketstuff/openlayers3/examples/WfsPage.java index ffe5552f6c..9c02d5f718 100644 --- a/openlayers3-parent/openlayers3-examples/src/main/java/org/wicketstuff/openlayers3/examples/WfsPage.java +++ b/openlayers3-parent/openlayers3-examples/src/main/java/org/wicketstuff/openlayers3/examples/WfsPage.java @@ -1,13 +1,10 @@ package org.wicketstuff.openlayers3.examples; -import com.google.gson.JsonArray; -import com.google.gson.JsonNull; -import com.google.gson.JsonObject; +import java.util.Arrays; + import org.apache.wicket.ajax.AjaxRequestTarget; import org.apache.wicket.model.Model; import org.apache.wicket.util.string.Strings; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.wicketstuff.annotation.mount.MountPath; import org.wicketstuff.openlayers3.DefaultOpenLayersMap; import org.wicketstuff.openlayers3.OpenLayersMap; @@ -32,7 +29,9 @@ import org.wicketstuff.openlayers3.component.PopoverPanel; import org.wicketstuff.openlayers3.examples.base.BasePage; -import java.util.Arrays; +import com.google.gson.JsonArray; +import com.google.gson.JsonNull; +import com.google.gson.JsonObject; /** * Provides a page demonstrating WFS (Web Feature Service) data. @@ -40,8 +39,6 @@ @MountPath("/wfs") public class WfsPage extends BasePage { - private final static Logger logger = LoggerFactory.getLogger(WfsPage.class); - /** * Popover for providing detail on markers. */ @@ -57,11 +54,6 @@ public class WfsPage extends BasePage { */ private MarkerPopover markerPopover; - /** - * Layer with our vector data of features. - */ - private Vector vectorLayer; - @Override protected void onInitialize() { super.onInitialize(); @@ -101,7 +93,7 @@ protected void onInitialize() { new Osm()), // add our vector layer with the data - vectorLayer = new Vector(new VectorSource(new GeoJsonFormat(), + new Vector(new VectorSource(new GeoJsonFormat(), new DefaultGeoJsonLoader( "http://mhc-macris.net:8080/geoserver/ows?service=WFS" + "&version=1.0.0&request=GetFeature&typeName=MHC:in_pts" diff --git a/openlayers3-parent/openlayers3-examples/src/main/java/org/wicketstuff/openlayers3/examples/WicketApplication.properties b/openlayers3-parent/openlayers3-examples/src/main/java/org/wicketstuff/openlayers3/examples/WicketApplication.properties new file mode 100644 index 0000000000..40c59d094b --- /dev/null +++ b/openlayers3-parent/openlayers3-examples/src/main/java/org/wicketstuff/openlayers3/examples/WicketApplication.properties @@ -0,0 +1,3 @@ +LayerOption.STREET=Street +LayerOption.SATELLITE=Satellite +LayerOption.OPEN_SPACE=Open Space \ No newline at end of file diff --git a/openlayers3-parent/openlayers3-examples/src/main/java/org/wicketstuff/openlayers3/examples/base/BasePage.java b/openlayers3-parent/openlayers3-examples/src/main/java/org/wicketstuff/openlayers3/examples/base/BasePage.java index 1a83cbab0c..c5b3a5282c 100644 --- a/openlayers3-parent/openlayers3-examples/src/main/java/org/wicketstuff/openlayers3/examples/base/BasePage.java +++ b/openlayers3-parent/openlayers3-examples/src/main/java/org/wicketstuff/openlayers3/examples/base/BasePage.java @@ -1,147 +1,142 @@ -package org.wicketstuff.openlayers3.examples.base; - -import de.agilecoders.wicket.core.markup.html.bootstrap.behavior.BootstrapBaseBehavior; -import de.agilecoders.wicket.core.markup.html.bootstrap.html.ChromeFrameMetaTag; -import de.agilecoders.wicket.core.markup.html.bootstrap.html.HtmlTag; -import de.agilecoders.wicket.core.markup.html.bootstrap.html.MetaTag; -import de.agilecoders.wicket.core.markup.html.bootstrap.html.OptimizedMobileViewportMetaTag; -import de.agilecoders.wicket.core.markup.html.bootstrap.navbar.Navbar; -import org.apache.wicket.Component; -import org.apache.wicket.markup.head.CssHeaderItem; -import org.apache.wicket.markup.head.IHeaderResponse; -import org.apache.wicket.markup.html.WebPage; -import org.apache.wicket.markup.html.basic.Label; -import org.apache.wicket.model.IModel; -import org.apache.wicket.model.Model; -import org.apache.wicket.model.ResourceModel; -import org.apache.wicket.model.StringResourceModel; -import org.apache.wicket.request.resource.CssResourceReference; - -import java.util.Locale; - -/** - * Provides a basic web page that other application pages may extend. - */ -public class BasePage extends WebPage { - - /** - * Title of this page. - */ - protected static final String RESKEY_PAGE_TITLE = "page.title"; - - /** - * Short title for this page. - */ - protected static final String RESKEY_TITLE_SHORT = "page.title.short"; - - /** - * Description of this page. - */ - protected static final String RESKEY_DESCRIPTION = "page.description"; - - /** - * Author of this page. - */ - protected static final String RESKEY_AUTHOR = "page.author"; - - /* - * Component IDs. - */ - private static final String CID_TITLE = "title"; - private static final String CID_PAGE_TITLE = "titleShort"; - private static final String CID_HEADER = "header"; - - /* - * Components. - */ - private Component debugBar; - - @Override - protected void onInitialize() { - super.onInitialize(); - - // add HTML declaration - add(new HtmlTag("html", Locale.ENGLISH)); - - // add Bootstrap - add(new BootstrapBaseBehavior()); - add(new OptimizedMobileViewportMetaTag("viewport")); - add(new ChromeFrameMetaTag("chrome-frame")); - add(new MetaTag("description", Model.of("description"), getPageDescriptionModel())); - add(new MetaTag("author", Model.of("author"), getPageAuthorModel())); - add(new Label(CID_TITLE, getPageTitleModel())); - - // add page components - add(getHeaderNavBar(CID_HEADER).setVisibilityAllowed(isShowHeader())); - add((new Label(CID_PAGE_TITLE, getPageShortTitleModel())).setVisibilityAllowed(isShowPageTitle())); - } - - /** - * Returns the page title model. - * - * @return model for the page title - */ - protected IModel getPageTitleModel() { - return new ResourceModel(RESKEY_PAGE_TITLE); - } - - /** - * Returns the short page title model. - * - * @return model for the short page title - */ - protected IModel getPageShortTitleModel() { - return new ResourceModel(RESKEY_TITLE_SHORT); - } - - /** - * Returns the page description model. - * - * @return model for the page description - */ - protected IModel getPageDescriptionModel() { - return new StringResourceModel(RESKEY_DESCRIPTION, this, null); - } - - /** - * Returns the page author model. - * - * @return model for the page author - */ - protected IModel getPageAuthorModel() { - return new StringResourceModel(RESKEY_AUTHOR, this, null); - } - - /** - * Returns the setTitleModel navigation bar for the page. - * - * @param cid Wicket component ID for the setTitleModel navigation bar - * @return The navigation bar for the setTitleModel - */ - protected Navbar getHeaderNavBar(final String cid) { - return new AppNavBar(cid); - } - - /** - * Override to disable setTitleModel. - * - * @return show (true) or hide (false) setTitleModel - */ - protected boolean isShowHeader() { - return true; - } - - /** - * Override to hide the page title. - * - * @return show (true) or hide (false) the page title - */ - protected boolean isShowPageTitle() { - return true; - } - - @Override - public void renderHead(final IHeaderResponse response) { - response.render(CssHeaderItem.forReference(new CssResourceReference(BasePage.class, "BasePage.css"))); - } -} +package org.wicketstuff.openlayers3.examples.base; + +import java.util.Locale; + +import org.apache.wicket.markup.head.CssHeaderItem; +import org.apache.wicket.markup.head.IHeaderResponse; +import org.apache.wicket.markup.html.WebPage; +import org.apache.wicket.markup.html.basic.Label; +import org.apache.wicket.model.IModel; +import org.apache.wicket.model.Model; +import org.apache.wicket.model.ResourceModel; +import org.apache.wicket.model.StringResourceModel; +import org.apache.wicket.request.resource.CssResourceReference; + +import de.agilecoders.wicket.core.markup.html.bootstrap.behavior.BootstrapBaseBehavior; +import de.agilecoders.wicket.core.markup.html.bootstrap.html.HtmlTag; +import de.agilecoders.wicket.core.markup.html.bootstrap.html.IeEdgeMetaTag; +import de.agilecoders.wicket.core.markup.html.bootstrap.html.MetaTag; +import de.agilecoders.wicket.core.markup.html.bootstrap.html.OptimizedMobileViewportMetaTag; +import de.agilecoders.wicket.core.markup.html.bootstrap.navbar.Navbar; + +/** + * Provides a basic web page that other application pages may extend. + */ +public class BasePage extends WebPage { + + /** + * Title of this page. + */ + protected static final String RESKEY_PAGE_TITLE = "page.title"; + + /** + * Short title for this page. + */ + protected static final String RESKEY_TITLE_SHORT = "page.title.short"; + + /** + * Description of this page. + */ + protected static final String RESKEY_DESCRIPTION = "page.description"; + + /** + * Author of this page. + */ + protected static final String RESKEY_AUTHOR = "page.author"; + + /* + * Component IDs. + */ + private static final String CID_TITLE = "title"; + private static final String CID_PAGE_TITLE = "titleShort"; + private static final String CID_HEADER = "header"; + + @Override + protected void onInitialize() { + super.onInitialize(); + + // add HTML declaration + add(new HtmlTag("html", Locale.ENGLISH)); + + // add Bootstrap + add(new BootstrapBaseBehavior()); + add(new OptimizedMobileViewportMetaTag("viewport")); + add(new IeEdgeMetaTag("chrome-frame")); + add(new MetaTag("description", Model.of("description"), getPageDescriptionModel())); + add(new MetaTag("author", Model.of("author"), getPageAuthorModel())); + add(new Label(CID_TITLE, getPageTitleModel())); + + // add page components + add(getHeaderNavBar(CID_HEADER).setVisibilityAllowed(isShowHeader())); + add((new Label(CID_PAGE_TITLE, getPageShortTitleModel())).setVisibilityAllowed(isShowPageTitle())); + } + + /** + * Returns the page title model. + * + * @return model for the page title + */ + protected IModel getPageTitleModel() { + return new ResourceModel(RESKEY_PAGE_TITLE); + } + + /** + * Returns the short page title model. + * + * @return model for the short page title + */ + protected IModel getPageShortTitleModel() { + return new ResourceModel(RESKEY_TITLE_SHORT); + } + + /** + * Returns the page description model. + * + * @return model for the page description + */ + protected IModel getPageDescriptionModel() { + return new StringResourceModel(RESKEY_DESCRIPTION, this, null); + } + + /** + * Returns the page author model. + * + * @return model for the page author + */ + protected IModel getPageAuthorModel() { + return new StringResourceModel(RESKEY_AUTHOR, this, null); + } + + /** + * Returns the setTitleModel navigation bar for the page. + * + * @param cid Wicket component ID for the setTitleModel navigation bar + * @return The navigation bar for the setTitleModel + */ + protected Navbar getHeaderNavBar(final String cid) { + return new AppNavBar(cid); + } + + /** + * Override to disable setTitleModel. + * + * @return show (true) or hide (false) setTitleModel + */ + protected boolean isShowHeader() { + return true; + } + + /** + * Override to hide the page title. + * + * @return show (true) or hide (false) the page title + */ + protected boolean isShowPageTitle() { + return true; + } + + @Override + public void renderHead(final IHeaderResponse response) { + response.render(CssHeaderItem.forReference(new CssResourceReference(BasePage.class, "BasePage.css"))); + } +} diff --git a/openlayers3-parent/openlayers3/src/main/java/org/wicketstuff/openlayers3/OpenLayersMap.java b/openlayers3-parent/openlayers3/src/main/java/org/wicketstuff/openlayers3/OpenLayersMap.java index 3a1476a161..c33326acfa 100644 --- a/openlayers3-parent/openlayers3/src/main/java/org/wicketstuff/openlayers3/OpenLayersMap.java +++ b/openlayers3-parent/openlayers3/src/main/java/org/wicketstuff/openlayers3/OpenLayersMap.java @@ -6,8 +6,6 @@ import org.apache.wicket.markup.head.IHeaderResponse; import org.apache.wicket.markup.html.panel.GenericPanel; import org.apache.wicket.model.IModel; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.wicketstuff.openlayers3.api.Extent; import org.wicketstuff.openlayers3.api.Feature; import org.wicketstuff.openlayers3.api.JavascriptObject; @@ -30,33 +28,28 @@ */ public abstract class OpenLayersMap extends GenericPanel { - private final static Logger logger = LoggerFactory.getLogger(OpenLayersMap.class); - /** * Map of layers to the data loaded handler that notifies their listeners. */ - private java.util.Map layerDataLoadedMap = - new HashMap(); + private java.util.Map layerDataLoadedMap = new HashMap(); /** * Map of layers to the data loaded handler that notifies their listeners. */ - private java.util.Map layerLoadedMap = - new HashMap(); + private java.util.Map layerLoadedMap = new HashMap(); /** * Creates a new instance * * @param id - * Wicket element ID + * Wicket element ID * @param model - * Backing modep for this map + * Backing modep for this map */ public OpenLayersMap(final String id, final IModel model) { super(id, model); } - @Override protected void onConfigure() { super.onConfigure(); @@ -72,36 +65,37 @@ protected void onConfigure() { final Vector vectorLayer = (Vector) layer; - if(vectorLayer.getFeatureDataLoadedListeners() != null + if (vectorLayer.getFeatureDataLoadedListeners() != null && vectorLayer.getFeatureDataLoadedListeners().size() > 0) { // add a behavior to be notified when new data is loaded - VectorFeatureDataLoadedListener vectorFeatureDataLoadedListener = - new VectorFeatureDataLoadedListener(vectorLayer) { - - @Override - public void handleDataLoaded(AjaxRequestTarget target, JsonArray features) { - vectorLayer.notifyFeatureDataLoadedListeners(target, features); - } - }; + VectorFeatureDataLoadedListener vectorFeatureDataLoadedListener = new VectorFeatureDataLoadedListener( + vectorLayer) { + + @Override + public void handleDataLoaded(AjaxRequestTarget target, JsonArray features) { + vectorLayer.notifyFeatureDataLoadedListeners(target, features); + } + }; add(vectorFeatureDataLoadedListener); // map the layer to the data loaded handler layerDataLoadedMap.put(layer, vectorFeatureDataLoadedListener); } - if(vectorLayer.getFeaturesLoadedListeners() != null + if (vectorLayer.getFeaturesLoadedListeners() != null && vectorLayer.getFeaturesLoadedListeners().size() > 0) { - // add a behavior to be notified when new features are loaded - VectorFeaturesLoadedListener vectorFeatureLoadedListener = - new VectorFeaturesLoadedListener(vectorLayer) { + // add a behavior to be notified when new features are + // loaded + VectorFeaturesLoadedListener vectorFeatureLoadedListener = new VectorFeaturesLoadedListener( + vectorLayer) { - @Override - public void handleDataLoaded(AjaxRequestTarget target) { - vectorLayer.notifyFeaturesLoadedListeners(target); - } - }; + @Override + public void handleDataLoaded(AjaxRequestTarget target) { + vectorLayer.notifyFeaturesLoadedListeners(target); + } + }; add(vectorFeatureLoadedListener); // map the layer to the data loaded handler @@ -113,26 +107,28 @@ public void handleDataLoaded(AjaxRequestTarget target) { } /** - * Zooms the map to an extent that includes all of the features on the provided vector layer. + * Zooms the map to an extent that includes all of the features on the + * provided vector layer. * * @param target - * Ajax request target + * Ajax request target * @param vector - * Vector layer containing features + * Vector layer containing features */ public void zoomToFeatureExtent(AjaxRequestTarget target, Vector vector) { this.zoomToFeatureExtent(target, vector, 0); } /** - * Zooms the map to an extent that includes all of the features on the provided vector layer. + * Zooms the map to an extent that includes all of the features on the + * provided vector layer. * * @param target - * Ajax request target + * Ajax request target * @param vector - * Vector layer containing features + * Vector layer containing features * @param buffer - * Buffer around the calculated extent + * Buffer around the calculated extent */ public void zoomToFeatureExtent(AjaxRequestTarget target, Vector vector, Number buffer) { StringBuilder builder = new StringBuilder(); @@ -145,7 +141,7 @@ public void zoomToFeatureExtent(AjaxRequestTarget target, Vector vector, Number builder.append("extent = ol.extent.buffer(extent, parseFloat('" + buffer + "'));"); String jsId = getModelObject().getJsId(); - builder.append(jsId + ".getView().fitExtent(extent, " + jsId + ".getSize());"); + builder.append(jsId + ".getView().fit(extent, " + jsId + ".getSize());"); target.appendJavaScript(builder.toString()); } @@ -153,8 +149,10 @@ public void zoomToFeatureExtent(AjaxRequestTarget target, Vector vector, Number /** * Sets the center of the map's current view. * - * @param target Ajax request target - * @param point New center location for the map + * @param target + * Ajax request target + * @param point + * New center location for the map */ public void setViewCenter(AjaxRequestTarget target, Point point) { @@ -169,8 +167,10 @@ public void setViewCenter(AjaxRequestTarget target, Point point) { /** * Adds the provided interaction to the map. * - * @param target Ajax request target - * @param interaction Interaction to add + * @param target + * Ajax request target + * @param interaction + * Interaction to add */ public void addInteraction(AjaxRequestTarget target, Interaction interaction) { @@ -178,16 +178,18 @@ public void addInteraction(AjaxRequestTarget target, Interaction interaction) { getModelObject().getInteractions().add(interaction); // update the map - target.appendJavaScript(interaction.getJsId() + " = new " + interaction.getJsType() - + "(" + interaction.renderJs() + ");" - + JavascriptObject.JS_GLOBAL + "['map_" + getMarkupId() + "'].addInteraction(" + interaction.getJsId() + ");"); + target.appendJavaScript(interaction.getJsId() + " = new " + interaction.getJsType() + "(" + + interaction.renderJs() + ");" + JavascriptObject.JS_GLOBAL + "['map_" + getMarkupId() + + "'].addInteraction(" + interaction.getJsId() + ");"); } /** * Removes provided interaction from the map. * - * @param target Ajax request target - * @param interaction Interaction to add + * @param target + * Ajax request target + * @param interaction + * Interaction to add */ public void removeInteraction(AjaxRequestTarget target, Interaction interaction) { @@ -196,20 +198,22 @@ public void removeInteraction(AjaxRequestTarget target, Interaction interaction) // update the map target.appendJavaScript(JavascriptObject.JS_GLOBAL + "['map_" + getMarkupId() + "'].removeInteraction(" - + interaction.getJsId() + ");"); + + interaction.getJsId() + ");"); } - /** + /** * Sets the extent for the map and zooms to that extent. * - * @param target Ajax request target - * @param extent Extent to which the map will be zoomed + * @param target + * Ajax request target + * @param extent + * Extent to which the map will be zoomed */ - public void zoomToExtent(AjaxRequestTarget target, Extent extent) { - target.appendJavaScript(JavascriptObject.JS_GLOBAL + "['map_" + getMarkupId() + "'].getView().fitExtent(" - + getModelObject().getView().getExtent().renderJsForView(getModelObject().getView()) + "," - + JavascriptObject.JS_GLOBAL + "['map_" + getMarkupId() + "']" + ".getSize());"); - } + public void zoomToExtent(AjaxRequestTarget target, Extent extent) { + target.appendJavaScript(JavascriptObject.JS_GLOBAL + "['map_" + getMarkupId() + "'].getView().fit(" + + getModelObject().getView().getExtent().renderJsForView(getModelObject().getView()) + "," + + JavascriptObject.JS_GLOBAL + "['map_" + getMarkupId() + "']" + ".getSize());"); + } @Override public abstract void renderHead(final IHeaderResponse response); @@ -223,9 +227,9 @@ public String renderBeforeConstructorJs() { StringBuilder builder = new StringBuilder(); - // make sure our global variable exists - builder.append("if(typeof " + JavascriptObject.JS_GLOBAL + " === 'undefined') { " - + JavascriptObject.JS_GLOBAL + " = []};\n\n"); + // make sure our global variable exists + builder.append("if(typeof " + JavascriptObject.JS_GLOBAL + " === 'undefined') { " + JavascriptObject.JS_GLOBAL + + " = []};\n\n"); Map map = getModelObject(); if (map != null) { @@ -239,9 +243,9 @@ public String renderBeforeConstructorJs() { VectorSource vectorSource = vectorLayer.getSource(); // render any persistent features - if(vectorSource.getFeatures() != null) { - for(Feature feature : vectorSource.getFeatures()) { - if(feature instanceof PersistentFeature) { + if (vectorSource.getFeatures() != null) { + for (Feature feature : vectorSource.getFeatures()) { + if (feature instanceof PersistentFeature) { builder.append(feature.getJsId() + " = new " + feature.getJsType() + "(" + feature.renderJs() + ");\n"); } @@ -249,12 +253,12 @@ public String renderBeforeConstructorJs() { } // ensure our loader has a reference to our source - if(vectorSource.getLoader() != null) { + if (vectorSource.getLoader() != null) { vectorSource.getLoader().setSource(vectorSource); } // render the source for the cluster source - if(vectorSource instanceof Cluster) { + if (vectorSource instanceof Cluster) { Cluster source = (Cluster) vectorSource; builder.append(renderServerVectorJs(source.getSource(), layer)); } @@ -263,8 +267,7 @@ public String renderBeforeConstructorJs() { builder.append(renderServerVectorJs(vectorSource, layer)); } - builder.append(layer.getJsId() + " = new " + layer.getJsType() + "(" + layer.renderJs() - + ");\n"); + builder.append(layer.getJsId() + " = new " + layer.getJsType() + "(" + layer.renderJs() + ");\n"); } } @@ -275,8 +278,8 @@ private String renderServerVectorJs(VectorSource vectorSource, Layer layer) { StringBuilder builder = new StringBuilder(); - builder.append(vectorSource.getJsId() + " = new " + vectorSource.getJsType() + "(" - + vectorSource.renderJs() + ");\n"); + builder.append( + vectorSource.getJsId() + " = new " + vectorSource.getJsType() + "(" + vectorSource.renderJs() + ");\n"); if (vectorSource.getLoader() instanceof DefaultGeoJsonLoader) { DefaultGeoJsonLoader loader = (DefaultGeoJsonLoader) vectorSource.getLoader(); @@ -284,13 +287,13 @@ private String renderServerVectorJs(VectorSource vectorSource, Layer layer) { if (layerDataLoadedMap.get(layer) != null) { // add our listener for the feature data loading - loader.vectorFeatureDataLoadedListener(layerDataLoadedMap.get((Vector) layer)); + loader.vectorFeatureDataLoadedListener(layerDataLoadedMap.get(layer)); } if (layerLoadedMap.get(layer) != null) { // add our listener for the feature loading - loader.vectorFeaturesLoadedListener(layerLoadedMap.get((Vector) layer)); + loader.vectorFeaturesLoadedListener(layerLoadedMap.get(layer)); } builder.append(loader.renderBeforeConstructorJs() + ";\n"); } @@ -299,7 +302,8 @@ private String renderServerVectorJs(VectorSource vectorSource, Layer layer) { } /** - * Renders the Javascript after the construction of our OL3 objects (but not the map). + * Renders the Javascript after the construction of our OL3 objects (but not + * the map). * * @return String with the rendered Javascript */ @@ -313,7 +317,7 @@ public String renderAfterConstructorJs() { for (Layer layer : map.getLayers()) { - if(layer instanceof Vector) { + if (layer instanceof Vector) { Vector vectorLayer = (Vector) layer; VectorSource vectorSource = vectorLayer.getSource(); @@ -340,9 +344,9 @@ public String renderAfterMapConstructorJs() { Map map = getModelObject(); - if(map.getView().getExtent() != null) { - builder.append(map.getJsId() + ".getView().fitExtent(" + map.getView().getExtent().renderJsForView(map.getView()) + "," - + map.getJsId() + ".getSize());"); + if (map.getView().getExtent() != null) { + builder.append(map.getJsId() + ".getView().fit(" + map.getView().getExtent().renderJsForView(map.getView()) + + "," + map.getJsId() + ".getSize());"); } return builder.toString(); @@ -355,7 +359,7 @@ public String renderAfterMapConstructorJs() { */ public String renderJs() { - Map map = getModelObject(); + Map map = getModelObject(); StringBuilder builder = new StringBuilder(); builder.append(renderBeforeConstructorJs() + "\n\n"); @@ -366,4 +370,30 @@ public String renderJs() { builder.append(renderAfterMapConstructorJs()); return builder.toString(); } + + /** + * Refreshes via ajax all of the maps layers. + * + * @param target + */ + public void updateLayers(AjaxRequestTarget target) { + for (Layer layer : getModelObject().getLayers()) { + layer.onUpdate(target); + } + } + + /** + * Calls detach on all of the layers in the map. + * + * @see org.apache.wicket.MarkupContainer#onDetach() + */ + @Override + public void onDetach() { + super.onDetach(); + Map map = getModel().getObject(); + for (Layer layer : map.getLayers()) { + layer.detach(); + } + } + } diff --git a/openlayers3-parent/openlayers3/src/main/java/org/wicketstuff/openlayers3/api/JavascriptObject.java b/openlayers3-parent/openlayers3/src/main/java/org/wicketstuff/openlayers3/api/JavascriptObject.java index 3c4d428f06..84c6be41bd 100644 --- a/openlayers3-parent/openlayers3/src/main/java/org/wicketstuff/openlayers3/api/JavascriptObject.java +++ b/openlayers3-parent/openlayers3/src/main/java/org/wicketstuff/openlayers3/api/JavascriptObject.java @@ -1,89 +1,84 @@ -package org.wicketstuff.openlayers3.api; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.HashMap; - -/** - * Provides an interface that all Javascript objects must implement in order to be rendered properly. - */ -public abstract class JavascriptObject implements IJavascriptObject { - - private static Logger logger = LoggerFactory.getLogger(JavascriptObject.class); - - /** - * Global variable for holding all of our Javascript objects and data. - */ - public static final String JS_GLOBAL = "window.org_wicketstuff_openlayers3"; - - /** - * Counter for generating instance identifiers. - */ - public static long counter = 0; - - /** - * Map for starting object Ids. - */ - public static java.util.Map objectIds = new HashMap(); - - /** - * Creates a new instance. - */ - public JavascriptObject() { - - // generate our ID - getJsId(); - } - - /** - * Returns a String with the type of Javascript object that this object represents. This will be used when creating - * new instances. - * - * @return String with the Javascript object type - */ - @Override - public abstract String getJsType(); - - /** - * Returns a String with the unique ID used to identify this object. - * - * @return String with the object's unique ID - */ - @Override - public String getJsId() { - - String objectId = JS_GLOBAL + "['" + getClass().getSimpleName().toLowerCase() + this.hashCode() + "']"; - return objectId; - } - - /** - * Returns a String with the unique ID used to identify this object with the suffix appended. - * - * @return String with the object's unique ID - */ - public String getJsIdWithSuffix(String suffix) { - - String objectId = JS_GLOBAL + "['" + getClass().getSimpleName().toLowerCase() + this.hashCode() + suffix + "']"; - return objectId; - } - - /** - * Returns a String with containing the rendered Javascript code for this object. - * - * @return String with rendered Javascript code - */ - @Override - public abstract String renderJs(); - - /** - * Escapes single quotation marks in the provided String. When outputting Javascript code, we're using single - * quotation marks to surround the content. - * - * @param text Text to escape - * @return String with escaped text - */ - protected String escapeQuoteJs(String text) { - return text.replaceAll("\'", "'"); - } -} +package org.wicketstuff.openlayers3.api; + +import java.util.HashMap; + +/** + * Provides an interface that all Javascript objects must implement in order to be rendered properly. + */ +public abstract class JavascriptObject implements IJavascriptObject { + + /** + * Global variable for holding all of our Javascript objects and data. + */ + public static final String JS_GLOBAL = "window.org_wicketstuff_openlayers3"; + + /** + * Counter for generating instance identifiers. + */ + public static long counter = 0; + + /** + * Map for starting object Ids. + */ + public static java.util.Map objectIds = new HashMap(); + + /** + * Creates a new instance. + */ + public JavascriptObject() { + + // generate our ID + getJsId(); + } + + /** + * Returns a String with the type of Javascript object that this object represents. This will be used when creating + * new instances. + * + * @return String with the Javascript object type + */ + @Override + public abstract String getJsType(); + + /** + * Returns a String with the unique ID used to identify this object. + * + * @return String with the object's unique ID + */ + @Override + public String getJsId() { + + String objectId = JS_GLOBAL + "['" + getClass().getSimpleName().toLowerCase() + this.hashCode() + "']"; + return objectId; + } + + /** + * Returns a String with the unique ID used to identify this object with the suffix appended. + * + * @return String with the object's unique ID + */ + public String getJsIdWithSuffix(String suffix) { + + String objectId = JS_GLOBAL + "['" + getClass().getSimpleName().toLowerCase() + this.hashCode() + suffix + "']"; + return objectId; + } + + /** + * Returns a String with containing the rendered Javascript code for this object. + * + * @return String with rendered Javascript code + */ + @Override + public abstract String renderJs(); + + /** + * Escapes single quotation marks in the provided String. When outputting Javascript code, we're using single + * quotation marks to surround the content. + * + * @param text Text to escape + * @return String with escaped text + */ + protected String escapeQuoteJs(String text) { + return text.replaceAll("\'", "'"); + } +} diff --git a/openlayers3-parent/openlayers3/src/main/java/org/wicketstuff/openlayers3/api/interaction/Modify.java b/openlayers3-parent/openlayers3/src/main/java/org/wicketstuff/openlayers3/api/interaction/Modify.java index abd4cf5e58..92981e44a8 100644 --- a/openlayers3-parent/openlayers3/src/main/java/org/wicketstuff/openlayers3/api/interaction/Modify.java +++ b/openlayers3-parent/openlayers3/src/main/java/org/wicketstuff/openlayers3/api/interaction/Modify.java @@ -1,257 +1,251 @@ -package org.wicketstuff.openlayers3.api.interaction; - -import org.apache.wicket.ajax.AjaxRequestTarget; -import org.wicketstuff.openlayers3.api.Feature; -import org.wicketstuff.openlayers3.api.layer.Vector; -import org.wicketstuff.openlayers3.api.style.Style; - -import java.io.Serializable; -import java.util.List; - -/** - * Provides an object that models a map modify interaction. - */ -public class Modify extends Interaction implements Serializable { - - /** - * Features for this interaction. - */ - private Feature[] features; - - /** - * Source of features for this interaction. - */ - private Vector vector; - - /** - * Style for this interaction. - */ - private Style style; - - /** - * Creates a new instance. - * - * @param features - * Features for this interaction - */ - public Modify(Feature... features) { - this(new Style(), features); - } - - /** - * Creates a new instance. - * - * @param style - * Style for this interaction (applied to feature when interacting) - * @param features - * Features for this interaction - */ - public Modify(Style style, Feature... features) { - this.features = features; - this.vector = null; - this.style = style; - } - - /** - * Creates a new instance. - * - * @param vector - * Vector of features for this interaction - */ - public Modify(Vector vector) { - this(null, vector); - } - - /** - * Creates a new instance. - * - * @param style - * Style for this interaction (applied to feature when interacting) - * @param vector - * Vector of features for this interaction - */ - public Modify(Style style, Vector vector) { - super(); - - this.features = null; - this.vector = vector; - this.style = style; - } - - /** - * Creates a new instance. - * - * @param style - * Style for this interaction (applied to feature when interacting) - * @param vector - * Vector of features for this interaction - * @param features - * Features for this interaction - */ - private Modify(Style style, Vector vector, Feature... features) { - super(); - - this.features = features; - this.vector = vector; - this.style = style; - } - - /** - * Returns the features for this interaction. - * - * @return Array of features - */ - public Feature[] getFeatures() { - return features; - } - - /** - * Sets the features for this interaction. - * - * @param features - * New value - */ - public void setFeatures(Feature[] features) { - this.features = features; - } - - /** - * Sets the features for this interaction. - * - * @param features - * New value - * @return This instance - */ - public Modify features(Feature[] features) { - setFeatures(features); - return this; - } - - /** - * Sets the features for this interaction. - * - * @param featuresList - * A list of feature instances - * @return This instance - */ - public Modify features(List featuresList) { - setFeatures(featuresList.toArray(new Feature[]{})); - return this; - } - - /** - * Returns the vector source for this interaction's features. - * - * @return Vector source of features - */ - public Vector getVector() { - return vector; - } - - /** - * Sets the vector source for this interaction's features. - * - * @param vector - * Vector source of features - */ - public void setVector(Vector vector) { - this.vector = vector; - } - - /** - * Sets the vector source for this interaction's features. - * - * @param vector - * Vector source of features - * @return This instance - */ - public Modify vector(Vector vector) { - setVector(vector); - return this; - } - - /** - * Returns the style for this interaction. - * - * @return Current value - */ - public Style getStyle() { - return style; - } - - /** - * Sets the style for this interaction. - * - * @param style - * New value - */ - public void setStyle(Style style) { - this.style = style; - } - - /** - * Sets the style for this interaction. - * - * @param style - * New value - * @return This instance - */ - public Modify style(Style style) { - setStyle(style); - return this; - } - - /** - * Notifies the interaction that it has changed. - * - * @param target AJAX request target - */ - public void changed(AjaxRequestTarget target) { - target.appendJavaScript(getJsId() + ".changed();"); - } - - @Override - public String getJsType() { - return "ol.interaction.Modify"; - } - - @Override - public String renderJs() { - - StringBuilder builder = new StringBuilder(); - builder.append("{"); - builder.append(renderAttributesJs()); - builder.append("}"); - - return builder.toString(); - } - - protected String renderAttributesJs() { - - StringBuilder builder = new StringBuilder(); - - if (features != null && features.length > 0) { - builder.append("'features': new ol.Collection(["); - - for (Feature feature : features) { - builder.append(feature.getJsId() + ","); - } - - builder.append("]),"); - } - - if (vector != null) { - builder.append("'features': new ol.Collection("); - builder.append(vector.getJsId() + ".getSource().getFeatures()"); - builder.append("),"); - } - - if (style != null) { - builder.append("'style': new " + style.getJsType() + "(" + style.renderJs() + "), "); - } else { - builder.append("'style': new ol.style.Style(), "); - } - - return builder.toString(); - } -} +package org.wicketstuff.openlayers3.api.interaction; + +import java.io.Serializable; +import java.util.List; + +import org.apache.wicket.ajax.AjaxRequestTarget; +import org.wicketstuff.openlayers3.api.Feature; +import org.wicketstuff.openlayers3.api.layer.Vector; +import org.wicketstuff.openlayers3.api.style.Style; + +/** + * Provides an object that models a map modify interaction. + */ +public class Modify extends Interaction implements Serializable { + + /** + * Features for this interaction. + */ + private Feature[] features; + + /** + * Source of features for this interaction. + */ + private Vector vector; + + /** + * Style for this interaction. + */ + private Style style; + + /** + * Creates a new instance. + * + * @param features + * Features for this interaction + */ + public Modify(Feature... features) { + this(new Style(), features); + } + + /** + * Creates a new instance. + * + * @param style + * Style for this interaction (applied to feature when interacting) + * @param features + * Features for this interaction + */ + public Modify(Style style, Feature... features) { + this(style, null, features); + } + + /** + * Creates a new instance. + * + * @param vector + * Vector of features for this interaction + */ + public Modify(Vector vector) { + this(null, vector); + } + + /** + * Creates a new instance. + * + * @param style + * Style for this interaction (applied to feature when interacting) + * @param vector + * Vector of features for this interaction + */ + public Modify(Style style, Vector vector) { + this(style, vector, (Feature[]) null); + } + + /** + * Creates a new instance. + * + * @param style + * Style for this interaction (applied to feature when interacting) + * @param vector + * Vector of features for this interaction + * @param features + * Features for this interaction + */ + public Modify(Style style, Vector vector, Feature... features) { + super(); + + this.features = features; + this.vector = vector; + this.style = style; + } + + /** + * Returns the features for this interaction. + * + * @return Array of features + */ + public Feature[] getFeatures() { + return features; + } + + /** + * Sets the features for this interaction. + * + * @param features + * New value + */ + public void setFeatures(Feature[] features) { + this.features = features; + } + + /** + * Sets the features for this interaction. + * + * @param features + * New value + * @return This instance + */ + public Modify features(Feature[] features) { + setFeatures(features); + return this; + } + + /** + * Sets the features for this interaction. + * + * @param featuresList + * A list of feature instances + * @return This instance + */ + public Modify features(List featuresList) { + setFeatures(featuresList.toArray(new Feature[]{})); + return this; + } + + /** + * Returns the vector source for this interaction's features. + * + * @return Vector source of features + */ + public Vector getVector() { + return vector; + } + + /** + * Sets the vector source for this interaction's features. + * + * @param vector + * Vector source of features + */ + public void setVector(Vector vector) { + this.vector = vector; + } + + /** + * Sets the vector source for this interaction's features. + * + * @param vector + * Vector source of features + * @return This instance + */ + public Modify vector(Vector vector) { + setVector(vector); + return this; + } + + /** + * Returns the style for this interaction. + * + * @return Current value + */ + public Style getStyle() { + return style; + } + + /** + * Sets the style for this interaction. + * + * @param style + * New value + */ + public void setStyle(Style style) { + this.style = style; + } + + /** + * Sets the style for this interaction. + * + * @param style + * New value + * @return This instance + */ + public Modify style(Style style) { + setStyle(style); + return this; + } + + /** + * Notifies the interaction that it has changed. + * + * @param target AJAX request target + */ + public void changed(AjaxRequestTarget target) { + target.appendJavaScript(getJsId() + ".changed();"); + } + + @Override + public String getJsType() { + return "ol.interaction.Modify"; + } + + @Override + public String renderJs() { + + StringBuilder builder = new StringBuilder(); + builder.append("{"); + builder.append(renderAttributesJs()); + builder.append("}"); + + return builder.toString(); + } + + protected String renderAttributesJs() { + + StringBuilder builder = new StringBuilder(); + + if (features != null && features.length > 0) { + builder.append("'features': new ol.Collection(["); + + for (Feature feature : features) { + builder.append(feature.getJsId() + ","); + } + + builder.append("]),"); + } + + if (vector != null) { + builder.append("'features': new ol.Collection("); + builder.append(vector.getJsId() + ".getSource().getFeatures()"); + builder.append("),"); + } + + if (style != null) { + builder.append("'style': new " + style.getJsType() + "(" + style.renderJs() + "), "); + } else { + builder.append("'style': new ol.style.Style(), "); + } + + return builder.toString(); + } +} diff --git a/openlayers3-parent/openlayers3/src/main/java/org/wicketstuff/openlayers3/api/layer/Layer.java b/openlayers3-parent/openlayers3/src/main/java/org/wicketstuff/openlayers3/api/layer/Layer.java index ddf2c0fa0b..36345887d1 100644 --- a/openlayers3-parent/openlayers3/src/main/java/org/wicketstuff/openlayers3/api/layer/Layer.java +++ b/openlayers3-parent/openlayers3/src/main/java/org/wicketstuff/openlayers3/api/layer/Layer.java @@ -1,24 +1,105 @@ package org.wicketstuff.openlayers3.api.layer; +import java.io.Serializable; + import org.apache.wicket.ajax.AjaxRequestTarget; +import org.apache.wicket.model.IModel; import org.wicketstuff.openlayers3.api.JavascriptObject; -import java.io.Serializable; - /** * Provides an object that models a layer. */ public abstract class Layer extends JavascriptObject implements Serializable { + private IModel visibleModel; + private IModel opacityModel; + + /** + * Creates a new Layer. + */ + public Layer() { + super(); + } + + /** + * Creates a new Layer with the visibility determined by the + * {@code visibleModel} + * + * @param visibleModel + * a modle of whether the layer is visible + */ + public Layer(IModel visibleModel) { + this.visibleModel = visibleModel; + } + /** * Sets the visiblity of the layer. * * @param target - * Ajax request target + * Ajax request target * @param visible - * Flag with the visibility of the layer + * Flag with the visibility of the layer */ public void setVisible(AjaxRequestTarget target, boolean visible) { target.appendJavaScript(getJsId() + ".setVisible(" + visible + ")"); } + + /** + * Callback for updating the layer via ajax. + * + * @param target + */ + public void onUpdate(AjaxRequestTarget target) { + if (visibleModel != null) { + target.appendJavaScript(getJsId() + ".setVisible(" + visibleModel.getObject() + ")"); + } + if (opacityModel != null) { + target.appendJavaScript(getJsId() + ".setOpacity(" + opacityModel.getObject() + ")"); + } + } + + /** + * @return the model of the visibility of the layer + */ + public IModel getVisibleModel() { + return visibleModel; + } + + /** + * @param visibleModel visible model to set + * @return this + */ + public Layer setVisibleModel(IModel visibleModel) { + this.visibleModel = visibleModel; + return this; + } + + /** + * @return the model of the opacity of the layer + */ + public IModel getOpacityModel() { + return opacityModel; + } + + /** + * @param opacityModel opacity model to set + * @return this + */ + public Layer setOpacityModel(IModel opacityModel) { + this.opacityModel = opacityModel; + return this; + } + + /** + * Callback for detaching any resources (e.g., models). + */ + public void detach() { + if (visibleModel != null) { + visibleModel.detach(); + } + if (opacityModel != null) { + opacityModel.detach(); + } + } + } diff --git a/openlayers3-parent/openlayers3/src/main/java/org/wicketstuff/openlayers3/api/layer/Tile.java b/openlayers3-parent/openlayers3/src/main/java/org/wicketstuff/openlayers3/api/layer/Tile.java index 4ee6d05514..a8ae55e9ad 100644 --- a/openlayers3-parent/openlayers3/src/main/java/org/wicketstuff/openlayers3/api/layer/Tile.java +++ b/openlayers3-parent/openlayers3/src/main/java/org/wicketstuff/openlayers3/api/layer/Tile.java @@ -1,5 +1,6 @@ package org.wicketstuff.openlayers3.api.layer; +import org.apache.wicket.model.IModel; import org.wicketstuff.openlayers3.api.source.tile.TileSource; /** @@ -42,6 +43,21 @@ public Tile(String title, TileSource source) { setSource(source); } + /** + * Creates a new instance. + * + * @param title + * The title for the layer + * @param source + * The source of data for this layer + */ + public Tile(String title, TileSource source, IModel visibleModel) { + super(visibleModel); + + this.title = title; + setSource(source); + } + /** * Returns the title for this layer. * @@ -119,6 +135,10 @@ public String renderJs() { builder.append("'title': '" + getTitle() + "',"); } + if (getVisibleModel() != null) { + builder.append("'visible': " + getVisibleModel().getObject() + ","); + } + builder.append("'source': new " + getSource().getJsType() + "("); builder.append(getSource().renderJs()); builder.append(")"); diff --git a/openlayers3-parent/openlayers3/src/main/java/org/wicketstuff/openlayers3/api/source/tile/TileArcGISRest.java b/openlayers3-parent/openlayers3/src/main/java/org/wicketstuff/openlayers3/api/source/tile/TileArcGISRest.java new file mode 100644 index 0000000000..5972c6b1cc --- /dev/null +++ b/openlayers3-parent/openlayers3/src/main/java/org/wicketstuff/openlayers3/api/source/tile/TileArcGISRest.java @@ -0,0 +1,41 @@ +package org.wicketstuff.openlayers3.api.source.tile; + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.List; + +import com.google.common.base.Joiner; + +/** + * Provides an object that models an ESRI ArcGIS tile end-point. + *

+ * Example: new TileArcGISRest().setUrl("http://www.orthos.dhses.ny.gov/arcgis/rest/services/Latest/MapServer") + *

+ * @see http://openlayers.org/en/latest/apidoc/ol.source.TileArcGISRest.html + */ +public class TileArcGISRest extends TileSource implements Serializable { + + private String url; + + @Override + public String getJsType() { + return "ol.source.TileArcGISRest"; + } + + @Override + public String renderJs() { + List list = new ArrayList<>(); + list.add("'url': '" + getUrl() + "'"); + return "{" + Joiner.on(", ").join(list) + "}"; + } + + public String getUrl() { + return url; + } + + public TileArcGISRest setUrl(String url) { + this.url = url; + return this; + } + +} diff --git a/openlayers3-parent/openlayers3/src/main/java/org/wicketstuff/openlayers3/api/source/tile/XYZ.java b/openlayers3-parent/openlayers3/src/main/java/org/wicketstuff/openlayers3/api/source/tile/XYZ.java new file mode 100644 index 0000000000..eb51a4bbdd --- /dev/null +++ b/openlayers3-parent/openlayers3/src/main/java/org/wicketstuff/openlayers3/api/source/tile/XYZ.java @@ -0,0 +1,41 @@ +package org.wicketstuff.openlayers3.api.source.tile; + +import java.util.ArrayList; +import java.util.List; + +import com.google.common.base.Joiner; + +/** + * Provides an object that models an XYZ data source where the requested tiles are part of the URL. + * The placeholders {x}, {y} and {z} are used to indicate how the URL is to be constructed. + *

+ * Example: new XYZ().setUrl("http://tiles.arcgis.com/tiles/hGdibHYSPO59RG1h/arcgis/rest/services/USGS_Orthos_2013_2014/MapServer/tile/{z}/{y}/{x}") + *

+ * @see http://openlayers.org/en/latest/apidoc/ol.source.XYZ.html + */ +public class XYZ extends TileSource { + + private String url; + + public String getUrl() { + return url; + } + + public XYZ setUrl(String url) { + this.url = url; + return this; + } + + @Override + public String getJsType() { + return "ol.source.XYZ"; + } + + @Override + public String renderJs() { + List list = new ArrayList<>(); + list.add("'url': '" + getUrl() + "'"); + return "{" + Joiner.on(", ").join(list) + "}"; + } + +} diff --git a/openlayers3-parent/openlayers3/src/main/java/org/wicketstuff/openlayers3/api/source/vector/loader/VectorFeatureDataLoadedListener.java b/openlayers3-parent/openlayers3/src/main/java/org/wicketstuff/openlayers3/api/source/vector/loader/VectorFeatureDataLoadedListener.java index ebdade3bbf..3ed340b8c2 100644 --- a/openlayers3-parent/openlayers3/src/main/java/org/wicketstuff/openlayers3/api/source/vector/loader/VectorFeatureDataLoadedListener.java +++ b/openlayers3-parent/openlayers3/src/main/java/org/wicketstuff/openlayers3/api/source/vector/loader/VectorFeatureDataLoadedListener.java @@ -1,23 +1,21 @@ package org.wicketstuff.openlayers3.api.source.vector.loader; -import com.google.gson.JsonArray; -import com.google.gson.JsonElement; -import com.google.gson.JsonNull; -import com.google.gson.JsonParser; +import java.util.HashMap; +import java.util.Map; + import org.apache.wicket.Component; import org.apache.wicket.ajax.AbstractDefaultAjaxBehavior; import org.apache.wicket.ajax.AjaxRequestTarget; import org.apache.wicket.markup.head.IHeaderResponse; -import org.apache.wicket.markup.head.OnDomReadyHeaderItem; import org.apache.wicket.request.IRequestParameters; import org.apache.wicket.request.cycle.RequestCycle; -import org.apache.wicket.util.template.PackageTextTemplate; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.wicketstuff.openlayers3.api.layer.Vector; +import org.wicketstuff.openlayers3.api.util.HeaderUtils; -import java.util.HashMap; -import java.util.Map; +import com.google.gson.JsonArray; +import com.google.gson.JsonElement; +import com.google.gson.JsonNull; +import com.google.gson.JsonParser; /** * Provides a behavior that invokes a callback after feature data has been loaded into the vector layer. This is used to @@ -26,8 +24,6 @@ */ public abstract class VectorFeatureDataLoadedListener extends AbstractDefaultAjaxBehavior { - private final static Logger logger = LoggerFactory.getLogger(VectorFeatureDataLoadedListener.class); - /** * Counter for generating instance identifiers. */ @@ -117,8 +113,7 @@ public void renderHead(Component component, IHeaderResponse response) { params.put("componentId", vector.getJsId()); params.put("dataLoaderId", getId()); - PackageTextTemplate template = new PackageTextTemplate(VectorFeatureDataLoadedListener.class, - "VectorFeatureDataLoadedListener.js"); - response.render(OnDomReadyHeaderItem.forScript(template.asString(params))); + HeaderUtils.renderOnDomReady(response, VectorFeatureDataLoadedListener.class, + "VectorFeatureDataLoadedListener.js", params); } } diff --git a/openlayers3-parent/openlayers3/src/main/java/org/wicketstuff/openlayers3/api/source/vector/loader/VectorFeaturesLoadedListener.java b/openlayers3-parent/openlayers3/src/main/java/org/wicketstuff/openlayers3/api/source/vector/loader/VectorFeaturesLoadedListener.java index 083ab6ecac..4de68f0291 100644 --- a/openlayers3-parent/openlayers3/src/main/java/org/wicketstuff/openlayers3/api/source/vector/loader/VectorFeaturesLoadedListener.java +++ b/openlayers3-parent/openlayers3/src/main/java/org/wicketstuff/openlayers3/api/source/vector/loader/VectorFeaturesLoadedListener.java @@ -1,17 +1,14 @@ package org.wicketstuff.openlayers3.api.source.vector.loader; +import java.util.HashMap; +import java.util.Map; + import org.apache.wicket.Component; import org.apache.wicket.ajax.AbstractDefaultAjaxBehavior; import org.apache.wicket.ajax.AjaxRequestTarget; import org.apache.wicket.markup.head.IHeaderResponse; -import org.apache.wicket.markup.head.OnDomReadyHeaderItem; -import org.apache.wicket.util.template.PackageTextTemplate; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.wicketstuff.openlayers3.api.layer.Vector; - -import java.util.HashMap; -import java.util.Map; +import org.wicketstuff.openlayers3.api.util.HeaderUtils; /** * Provides a behavior that invokes a callback after feature data has been loaded into the vector layer. This is used to @@ -20,8 +17,6 @@ */ public abstract class VectorFeaturesLoadedListener extends AbstractDefaultAjaxBehavior { - private final static Logger logger = LoggerFactory.getLogger(VectorFeatureDataLoadedListener.class); - /** * Counter for generating instance identifiers. */ @@ -108,8 +103,7 @@ public void renderHead(Component component, IHeaderResponse response) { params.put("componentId", vector.getJsId()); params.put("dataLoaderId", getId()); - PackageTextTemplate template = new PackageTextTemplate(VectorFeatureDataLoadedListener.class, - "VectorFeaturesLoadedListener.js"); - response.render(OnDomReadyHeaderItem.forScript(template.asString(params))); + HeaderUtils.renderOnDomReady(response, VectorFeaturesLoadedListener.class, + "VectorFeaturesLoadedListener.js", params); } } diff --git a/openlayers3-parent/openlayers3/src/main/java/org/wicketstuff/openlayers3/api/style/Circle.java b/openlayers3-parent/openlayers3/src/main/java/org/wicketstuff/openlayers3/api/style/Circle.java index b68217d3b0..41c3d9456d 100644 --- a/openlayers3-parent/openlayers3/src/main/java/org/wicketstuff/openlayers3/api/style/Circle.java +++ b/openlayers3-parent/openlayers3/src/main/java/org/wicketstuff/openlayers3/api/style/Circle.java @@ -1,172 +1,166 @@ -package org.wicketstuff.openlayers3.api.style; - -/** - * Provides a class that models a circle style. - */ -public class Circle extends Style { - - /** - * The radius of the circle. - */ - private Number radius; - - /** - * If true integral numbers of pixels are used as the X and Y pixel coordinate when drawing the circle in the - * output canvas. If false fractional numbers may be used. Using true allows for "sharp" rendering (no blur), - * while using false allows for "accurate" rendering. Note that accuracy is important if the circle's position - * is animated. Without it, the circle may jitter noticeably. Default value is true. - */ - private boolean snapToPixel; - - /** - * The stroke used when drawing the circle. - */ - private Stroke stroke; - - /** - * Creates a new instance. - * - * @param fill The fill for the circle - * @param radius The radius of the circle - * @param stroke The stroke for the circle - */ - public Circle(Fill fill, Number radius, Stroke stroke) { - this(fill, radius, true, stroke); - } - - /** - * Creates a new instance. - * - * @param fill The fill for the circle - * @param radius The radius of the circle - * @param snapToPixel Indicates if pixels or fractional values are used - * @param stroke The stroke for the circle - */ - public Circle(Fill fill, Number radius, boolean snapToPixel, Stroke stroke) { - super(fill); - this.radius = radius; - this.snapToPixel = snapToPixel; - this.stroke = stroke; - } - - @Override - public Circle fill(Fill fill) { - super.fill(fill); - return this; - } - - @Override - public Circle image(Image image) { - super.image(image); - return this; - } - - @Override - public Circle stroke(Stroke stroke) { - super.stroke(stroke); - return this; - } - - @Override - public Circle text(Text text) { - super.text(text); - return this; - } - - @Override - public Style zIndex(Number zIndex) { - super.zIndex(zIndex); - return this; - } - - /** - * Returns the radius of the circle. - * - * @return Current value - */ - public Number getRadius() { - return radius; - } - - /** - * Sets the radius of the circle. - * - * @param radius New value - */ - public void setRadius(Number radius) { - this.radius = radius; - } - - /** - * Sets the radius of the circle. - * - * @param radius New value - * @return This instance - */ - public Circle radius(Number radius) { - setRadius(radius); - return this; - } - - /** - * Returns the snap to pixel value. - * - * @return Current value - */ - public boolean isSnapToPixel() { - return snapToPixel; - } - - /** - * Sets the snap to pixel value. - * - * @param snapToPixel New value - */ - public void setSnapToPixel(boolean snapToPixel) { - this.snapToPixel = snapToPixel; - } - - /** - * Sets the snap to pixel value. - * - * @param snapToPixel New value - * @return This instace - */ - public Circle snapToPixel(boolean snapToPixel) { - setSnapToPixel(snapToPixel); - return this; - } - - @Override - public String getJsType() { - return "ol.style.Circle"; - } - - @Override - public String renderAttributesJs() { - - StringBuilder builder = new StringBuilder(); - - builder.append(super.renderAttributesJs()); - - if (getRadius() != null) { - builder.append("'radius': " + getRadius() + ","); - } - - if (isSnapToPixel()) { - builder.append("'snapToPixel': " + isSnapToPixel() + ","); - } - - return builder.toString(); - } - - @Override - public String renderJs() { - - StringBuilder builder = new StringBuilder(); - builder.append("{"); - builder.append(renderAttributesJs()); - builder.append("}"); - return builder.toString(); - } -} +package org.wicketstuff.openlayers3.api.style; + +/** + * Provides a class that models a circle style. + */ +public class Circle extends Style { + + /** + * The radius of the circle. + */ + private Number radius; + + /** + * If true integral numbers of pixels are used as the X and Y pixel coordinate when drawing the circle in the + * output canvas. If false fractional numbers may be used. Using true allows for "sharp" rendering (no blur), + * while using false allows for "accurate" rendering. Note that accuracy is important if the circle's position + * is animated. Without it, the circle may jitter noticeably. Default value is true. + */ + private boolean snapToPixel; + + /** + * Creates a new instance. + * + * @param fill The fill for the circle + * @param radius The radius of the circle + * @param stroke The stroke for the circle + */ + public Circle(Fill fill, Number radius, Stroke stroke) { + this(fill, radius, true, stroke); + } + + /** + * Creates a new instance. + * + * @param fill The fill for the circle + * @param radius The radius of the circle + * @param snapToPixel Indicates if pixels or fractional values are used + * @param stroke The stroke for the circle + */ + public Circle(Fill fill, Number radius, boolean snapToPixel, Stroke stroke) { + super(fill, stroke); + this.radius = radius; + this.snapToPixel = snapToPixel; + } + + @Override + public Circle fill(Fill fill) { + super.fill(fill); + return this; + } + + @Override + public Circle image(Image image) { + super.image(image); + return this; + } + + @Override + public Circle stroke(Stroke stroke) { + super.stroke(stroke); + return this; + } + + @Override + public Circle text(Text text) { + super.text(text); + return this; + } + + @Override + public Style zIndex(Number zIndex) { + super.zIndex(zIndex); + return this; + } + + /** + * Returns the radius of the circle. + * + * @return Current value + */ + public Number getRadius() { + return radius; + } + + /** + * Sets the radius of the circle. + * + * @param radius New value + */ + public void setRadius(Number radius) { + this.radius = radius; + } + + /** + * Sets the radius of the circle. + * + * @param radius New value + * @return This instance + */ + public Circle radius(Number radius) { + setRadius(radius); + return this; + } + + /** + * Returns the snap to pixel value. + * + * @return Current value + */ + public boolean isSnapToPixel() { + return snapToPixel; + } + + /** + * Sets the snap to pixel value. + * + * @param snapToPixel New value + */ + public void setSnapToPixel(boolean snapToPixel) { + this.snapToPixel = snapToPixel; + } + + /** + * Sets the snap to pixel value. + * + * @param snapToPixel New value + * @return This instace + */ + public Circle snapToPixel(boolean snapToPixel) { + setSnapToPixel(snapToPixel); + return this; + } + + @Override + public String getJsType() { + return "ol.style.Circle"; + } + + @Override + public String renderAttributesJs() { + + StringBuilder builder = new StringBuilder(); + + builder.append(super.renderAttributesJs()); + + if (getRadius() != null) { + builder.append("'radius': " + getRadius() + ","); + } + + if (isSnapToPixel()) { + builder.append("'snapToPixel': " + isSnapToPixel() + ","); + } + + return builder.toString(); + } + + @Override + public String renderJs() { + + StringBuilder builder = new StringBuilder(); + builder.append("{"); + builder.append(renderAttributesJs()); + builder.append("}"); + return builder.toString(); + } +} diff --git a/openlayers3-parent/openlayers3/src/main/java/org/wicketstuff/openlayers3/api/style/Style.java b/openlayers3-parent/openlayers3/src/main/java/org/wicketstuff/openlayers3/api/style/Style.java index 140d4f3655..1d996edebe 100644 --- a/openlayers3-parent/openlayers3/src/main/java/org/wicketstuff/openlayers3/api/style/Style.java +++ b/openlayers3-parent/openlayers3/src/main/java/org/wicketstuff/openlayers3/api/style/Style.java @@ -1,284 +1,296 @@ -package org.wicketstuff.openlayers3.api.style; - -import org.wicketstuff.openlayers3.api.JavascriptObject; - -import java.io.Serializable; - -/** - * Provides an object that models a style for rendering vector features. - */ -public class Style extends JavascriptObject implements Serializable { - - /** - * The fill for the style. - */ - private Fill fill; - - /** - * The image for the style. - */ - private Image image; - - /** - * The stroke for this style. - */ - private Stroke stroke; - - /** - * The text for this style. - */ - private Text text; - - /** - * The z-index for this style. - */ - private Number zIndex; - - /** - * Creates a new instance. - */ - public Style() { - - } - - /** - * Creates a new instance. - * - * @param image - * The image for the style - */ - public Style(final Image image) { - this(null, image, null, null, null); - } - - /** - * Creates a new instance. - * - * @param fill - * The fill for the style. - */ - public Style(final Fill fill) { - this(fill, null, null, null, null); - } - - /** - * Creates a new instance. - * - * @param fill - * The fill for this style - * @param image - * The image for this style - * @param stroke - * The stroke for this style - * @param text - * The text for this style - * @param zIndex - * The z-index for this style - */ - public Style(Fill fill, Image image, Stroke stroke, Text text, Number zIndex) { - super(); - - this.fill = fill; - this.image = image; - this.stroke = stroke; - this.text = text; - this.zIndex = zIndex; - } - - /** - * Returns the fill for the style. - * - * @return This style's fill - */ - public Fill getFill() { - return fill; - } - - /** - * Sets the fill for the style. - * - * @param fill - * New value - */ - public void setFill(Fill fill) { - this.fill = fill; - } - - /** - * Sets the fill for the style. - * - * @param fill - * New value - * @return This instance - */ - public Style fill(Fill fill) { - setFill(fill); - return this; - } - - /** - * Returns the image for this style. - * - * @return This style's image - */ - public Image getImage() { - return image; - } - - /** - * Sets the image for this style. - * - * @param image - * New value - */ - public void setImage(Image image) { - this.image = image; - } - - /** - * Sets the image for this style. - * - * @param image - * New value - * @return This instance - */ - public Style image(Image image) { - setImage(image); - return this; - } - - /** - * Returns the stroke for this style. - * - * @return Current value - */ - public Stroke getStroke() { - return stroke; - } - - /** - * Sets the stroke for this style - * - * @param stroke - * New value - */ - public void setStroke(Stroke stroke) { - this.stroke = stroke; - } - - /** - * Sets the stroke for this style. - * - * @param stroke - * New value - * @return This instance - */ - public Style stroke(Stroke stroke) { - setStroke(stroke); - return this; - } - - /** - * Returns the text for this style. - * - * @return Current value - */ - public Text getText() { - return text; - } - - /** - * Sets the text for this instance. - * - * @param text - * New value - */ - public void setText(Text text) { - this.text = text; - } - - /** - * Sets the text for this style. - * - * @param text - * New value - * @return This instance - */ - public Style text(Text text) { - setText(text); - return this; - } - - /** - * Returns the current z-index for this style. - * - * @return Current value - */ - public Number getzIndex() { - return zIndex; - } - - /** - * Sets the z-index for this style. - * - * @param zIndex - * New value - */ - public void setzIndex(Number zIndex) { - this.zIndex = zIndex; - } - - /** - * Sets the z-index for this style. - * - * @param zIndex - * New value - * @return This instance - */ - public Style zIndex(Number zIndex) { - setzIndex(zIndex); - return this; - } - - @Override - public String getJsType() { - return "ol.style.Style"; - } - - public String renderAttributesJs() { - - StringBuilder builder = new StringBuilder(); - - if (getFill() != null) { - builder.append("'fill': new " + getFill().getJsType() + "(" + getFill().renderJs() + "),"); - } - - if (getImage() != null) { - builder.append("'image': new " + getImage().getJsType() + "(" + getImage().renderJs() + "),"); - } - - if (getStroke() != null) { - builder.append("'stroke': new " + getStroke().getJsType() + "(" + getStroke().renderJs() + "),"); - } - - if (getText() != null) { - builder.append("'text': new " + getText().getJsType() + "(" + getText().renderJs() + "),"); - } - - if (getzIndex() != null) { - builder.append("'zIndex': " + getzIndex() + ","); - } - - return builder.toString(); - } - - @Override - public String renderJs() { - - StringBuilder builder = new StringBuilder(); - builder.append("{"); - builder.append(renderAttributesJs()); - builder.append("}"); - return builder.toString(); - } -} +package org.wicketstuff.openlayers3.api.style; + +import java.io.Serializable; + +import org.wicketstuff.openlayers3.api.JavascriptObject; + +/** + * Provides an object that models a style for rendering vector features. + */ +public class Style extends JavascriptObject implements Serializable { + + /** + * The fill for the style. + */ + private Fill fill; + + /** + * The image for the style. + */ + private Image image; + + /** + * The stroke for this style. + */ + private Stroke stroke; + + /** + * The text for this style. + */ + private Text text; + + /** + * The z-index for this style. + */ + private Number zIndex; + + /** + * Creates a new instance. + */ + public Style() { + + } + + /** + * Creates a new instance. + * + * @param image + * The image for the style + */ + public Style(final Image image) { + this(null, image, null, null, null); + } + + /** + * Creates a new instance. + * + * @param fill + * The fill for the style. + */ + public Style(final Fill fill) { + this(fill, null, null, null, null); + } + + /** + * Creates a new instance. + * + * @param fill + * The fill for this style + * @param stroke + * The stroke for this style + */ + public Style(final Fill fill, final Stroke stroke) { + this(fill, null, stroke, null, null); + } + + /** + * Creates a new instance. + * + * @param fill + * The fill for this style + * @param image + * The image for this style + * @param stroke + * The stroke for this style + * @param text + * The text for this style + * @param zIndex + * The z-index for this style + */ + public Style(Fill fill, Image image, Stroke stroke, Text text, Number zIndex) { + super(); + + this.fill = fill; + this.image = image; + this.stroke = stroke; + this.text = text; + this.zIndex = zIndex; + } + + /** + * Returns the fill for the style. + * + * @return This style's fill + */ + public Fill getFill() { + return fill; + } + + /** + * Sets the fill for the style. + * + * @param fill + * New value + */ + public void setFill(Fill fill) { + this.fill = fill; + } + + /** + * Sets the fill for the style. + * + * @param fill + * New value + * @return This instance + */ + public Style fill(Fill fill) { + setFill(fill); + return this; + } + + /** + * Returns the image for this style. + * + * @return This style's image + */ + public Image getImage() { + return image; + } + + /** + * Sets the image for this style. + * + * @param image + * New value + */ + public void setImage(Image image) { + this.image = image; + } + + /** + * Sets the image for this style. + * + * @param image + * New value + * @return This instance + */ + public Style image(Image image) { + setImage(image); + return this; + } + + /** + * Returns the stroke for this style. + * + * @return Current value + */ + public Stroke getStroke() { + return stroke; + } + + /** + * Sets the stroke for this style + * + * @param stroke + * New value + */ + public void setStroke(Stroke stroke) { + this.stroke = stroke; + } + + /** + * Sets the stroke for this style. + * + * @param stroke + * New value + * @return This instance + */ + public Style stroke(Stroke stroke) { + setStroke(stroke); + return this; + } + + /** + * Returns the text for this style. + * + * @return Current value + */ + public Text getText() { + return text; + } + + /** + * Sets the text for this instance. + * + * @param text + * New value + */ + public void setText(Text text) { + this.text = text; + } + + /** + * Sets the text for this style. + * + * @param text + * New value + * @return This instance + */ + public Style text(Text text) { + setText(text); + return this; + } + + /** + * Returns the current z-index for this style. + * + * @return Current value + */ + public Number getzIndex() { + return zIndex; + } + + /** + * Sets the z-index for this style. + * + * @param zIndex + * New value + */ + public void setzIndex(Number zIndex) { + this.zIndex = zIndex; + } + + /** + * Sets the z-index for this style. + * + * @param zIndex + * New value + * @return This instance + */ + public Style zIndex(Number zIndex) { + setzIndex(zIndex); + return this; + } + + @Override + public String getJsType() { + return "ol.style.Style"; + } + + public String renderAttributesJs() { + + StringBuilder builder = new StringBuilder(); + + if (getFill() != null) { + builder.append("'fill': new " + getFill().getJsType() + "(" + getFill().renderJs() + "),"); + } + + if (getImage() != null) { + builder.append("'image': new " + getImage().getJsType() + "(" + getImage().renderJs() + "),"); + } + + if (getStroke() != null) { + builder.append("'stroke': new " + getStroke().getJsType() + "(" + getStroke().renderJs() + "),"); + } + + if (getText() != null) { + builder.append("'text': new " + getText().getJsType() + "(" + getText().renderJs() + "),"); + } + + if (getzIndex() != null) { + builder.append("'zIndex': " + getzIndex() + ","); + } + + return builder.toString(); + } + + @Override + public String renderJs() { + + StringBuilder builder = new StringBuilder(); + builder.append("{"); + builder.append(renderAttributesJs()); + builder.append("}"); + return builder.toString(); + } +} diff --git a/openlayers3-parent/openlayers3/src/main/java/org/wicketstuff/openlayers3/api/util/HeaderUtils.java b/openlayers3-parent/openlayers3/src/main/java/org/wicketstuff/openlayers3/api/util/HeaderUtils.java new file mode 100644 index 0000000000..b82bd806f7 --- /dev/null +++ b/openlayers3-parent/openlayers3/src/main/java/org/wicketstuff/openlayers3/api/util/HeaderUtils.java @@ -0,0 +1,42 @@ +package org.wicketstuff.openlayers3.api.util; + +import java.io.IOException; +import java.util.Map; + +import org.apache.wicket.WicketRuntimeException; +import org.apache.wicket.markup.head.IHeaderResponse; +import org.apache.wicket.markup.head.OnDomReadyHeaderItem; +import org.apache.wicket.util.template.PackageTextTemplate; + +/** + * Header-related utility methods. + */ +public final class HeaderUtils { + + /** + * Renders the template, filled with the {@code params}, as an + * {@link OnDomReadyHeaderItem}. + * + * @param response + * {@link IHeaderResponse} to render the script into + * @param clazz + * Class the template file is relative to + * @param fileName + * Name of the template file + * @param params + * {@link Map} of the parameters to use to fill the template + */ + public static void renderOnDomReady(IHeaderResponse response, Class clazz, String fileName, + Map params) { + try (PackageTextTemplate template = new PackageTextTemplate(clazz, fileName);) { + response.render(OnDomReadyHeaderItem.forScript(template.asString(params))); + } catch (IOException e) { + throw new WicketRuntimeException(e); + } + } + + private HeaderUtils() { + + } + +} diff --git a/openlayers3-parent/openlayers3/src/main/java/org/wicketstuff/openlayers3/behavior/ClickFeatureHandler.java b/openlayers3-parent/openlayers3/src/main/java/org/wicketstuff/openlayers3/behavior/ClickFeatureHandler.java index 8aee987cff..3b010c32ef 100644 --- a/openlayers3-parent/openlayers3/src/main/java/org/wicketstuff/openlayers3/behavior/ClickFeatureHandler.java +++ b/openlayers3-parent/openlayers3/src/main/java/org/wicketstuff/openlayers3/behavior/ClickFeatureHandler.java @@ -1,127 +1,123 @@ -package org.wicketstuff.openlayers3.behavior; - -import com.google.gson.JsonElement; -import com.google.gson.JsonNull; -import com.google.gson.JsonObject; -import com.google.gson.JsonParser; -import org.apache.wicket.Component; -import org.apache.wicket.ajax.AbstractDefaultAjaxBehavior; -import org.apache.wicket.ajax.AjaxRequestTarget; -import org.apache.wicket.markup.head.IHeaderResponse; -import org.apache.wicket.markup.head.OnDomReadyHeaderItem; -import org.apache.wicket.request.IRequestParameters; -import org.apache.wicket.request.cycle.RequestCycle; -import org.apache.wicket.util.string.Strings; -import org.apache.wicket.util.template.PackageTextTemplate; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.wicketstuff.openlayers3.api.coordinate.LongLat; - -import java.util.HashMap; -import java.util.Map; - -/** - * Provides a behavior that handles clicking on features on the map. - */ -public abstract class ClickFeatureHandler extends AbstractDefaultAjaxBehavior { - - private final static Logger logger = LoggerFactory.getLogger(ClickFeatureHandler.class); - - /** - * Default projection. - */ - public final static String DEFAULT_PROJECTION = "EPSG:4326"; - /** - * Counter for generating instance identifiers. - */ - public static Long counter = 0L; - /** - * The projection for this behavior, used to translate the 'clicked' coordinates. - */ - private final String projection; - - /** - * Creates a new instance. - */ - public ClickFeatureHandler() { - this(DEFAULT_PROJECTION); - } - - /** - * Creates a new instance. - * - * @param projection - * The clicked coordinate will be transformed into this projection - */ - public ClickFeatureHandler(String projection) { - this.projection = projection; - } - - /** - * Callback for handling mouse clicks on map features. - * - * @param target - * Ajax request target - * @param featureId - * Unique element ID of the clicked feature - * @param longLat - * Coordinate of the feature - * @param properties - * JsonObject with the clicked feature's properties - */ - public abstract void handleClick(AjaxRequestTarget target, String featureId, LongLat longLat, - JsonObject properties); - - /** - * Callback for handling mouse clicks on the map that do not intersect with features. - * - * @param target - * Ajax request target - * @param longLat - * Coordinate of the click - */ - public void handleClickMiss(AjaxRequestTarget target, LongLat longLat) { - - } - - @Override - protected void respond(AjaxRequestTarget target) { - - IRequestParameters params = RequestCycle.get().getRequest().getRequestParameters(); - String coordinateRaw = params.getParameterValue("coordinate").toString(); - String featureId = params.getParameterValue("id").toString(); - String properties = params.getParameterValue("properties").toString(); - - JsonObject propertiesJson = null; - JsonElement propertiesParsed = new JsonParser().parse(properties); - if (!(propertiesParsed instanceof JsonNull)) { - propertiesJson = propertiesParsed.getAsJsonObject(); - } - - String[] coordinates = Strings.split(coordinateRaw, ','); - Double longitude = Double.parseDouble(coordinates[0]); - Double latitude = Double.parseDouble(coordinates[1]); - - if (!properties.isEmpty()) { - - handleClick(target, featureId, new LongLat(longitude, latitude, projection), propertiesJson); - } else { - - handleClickMiss(target, new LongLat(longitude, latitude, projection)); - } - } - - @Override - public void renderHead(Component component, IHeaderResponse response) { - super.renderHead(component, response); - - final Map params = new HashMap(); - params.put("callbackUrl", getCallbackUrl()); - params.put("clickHandlerId", (counter++).toString()); - params.put("componentId", component.getMarkupId()); - params.put("projection", projection != null ? projection : "NULL"); - - PackageTextTemplate template = new PackageTextTemplate(ClickHandler.class, "ClickFeatureHandler.js"); - response.render(OnDomReadyHeaderItem.forScript(template.asString(params))); - } -} +package org.wicketstuff.openlayers3.behavior; + +import java.util.HashMap; +import java.util.Map; + +import org.apache.wicket.Component; +import org.apache.wicket.ajax.AbstractDefaultAjaxBehavior; +import org.apache.wicket.ajax.AjaxRequestTarget; +import org.apache.wicket.markup.head.IHeaderResponse; +import org.apache.wicket.request.IRequestParameters; +import org.apache.wicket.request.cycle.RequestCycle; +import org.apache.wicket.util.string.Strings; +import org.wicketstuff.openlayers3.api.coordinate.LongLat; +import org.wicketstuff.openlayers3.api.util.HeaderUtils; + +import com.google.gson.JsonElement; +import com.google.gson.JsonNull; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; + +/** + * Provides a behavior that handles clicking on features on the map. + */ +public abstract class ClickFeatureHandler extends AbstractDefaultAjaxBehavior { + + /** + * Default projection. + */ + public final static String DEFAULT_PROJECTION = "EPSG:4326"; + /** + * Counter for generating instance identifiers. + */ + public static Long counter = 0L; + /** + * The projection for this behavior, used to translate the 'clicked' coordinates. + */ + private final String projection; + + /** + * Creates a new instance. + */ + public ClickFeatureHandler() { + this(DEFAULT_PROJECTION); + } + + /** + * Creates a new instance. + * + * @param projection + * The clicked coordinate will be transformed into this projection + */ + public ClickFeatureHandler(String projection) { + this.projection = projection; + } + + /** + * Callback for handling mouse clicks on map features. + * + * @param target + * Ajax request target + * @param featureId + * Unique element ID of the clicked feature + * @param longLat + * Coordinate of the feature + * @param properties + * JsonObject with the clicked feature's properties + */ + public abstract void handleClick(AjaxRequestTarget target, String featureId, LongLat longLat, + JsonObject properties); + + /** + * Callback for handling mouse clicks on the map that do not intersect with features. + * + * @param target + * Ajax request target + * @param longLat + * Coordinate of the click + */ + public void handleClickMiss(AjaxRequestTarget target, LongLat longLat) { + + } + + @Override + protected void respond(AjaxRequestTarget target) { + + IRequestParameters params = RequestCycle.get().getRequest().getRequestParameters(); + String coordinateRaw = params.getParameterValue("coordinate").toString(); + String featureId = params.getParameterValue("id").toString(); + String properties = params.getParameterValue("properties").toString(); + + JsonObject propertiesJson = null; + JsonElement propertiesParsed = new JsonParser().parse(properties); + if (!(propertiesParsed instanceof JsonNull)) { + propertiesJson = propertiesParsed.getAsJsonObject(); + } + + String[] coordinates = Strings.split(coordinateRaw, ','); + Double longitude = Double.parseDouble(coordinates[0]); + Double latitude = Double.parseDouble(coordinates[1]); + + if (!properties.isEmpty()) { + + handleClick(target, featureId, new LongLat(longitude, latitude, projection), propertiesJson); + } else { + + handleClickMiss(target, new LongLat(longitude, latitude, projection)); + } + } + + @Override + public void renderHead(final Component component, final IHeaderResponse response) { + super.renderHead(component, response); + + final Map params = new HashMap(); + params.put("callbackUrl", getCallbackUrl()); + params.put("clickHandlerId", (counter++).toString()); + params.put("componentId", component.getMarkupId()); + params.put("projection", projection != null ? projection : "NULL"); + + HeaderUtils.renderOnDomReady(response, ClickFeatureHandler.class, + "ClickFeatureHandler.js", params); + } +} diff --git a/openlayers3-parent/openlayers3/src/main/java/org/wicketstuff/openlayers3/behavior/ClickHandler.java b/openlayers3-parent/openlayers3/src/main/java/org/wicketstuff/openlayers3/behavior/ClickHandler.java index de1e918059..33796805e3 100644 --- a/openlayers3-parent/openlayers3/src/main/java/org/wicketstuff/openlayers3/behavior/ClickHandler.java +++ b/openlayers3-parent/openlayers3/src/main/java/org/wicketstuff/openlayers3/behavior/ClickHandler.java @@ -1,83 +1,82 @@ -package org.wicketstuff.openlayers3.behavior; - -import org.apache.wicket.Component; -import org.apache.wicket.ajax.AbstractDefaultAjaxBehavior; -import org.apache.wicket.ajax.AjaxRequestTarget; -import org.apache.wicket.markup.head.IHeaderResponse; -import org.apache.wicket.markup.head.OnDomReadyHeaderItem; -import org.apache.wicket.request.cycle.RequestCycle; -import org.apache.wicket.util.template.PackageTextTemplate; -import org.wicketstuff.openlayers3.api.coordinate.LongLat; - -import java.util.HashMap; -import java.util.Map; - -/** - * Provides a behavior that handles clicks on the map. - */ -public abstract class ClickHandler extends AbstractDefaultAjaxBehavior { - - /** - * Default projection. - */ - public final static String DEFAULT_PROJECTION = "EPSG:4326"; - /** - * Counter for generating instance identifiers. - */ - private static Long counter = 0L; - /** - * The projection for this behavior, used to translate the 'clicked' coordinates. - */ - private final String projection; - - /** - * Creates a new instance. - */ - public ClickHandler() { - this(DEFAULT_PROJECTION); - } - - /** - * Creates a new instance. - * - * @param projection - * The clicked coordinate will be transformed into this projection - */ - public ClickHandler(String projection) { - this.projection = projection; - } - - /** - * Dispatches the clicked coordinates. - * - * @param target - * Ajax request target - * @param longLat - * Longitude and latitude of the click - */ - public abstract void handleClick(AjaxRequestTarget target, LongLat longLat); - - @Override - protected void respond(AjaxRequestTarget target) { - - String[] coordinates = RequestCycle.get().getRequest().getRequestParameters().getParameterValue("coordinate") - .toString().split(","); - Double longitude = Double.parseDouble(coordinates[0]); - Double latitude = Double.parseDouble(coordinates[1]); - handleClick(target, new LongLat(longitude, latitude, projection)); - } - - @Override - public void renderHead(Component component, IHeaderResponse response) { - super.renderHead(component, response); - - final Map params = new HashMap(); - params.put("callbackUrl", getCallbackUrl()); - params.put("componentId", component.getMarkupId()); - params.put("clickHandlerId", (counter++).toString()); - params.put("projection", projection != null ? projection : "NULL"); - - PackageTextTemplate template = new PackageTextTemplate(ClickHandler.class, "ClickHandler.js"); - response.render(OnDomReadyHeaderItem.forScript(template.asString(params))); - } +package org.wicketstuff.openlayers3.behavior; + +import java.util.HashMap; +import java.util.Map; + +import org.apache.wicket.Component; +import org.apache.wicket.ajax.AbstractDefaultAjaxBehavior; +import org.apache.wicket.ajax.AjaxRequestTarget; +import org.apache.wicket.markup.head.IHeaderResponse; +import org.apache.wicket.request.cycle.RequestCycle; +import org.wicketstuff.openlayers3.api.coordinate.LongLat; +import org.wicketstuff.openlayers3.api.util.HeaderUtils; + +/** + * Provides a behavior that handles clicks on the map. + */ +public abstract class ClickHandler extends AbstractDefaultAjaxBehavior { + + /** + * Default projection. + */ + public final static String DEFAULT_PROJECTION = "EPSG:4326"; + /** + * Counter for generating instance identifiers. + */ + private static Long counter = 0L; + /** + * The projection for this behavior, used to translate the 'clicked' coordinates. + */ + private final String projection; + + /** + * Creates a new instance. + */ + public ClickHandler() { + this(DEFAULT_PROJECTION); + } + + /** + * Creates a new instance. + * + * @param projection + * The clicked coordinate will be transformed into this projection + */ + public ClickHandler(String projection) { + this.projection = projection; + } + + /** + * Dispatches the clicked coordinates. + * + * @param target + * Ajax request target + * @param longLat + * Longitude and latitude of the click + */ + public abstract void handleClick(AjaxRequestTarget target, LongLat longLat); + + @Override + protected void respond(AjaxRequestTarget target) { + + String[] coordinates = RequestCycle.get().getRequest().getRequestParameters().getParameterValue("coordinate") + .toString().split(","); + Double longitude = Double.parseDouble(coordinates[0]); + Double latitude = Double.parseDouble(coordinates[1]); + handleClick(target, new LongLat(longitude, latitude, projection)); + } + + @Override + public void renderHead(Component component, IHeaderResponse response) { + super.renderHead(component, response); + + final Map params = new HashMap(); + params.put("callbackUrl", getCallbackUrl()); + params.put("componentId", component.getMarkupId()); + params.put("clickHandlerId", (counter++).toString()); + params.put("projection", projection != null ? projection : "NULL"); + + HeaderUtils.renderOnDomReady(response, ClickHandler.class, + "ClickHandler.js", params); + } } \ No newline at end of file diff --git a/openlayers3-parent/openlayers3/src/main/java/org/wicketstuff/openlayers3/behavior/FeatureChangeListener.java b/openlayers3-parent/openlayers3/src/main/java/org/wicketstuff/openlayers3/behavior/FeatureChangeListener.java index 01e1815041..8d046b8a36 100644 --- a/openlayers3-parent/openlayers3/src/main/java/org/wicketstuff/openlayers3/behavior/FeatureChangeListener.java +++ b/openlayers3-parent/openlayers3/src/main/java/org/wicketstuff/openlayers3/behavior/FeatureChangeListener.java @@ -1,114 +1,110 @@ -package org.wicketstuff.openlayers3.behavior; - -import com.google.gson.JsonElement; -import com.google.gson.JsonNull; -import com.google.gson.JsonObject; -import com.google.gson.JsonParser; -import org.apache.wicket.Component; -import org.apache.wicket.ajax.AbstractDefaultAjaxBehavior; -import org.apache.wicket.ajax.AjaxRequestTarget; -import org.apache.wicket.markup.head.IHeaderResponse; -import org.apache.wicket.markup.head.OnDomReadyHeaderItem; -import org.apache.wicket.request.IRequestParameters; -import org.apache.wicket.request.cycle.RequestCycle; -import org.apache.wicket.util.string.Strings; -import org.apache.wicket.util.template.PackageTextTemplate; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.wicketstuff.openlayers3.api.Feature; -import org.wicketstuff.openlayers3.api.coordinate.LongLat; - -import java.util.HashMap; -import java.util.Map; - -/** - * Provides a behavior that reports changes to a feature. - */ -public abstract class FeatureChangeListener extends AbstractDefaultAjaxBehavior { - - private final static Logger logger = LoggerFactory.getLogger(FeatureChangeListener.class); - - /** - * Default projection. - */ - public final static String DEFAULT_PROJECTION = "EPSG:4326"; - /** - * Counter for generating instance identifiers. - */ - public static Long counter = 0L; - /** - * The projection for this behavior, used to translate the 'clicked' coordinates. - */ - private final String projection; - /** - * The feature that will be monitored for changed. - */ - private final Feature feature; - - /** - * Creates a new instance. - * - * @param feature Feature to which we are listening - */ - public FeatureChangeListener(Feature feature) { - this(DEFAULT_PROJECTION, feature); - } - - /** - * Creates a new instance. - * - * @param projection Projection for returned data - * @param feature Feature to which we are listening - */ - public FeatureChangeListener(String projection, Feature feature) { - this.projection = projection; - this.feature = feature; - } - - /** - * Invoked when the feature has been modified. - * - * @param target Ajax request target - * @param featureId Element ID of the feature modified - * @param longLat The new coordinate of the modified feature - * @param properties The properties of the modified feature - */ - public abstract void handleChange(AjaxRequestTarget target, String featureId, LongLat longLat, - JsonObject properties); - - @Override - protected void respond(AjaxRequestTarget target) { - - IRequestParameters params = RequestCycle.get().getRequest().getRequestParameters(); - String coordinateRaw = params.getParameterValue("coordinate").toString(); - String featureId = params.getParameterValue("id").toString(); - String properties = params.getParameterValue("properties").toString(); - - JsonObject propertiesJson = null; - JsonElement propertiesParsed = new JsonParser().parse(properties); - if (!(propertiesParsed instanceof JsonNull)) { - propertiesJson = propertiesParsed.getAsJsonObject(); - } - - String[] coordinates = Strings.split(coordinateRaw, ','); - Double longitude = Double.parseDouble(coordinates[0]); - Double latitude = Double.parseDouble(coordinates[1]); - - handleChange(target, featureId, new LongLat(longitude, latitude, projection), propertiesJson); - } - - @Override - public void renderHead(Component component, IHeaderResponse response) { - super.renderHead(component, response); - - final Map params = new HashMap(); - params.put("callbackUrl", getCallbackUrl()); - params.put("changeHandlerId", (counter++).toString()); - params.put("componentId", component.getMarkupId()); - params.put("featureId", feature.getJsId()); - params.put("projection", projection != null ? projection : "NULL"); - - PackageTextTemplate template = new PackageTextTemplate(ClickHandler.class, "FeatureChangeListener.js"); - response.render(OnDomReadyHeaderItem.forScript(template.asString(params))); - } -} +package org.wicketstuff.openlayers3.behavior; + +import java.util.HashMap; +import java.util.Map; + +import org.apache.wicket.Component; +import org.apache.wicket.ajax.AbstractDefaultAjaxBehavior; +import org.apache.wicket.ajax.AjaxRequestTarget; +import org.apache.wicket.markup.head.IHeaderResponse; +import org.apache.wicket.request.IRequestParameters; +import org.apache.wicket.request.cycle.RequestCycle; +import org.apache.wicket.util.string.Strings; +import org.wicketstuff.openlayers3.api.Feature; +import org.wicketstuff.openlayers3.api.coordinate.LongLat; +import org.wicketstuff.openlayers3.api.util.HeaderUtils; + +import com.google.gson.JsonElement; +import com.google.gson.JsonNull; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; + +/** + * Provides a behavior that reports changes to a feature. + */ +public abstract class FeatureChangeListener extends AbstractDefaultAjaxBehavior { + + /** + * Default projection. + */ + public final static String DEFAULT_PROJECTION = "EPSG:4326"; + /** + * Counter for generating instance identifiers. + */ + public static Long counter = 0L; + /** + * The projection for this behavior, used to translate the 'clicked' coordinates. + */ + private final String projection; + /** + * The feature that will be monitored for changed. + */ + private final Feature feature; + + /** + * Creates a new instance. + * + * @param feature Feature to which we are listening + */ + public FeatureChangeListener(Feature feature) { + this(DEFAULT_PROJECTION, feature); + } + + /** + * Creates a new instance. + * + * @param projection Projection for returned data + * @param feature Feature to which we are listening + */ + public FeatureChangeListener(String projection, Feature feature) { + this.projection = projection; + this.feature = feature; + } + + /** + * Invoked when the feature has been modified. + * + * @param target Ajax request target + * @param featureId Element ID of the feature modified + * @param longLat The new coordinate of the modified feature + * @param properties The properties of the modified feature + */ + public abstract void handleChange(AjaxRequestTarget target, String featureId, LongLat longLat, + JsonObject properties); + + @Override + protected void respond(AjaxRequestTarget target) { + + IRequestParameters params = RequestCycle.get().getRequest().getRequestParameters(); + String coordinateRaw = params.getParameterValue("coordinate").toString(); + String featureId = params.getParameterValue("id").toString(); + String properties = params.getParameterValue("properties").toString(); + + JsonObject propertiesJson = null; + JsonElement propertiesParsed = new JsonParser().parse(properties); + if (!(propertiesParsed instanceof JsonNull)) { + propertiesJson = propertiesParsed.getAsJsonObject(); + } + + String[] coordinates = Strings.split(coordinateRaw, ','); + Double longitude = Double.parseDouble(coordinates[0]); + Double latitude = Double.parseDouble(coordinates[1]); + + handleChange(target, featureId, new LongLat(longitude, latitude, projection), propertiesJson); + } + + @Override + public void renderHead(Component component, IHeaderResponse response) { + super.renderHead(component, response); + + final Map params = new HashMap(); + params.put("callbackUrl", getCallbackUrl()); + params.put("changeHandlerId", (counter++).toString()); + params.put("componentId", component.getMarkupId()); + params.put("featureId", feature.getJsId()); + params.put("projection", projection != null ? projection : "NULL"); + + HeaderUtils.renderOnDomReady(response, FeatureChangeListener.class, + "FeatureChangeListener.js", params); + } +} diff --git a/openlayers3-parent/openlayers3/src/main/java/org/wicketstuff/openlayers3/behavior/ZoomToFeatureExtent.java b/openlayers3-parent/openlayers3/src/main/java/org/wicketstuff/openlayers3/behavior/ZoomToFeatureExtent.java index 5a58568e4b..1f20329124 100644 --- a/openlayers3-parent/openlayers3/src/main/java/org/wicketstuff/openlayers3/behavior/ZoomToFeatureExtent.java +++ b/openlayers3-parent/openlayers3/src/main/java/org/wicketstuff/openlayers3/behavior/ZoomToFeatureExtent.java @@ -1,190 +1,185 @@ -package org.wicketstuff.openlayers3.behavior; - -import org.apache.wicket.Component; -import org.apache.wicket.behavior.Behavior; -import org.apache.wicket.markup.head.IHeaderResponse; -import org.apache.wicket.markup.head.OnDomReadyHeaderItem; -import org.apache.wicket.util.template.PackageTextTemplate; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.wicketstuff.openlayers3.api.layer.Layer; - -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -/** - * Provides a behavior that zooms to the map to include all features, excluding features loaded from a remote data - * source. If you need to zoom in order to view all remotely loaded features, please take a look at the - * VectorLayerFeaturesLoaded listener. - */ -public class ZoomToFeatureExtent extends Behavior { - - private final static Logger logger = LoggerFactory.getLogger(ZoomToFeatureExtent.class); - - /** - * Buffer to place around the zoomed extent. - */ - private Number buffer; - - /** - * List of layers to inspect when calculating the extent. - */ - private List layers; - - /** - * Creates a new instance. - */ - public ZoomToFeatureExtent() { - this.buffer = buffer; - this.layers = null; - } - - /** - * Creates a new instance. - * - * @param buffer - * Buffer to add when calculating extent - */ - public ZoomToFeatureExtent(Number buffer) { - this.buffer = buffer; - this.layers = null; - } - - /** - * Creates a new instance. - * - * @param layers - * Layers to use when calculating extent - */ - public ZoomToFeatureExtent(Layer... layers) { - this(null, Arrays.asList(layers)); - } - - /** - * Creates a new instance. - * - * @param layers - * List of layers to use when calculating extent - */ - public ZoomToFeatureExtent(List layers) { - this(null, layers); - } - - /** - * Creates a new instance. - * - * @param buffer - * Buffer to add when calculating extent - * @param layers - * Layers to use when calculating extent - */ - public ZoomToFeatureExtent(Number buffer, Layer... layers) { - this(buffer, Arrays.asList(layers)); - } - - /** - * Creates a new instance. - * - * @param buffer - * Buffer to add when calculating extent - * @param layers - * List of layers to use when calculating extent - */ - public ZoomToFeatureExtent(Number buffer, List layers) { - this.buffer = buffer; - this.layers = layers; - } - - /** - * Returns the buffer added to the calculated extent. - * - * @return Number with the buffer - */ - public Number getBuffer() { - return buffer; - } - - /** - * Sets the buffer added to the calculated extent. - * - * @param buffer - * New value - */ - public void setBuffer(Number buffer) { - this.buffer = buffer; - } - - /** - * Sets the buffer added to the calculated extent. - * - * @param buffer - * New value - * @return This instance - */ - public ZoomToFeatureExtent buffer(Number buffer) { - this.setBuffer(buffer); - return this; - } - - /** - * Returns the layers used when calculating the extent. - * - * @return List of layers - */ - public List getLayers() { - return layers; - } - - /** - * Sets the layers used when calculating the extent. - * - * @param layers - * List of layers - */ - public void setLayers(List layers) { - this.layers = layers; - } - - /** - * Sets the layers used when calculating the extent. - * - * @param layers - * List of layers - * @return This instance - */ - public ZoomToFeatureExtent layers(List layers) { - setLayers(layers); - return this; - } - - @Override - public void renderHead(Component component, IHeaderResponse response) { - super.renderHead(component, response); - - final Map params = new HashMap(); - params.put("componentId", component.getMarkupId()); - params.put("buffer", buffer != null ? buffer.toString() : "NULL"); - - if (getLayers() != null) { - StringBuilder layersOut = new StringBuilder(); - - for (Layer layer : getLayers()) { - - layersOut.append(layer.getJsId()); - - if (layersOut.length() > 0) { - layersOut.append(","); - } - } - - params.put("layers", layersOut); - } else { - params.put("layers", "NULL"); - } - - PackageTextTemplate template = new PackageTextTemplate(ZoomToOverlayExtent.class, "ZoomToFeatureExtent.js"); - response.render(OnDomReadyHeaderItem.forScript(template.asString(params))); - } -} +package org.wicketstuff.openlayers3.behavior; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.apache.wicket.Component; +import org.apache.wicket.behavior.Behavior; +import org.apache.wicket.markup.head.IHeaderResponse; +import org.wicketstuff.openlayers3.api.layer.Layer; +import org.wicketstuff.openlayers3.api.util.HeaderUtils; + +/** + * Provides a behavior that zooms to the map to include all features, excluding features loaded from a remote data + * source. If you need to zoom in order to view all remotely loaded features, please take a look at the + * VectorLayerFeaturesLoaded listener. + */ +public class ZoomToFeatureExtent extends Behavior { + + /** + * Buffer to place around the zoomed extent. + */ + private Number buffer; + + /** + * List of layers to inspect when calculating the extent. + */ + private List layers; + + /** + * Creates a new instance. + */ + public ZoomToFeatureExtent() { + this.buffer = null; + this.layers = null; + } + + /** + * Creates a new instance. + * + * @param buffer + * Buffer to add when calculating extent + */ + public ZoomToFeatureExtent(Number buffer) { + this.buffer = buffer; + this.layers = null; + } + + /** + * Creates a new instance. + * + * @param layers + * Layers to use when calculating extent + */ + public ZoomToFeatureExtent(Layer... layers) { + this(null, Arrays.asList(layers)); + } + + /** + * Creates a new instance. + * + * @param layers + * List of layers to use when calculating extent + */ + public ZoomToFeatureExtent(List layers) { + this(null, layers); + } + + /** + * Creates a new instance. + * + * @param buffer + * Buffer to add when calculating extent + * @param layers + * Layers to use when calculating extent + */ + public ZoomToFeatureExtent(Number buffer, Layer... layers) { + this(buffer, Arrays.asList(layers)); + } + + /** + * Creates a new instance. + * + * @param buffer + * Buffer to add when calculating extent + * @param layers + * List of layers to use when calculating extent + */ + public ZoomToFeatureExtent(Number buffer, List layers) { + this.buffer = buffer; + this.layers = layers; + } + + /** + * Returns the buffer added to the calculated extent. + * + * @return Number with the buffer + */ + public Number getBuffer() { + return buffer; + } + + /** + * Sets the buffer added to the calculated extent. + * + * @param buffer + * New value + */ + public void setBuffer(Number buffer) { + this.buffer = buffer; + } + + /** + * Sets the buffer added to the calculated extent. + * + * @param buffer + * New value + * @return This instance + */ + public ZoomToFeatureExtent buffer(Number buffer) { + this.setBuffer(buffer); + return this; + } + + /** + * Returns the layers used when calculating the extent. + * + * @return List of layers + */ + public List getLayers() { + return layers; + } + + /** + * Sets the layers used when calculating the extent. + * + * @param layers + * List of layers + */ + public void setLayers(List layers) { + this.layers = layers; + } + + /** + * Sets the layers used when calculating the extent. + * + * @param layers + * List of layers + * @return This instance + */ + public ZoomToFeatureExtent layers(List layers) { + setLayers(layers); + return this; + } + + @Override + public void renderHead(Component component, IHeaderResponse response) { + super.renderHead(component, response); + + final Map params = new HashMap(); + params.put("componentId", component.getMarkupId()); + params.put("buffer", buffer != null ? buffer.toString() : "NULL"); + + if (getLayers() != null) { + StringBuilder layersOut = new StringBuilder(); + + for (Layer layer : getLayers()) { + + layersOut.append(layer.getJsId()); + + if (layersOut.length() > 0) { + layersOut.append(","); + } + } + + params.put("layers", layersOut); + } else { + params.put("layers", "NULL"); + } + + HeaderUtils.renderOnDomReady(response, ZoomToFeatureExtent.class, + "ZoomToFeatureExtent.js", params); + } +} diff --git a/openlayers3-parent/openlayers3/src/main/java/org/wicketstuff/openlayers3/behavior/ZoomToFeatureExtent.js b/openlayers3-parent/openlayers3/src/main/java/org/wicketstuff/openlayers3/behavior/ZoomToFeatureExtent.js index 96ace0812c..04fed5fce0 100644 --- a/openlayers3-parent/openlayers3/src/main/java/org/wicketstuff/openlayers3/behavior/ZoomToFeatureExtent.js +++ b/openlayers3-parent/openlayers3/src/main/java/org/wicketstuff/openlayers3/behavior/ZoomToFeatureExtent.js @@ -1,27 +1,27 @@ -var map = window.org_wicketstuff_openlayers3['map_${componentId}']; -var view = map.getView(); -var layers = 'NULL'; - -if("${layers}" != 'NULL') { - layers = "${layers}".split(','); -} - -var points = [] -map.getLayers().forEach(function (layer) { - - if(typeof layer.getSource().forEachFeature === 'function') { - - if(layers == 'NULL' || layers.indexOf(layer.get('id')) > -1) { - layer.getSource().forEachFeature(function(feature) { - points.push(feature.getGeometry().getCoordinates()); - }); - } - } -}); - -var extent = ol.extent.boundingExtent(points); -if('${buffer}' != 'NULL') { - extent = ol.extent.buffer(extent, parseFloat('${buffer}')); -} - -map.getView().fitExtent(extent, map.getSize()); +var map = window.org_wicketstuff_openlayers3['map_${componentId}']; +var view = map.getView(); +var layers = 'NULL'; + +if("${layers}" != 'NULL') { + layers = "${layers}".split(','); +} + +var points = [] +map.getLayers().forEach(function (layer) { + + if(typeof layer.getSource().forEachFeature === 'function') { + + if(layers == 'NULL' || layers.indexOf(layer.get('id')) > -1) { + layer.getSource().forEachFeature(function(feature) { + points.push(feature.getGeometry().getCoordinates()); + }); + } + } +}); + +var extent = ol.extent.boundingExtent(points); +if('${buffer}' != 'NULL') { + extent = ol.extent.buffer(extent, parseFloat('${buffer}')); +} + +map.getView().fit(extent, map.getSize()); diff --git a/openlayers3-parent/openlayers3/src/main/java/org/wicketstuff/openlayers3/behavior/ZoomToOverlayExtent.java b/openlayers3-parent/openlayers3/src/main/java/org/wicketstuff/openlayers3/behavior/ZoomToOverlayExtent.java index 44beff485b..019d3e3ac8 100644 --- a/openlayers3-parent/openlayers3/src/main/java/org/wicketstuff/openlayers3/behavior/ZoomToOverlayExtent.java +++ b/openlayers3-parent/openlayers3/src/main/java/org/wicketstuff/openlayers3/behavior/ZoomToOverlayExtent.java @@ -1,81 +1,80 @@ -package org.wicketstuff.openlayers3.behavior; - -import org.apache.wicket.Component; -import org.apache.wicket.behavior.Behavior; -import org.apache.wicket.markup.head.IHeaderResponse; -import org.apache.wicket.markup.head.OnDomReadyHeaderItem; -import org.apache.wicket.util.template.PackageTextTemplate; - -import java.util.HashMap; -import java.util.Map; - -/** - * Provides a behavior that zooms the map to include all overlays. - */ -public class ZoomToOverlayExtent extends Behavior { - - /** - * Buffer to place around the zoomed extent. - */ - private Number buffer; - - /** - * Creates a new instance. - */ - public ZoomToOverlayExtent() { - this(null); - } - - /** - * Creates a new instance. - * - * @param buffer - * Buffer to add around the zoomed extent. - */ - public ZoomToOverlayExtent(Number buffer) { - this.buffer = buffer; - } - - /** - * Returns the buffer value. - * - * @return Buffer value - */ - public Number getBuffer() { - return buffer; - } - - /** - * Sets the buffer value. - * - * @param buffer - * New value - */ - public void setBuffer(Number buffer) { - this.buffer = buffer; - } - - /** - * Sets the buffer value. - * - * @param buffer - * New value - * @return This instance - */ - public ZoomToOverlayExtent buffer(Number buffer) { - setBuffer(buffer); - return this; - } - - @Override - public void renderHead(Component component, IHeaderResponse response) { - super.renderHead(component, response); - - final Map params = new HashMap(); - params.put("componentId", component.getMarkupId()); - params.put("buffer", buffer != null ? buffer.toString() : "NULL"); - - PackageTextTemplate template = new PackageTextTemplate(ZoomToOverlayExtent.class, "ZoomToOverlayExtent.js"); - response.render(OnDomReadyHeaderItem.forScript(template.asString(params))); - } -} +package org.wicketstuff.openlayers3.behavior; + +import java.util.HashMap; +import java.util.Map; + +import org.apache.wicket.Component; +import org.apache.wicket.behavior.Behavior; +import org.apache.wicket.markup.head.IHeaderResponse; +import org.wicketstuff.openlayers3.api.util.HeaderUtils; + +/** + * Provides a behavior that zooms the map to include all overlays. + */ +public class ZoomToOverlayExtent extends Behavior { + + /** + * Buffer to place around the zoomed extent. + */ + private Number buffer; + + /** + * Creates a new instance. + */ + public ZoomToOverlayExtent() { + this(null); + } + + /** + * Creates a new instance. + * + * @param buffer + * Buffer to add around the zoomed extent. + */ + public ZoomToOverlayExtent(Number buffer) { + this.buffer = buffer; + } + + /** + * Returns the buffer value. + * + * @return Buffer value + */ + public Number getBuffer() { + return buffer; + } + + /** + * Sets the buffer value. + * + * @param buffer + * New value + */ + public void setBuffer(Number buffer) { + this.buffer = buffer; + } + + /** + * Sets the buffer value. + * + * @param buffer + * New value + * @return This instance + */ + public ZoomToOverlayExtent buffer(Number buffer) { + setBuffer(buffer); + return this; + } + + @Override + public void renderHead(Component component, IHeaderResponse response) { + super.renderHead(component, response); + + final Map params = new HashMap(); + params.put("componentId", component.getMarkupId()); + params.put("buffer", buffer != null ? buffer.toString() : "NULL"); + + HeaderUtils.renderOnDomReady(response, ZoomToOverlayExtent.class, + "ZoomToOverlayExtent.js", params); + } +} diff --git a/openlayers3-parent/openlayers3/src/main/java/org/wicketstuff/openlayers3/behavior/ZoomToOverlayExtent.js b/openlayers3-parent/openlayers3/src/main/java/org/wicketstuff/openlayers3/behavior/ZoomToOverlayExtent.js index cfe759ce6a..c4797a8a48 100644 --- a/openlayers3-parent/openlayers3/src/main/java/org/wicketstuff/openlayers3/behavior/ZoomToOverlayExtent.js +++ b/openlayers3-parent/openlayers3/src/main/java/org/wicketstuff/openlayers3/behavior/ZoomToOverlayExtent.js @@ -1,17 +1,17 @@ -var map = window.org_wicketstuff_openlayers3['map_${componentId}']; -var view = map.getView(); - -var points = [] -map.getOverlays().forEach(function (o) { - if(!(typeof o.getPosition() === 'undefined')) { - points.push(o.getPosition()); - } -}); - -var extent = ol.extent.boundingExtent(points); - -if('${buffer}' != 'NULL') { - extent = ol.extent.buffer(extent, parseFloat('${buffer}')); -} - -map.getView().fitExtent(extent, map.getSize()); +var map = window.org_wicketstuff_openlayers3['map_${componentId}']; +var view = map.getView(); + +var points = [] +map.getOverlays().forEach(function (o) { + if(!(typeof o.getPosition() === 'undefined')) { + points.push(o.getPosition()); + } +}); + +var extent = ol.extent.boundingExtent(points); + +if('${buffer}' != 'NULL') { + extent = ol.extent.buffer(extent, parseFloat('${buffer}')); +} + +map.getView().fit(extent, map.getSize()); diff --git a/openlayers3-parent/openlayers3/src/main/java/org/wicketstuff/openlayers3/behavior/view/ViewEvent.java b/openlayers3-parent/openlayers3/src/main/java/org/wicketstuff/openlayers3/behavior/view/ViewEvent.java index 56a9e375f9..acbbad15f0 100644 --- a/openlayers3-parent/openlayers3/src/main/java/org/wicketstuff/openlayers3/behavior/view/ViewEvent.java +++ b/openlayers3-parent/openlayers3/src/main/java/org/wicketstuff/openlayers3/behavior/view/ViewEvent.java @@ -1,151 +1,148 @@ -package org.wicketstuff.openlayers3.behavior.view; - -import com.google.gson.JsonArray; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; -import com.google.gson.JsonPrimitive; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.wicketstuff.openlayers3.api.coordinate.CoordinateProjected; - -import java.io.Serializable; -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; - -/** - * Provides a data object that models an event emitted by the map's ol.View. - */ -public class ViewEvent implements Serializable { - - private final static Logger logger = LoggerFactory.getLogger(ViewEvent.class); - - /** - * The JsonObject that backs this instance. - */ - private final JsonObject jsonViewEvent; - - /** - * The projection for this view. - */ - private String projection; - - /** - * Array of two coordinates that specify the extent of this view. - */ - private CoordinateProjected[] extent; - - /** - * Coordinate at the center of this view. - */ - private CoordinateProjected center; - - /** - * Creates a new view event from the JSON encoded ol.View instance. - * - * @param jsonViewEvent JSON encoded ol.View instance - */ - public ViewEvent(JsonObject jsonViewEvent) { - this.jsonViewEvent = jsonViewEvent; - - transformValues(); - } - - /** - * Returns the projection for this view. - * - * @return String with the view for this projection - */ - public String getProjection() { - return projection; - } - - /** - * Returns an array of two coordinates describing the extent of this view. - * - * @return Array of two coordinates - */ - public CoordinateProjected[] getExtent() { - return extent; - } - - /** - * Returns the coordinate at the center of this view. - * - * @return Coordinate of the view's center - */ - public CoordinateProjected getCenter() { - return center; - } - - /** - * Returns the JsonObject backing this instance. You may want to inspect this if you need data that isn't easily - * accessible from this wrapper. - * - * @return The JsonObject backing this instance - */ - public JsonObject getJsonObject() { - return jsonViewEvent; - } - - /** - * Transforms the source projection data and sets this instance's fields. - * - * @return The projection - */ - private String transformProjection() { - JsonPrimitive projection = jsonViewEvent.getAsJsonPrimitive("transformedProjection"); - return projection.toString(); - } - - /** - * Transforms the source extent data and sets this instance's fields. - * - * @return The extent - */ - private CoordinateProjected[] transFormExtent() { - - List values = new ArrayList(); - JsonArray transformedExtent = jsonViewEvent.getAsJsonArray("transformedExtent"); - Iterator iterator = transformedExtent.iterator(); - while(iterator.hasNext()) { - JsonElement element = iterator.next(); - values.add(element.getAsDouble()); - } - - CoordinateProjected coordinate1 = new CoordinateProjected(values.get(0), values.get(1), getProjection()); - CoordinateProjected coordinate2 = new CoordinateProjected(values.get(2), values.get(3), getProjection()); - - return new CoordinateProjected[]{coordinate1, coordinate2}; - } - - /** - * Transforms the source center data and sets this instance's fields. - * - * @return The center - */ - private CoordinateProjected transformCenter() { - - List values = new ArrayList(); - JsonArray transformedExtent = jsonViewEvent.getAsJsonArray("center"); - Iterator iterator = transformedExtent.iterator(); - while(iterator.hasNext()) { - JsonElement element = iterator.next(); - values.add(element.getAsDouble()); - } - - CoordinateProjected coordinate = new CoordinateProjected(values.get(0), values.get(1), getProjection()); - - return coordinate; - } - - /** - * Transforms the source data and sets this instance's fields. - */ - private void transformValues() { - - projection = transformProjection(); - extent = transFormExtent(); - center = transformCenter(); - } -} +package org.wicketstuff.openlayers3.behavior.view; + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +import org.wicketstuff.openlayers3.api.coordinate.CoordinateProjected; + +import com.google.gson.JsonArray; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonPrimitive; + +/** + * Provides a data object that models an event emitted by the map's ol.View. + */ +public class ViewEvent implements Serializable { + + /** + * The JsonObject that backs this instance. + */ + private final JsonObject jsonViewEvent; + + /** + * The projection for this view. + */ + private String projection; + + /** + * Array of two coordinates that specify the extent of this view. + */ + private CoordinateProjected[] extent; + + /** + * Coordinate at the center of this view. + */ + private CoordinateProjected center; + + /** + * Creates a new view event from the JSON encoded ol.View instance. + * + * @param jsonViewEvent JSON encoded ol.View instance + */ + public ViewEvent(JsonObject jsonViewEvent) { + this.jsonViewEvent = jsonViewEvent; + + transformValues(); + } + + /** + * Returns the projection for this view. + * + * @return String with the view for this projection + */ + public String getProjection() { + return projection; + } + + /** + * Returns an array of two coordinates describing the extent of this view. + * + * @return Array of two coordinates + */ + public CoordinateProjected[] getExtent() { + return extent; + } + + /** + * Returns the coordinate at the center of this view. + * + * @return Coordinate of the view's center + */ + public CoordinateProjected getCenter() { + return center; + } + + /** + * Returns the JsonObject backing this instance. You may want to inspect this if you need data that isn't easily + * accessible from this wrapper. + * + * @return The JsonObject backing this instance + */ + public JsonObject getJsonObject() { + return jsonViewEvent; + } + + /** + * Transforms the source projection data and sets this instance's fields. + * + * @return The projection + */ + private String transformProjection() { + JsonPrimitive projection = jsonViewEvent.getAsJsonPrimitive("transformedProjection"); + return projection.toString(); + } + + /** + * Transforms the source extent data and sets this instance's fields. + * + * @return The extent + */ + private CoordinateProjected[] transFormExtent() { + + List values = new ArrayList(); + JsonArray transformedExtent = jsonViewEvent.getAsJsonArray("transformedExtent"); + Iterator iterator = transformedExtent.iterator(); + while(iterator.hasNext()) { + JsonElement element = iterator.next(); + values.add(element.getAsDouble()); + } + + CoordinateProjected coordinate1 = new CoordinateProjected(values.get(0), values.get(1), getProjection()); + CoordinateProjected coordinate2 = new CoordinateProjected(values.get(2), values.get(3), getProjection()); + + return new CoordinateProjected[]{coordinate1, coordinate2}; + } + + /** + * Transforms the source center data and sets this instance's fields. + * + * @return The center + */ + private CoordinateProjected transformCenter() { + + List values = new ArrayList(); + JsonArray transformedExtent = jsonViewEvent.getAsJsonArray("center"); + Iterator iterator = transformedExtent.iterator(); + while(iterator.hasNext()) { + JsonElement element = iterator.next(); + values.add(element.getAsDouble()); + } + + CoordinateProjected coordinate = new CoordinateProjected(values.get(0), values.get(1), getProjection()); + + return coordinate; + } + + /** + * Transforms the source data and sets this instance's fields. + */ + private void transformValues() { + + projection = transformProjection(); + extent = transFormExtent(); + center = transformCenter(); + } +} diff --git a/openlayers3-parent/openlayers3/src/main/java/org/wicketstuff/openlayers3/behavior/view/ViewEventFeaturesListener.java b/openlayers3-parent/openlayers3/src/main/java/org/wicketstuff/openlayers3/behavior/view/ViewEventFeaturesListener.java index aa6cd0a3ea..34adf3779f 100644 --- a/openlayers3-parent/openlayers3/src/main/java/org/wicketstuff/openlayers3/behavior/view/ViewEventFeaturesListener.java +++ b/openlayers3-parent/openlayers3/src/main/java/org/wicketstuff/openlayers3/behavior/view/ViewEventFeaturesListener.java @@ -1,139 +1,134 @@ -package org.wicketstuff.openlayers3.behavior.view; - -import com.google.gson.JsonArray; -import com.google.gson.JsonElement; -import com.google.gson.JsonNull; -import com.google.gson.JsonParser; -import org.apache.wicket.Component; -import org.apache.wicket.ajax.AbstractDefaultAjaxBehavior; -import org.apache.wicket.ajax.AjaxRequestTarget; -import org.apache.wicket.markup.head.IHeaderResponse; -import org.apache.wicket.markup.head.OnDomReadyHeaderItem; -import org.apache.wicket.request.IRequestParameters; -import org.apache.wicket.request.cycle.RequestCycle; -import org.apache.wicket.util.template.PackageTextTemplate; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.wicketstuff.openlayers3.api.layer.Vector; - -import java.util.HashMap; -import java.util.Map; - -/** - * Provides a behavior that invokes a callback event whenever the map view changes. Specifically, this behavior will - * monitor the map's ol.View for 'center' and 'resolution' events. In addition to providing information on the current - * view, it will also provide all of the features in the view extent for the provided layer. - */ -public abstract class ViewEventFeaturesListener extends AbstractDefaultAjaxBehavior { - - private final static Logger logger = LoggerFactory.getLogger(ViewEventFeaturesListener.class); - - /** - * Default projection. - */ - public final static String DEFAULT_PROJECTION = "EPSG:4326"; - - /** - * Counter for generating instance identifiers. - */ - private static Long counter = 0L; - - /** - * Map for storing object Ids. - */ - private static java.util.Map objectIds = new HashMap(); - - /** - * Projection for this map. - */ - private String projection; - - /** - * Layer for which features will be enumerated. - */ - private Vector vector; - - public ViewEventFeaturesListener(Vector vectorLayer) { - this(DEFAULT_PROJECTION, vectorLayer); - } - - public ViewEventFeaturesListener(String projection, Vector vectorLayer) { - this.vector = vectorLayer; - this.projection = projection; - } - - /** - * Callback method that is invoked when the map's view is changed. - * - * @param target Ajax request target - * @param viewEvent The current view - * @param features JSON array of features in the view - */ - public abstract void handleViewEvent(AjaxRequestTarget target, ViewEvent viewEvent, JsonArray features); - - /** - * Javascript name of the callback function to invoke when feature data has been loaded into the layer. - * - * @return String with the Javascript callback function name - */ - public String getCallbackFunctionName() { - return "viewEventFeaturesHandler_" + getId(); - } - - /** - * Returns a unique ID for this behavior. - * - * @return String with a unique ID - */ - public String getId() { - - String objectId = null; - - if (objectIds.get(this) != null) { - objectId = objectIds.get(this); - } else { - objectId = getClass().getSimpleName().toLowerCase() + counter++; - objectIds.put(this, objectId); - } - - return objectId; - } - - @Override - protected void respond(AjaxRequestTarget target) { - - IRequestParameters params = RequestCycle.get().getRequest().getRequestParameters(); - String viewJson = params.getParameterValue("view").toString(); - String featuresJson = params.getParameterValue("features").toString(); - - ViewEvent viewEvent = null; - JsonElement viewParsed = new JsonParser().parse(viewJson); - if(!(viewParsed instanceof JsonNull)) { - viewEvent = new ViewEvent(viewParsed.getAsJsonObject()); - } - - JsonArray features= null; - JsonElement featuresParsed = new JsonParser().parse(featuresJson); - if(!(featuresParsed instanceof JsonNull)) { - features = featuresParsed.getAsJsonArray(); - } - - handleViewEvent(target, viewEvent, features); - } - - @Override - public void renderHead(Component component, IHeaderResponse response) { - super.renderHead(component, response); - - final Map params = new HashMap(); - params.put("callbackUrl", getCallbackUrl()); - params.put("callbackFunctionName", getCallbackFunctionName()); - params.put("componentId", component.getMarkupId()); - params.put("layerId", vector.getJsId()); - params.put("projection", projection != null ? projection : "NULL"); - - PackageTextTemplate template = new PackageTextTemplate(ViewEventListener.class, - "ViewEventFeaturesListener.js"); - response.render(OnDomReadyHeaderItem.forScript(template.asString(params))); - } -} +package org.wicketstuff.openlayers3.behavior.view; + +import java.util.HashMap; +import java.util.Map; + +import org.apache.wicket.Component; +import org.apache.wicket.ajax.AbstractDefaultAjaxBehavior; +import org.apache.wicket.ajax.AjaxRequestTarget; +import org.apache.wicket.markup.head.IHeaderResponse; +import org.apache.wicket.request.IRequestParameters; +import org.apache.wicket.request.cycle.RequestCycle; +import org.wicketstuff.openlayers3.api.layer.Vector; +import org.wicketstuff.openlayers3.api.util.HeaderUtils; + +import com.google.gson.JsonArray; +import com.google.gson.JsonElement; +import com.google.gson.JsonNull; +import com.google.gson.JsonParser; + +/** + * Provides a behavior that invokes a callback event whenever the map view changes. Specifically, this behavior will + * monitor the map's ol.View for 'center' and 'resolution' events. In addition to providing information on the current + * view, it will also provide all of the features in the view extent for the provided layer. + */ +public abstract class ViewEventFeaturesListener extends AbstractDefaultAjaxBehavior { + + /** + * Default projection. + */ + public final static String DEFAULT_PROJECTION = "EPSG:4326"; + + /** + * Counter for generating instance identifiers. + */ + private static Long counter = 0L; + + /** + * Map for storing object Ids. + */ + private static java.util.Map objectIds = new HashMap(); + + /** + * Projection for this map. + */ + private String projection; + + /** + * Layer for which features will be enumerated. + */ + private Vector vector; + + public ViewEventFeaturesListener(Vector vectorLayer) { + this(DEFAULT_PROJECTION, vectorLayer); + } + + public ViewEventFeaturesListener(String projection, Vector vectorLayer) { + this.vector = vectorLayer; + this.projection = projection; + } + + /** + * Callback method that is invoked when the map's view is changed. + * + * @param target Ajax request target + * @param viewEvent The current view + * @param features JSON array of features in the view + */ + public abstract void handleViewEvent(AjaxRequestTarget target, ViewEvent viewEvent, JsonArray features); + + /** + * Javascript name of the callback function to invoke when feature data has been loaded into the layer. + * + * @return String with the Javascript callback function name + */ + public String getCallbackFunctionName() { + return "viewEventFeaturesHandler_" + getId(); + } + + /** + * Returns a unique ID for this behavior. + * + * @return String with a unique ID + */ + public String getId() { + + String objectId = null; + + if (objectIds.get(this) != null) { + objectId = objectIds.get(this); + } else { + objectId = getClass().getSimpleName().toLowerCase() + counter++; + objectIds.put(this, objectId); + } + + return objectId; + } + + @Override + protected void respond(AjaxRequestTarget target) { + + IRequestParameters params = RequestCycle.get().getRequest().getRequestParameters(); + String viewJson = params.getParameterValue("view").toString(); + String featuresJson = params.getParameterValue("features").toString(); + + ViewEvent viewEvent = null; + JsonElement viewParsed = new JsonParser().parse(viewJson); + if(!(viewParsed instanceof JsonNull)) { + viewEvent = new ViewEvent(viewParsed.getAsJsonObject()); + } + + JsonArray features= null; + JsonElement featuresParsed = new JsonParser().parse(featuresJson); + if(!(featuresParsed instanceof JsonNull)) { + features = featuresParsed.getAsJsonArray(); + } + + handleViewEvent(target, viewEvent, features); + } + + @Override + public void renderHead(Component component, IHeaderResponse response) { + super.renderHead(component, response); + + final Map params = new HashMap(); + params.put("callbackUrl", getCallbackUrl()); + params.put("callbackFunctionName", getCallbackFunctionName()); + params.put("componentId", component.getMarkupId()); + params.put("layerId", vector.getJsId()); + params.put("projection", projection != null ? projection : "NULL"); + + HeaderUtils.renderOnDomReady(response, ViewEventFeaturesListener.class, + "ViewEventFeaturesListener.js", params); + } +} diff --git a/openlayers3-parent/openlayers3/src/main/java/org/wicketstuff/openlayers3/behavior/view/ViewEventListener.java b/openlayers3-parent/openlayers3/src/main/java/org/wicketstuff/openlayers3/behavior/view/ViewEventListener.java index 9ca755a72a..381fd2779a 100644 --- a/openlayers3-parent/openlayers3/src/main/java/org/wicketstuff/openlayers3/behavior/view/ViewEventListener.java +++ b/openlayers3-parent/openlayers3/src/main/java/org/wicketstuff/openlayers3/behavior/view/ViewEventListener.java @@ -1,125 +1,124 @@ -package org.wicketstuff.openlayers3.behavior.view; - -import com.google.gson.JsonElement; -import com.google.gson.JsonNull; -import com.google.gson.JsonParser; -import org.apache.wicket.Component; -import org.apache.wicket.ajax.AbstractDefaultAjaxBehavior; -import org.apache.wicket.ajax.AjaxRequestTarget; -import org.apache.wicket.markup.head.IHeaderResponse; -import org.apache.wicket.markup.head.OnDomReadyHeaderItem; -import org.apache.wicket.request.IRequestParameters; -import org.apache.wicket.request.cycle.RequestCycle; -import org.apache.wicket.util.template.PackageTextTemplate; - -import java.util.HashMap; -import java.util.Map; - -/** - * Provides a behavior that invokes a callback event whenever the map view changes. Specifically, this behavior will - * monitor the map's ol.View for 'center' and 'resolution' events. - */ -public abstract class ViewEventListener extends AbstractDefaultAjaxBehavior { - - /** - * Default projection. - */ - public final static String DEFAULT_PROJECTION = "EPSG:4326"; - - /** - * Counter for generating instance identifiers. - */ - private static Long counter = 0L; - - /** - * Map for storing object Ids. - */ - private static java.util.Map objectIds = new HashMap(); - - /** - * Projection for this map. - */ - private String projection; - - /** - * Creates a new instance. - */ - public ViewEventListener() { - this(DEFAULT_PROJECTION); - } - - /** - * Creates a new instance and sets the projection used to transform coordinates. - * - * @param projection Projection used to transform coordinates - */ - public ViewEventListener(String projection) { - this.projection = projection; - } - - /** - * Callback method that is invoked when the map's view is changed. - * - * @param target Ajax request target - * @param viewEvent The current view - */ - public abstract void handleViewEvent(AjaxRequestTarget target, ViewEvent viewEvent); - - /** - * Javascript name of the callback function to invoke when feature data has been loaded into the layer. - * - * @return String with the Javascript callback function name - */ - public String getCallbackFunctionName() { - return "viewEventHandler_" + getId(); - } - - /** - * Returns a unique ID for this behavior. - * - * @return String with a unique ID - */ - public String getId() { - - String objectId = null; - - if (objectIds.get(this) != null) { - objectId = objectIds.get(this); - } else { - objectId = getClass().getSimpleName().toLowerCase() + counter++; - objectIds.put(this, objectId); - } - - return objectId; - } - - @Override - protected void respond(AjaxRequestTarget target) { - - IRequestParameters params = RequestCycle.get().getRequest().getRequestParameters(); - String viewJson = params.getParameterValue("view").toString(); - - ViewEvent viewEvent = null; - JsonElement viewParsed = new JsonParser().parse(viewJson); - if(!(viewParsed instanceof JsonNull)) { - viewEvent = new ViewEvent(viewParsed.getAsJsonObject()); - } - - handleViewEvent(target, viewEvent); - } - - @Override - public void renderHead(Component component, IHeaderResponse response) { - super.renderHead(component, response); - - final Map params = new HashMap(); - params.put("callbackUrl", getCallbackUrl()); - params.put("callbackFunctionName", getCallbackFunctionName()); - params.put("componentId", component.getMarkupId()); - params.put("projection", projection != null ? projection : "NULL"); - - PackageTextTemplate template = new PackageTextTemplate(ViewEventListener.class, - "ViewEventListener.js"); - response.render(OnDomReadyHeaderItem.forScript(template.asString(params))); - } -} +package org.wicketstuff.openlayers3.behavior.view; + +import java.util.HashMap; +import java.util.Map; + +import org.apache.wicket.Component; +import org.apache.wicket.ajax.AbstractDefaultAjaxBehavior; +import org.apache.wicket.ajax.AjaxRequestTarget; +import org.apache.wicket.markup.head.IHeaderResponse; +import org.apache.wicket.request.IRequestParameters; +import org.apache.wicket.request.cycle.RequestCycle; +import org.wicketstuff.openlayers3.api.util.HeaderUtils; + +import com.google.gson.JsonElement; +import com.google.gson.JsonNull; +import com.google.gson.JsonParser; + +/** + * Provides a behavior that invokes a callback event whenever the map view changes. Specifically, this behavior will + * monitor the map's ol.View for 'center' and 'resolution' events. + */ +public abstract class ViewEventListener extends AbstractDefaultAjaxBehavior { + + /** + * Default projection. + */ + public final static String DEFAULT_PROJECTION = "EPSG:4326"; + + /** + * Counter for generating instance identifiers. + */ + private static Long counter = 0L; + + /** + * Map for storing object Ids. + */ + private static java.util.Map objectIds = new HashMap(); + + /** + * Projection for this map. + */ + private String projection; + + /** + * Creates a new instance. + */ + public ViewEventListener() { + this(DEFAULT_PROJECTION); + } + + /** + * Creates a new instance and sets the projection used to transform coordinates. + * + * @param projection Projection used to transform coordinates + */ + public ViewEventListener(String projection) { + this.projection = projection; + } + + /** + * Callback method that is invoked when the map's view is changed. + * + * @param target Ajax request target + * @param viewEvent The current view + */ + public abstract void handleViewEvent(AjaxRequestTarget target, ViewEvent viewEvent); + + /** + * Javascript name of the callback function to invoke when feature data has been loaded into the layer. + * + * @return String with the Javascript callback function name + */ + public String getCallbackFunctionName() { + return "viewEventHandler_" + getId(); + } + + /** + * Returns a unique ID for this behavior. + * + * @return String with a unique ID + */ + public String getId() { + + String objectId = null; + + if (objectIds.get(this) != null) { + objectId = objectIds.get(this); + } else { + objectId = getClass().getSimpleName().toLowerCase() + counter++; + objectIds.put(this, objectId); + } + + return objectId; + } + + @Override + protected void respond(AjaxRequestTarget target) { + + IRequestParameters params = RequestCycle.get().getRequest().getRequestParameters(); + String viewJson = params.getParameterValue("view").toString(); + + ViewEvent viewEvent = null; + JsonElement viewParsed = new JsonParser().parse(viewJson); + if(!(viewParsed instanceof JsonNull)) { + viewEvent = new ViewEvent(viewParsed.getAsJsonObject()); + } + + handleViewEvent(target, viewEvent); + } + + @Override + public void renderHead(final Component component, final IHeaderResponse response) { + super.renderHead(component, response); + + final Map params = new HashMap(); + params.put("callbackUrl", getCallbackUrl()); + params.put("callbackFunctionName", getCallbackFunctionName()); + params.put("componentId", component.getMarkupId()); + params.put("projection", projection != null ? projection : "NULL"); + + HeaderUtils.renderOnDomReady(response, ViewEventListener.class, + "ViewEventListener.js", params); + } +} diff --git a/openlayers3-parent/pom.xml b/openlayers3-parent/pom.xml index b43822467a..a0b406869c 100644 --- a/openlayers3-parent/pom.xml +++ b/openlayers3-parent/pom.xml @@ -1,46 +1,47 @@ - + - 4.0.0 + 4.0.0 - - org.wicketstuff - wicketstuff-core - 8.0.0-SNAPSHOT - + + org.wicketstuff + wicketstuff-core + 8.0.0-SNAPSHOT + - openlayers3-parent - pom + openlayers3-parent + pom - Openlayers3 Integration - Parent - + Openlayers3 Integration - Parent + See the child project "openlayers3" for a more complete description. This is the parent for the actual project and the examples. - - openlayers3 - openlayers3-bootstrap - openlayers3-examples - + + openlayers3 + openlayers3-bootstrap + openlayers3-examples + - - - - ${project.groupId} - wicketstuff-openlayers3 - ${project.version} - - - de.agilecoders.wicket.webjars - wicket-webjars - 0.5.3 - - - org.webjars - openlayers - 3.10.1 - - - + + + + ${project.groupId} + wicketstuff-openlayers3 + ${project.version} + + + de.agilecoders.wicket.webjars + wicket-webjars + 0.5.3 + + + org.webjars + openlayers + 3.10.1 + + +