|
| 1 | +/* |
| 2 | + * Copyright 2016 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.portal.webmap_keyword_search; |
| 18 | + |
| 19 | +import java.util.List; |
| 20 | + |
| 21 | +import com.esri.arcgisruntime.concurrent.ListenableFuture; |
| 22 | +import com.esri.arcgisruntime.loadable.LoadStatus; |
| 23 | +import com.esri.arcgisruntime.mapping.ArcGISMap; |
| 24 | +import com.esri.arcgisruntime.mapping.view.MapView; |
| 25 | +import com.esri.arcgisruntime.portal.Portal; |
| 26 | +import com.esri.arcgisruntime.portal.PortalItem; |
| 27 | +import com.esri.arcgisruntime.portal.PortalQueryParameters; |
| 28 | +import com.esri.arcgisruntime.portal.PortalQueryResultSet; |
| 29 | +import com.esri.arcgisruntime.security.AuthenticationManager; |
| 30 | +import com.esri.arcgisruntime.security.OAuthConfiguration; |
| 31 | + |
| 32 | +import javafx.event.ActionEvent; |
| 33 | +import javafx.fxml.FXML; |
| 34 | +import javafx.scene.control.Alert; |
| 35 | +import javafx.scene.control.Button; |
| 36 | +import javafx.scene.control.Dialog; |
| 37 | +import javafx.scene.control.ListCell; |
| 38 | +import javafx.scene.control.ListView; |
| 39 | +import javafx.scene.control.TextField; |
| 40 | + |
| 41 | +public class WebmapKeywordSearchController { |
| 42 | + |
| 43 | + |
| 44 | + @FXML |
| 45 | + private TextField keyword; |
| 46 | + @FXML |
| 47 | + private MapView mapView; |
| 48 | + @FXML |
| 49 | + private ListView<PortalItem> resultsList; |
| 50 | + @FXML |
| 51 | + private Button moreButton; |
| 52 | + |
| 53 | + private Portal portal; |
| 54 | + private PortalQueryResultSet<PortalItem> portalQueryResultSet; |
| 55 | + |
| 56 | + @FXML |
| 57 | + private void initialize() { |
| 58 | + // load a portal for arcgis.com |
| 59 | + portal = new Portal("http://arcgis.com"); |
| 60 | + portal.loadAsync(); |
| 61 | + |
| 62 | + resultsList.setCellFactory(c -> new PortalItemCell()); |
| 63 | + |
| 64 | + // show the selected webmap in a mapview |
| 65 | + resultsList.getSelectionModel().selectedItemProperty().addListener(o -> { |
| 66 | + PortalItem webmap = resultsList.getSelectionModel().getSelectedItem(); |
| 67 | + if (webmap != null) { |
| 68 | + webmap.loadAsync(); |
| 69 | + mapView.setMap(new ArcGISMap(webmap)); |
| 70 | + // check if webmap supported |
| 71 | + mapView.getMap().addDoneLoadingListener(() -> { |
| 72 | + if (mapView.getMap().getLoadError() != null) { |
| 73 | + showMessage("Unable to load map", mapView.getMap().getLoadError().getMessage(), Alert.AlertType.ERROR); |
| 74 | + } |
| 75 | + }); |
| 76 | + } |
| 77 | + }); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Searches a portal for webmaps matching query string in keyword textfield. The list view is updated with |
| 82 | + * the results. |
| 83 | + */ |
| 84 | + @FXML |
| 85 | + private void search() { |
| 86 | + |
| 87 | + // create query parameters specifying the type WEBMAP |
| 88 | + PortalQueryParameters params = new PortalQueryParameters(); |
| 89 | + params.setQuery(PortalItem.Type.WEBMAP, null, keyword.getText()); |
| 90 | + |
| 91 | + // find matching portal items |
| 92 | + ListenableFuture<PortalQueryResultSet<PortalItem>> results = portal.findItemsAsync(params); |
| 93 | + results.addDoneListener(() -> { |
| 94 | + try { |
| 95 | + // update the results list view with matching items |
| 96 | + portalQueryResultSet = results.get(); |
| 97 | + List<PortalItem> portalItems = portalQueryResultSet.getResults(); |
| 98 | + resultsList.getItems().clear(); |
| 99 | + resultsList.getItems().addAll(portalItems); |
| 100 | + moreButton.setDisable(false); |
| 101 | + } catch (Exception e) { |
| 102 | + e.printStackTrace(); |
| 103 | + } |
| 104 | + }); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Adds the next set of results to the list view. |
| 109 | + */ |
| 110 | + @FXML |
| 111 | + private void getMoreResults() { |
| 112 | + if (portalQueryResultSet.getNextQueryParameters() != null) { |
| 113 | + // find matching portal items |
| 114 | + ListenableFuture<PortalQueryResultSet<PortalItem>> results = portal.findItemsAsync(portalQueryResultSet.getNextQueryParameters()); |
| 115 | + results.addDoneListener(() -> { |
| 116 | + try { |
| 117 | + // replace the result set with the current set of results |
| 118 | + portalQueryResultSet = results.get(); |
| 119 | + List<PortalItem> portalItems =portalQueryResultSet.getResults(); |
| 120 | + |
| 121 | + // add set of results to list view |
| 122 | + resultsList.getItems().addAll(portalItems); |
| 123 | + } catch (Exception e) { |
| 124 | + e.printStackTrace(); |
| 125 | + } |
| 126 | + }); |
| 127 | + } else { |
| 128 | + showMessage("End of results", "There are no more results matching this query", Alert.AlertType.INFORMATION); |
| 129 | + moreButton.setDisable(true); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * Shows a Layer title in a ListView. |
| 135 | + */ |
| 136 | + private class PortalItemCell extends ListCell<PortalItem> { |
| 137 | + @Override |
| 138 | + protected void updateItem(PortalItem portalItem, boolean empty) { |
| 139 | + super.updateItem(portalItem, empty); |
| 140 | + setText(empty ? null : portalItem.getTitle()); |
| 141 | + setGraphic(null); |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * Display an alert to the user with the specified information. |
| 147 | + * @param title alert title |
| 148 | + * @param description alert content description |
| 149 | + * @param type alert type |
| 150 | + */ |
| 151 | + private void showMessage(String title, String description, Alert.AlertType type) { |
| 152 | + |
| 153 | + Alert alert = new Alert(type); |
| 154 | + alert.setTitle(title); |
| 155 | + alert.setContentText(description); |
| 156 | + alert.show(); |
| 157 | + } |
| 158 | + |
| 159 | + /** |
| 160 | + * Stops and releases all resources used in application. |
| 161 | + */ |
| 162 | + void terminate() { |
| 163 | + |
| 164 | + if (mapView != null) { |
| 165 | + mapView.dispose(); |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | +} |
0 commit comments