/* $Id: GSMarkupDialog.java 238 2011-06-23 18:11:44Z minao $ */
package smart_gs.smleditor.swingui;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;
import java.util.Vector;

import smart_gs.drawing_tool.drawing_mode.AnchorMode;
import smart_gs.drawing_tool.state.DefaultState;
import smart_gs.logical.AnchorTextSegment;
import smart_gs.logical.SmartDocument;
import smart_gs.logical.TextSegment;
import sml_editor.swingui.MarkupDialog;

public class GSMarkupDialog extends MarkupDialog{

	public GSMarkupDialog(GSEditorTextPane textPane, int start, int end) {
		super(textPane, start, end);
		
	}
	@Override
	public void show() {
		super.show();
		String tagname = this.getTagName();
		if(tagname == null){
			return;
		}
		SmartDocument document = ((GSEditorTextPane)this.textPane).getSmartDocument();
		if (tagname.equals("anchor")) {
			AnchorTextSegment anchorTextSegment = new AnchorTextSegment(document,this.id);
			AnchorMode.setAnchorTextSegment(anchorTextSegment);
			DefaultState.getInstance().setMode(AnchorMode.getInstance());
			document.addTextSegment(anchorTextSegment);
		} else {
			TextSegment segment = new TextSegment(tagname,document,this.id);
			document.addTextSegment(segment);
		}
		((GSEditorTextPane)this.textPane).save();
	}
	@Override
	protected 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();
		   }
		   List<String> tagnames = this.textPane.getGSHTMLEditorTextPane().getTagNames();
		   if(tagnames != null){
			   for(int i=0;i<tagnames.size();i++){
				   if(!taglist.contains(tagnames.get(i).trim())){
					   taglist.add(tagnames.get(i).trim());
				   }
			   }
		   }
		   if (!((GSEditorTextPane)this.textPane).getSmartDocument().isIDAT()) {
			   taglist.remove("anchor");
		   }		   
		   return taglist.toArray();
		}
}
