/*
 * Copyright (c) 2012-2018 Red Hat, Inc.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   Red Hat, Inc. - initial API and implementation
 */
package org.eclipse.che.ide.resources.action;

import static com.google.common.base.Preconditions.checkState;
import static java.util.Collections.singletonList;
import static org.eclipse.che.ide.part.perspectives.project.ProjectPerspective.PROJECT_PERSPECTIVE_ID;
import static org.eclipse.che.ide.resource.Path.valueOf;

import com.google.common.annotations.Beta;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.google.web.bindery.event.shared.EventBus;
import java.util.Map;
import javax.validation.constraints.NotNull;
import org.eclipse.che.ide.CoreLocalizationConstant;
import org.eclipse.che.ide.api.action.AbstractPerspectiveAction;
import org.eclipse.che.ide.api.action.ActionEvent;
import org.eclipse.che.ide.api.app.AppContext;
import org.eclipse.che.ide.api.resources.Resource;
import org.eclipse.che.ide.resource.Path;
import org.eclipse.che.ide.resources.reveal.RevealResourceEvent;

/**
 * Scrolls from resource in the context to the stored location in the Project View.
 *
 * @author Vlad Zhukovskiy
 * @since 4.4.0
 */
@Beta
@Singleton
public class RevealResourceAction extends AbstractPerspectiveAction {

  private static final String PATH = "path";

  private final AppContext appContext;
  private final EventBus eventBus;

  @Inject
  public RevealResourceAction(
      AppContext appContext, EventBus eventBus, CoreLocalizationConstant localizedConstant) {
    super(
        singletonList(PROJECT_PERSPECTIVE_ID),
        localizedConstant.actionRevealResourceText(),
        localizedConstant.actionRevealResourceDescription());
    this.appContext = appContext;
    this.eventBus = eventBus;
  }

  /** {@inheritDoc} */
  @Override
  public void updateInPerspective(@NotNull ActionEvent event) {
    final Resource[] resources = appContext.getResources();

    event.getPresentation().setVisible(true);
    event.getPresentation().setEnabled(resources != null && resources.length == 1);
  }

  /** {@inheritDoc} */
  @Override
  public void actionPerformed(ActionEvent e) {
    Map<String, String> params = e.getParameters();
    if (params != null && params.containsKey(PATH)) {
      String pathToReveal = params.get(PATH);
      Path path = valueOf(pathToReveal);

      checkState(!path.isEmpty());

      eventBus.fireEvent(new RevealResourceEvent(path));
    } else {
      final Resource[] resources = appContext.getResources();

      checkState(resources != null && resources.length == 1);

      eventBus.fireEvent(new RevealResourceEvent(resources[0]));
    }
  }
}

@@ -520,16 +520,86 @@
 onList;%0A
+import static org.eclipse.che.ide.api.parts.PartStackType.NAVIGATION;%0A
 import s
@@ -827,16 +827,51 @@
 Inject;%0A
+import com.google.inject.Provider;%0A
 import c
@@ -1269,26 +1269,254 @@
 api.
-resources.Resource
+parts.PartPresenter;%0Aimport org.eclipse.che.ide.api.parts.PartStack;%0Aimport org.eclipse.che.ide.api.parts.WorkspaceAgent;%0Aimport org.eclipse.che.ide.api.resources.Resource;%0Aimport org.eclipse.che.ide.part.explorer.project.ProjectExplorerPresenter
 ;%0Aim
@@ -1966,16 +1966,148 @@
 ventBus;
+%0A  private final Provider%3CProjectExplorerPresenter%3E projectExplorerPresenterProvider;%0A  private final WorkspaceAgent workspaceAgent;
 %0A%0A  @Inj
@@ -2169,16 +2169,22 @@
 Context,
+%0A     
  EventBu
@@ -2194,16 +2194,22 @@
 ventBus,
+%0A     
  CoreLoc
@@ -2243,16 +2243,128 @@
 Constant
+,%0A      Provider%3CProjectExplorerPresenter%3E projectExplorerPresenterProvider,%0A      WorkspaceAgent workspaceAgent
 ) %7B%0A    
@@ -2597,16 +2597,136 @@
 entBus;%0A
+    this.projectExplorerPresenterProvider = projectExplorerPresenterProvider;%0A    this.workspaceAgent = workspaceAgent;%0A
   %7D%0A%0A  /
@@ -3325,24 +3325,59 @@
 sEmpty());%0A%0A
+      ensureProjectExplorerPart();%0A
       eventB
@@ -3556,24 +3556,59 @@
 gth == 1);%0A%0A
+      ensureProjectExplorerPart();%0A
       eventB
@@ -3669,11 +3669,538 @@
    %7D%0A  %7D
+%0A%0A  private void ensureProjectExplorerPart() %7B%0A    PartStack navigationPartStack = workspaceAgent.getPartStack(NAVIGATION);%0A    PartPresenter activePart = navigationPartStack.getActivePart();%0A    ProjectExplorerPresenter projectExplorerPresenter = projectExplorerPresenterProvider.get();%0A%0A    if (activePart == null) %7B%0A      workspaceAgent.openPart(projectExplorerPresenter, NAVIGATION);%0A    %7D%0A%0A    if (!(activePart instanceof ProjectExplorerPresenter)) %7B%0A      workspaceAgent.setActivePart(projectExplorerPresenter);%0A    %7D%0A  %7D
 %0A%7D%0A

Switch to project explorer tab for reavel resources action. (#8460)