/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package it.cnr.iasi.giant.action;

import it.cnr.iasi.giant.Main;
import it.cnr.iasi.giant.frame.RoleRegionsWindow;
import it.cnr.iasi.giant.thread.ComputePZThread;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import org.cytoscape.application.CyApplicationManager;
import org.cytoscape.model.CyNetwork;
import org.cytoscape.model.CyNetworkManager;
import org.cytoscape.model.CyNetworkTableManager;
import org.cytoscape.model.CyNode;
import org.cytoscape.view.model.CyNetworkView;
import org.cytoscape.view.model.CyNetworkViewManager;

/**
 *
 * @author Fabio Cumbo
 * @info GIANT
 * @version v1.0
 * @organization IASI-CNR : Istituto di Analisi dei Sistemi ed Informatica 
 *                  "Antonio Ruberti" - Consiglio Nazionale delle Ricerche
 * 
 */
public class SearchNodeRegionAction extends Action {

    @Override
    public ArrayList<Object> execute(String command) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public ArrayList<Object> execute() {
        if (RoleRegionsWindow.getNodeRField().getText()!=null || !RoleRegionsWindow.getNodeRField().getText().equals("")) {
            
            if (getNode(RoleRegionsWindow.getNodeRField().getText()) != null) {
                for (int i=0; i<ComputePZThread.getNode2RegionData().length; i++) {
                    try {
                        if (((String)(ComputePZThread.getNode2RegionData()[i][0])).equals(RoleRegionsWindow.getNodeRField().getText())) {
                            RoleRegionsWindow.getSearchResult_label().setText((String)(ComputePZThread.getNode2RegionData()[i][3]));
                            return null;
                        }
                    } catch (NullPointerException e) {}
                }
            }
            else {
                JOptionPane.showMessageDialog(Main.getApp().getFrameOverride(), "Specified node does not exist!", "ERROR", JOptionPane.ERROR_MESSAGE); 
                RoleRegionsWindow.getSearchResult_label().setText("");
            }            
        }
        else {
            JOptionPane.showMessageDialog(Main.getApp().getFrameOverride(), "You have to type node name....", "ERROR", JOptionPane.ERROR_MESSAGE);
            RoleRegionsWindow.getSearchResult_label().setText("");
        }
        return null;
    }
    
    private static CyNode getNode(String node) {
        for (org.cytoscape.model.CyNode n: Main.getGiantAdapter().getCyApplicationManager().getCurrentNetwork().getNodeList()) {
            if (Main.getGiantAdapter().getCyApplicationManager().getCurrentNetwork().getRow(n).get(CyNetwork.NAME, String.class).equals(node))
                return n;
        }
        return null;
    }
    
}
