/* $Id: TagAnalysisAction.java 273 2011-07-26 13:53:39Z shayashi $ */
package smart_gs.reasoning_web.swingui.action;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;
import java.util.Vector;

import javax.swing.JOptionPane;

import org.xml.sax.SAXException;

import reasoning_web.logical.NotMarkupException;
import reasoning_web.logical.ResourcePointer;
import smart_gs.reasoning_web.logical.GSResourcePointer;
import smart_gs.reasoning_web.logical.GS_RWRepository;

public class TagAnalysisAction implements ActionListener{

	public void execute(){
		
		Object[] selectionValues = this.makeTagList();
		String tagname = (String)JOptionPane.showInputDialog(null,"select tag","Tag Analysis",JOptionPane.YES_NO_OPTION,null,selectionValues,selectionValues[0]);
		if(tagname == null){
			return ;
		}
		List<ResourcePointer> pointers = GS_RWRepository.getInstance().getResourcePointers();
	
		for(int i=0;i<pointers.size();i++){
			try {
				((GSResourcePointer)pointers.get(i)).getView().restoreDefaultColor();
				String source = ((GSResourcePointer)pointers.get(i)).getRwElement().getSource();
				
				if(source == null){
					continue;
				}
				/*
				SmlController controller = new SmlController(source);
				Document document = controller.getDocument();
				NodeList element = document.getElementsByTagName(tagname);
				if(element.getLength() > 0){
					((GSResourcePointer)pointers.get(i)).getView().setDrawColor(Color.red);
				}
				*/
			} catch (NotMarkupException e) {
				e.printStackTrace();
			}
			
		}
	}
	private Object[] makeTagList(){
		   Vector<String> taglist = new Vector<String>();
		   String line;
		   try{
			   BufferedReader reader = new BufferedReader(new FileReader("tags.txt"));
			   while((line = reader.readLine()) != null){
				   taglist.add(line.trim());
			   }
		   }
		   catch(IOException e){
			   e.printStackTrace();
		   }
		   return taglist.toArray();
		}
	
	public void actionPerformed(ActionEvent arg0) {
		this.execute();
	}
}
