Skip to content

Commit e360058

Browse files
authored
Merge pull request #112 from Esri/tyle8552/webmap_keyword_search
Webmap Keyword Search
2 parents 4ab157a + b3db637 commit e360058

File tree

5 files changed

+296
-0
lines changed

5 files changed

+296
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<h1>Webmap Keyword Search</h1>
2+
3+
<p>Shows how to search for webmaps within a portal using a keyword.</p>
4+
5+
<p><img src="WebmapKeywordSearch.png"/></p>
6+
7+
<h2>How to use the sample</h2>
8+
9+
Input a keyword into the text field and press Enter to search. Click on a result to show the webmap in the map view.
10+
Click on the "Find More Results" button to add more results to the list.
11+
12+
<h2>How it works</h2>
13+
14+
<p>To search for webmaps in a <code>Portal</code> matching a keyword:</p>
15+
<ol>
16+
<li>Create a <code>Portal</code> and load it</li>
17+
<li>Create <code>PortalItemQueryParameters</code>. Set the type to <code>PortalItem.Type.WEBMAP</code> and the
18+
query to the keyword you want to search</li>
19+
<li>Use <code>portal.findItemsAsync(params)</code> to get the first set of matching items.</li>
20+
<li>Get more results with <code>portal.findItemsAsync(portalQueryResultSet.getNextQueryParameters())</code></li>
21+
</ol>
22+
23+
<h2>Features</h2>
24+
25+
<ul>
26+
<li>ArcGISMap</li>
27+
<li>MapView</li>
28+
<li>Portal</li>
29+
<li>PortalItem</li>
30+
<li>PortalQueryParameters</li>
31+
<li>PortalQueryResultSet</li>
32+
</ul>
335 KB
Loading
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
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+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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.io.IOException;
20+
21+
import javafx.application.Application;
22+
import javafx.fxml.FXMLLoader;
23+
import javafx.scene.Parent;
24+
import javafx.scene.Scene;
25+
import javafx.stage.Stage;
26+
27+
public class WebmapKeywordSearchSample extends Application {
28+
29+
private static WebmapKeywordSearchController controller;
30+
31+
@Override
32+
public void start(Stage stage) throws IOException {
33+
// set up the scene
34+
FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/webmap_keyword_search.fxml"));
35+
Parent root = loader.load();
36+
controller = loader.getController();
37+
Scene scene = new Scene(root);
38+
39+
// set up the stage
40+
stage.setTitle("Webmap Keyword Search Sample");
41+
stage.setWidth(800);
42+
stage.setHeight(700);
43+
stage.setScene(scene);
44+
stage.show();
45+
}
46+
47+
/**
48+
* Stops and releases all resources used in application.
49+
*/
50+
@Override
51+
public void stop() {
52+
controller.terminate();
53+
}
54+
55+
/**
56+
* Opens and runs application.
57+
*
58+
* @param args arguments passed to this application
59+
*/
60+
public static void main(String[] args) {
61+
62+
Application.launch(args);
63+
}
64+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Copyright 2016 Esri.
4+
~
5+
~ Licensed under the Apache License, Version 2.0 (the "License"); you may not
6+
~ use this file except in compliance with the License. You may obtain a copy of
7+
~ the License at
8+
~
9+
~ http://www.apache.org/licenses/LICENSE-2.0
10+
~
11+
~ Unless required by applicable law or agreed to in writing, software
12+
~ distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13+
~ WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14+
~ License for the specific language governing permissions and limitations under
15+
~ the License.
16+
-->
17+
<?import javafx.scene.control.Button?>
18+
<?import javafx.scene.control.ListView?>
19+
<?import javafx.scene.control.TextField?>
20+
<?import javafx.scene.layout.StackPane?>
21+
<?import javafx.scene.layout.VBox?>
22+
<?import com.esri.arcgisruntime.mapping.view.MapView?>
23+
<StackPane fx:controller="com.esri.samples.portal.webmap_keyword_search.WebmapKeywordSearchController"
24+
xmlns:fx="http://javafx.com/fxml" stylesheets="/css/style.css">
25+
<MapView fx:id="mapView"/>
26+
<VBox StackPane.alignment="TOP_LEFT" spacing="10" maxWidth="250" styleClass="panel-region">
27+
<TextField fx:id="keyword" promptText="search" onAction="#search"/>
28+
<ListView fx:id="resultsList"/>
29+
<Button fx:id="moreButton" text="Find more results" onAction="#getMoreResults" disable="true"/>
30+
</VBox>
31+
</StackPane>

0 commit comments

Comments
 (0)