it.geosdi.era.client.controller.GeocoderController.java Source code

Java tutorial

Introduction

Here is the source code for it.geosdi.era.client.controller.GeocoderController.java

Source

/*
 *  GeoSDI ERA - The new era of webGIS
 *  http://code.google.com/p/geosdiera/
 * ====================================================================
 *
 * Copyright (C) 2008-2009 GeoSDI Group (CNR IMAA).
 *
 * 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; either version 2 of the License, or
 * (at your option) any later version.
 *
 * 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. 
 *
 * ====================================================================
 *
 * This software consists of voluntary contributions made by developers
 * of GeoSDI Group.  For more information on GeoSDI, please see
 * <http://www.geosdi.org/>.
 *
 */
package it.geosdi.era.client.controller;

import it.geosdi.era.client.GeocoderEvents;
import it.geosdi.era.client.model.GeocodingBean;
import it.geosdi.era.client.service.GeocoderRemote;
import it.geosdi.era.client.service.GeocoderRemoteAsync;
import it.geosdi.era.client.widget.GeocoderWidget;

import java.util.ArrayList;

import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.WindowEvent;
import com.extjs.gxt.ui.client.mvc.AppEvent;
import com.extjs.gxt.ui.client.mvc.Controller;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.rpc.AsyncCallback;

/**
 * @author giuseppe
 * 
 */
public abstract class GeocoderController extends Controller {

    protected GeocoderRemoteAsync geocoderService = GeocoderRemote.Util.getInstance();

    protected GeocoderWidget geocoderWidget;

    public GeocoderController() {
        registerEventTypes(GeocoderEvents.InitGeocoder, GeocoderEvents.ShowGeocodingView,
                GeocoderEvents.BeginSearch, GeocoderEvents.CleanUpTheStore, GeocoderEvents.SelectGeocoderRow);
    }

    public void initialize() {
        this.geocoderWidget = new GeocoderWidget();
    }

    @SuppressWarnings("unchecked")
    public void handleEvent(AppEvent event) {
        switch (event.type) {
        case GeocoderEvents.InitGeocoder:
            onInit();
            break;
        case GeocoderEvents.ShowGeocodingView:

            break;
        case GeocoderEvents.BeginSearch:
            onBeginSearch(event.data);
            break;
        case GeocoderEvents.CleanUpTheStore:
            onCleanUpTheStore();
            break;
        case GeocoderEvents.SelectGeocoderRow:
            onSelectGeocoderRow(event);
            break;
        }
    }

    /**
     * 
     * @param data
     */
    private void onBeginSearch(Object data) {
        checkWidgetStatus();
        findsLocation(data.toString());
    }

    /**
     * 
     */
    private void checkWidgetStatus() {
        Timer timer = new Timer() {

            @Override
            public void run() {
                if (geocoderWidget.getGrid().getView().getBody().isMasked()) {
                    MessageBox.confirm("Geocoder - Service",
                            "Geocoding service is demanding too much time, probably the connection problem, do you want to stop it?",
                            new Listener<WindowEvent>() {

                                public void handleEvent(WindowEvent be) {
                                    if (be.buttonClicked.getText().equalsIgnoreCase("yes")
                                            || be.buttonClicked.getText().equalsIgnoreCase("si")) {
                                        geocoderWidget.getGrid().getView().getBody().unmask();
                                        geocoderWidget.getGrid().getView().refresh(false);
                                    } else
                                        schedule(15000);
                                }
                            });
                }

            }
        };

        timer.schedule(15000);
    }

    private void findsLocation(String location) {
        geocoderWidget.maskGrid();
        geocoderService.findLocations(location, new AsyncCallback<ArrayList<GeocodingBean>>() {

            public void onSuccess(ArrayList<GeocodingBean> result) {
                ArrayList<GeocodingBean> beans = (ArrayList<GeocodingBean>) result;
                geocoderWidget.unMaskGrid();
                geocoderWidget.getStore().removeAll();
                if (result != null && result.size() > 0) {
                    geocoderWidget.fillStore(beans);
                } else
                    Info.display("Geocoding - Service", "There are no results for your search.");

            }

            public void onFailure(Throwable caught) {
                geocoderWidget.unMaskGrid();
                geocoderWidget.getGrid().getView().refresh(false);
                Info.display("Geocoding - Service", "An error occurred : " + caught.getMessage());
            }
        });

    }

    private void onCleanUpTheStore() {
        this.geocoderWidget.getStore().removeAll();
        removeMarkerOnTheMap();
    }

    public abstract void onInit();

    public abstract void onSelectGeocoderRow(AppEvent<GeocodingBean> event);

    public abstract void removeMarkerOnTheMap();
}