/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* Author: Damian Waradzyn
*/
package com.mapmidlet.geocoding;
import java.util.Vector;
import javax.microedition.lcdui.*;
import com.mapmidlet.CloudGps;
import com.mapmidlet.options.Options;
import com.mapmidlet.tile.ui.*;
/**
* Search form allowing user to enter search keywords and using geocoder to
* provide results. User can choose which results he is interested in and going
* back to main map screen scales and centers the map in a way that all chosen
* results are shown on screen.
*
* @author Damian Waradzyn
*/
public class SearchForm extends Form implements CommandListener {
private final TileCanvas tileCanvas;
private TextField search;
private ChoiceGroup resultChoice;
Vector results;
public SearchForm(TileCanvas tileCanvas) {
super("Search map");
this.tileCanvas = tileCanvas;
addElements();
addCommand(new Command("Back", Command.BACK, 2));
addCommand(new Command("Search", Command.OK, 1));
setCommandListener(this);
}
private void addElements() {
search = new TextField("Enter search keywords:", null, 512, 0);
append(search);
resultChoice = new ChoiceGroup("", Choice.MULTIPLE);
append(resultChoice);
}
public void commandAction(Command c, Displayable d) {
String label = c.getLabel();
Options options = Options.getInstance();
if ("Search".equals(label)) {
resultChoice.deleteAll();
if (search.getString() != null && search.getString().length() > 0) {
resultChoice.setLabel("Searching...");
results = options.geocoder.search(search.getString());
if (results == null) {
resultChoice.setLabel("Search failed.");
} else if (results.isEmpty()) {
resultChoice.setLabel("No results found.");
} else {
resultChoice.setLabel("Search results:");
for (int i = 0; i < results.size(); i++) {
resultChoice.append(((ScreenMarker) results.elementAt(i)).name, null);
resultChoice.setSelectedIndex(i, true);
}
}
}
} else if ("Back".equals(label)) {
Vector newSearchResults = new Vector();
for (int i = 0; i < resultChoice.size(); i++) {
if (resultChoice.isSelected(i)) {
newSearchResults.addElement(results.elementAt(i));
}
}
options.searchResults = newSearchResults;
options.refreshMarkers();
tileCanvas.showSearchResults();
CloudGps.showTileCanvas();
}
}
}