Skip to main content
summaryrefslogtreecommitdiffstats
blob: c475e98cee8fef79e3623ebe1851c8a172e42943 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/**
 * Copyright (c) 2002-2006 IBM Corporation and others.
 * 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: 
 *   IBM - Initial API and implementation
 */
package org.eclipse.emf.cdo.releng.preferences.action;

import org.eclipse.emf.cdo.releng.preferences.PreferenceNode;
import org.eclipse.emf.cdo.releng.preferences.presentation.PreferencesEditorPlugin;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.actions.ActionDelegate;
import org.eclipse.ui.part.IShowInTarget;
import org.eclipse.ui.part.ShowInContext;

import java.io.File;
import java.net.URI;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ShowInExplorerAction extends ActionDelegate
{
  private static final IWorkspaceRoot WORKSPACE_ROOT = ResourcesPlugin.getWorkspace().getRoot();

  protected ISelection targetSelection;

  public ShowInExplorerAction()
  {
    super();
  }

  @Override
  public void run(IAction action)
  {
    IWorkbenchWindow workbenchWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
    IWorkbenchPage page = workbenchWindow.getActivePage();
    try
    {
      IViewPart packageExplorerView = page.showView("org.eclipse.jdt.ui.PackageExplorer");
      IShowInTarget showInTarget = (IShowInTarget)packageExplorerView.getAdapter(IShowInTarget.class);
      showInTarget.show(new ShowInContext(null, targetSelection));
    }
    catch (PartInitException ex)
    {
      PreferencesEditorPlugin.INSTANCE.log(ex);
    }
  }

  @Override
  public void selectionChanged(IAction action, ISelection selection)
  {
    if (selection instanceof IStructuredSelection)
    {
      List<IResource> resources = new ArrayList<IResource>();
      for (Object object : ((IStructuredSelection)selection).toArray())
      {
        if (object instanceof PreferenceNode)
        {
          PreferenceNode preferenceNode = (PreferenceNode)object;
          String path = preferenceNode.getLocation();

          if (path != null)
          {
            File file = new File(path);
            URI uri = file.toURI();

            IFile[] files = WORKSPACE_ROOT.findFilesForLocationURI(uri);
            if (files.length > 0)
            {
              resources.addAll(Arrays.asList(files));
            }
          }
          else
          {
            PreferenceNode parent = preferenceNode.getParent();
            if (parent != null && "project".equals(parent.getName()))
            {
              PreferenceNode grandParent = parent.getParent();
              if (grandParent != null && "".equals(grandParent.getName()))
              {
                IProject project = WORKSPACE_ROOT.getProject(preferenceNode.getName());
                if (project.isAccessible())
                {
                  resources.add(project);
                }
              }
            }
          }
        }
      }
      if (!resources.isEmpty())
      {
        targetSelection = new StructuredSelection(resources);
        action.setEnabled(true);
        return;
      }
    }

    targetSelection = null;
    action.setEnabled(false);
  }
}

Back to the top