/*
 * Autopsy Forensic Browser
 *
 * Copyright 2011 Basis Technology Corp.
 * Contact: carrier <at> sleuthkit <dot> org
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.sleuthkit.autopsy.directorytree;

import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.AbstractAction;
import javax.swing.Action;
import org.openide.nodes.FilterNode;
import org.openide.nodes.Node;
import org.openide.util.lookup.Lookups;
import org.openide.util.lookup.ProxyLookup;
import org.sleuthkit.autopsy.datamodel.DisplayableItemNode;
import org.sleuthkit.autopsy.datamodel.DisplayableItemNodeVisitor;
import org.sleuthkit.autopsy.datamodel.KeywordHits;
import org.sleuthkit.autopsy.ingest.IngestDialog;
import org.sleuthkit.datamodel.BlackboardArtifact;
import org.sleuthkit.datamodel.Content;
import org.sleuthkit.datamodel.Directory;
import org.sleuthkit.datamodel.Image;

/**
 * This class sets the actions for the nodes in the directory tree and creates
 * the children filter so that files and such are hidden from the tree.
 *
 */
class DirectoryTreeFilterNode extends FilterNode {

    private static final Action collapseAll = new CollapseAction("Collapse All");
    private static final DeleteActionsDisplayableItemNodeVisitor getDeleteActionVisitor = new DeleteActionsDisplayableItemNodeVisitor();

    /**
     * the constructor
     */
    DirectoryTreeFilterNode(Node arg, boolean createChildren) {
        super(arg, DirectoryTreeFilterChildren.createInstance(arg, createChildren),
                new ProxyLookup(Lookups.singleton(new OriginalNode(arg)),
                arg.getLookup()));
    }

    /**
     * Right click action for the nodes in the directory tree.
     *
     * @param popup
     * @return
     */
    @Override
    public Action[] getActions(boolean popup) {
        List<Action> actions = new ArrayList<Action>();

        Content c = this.getLookup().lookup(Content.class);
        if (c != null) {
            actions.addAll(DirectoryTreeFilterNode.getDetailActions(c));

            Directory dir = this.getLookup().lookup(Directory.class);
            if (dir != null) {
                actions.add(new ExtractAction("Extract Directory",
                        getOriginal()));
            }
            final Image img = this.getLookup().lookup(Image.class);
            if (img != null) {
                actions.add(new FileSearchAction("Open File Search by Attributes"));
                actions.add(new AbstractAction("Restart Ingest Modules") {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        final IngestDialog ingestDialog = new IngestDialog();
                        ingestDialog.setImage(img);
                        ingestDialog.display();
                    }
                });
            }
        }

        //check if delete actions should be added
        final Node orig = getOriginal();
        //TODO add a mechanism to determine if DisplayableItemNode
        if (orig instanceof DisplayableItemNode) {
            actions.addAll(getDeleteActions((DisplayableItemNode) orig));
        }
        
        actions.add(collapseAll);
        return actions.toArray(new Action[actions.size()]);
    }

    private static List<Action> getDeleteActions(DisplayableItemNode original) {
        List<Action> actions = new ArrayList<Action>();
        //actions.addAll(original.accept(getDeleteActionVisitor));
        return actions;
    }

    private static class DeleteActionsDisplayableItemNodeVisitor extends DisplayableItemNodeVisitor.Default<List<Action>> {

        @Override
        public List<Action> visit(KeywordHits.KeywordHitsRootNode khrn) {
            List<Action> actions = new ArrayList<Action>();
            actions.add(null); // creates a menu separator

            actions.add(new ResultDeleteAction("Delete Results", BlackboardArtifact.ARTIFACT_TYPE.TSK_KEYWORD_HIT));
            return actions;
        }

        @Override
        public List<Action> visit(KeywordHits.KeywordHitsListNode khsn) {
            //TODO delete by list
            return super.visit(khsn);
        }

        @Override
        public List<Action> visit(KeywordHits.KeywordHitsKeywordNode khmln) {
            //TODO delete by keyword hit
            return super.visit(khmln);
        }

        @Override
        protected List<Action> defaultVisit(DisplayableItemNode c) {
            return new ArrayList<Action>();
        }
    }

    private static List<Action> getDetailActions(Content c) {
        List<Action> actions = new ArrayList<Action>();

        actions.addAll(ShowDetailActionVisitor.getActions(c));

        return actions;
    }
}

class OriginalNode {

    private Node original;

    OriginalNode(Node original) {
        this.original = original;
    }

    Node getNode() {
        return original;
    }
}
@@ -1721,145 +1721,8 @@
 l%22);
-%0A    private static final DeleteActionsDisplayableItemNodeVisitor getDeleteActionVisitor = new DeleteActionsDisplayableItemNodeVisitor();
 %0A%0A  
@@ -3889,1005 +3889,8 @@
     
-private static class DeleteActionsDisplayableItemNodeVisitor extends DisplayableItemNodeVisitor.Default%3CList%3CAction%3E%3E %7B%0A%0A        @Override%0A        public List%3CAction%3E visit(KeywordHits.KeywordHitsRootNode khrn) %7B%0A            List%3CAction%3E actions = new ArrayList%3CAction%3E();%0A            actions.add(null); // creates a menu separator%0A%0A            actions.add(new ResultDeleteAction(%22Delete Results%22, BlackboardArtifact.ARTIFACT_TYPE.TSK_KEYWORD_HIT));%0A            return actions;%0A        %7D%0A%0A        @Override%0A        public List%3CAction%3E visit(KeywordHits.KeywordHitsListNode khsn) %7B%0A            //TODO delete by list%0A            return super.visit(khsn);%0A        %7D%0A%0A        @Override%0A        public List%3CAction%3E visit(KeywordHits.KeywordHitsKeywordNode khmln) %7B%0A            //TODO delete by keyword hit%0A            return super.visit(khmln);%0A        %7D%0A%0A        @Override%0A        protected List%3CAction%3E defaultVisit(DisplayableItemNode c) %7B%0A            return new ArrayList%3CAction%3E();%0A        %7D%0A    %7D
 %0A%0A  

remove unused code