/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.netbeans.modules.java.testrunner.ui.api;

import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import org.netbeans.modules.gsf.testrunner.ui.api.CallstackFrameNode;
import org.netbeans.modules.gsf.testrunner.ui.api.TestMethodNode;
import org.netbeans.modules.gsf.testrunner.ui.api.TestsuiteNode;
import org.openide.nodes.Node;
import org.openide.util.NbBundle;
import org.openide.util.RequestProcessor;

/**
 *
 * @author Marian Petras
 */
public class JumpAction extends AbstractAction {

    private static final RequestProcessor RP = new RequestProcessor(JumpAction.class);
    /** */
    private final Node node;
    /** */
    private final String callstackFrameInfo;
    private final String projectType;
    private final String testingFramework;

    /** Creates a new instance of JumpAction */
    public JumpAction(Node node, String callstackFrameInfo, String projectType, String testingFramework) {
        this.node = node;
        this.callstackFrameInfo = callstackFrameInfo;
        this.projectType = projectType;
        this.testingFramework = testingFramework;
    }

    /**
     * If the <code>callstackFrameInfo</code> is not <code>null</code>,
     * tries to jump to the callstack frame source code. Otherwise does nothing.
     */
    @Override
    public void actionPerformed(ActionEvent e) {
        RP.post(new Runnable() {
            @Override
            public void run() {
                if (node instanceof TestsuiteNode) {
                    UIJavaUtils.openTestsuite((TestsuiteNode) node, projectType, testingFramework);
                } else if (node instanceof CallstackFrameNode) {
                    UIJavaUtils.openCallstackFrame(node, callstackFrameInfo, projectType, testingFramework);
                } else if (node instanceof TestMethodNode) {
                    if (((TestMethodNode) node).getTestcase().getTrouble() != null) {
                        // method failed, find failing line within the testMethod using the stacktrace
                        UIJavaUtils.openCallstackFrame(node, "", projectType, testingFramework);
                    } else {
                        UIJavaUtils.openTestMethod((TestMethodNode) node, projectType, testingFramework);
                    }
                }
            }
        });
    }

    @Override
    @NbBundle.Messages("LBL_GotoSource=Go to Source")
    public Object getValue(String key) {
        if (key.equals(Action.NAME)) {
            return Bundle.LBL_GotoSource();
        }else{
            return super.getValue(key);
        }
    }

    public Node getNode() {
        return node;
    }

}
