|
| 1 | +/* |
| 2 | + * Copyright 2018 Esri. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | + * use this file except in compliance with the License. You may obtain a copy of |
| 6 | + * the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations under |
| 14 | + * the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.esri.samples.displayinformation.display_grid; |
| 18 | + |
| 19 | +import javafx.beans.binding.Bindings; |
| 20 | +import javafx.fxml.FXML; |
| 21 | +import javafx.scene.control.CheckBox; |
| 22 | +import javafx.scene.control.ColorPicker; |
| 23 | +import javafx.scene.control.ComboBox; |
| 24 | +import javafx.scene.paint.Color; |
| 25 | + |
| 26 | +import com.esri.arcgisruntime.geometry.Point; |
| 27 | +import com.esri.arcgisruntime.geometry.SpatialReference; |
| 28 | +import com.esri.arcgisruntime.mapping.ArcGISMap; |
| 29 | +import com.esri.arcgisruntime.mapping.Basemap; |
| 30 | +import com.esri.arcgisruntime.mapping.Viewpoint; |
| 31 | +import com.esri.arcgisruntime.mapping.view.Grid; |
| 32 | +import com.esri.arcgisruntime.mapping.view.LatitudeLongitudeGrid; |
| 33 | +import com.esri.arcgisruntime.mapping.view.MapView; |
| 34 | +import com.esri.arcgisruntime.mapping.view.MgrsGrid; |
| 35 | +import com.esri.arcgisruntime.mapping.view.UsngGrid; |
| 36 | +import com.esri.arcgisruntime.mapping.view.UtmGrid; |
| 37 | +import com.esri.arcgisruntime.symbology.ColorUtil; |
| 38 | +import com.esri.arcgisruntime.symbology.LineSymbol; |
| 39 | +import com.esri.arcgisruntime.symbology.SimpleLineSymbol; |
| 40 | +import com.esri.arcgisruntime.symbology.TextSymbol; |
| 41 | + |
| 42 | +public class DisplayGridController { |
| 43 | + |
| 44 | + @FXML private MapView mapView; |
| 45 | + @FXML private ComboBox<GridType> gridTypeComboBox; |
| 46 | + @FXML private CheckBox labelsVisibleCheckBox; |
| 47 | + @FXML private CheckBox gridVisibleCheckBox; |
| 48 | + @FXML private ColorPicker gridColorPicker; |
| 49 | + @FXML private ColorPicker labelColorPicker; |
| 50 | + @FXML private ComboBox<Grid.LabelPosition> labelPositionComboBox; |
| 51 | + @FXML private ComboBox<LatitudeLongitudeGrid.LabelFormat> labelFormatComboBox; |
| 52 | + |
| 53 | + /** |
| 54 | + * Used for combo box. |
| 55 | + */ |
| 56 | + private enum GridType { |
| 57 | + LAT_LON, UTM, USNG, MGRS |
| 58 | + } |
| 59 | + |
| 60 | + public void initialize() { |
| 61 | + ArcGISMap map = new ArcGISMap(Basemap.createImagery()); |
| 62 | + map.setInitialViewpoint(new Viewpoint(new Point(-10336141.70018318, 5418213.05332071, SpatialReference.create |
| 63 | + (3857)), 6450785)); |
| 64 | + mapView.setMap(map); |
| 65 | + |
| 66 | + // set initial values for options |
| 67 | + gridTypeComboBox.getItems().addAll(GridType.values()); |
| 68 | + gridTypeComboBox.setValue(GridType.LAT_LON); |
| 69 | + gridColorPicker.setValue(Color.WHITE); |
| 70 | + labelColorPicker.setValue(Color.RED); |
| 71 | + labelPositionComboBox.getItems().addAll(Grid.LabelPosition.values()); |
| 72 | + labelPositionComboBox.setValue(Grid.LabelPosition.TOP_LEFT); |
| 73 | + labelFormatComboBox.getItems().addAll(LatitudeLongitudeGrid.LabelFormat.values()); |
| 74 | + labelFormatComboBox.setValue(LatitudeLongitudeGrid.LabelFormat.DECIMAL_DEGREES); |
| 75 | + |
| 76 | + // label position and format only apply to Lat Lon grid type |
| 77 | + labelPositionComboBox.disableProperty().bind(Bindings.createBooleanBinding(() -> |
| 78 | + gridTypeComboBox.getSelectionModel().getSelectedItem() != GridType.LAT_LON, |
| 79 | + gridTypeComboBox.getSelectionModel().selectedItemProperty()) |
| 80 | + ); |
| 81 | + labelFormatComboBox.disableProperty().bind(Bindings.createBooleanBinding(() -> |
| 82 | + gridTypeComboBox.getSelectionModel().getSelectedItem() != GridType.LAT_LON, |
| 83 | + gridTypeComboBox.getSelectionModel().selectedItemProperty()) |
| 84 | + ); |
| 85 | + |
| 86 | + // update the grid with the default values on start |
| 87 | + updateGrid(); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Updates the map view's grid when the "Update" button is clicked. |
| 92 | + */ |
| 93 | + @FXML |
| 94 | + private void updateGrid() { |
| 95 | + // grid type |
| 96 | + Grid grid = null; |
| 97 | + switch (gridTypeComboBox.getSelectionModel().getSelectedItem()) { |
| 98 | + case LAT_LON: |
| 99 | + grid = new LatitudeLongitudeGrid(); |
| 100 | + break; |
| 101 | + case UTM: |
| 102 | + grid = new UtmGrid(); |
| 103 | + break; |
| 104 | + case USNG: |
| 105 | + grid = new UsngGrid(); |
| 106 | + break; |
| 107 | + case MGRS: |
| 108 | + grid = new MgrsGrid(); |
| 109 | + break; |
| 110 | + } |
| 111 | + |
| 112 | + // color the grid lines and labels for each grid level |
| 113 | + for (int i = 0; i < grid.getLevelCount(); i++) { |
| 114 | + // grid lines |
| 115 | + LineSymbol gridLineSymbol = new SimpleLineSymbol(SimpleLineSymbol.Style.SOLID, ColorUtil.colorToArgb(gridColorPicker |
| 116 | + .getValue()), 1 + i); |
| 117 | + grid.setLineSymbol(i, gridLineSymbol); |
| 118 | + |
| 119 | + // labels |
| 120 | + TextSymbol labelTextSymbol = new TextSymbol(14, "text", ColorUtil.colorToArgb(labelColorPicker.getValue()), |
| 121 | + TextSymbol.HorizontalAlignment.LEFT, TextSymbol.VerticalAlignment.BOTTOM); |
| 122 | + labelTextSymbol.setHaloColor(0xFFFFFFFF); //white |
| 123 | + labelTextSymbol.setHaloWidth(2 + i); |
| 124 | + grid.setTextSymbol(i, labelTextSymbol); |
| 125 | + } |
| 126 | + |
| 127 | + // grid visibility |
| 128 | + grid.setVisible(gridVisibleCheckBox.isSelected()); |
| 129 | + |
| 130 | + // label visibility |
| 131 | + grid.setLabelVisible(labelsVisibleCheckBox.isSelected()); |
| 132 | + |
| 133 | + // label position and format |
| 134 | + if (grid instanceof LatitudeLongitudeGrid) { |
| 135 | + grid.setLabelPosition(labelPositionComboBox.getSelectionModel().getSelectedItem()); |
| 136 | + ((LatitudeLongitudeGrid) grid).setLabelFormat(labelFormatComboBox.getSelectionModel().getSelectedItem()); |
| 137 | + } |
| 138 | + |
| 139 | + // set the grid |
| 140 | + mapView.setGrid(grid); |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * Disposes of application resources. |
| 145 | + */ |
| 146 | + void terminate() { |
| 147 | + |
| 148 | + if (mapView != null) { |
| 149 | + mapView.dispose(); |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | +} |
0 commit comments