/* $Id: GSEditorPopupMenu.java 439 2011-10-19 12:50:30Z shayashi $ */
package smart_gs.smleditor.swingui;

import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;

import smart_gs.connection.ConnectionCreator;
import smart_gs.drawing_tool.action.AddToBasketAction;
import smart_gs.drawing_tool.action.AnchorJumpAction;
import smart_gs.drawing_tool.action.JumpWebPageAction;
import smart_gs.drawing_tool.action.RemoveExternalLinkAction;
import smart_gs.drawing_tool.action.SetExternalLinkAction;
import smart_gs.drawing_tool.action.SetLinkSourceAction;
import smart_gs.drawing_tool.action.SetLinkTargetAction;
import smart_gs.drawing_tool.action.cancelConnectionCreationAction;
import smart_gs.logical.AnchorTextSegment;
import smart_gs.logical.GSResource;
import smart_gs.logical.ResourceBasket;
import smart_gs.logical.SmartDocument;
import smart_gs.logical.TextSegment;
import smart_gs.reasoning_web.logical.GS_RWRepository;
import smart_gs.reasoning_web.logical.LinkRemover;
import smart_gs.spread_selection.MarkupUtil;
import smart_gs.swingui.toolbar.action.ShowLocalViewAction;
import sml_editor.swingui.EditorTextPane;
 
@SuppressWarnings("serial")
public class GSEditorPopupMenu extends JPopupMenu{
	
	public GSEditorPopupMenu(final TextSegment segment,final EditorTextPane textPane,final Point point,boolean href) {
		if(segment.getTagName().equals("anchor")) {
			JMenuItem jumpToAnchorRegion = new JMenuItem("Jump to Linked Paragraph");
			jumpToAnchorRegion.addActionListener(new AnchorJumpAction((AnchorTextSegment)segment));
			this.add(jumpToAnchorRegion);
		}
		
		JMenuItem setSource = new JMenuItem("Set Link Source");
		if((!ConnectionCreator.isMakingLink()) ||
				ConnectionCreator.isMakingLinkTargetAlreadySet()){
			setSource.setEnabled(true);
			setSource.addActionListener(new SetLinkSourceAction(segment));
		}else{
			setSource.setEnabled(false);
		}
		if(href){
			setSource.setEnabled(false);
		}
		this.add(setSource);
	
		JMenuItem setTarget = new JMenuItem("Set Link Target");
		if((!ConnectionCreator.isMakingLink()) ||
				ConnectionCreator.isMakingLinkSourceAlreadySet()){
			setTarget.setEnabled(true);
			setTarget.addActionListener(new SetLinkTargetAction(segment));
		}else{
			setTarget.setEnabled(false);
		}
		if(href){
			setTarget.setEnabled(false);
		}
		this.add(setTarget);
		
		
		JMenuItem cancelConnectionCreation = new JMenuItem("Cancel Connection Creation");
		cancelConnectionCreation.addActionListener(new cancelConnectionCreationAction());
		if(href){
			cancelConnectionCreation.setEnabled(false);
		} else if(ConnectionCreator.isMakingLink()) {
			cancelConnectionCreation.setEnabled(true);
		} else {
			cancelConnectionCreation.setEnabled(false);
		}
		this.add(cancelConnectionCreation);

		JMenuItem removeMarkup = new JMenuItem("Remove Mark up");
		removeMarkup.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent arg0) {
				SmartDocument doc = segment.getDocument();
				int id = segment.getId();
				doc.removeTextSegment(id);
				LinkRemover.removeLinksOf(segment.getURI());
				MarkupUtil markupUtil = new MarkupUtil(textPane,textPane.getSource().length());
				int start = textPane.getGSHTMLEditorTextPane().getSelectionStart();
				int end = textPane.getGSHTMLEditorTextPane().getSelectionEnd();
				if(markupUtil.check(start,end)){
					while(markupUtil.check(start,end)){
						markupUtil.releasMakup();
					}
					return;
				}
			}
		});
		this.add(removeMarkup);
		
		JMenuItem setExternal = new JMenuItem("Set External Link");
	    setExternal.addActionListener(new SetExternalLinkAction(segment,textPane,point));
	    if(ConnectionCreator.getSource() != null){
	    	setExternal.setEnabled(false);
	    }
		this.add(setExternal);

		JMenuItem removeExternal = new JMenuItem("Remove External Link");
	    removeExternal.addActionListener(new RemoveExternalLinkAction(segment,textPane,point));
		this.add(removeExternal);

		JMenuItem localView = new JMenuItem("Show Local View");
		if(!GS_RWRepository.getInstance().hasLink(segment)){
			localView.setEnabled(false);
		}
		localView.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent arg0) {
				new ShowLocalViewAction(segment).execute();
			}
		});
		this.add(localView);

		JMenuItem webjump = new JMenuItem("Show Web Page");
		webjump.addActionListener(new JumpWebPageAction(textPane,point));
		if(!href){
			webjump.setEnabled(false);
		}
		this.add(webjump);
		
		JMenu bucket = new JMenu("Add to Basket");
		List<GSResource> baskets = GS_RWRepository.getInstance().getResourceBaskets();
		for (int i = 0; i < baskets.size(); i++) {
			JMenuItem item = new JMenuItem(baskets.get(i).getName());
			item.addActionListener(new AddToBasketAction((ResourceBasket) baskets.get(i), segment));
			bucket.add(item);
		}
		this.add(bucket);
		JMenuItem basketFrame = new JMenuItem("Send to Basket List");
		basketFrame.addActionListener(new AddToBasketAction(segment));
		this.add(basketFrame);


	}
}
