|
| 1 | +/* (c) 2014 - 2016 Open Source Geospatial Foundation - all rights reserved |
| 2 | + * (c) 2001 - 2013 OpenPlans |
| 3 | + * This code is licensed under the GPL 2.0 license, available at the root |
| 4 | + * application directory. |
| 5 | + */ |
| 6 | +package gov.nasa.worldwind.gs.geopkg.web; |
| 7 | + |
| 8 | +import org.apache.wicket.markup.html.basic.Label; |
| 9 | +import org.apache.wicket.markup.html.form.FormComponentPanel; |
| 10 | +import org.apache.wicket.markup.html.form.TextField; |
| 11 | +import org.apache.wicket.model.IModel; |
| 12 | +import org.apache.wicket.model.Model; |
| 13 | +import org.apache.wicket.model.PropertyModel; |
| 14 | +import org.apache.wicket.model.ResourceModel; |
| 15 | + |
| 16 | +import com.vividsolutions.jts.geom.Envelope; |
| 17 | +import org.apache.wicket.Component; |
| 18 | +import org.apache.wicket.util.visit.IVisit; |
| 19 | +import org.apache.wicket.util.visit.IVisitor; |
| 20 | +import org.geoserver.web.wicket.DecimalTextField; |
| 21 | + |
| 22 | +/** |
| 23 | + * A form component for a {@link Envelope} object. |
| 24 | + * |
| 25 | + * @author Justin Deoliveira, OpenGeo |
| 26 | + * @author Andrea Aime, OpenGeo |
| 27 | + */ |
| 28 | +public class BBoxPanel extends FormComponentPanel<Envelope> { |
| 29 | + |
| 30 | + private static final long serialVersionUID = -2975427786330616705L; |
| 31 | + |
| 32 | + protected Label minXLabel, minYLabel, maxXLabel, maxYLabel; |
| 33 | + |
| 34 | + protected Double minX, minY, maxX, maxY; |
| 35 | + |
| 36 | + protected DecimalTextField minXInput, minYInput, maxXInput, maxYInput; |
| 37 | + |
| 38 | + public BBoxPanel(String id) { |
| 39 | + super(id); |
| 40 | + |
| 41 | + initComponents(); |
| 42 | + } |
| 43 | + |
| 44 | + public BBoxPanel(String id, Envelope e) { |
| 45 | + this(id, new Model<Envelope>(e)); |
| 46 | + } |
| 47 | + |
| 48 | + public BBoxPanel(String id, IModel<Envelope> model) { |
| 49 | + super(id, model); |
| 50 | + |
| 51 | + initComponents(); |
| 52 | + } |
| 53 | + |
| 54 | + public void setLabelsVisibility(boolean visible) { |
| 55 | + minXLabel.setVisible(visible); |
| 56 | + minYLabel.setVisible(visible); |
| 57 | + maxXLabel.setVisible(visible); |
| 58 | + maxYLabel.setVisible(visible); |
| 59 | + } |
| 60 | + |
| 61 | + void initComponents() { |
| 62 | + updateFields(); |
| 63 | + |
| 64 | + add(minXLabel = new Label("minXL", new ResourceModel("minX"))); |
| 65 | + add(minYLabel = new Label("minYL", new ResourceModel("minY"))); |
| 66 | + add(maxXLabel = new Label("maxXL", new ResourceModel("maxX"))); |
| 67 | + add(maxYLabel = new Label("maxYL", new ResourceModel("maxY"))); |
| 68 | + |
| 69 | + add(minXInput = new DecimalTextField("minX", new PropertyModel<>(this, "minX"))); |
| 70 | + add(minYInput = new DecimalTextField("minY", new PropertyModel<>(this, "minY"))); |
| 71 | + add(maxXInput = new DecimalTextField("maxX", new PropertyModel<>(this, "maxX"))); |
| 72 | + add(maxYInput = new DecimalTextField("maxY", new PropertyModel<>(this, "maxY"))); |
| 73 | + |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + protected void onBeforeRender() { |
| 78 | + updateFields(); |
| 79 | + super.onBeforeRender(); |
| 80 | + } |
| 81 | + |
| 82 | + private void updateFields() { |
| 83 | + Envelope e = (Envelope) getModelObject(); |
| 84 | + if (e != null) { |
| 85 | + this.minX = e.getMinX(); |
| 86 | + this.minY = e.getMinY(); |
| 87 | + this.maxX = e.getMaxX(); |
| 88 | + this.maxY = e.getMaxY(); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + @SuppressWarnings("unchecked") |
| 93 | + @Override |
| 94 | + public void convertInput() { |
| 95 | + visitChildren(TextField.class, (component, visit) -> { |
| 96 | + ((TextField<String>) component).processInput(); |
| 97 | + }); |
| 98 | + |
| 99 | + // update the envelope model |
| 100 | + if (minX != null && maxX != null && minY != null && maxY != null) { |
| 101 | + setConvertedInput(new Envelope(minX, maxX, minY, maxY)); |
| 102 | + } else { |
| 103 | + setConvertedInput(null); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + @SuppressWarnings("unchecked") |
| 108 | + @Override |
| 109 | + protected void onModelChanged() { |
| 110 | + // when the client programmatically changed the model, update the fields |
| 111 | + // so that the textfields will change too |
| 112 | + updateFields(); |
| 113 | + visitChildren(TextField.class, (Component component, IVisit<Object> visit) -> { |
| 114 | + ((TextField<String>) component).clearInput(); |
| 115 | + }); |
| 116 | + } |
| 117 | + |
| 118 | +} |
0 commit comments