
/**
 * PegaLocaisDiponiveis.java
 *
 * @author Wanderson C. Bragança <wanderson.wbc@gmail.com>
 * @link http://www.wanderson.wordpress.com
 * @copyright Copyright © 2009
 * @version  14/04/2009
 */


import jade.proto.*;
import jade.lang.acl.*;
import jade.domain.JADEAgentManagement.*;
import jade.domain.mobility.MobilityOntology;
import jade.domain.FIPANames;
import jade.content.onto.basic.Action;
import jade.content.onto.basic.Result;
import javax.swing.JOptionPane;

public class PegaLocaisDiponiveis extends SimpleAchieveREInitiator {
    
    private ACLMessage request;
    
    public PegaLocaisDiponiveis(SGRBA a) {
        // chama o construtor de FipaRequestInitiatorBehaviour
        super(a, new ACLMessage(ACLMessage.REQUEST));
        request = (ACLMessage)getDataStore().get(REQUEST_KEY);
        // enche todos os parâmetros do pedido ACLMessage
        request.clearAllReceiver();
        request.addReceiver(a.getAMS());
        request.setLanguage(FIPANames.ContentLanguage.FIPA_SL0);
        request.setOntology(MobilityOntology.NAME);
        request.setProtocol(FIPANames.InteractionProtocol.FIPA_REQUEST);
        // cr�a o �ndice da ACLMessage
        try {
            Action action = new Action();
            action.setActor(a.getAMS());
            action.setAction(new QueryPlatformLocationsAction());
            a.getContentManager().fillContent(request, action);
        } catch(Exception fe) {
            fe.printStackTrace();
        }
        reset(request);
    }
    
    @Override
    protected void handleNotUnderstood(ACLMessage reply) {
        JOptionPane.showMessageDialog(null,myAgent.getLocalName()+" handle não compreendido\n "+reply.toString());
    }
    
    @Override
    protected void handleRefuse(ACLMessage reply) {
        JOptionPane.showMessageDialog(null,myAgent.getLocalName()+" handle recusado\n "+reply.toString());         
    }
    
    @Override
    protected void handleFailure(ACLMessage reply) {
       JOptionPane.showMessageDialog(null,myAgent.getLocalName()+" handle falhou\n "+reply.toString());       
    }
    
    @Override
    protected void handleAgree(ACLMessage reply) {
    }
    
    @Override
    protected void handleInform(ACLMessage inform) {
        String content = inform.getContent();        
        try {
            Result results = (Result)myAgent.getContentManager().extractContent(inform);
            //update  GUI
           ((SGRBA)myAgent).myGui.updateLocais(results.getItems().iterator());
           ((SGRBA)myAgent).myGui.lv = results.getItems().iterator();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
    
}

