package com.quantum.actions;

import java.util.Iterator;
import java.util.List;

import com.quantum.ImageStore;
import com.quantum.Messages;
import com.quantum.model.BookmarkCollection;
import com.quantum.view.bookmark.BookmarkNode;

import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.actions.SelectionListenerAction;

/**
 * @author root
 *
 */
public class DeleteBookmarkAction extends SelectionListenerAction {
    
    private IViewPart view;
    
	public DeleteBookmarkAction(IViewPart view) {
        super(Messages.getString(DeleteBookmarkAction.class.getName() + ".text"));
        setImageDescriptor(ImageStore.getImageDescriptor(ImageStore.DELETE));
		this.view = view;
	}

	/**
	 * @see org.eclipse.ui.IActionDelegate#run(IAction)
	 */
	public void run(IAction action) {
		run();
	}
	
	public void run() {
		List bookmarkNodes = getSelectedNonResources();
        boolean flag = MessageDialog.openConfirm(view.getSite().getShell(), Messages.getString("bookmarkview.deleteBookmark"), Messages.getString("bookmarkview.confirm")); //$NON-NLS-1$ //$NON-NLS-2$

        for (Iterator i = bookmarkNodes.iterator(); flag && i.hasNext();) {
            BookmarkNode bookmarkNode = (BookmarkNode) i.next();
            if (bookmarkNode != null) {
                BookmarkCollection.getInstance().removeBookmark(bookmarkNode.getBookmark());
            }
        }
	}

	public boolean updateSelection(IStructuredSelection selection) {
        boolean enabled = super.updateSelection(selection);
        for (Iterator i = selection.iterator(); enabled && i.hasNext(); ) {
            enabled &= (i.next() instanceof BookmarkNode);
        }
        
        return enabled;
	}

}