Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 0f4bd98d61b2188eda099ea67d9503b3a43692ce (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
/*
 * Copyright (c) 2014, 2015 Eike Stepper (Loehne, Germany) 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:
 *    Eike Stepper - initial API and implementation
 */
package org.eclipse.oomph.manifests;

import org.eclipse.oomph.util.ObjectUtil;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IFileEditorInput;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.handlers.HandlerUtil;

/**
 * @author Eike Stepper
 */
public abstract class AbstractProjectHandler extends AbstractHandler
{
  public AbstractProjectHandler()
  {
  }

  public final Object execute(ExecutionEvent event) throws ExecutionException
  {
    IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
    IWorkbenchPage page = window.getActivePage();
    if (page == null)
    {
      return null;
    }

    IProject project = getProject(page);
    if (project != null)
    {
      if (executeWithProject(page, project))
      {
        return null;
      }
    }

    try
    {
      Object element = getSelectedElement(page);
      executeWithElement(page, element);
    }
    catch (Exception ex)
    {
      //$FALL-THROUGH$
    }

    return null;
  }

  protected abstract boolean executeWithProject(IWorkbenchPage page, IProject project);

  protected void executeWithElement(IWorkbenchPage page, Object element) throws Exception
  {
  }

  private static Object getSelectedElement(IWorkbenchPage page)
  {
    ISelection selection = page.getSelection();
    if (selection instanceof IStructuredSelection)
    {
      IStructuredSelection ssel = (IStructuredSelection)selection;
      if (ssel.size() == 1)
      {
        return ssel.getFirstElement();
      }
    }

    return null;
  }

  private static IProject getProject(IWorkbenchPage page)
  {
    Object element = getSelectedElement(page);

    IResource resource = ObjectUtil.adapt(element, IResource.class);
    if (resource == null)
    {
      IEditorPart editor = page.getActiveEditor();
      if (editor != null)
      {
        IEditorInput input = editor.getEditorInput();
        if (input instanceof IFileEditorInput)
        {
          resource = ((IFileEditorInput)input).getFile();
        }
      }
    }

    if (resource == null)
    {
      return null;
    }

    return resource.getProject();
  }
}

Back to the top