Skip to main content
summaryrefslogtreecommitdiffstats
blob: 30255968b35a253875853362588e79b81482b2b0 (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
/*
 * Copyright (c) 2004 - 2012 Eike Stepper (Berlin, 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:
 *     Martin Fluegge - initial API and implementation
 */
package org.eclipse.emf.cdo.dawn.ui.helper;

import org.eclipse.emf.cdo.dawn.spi.IDawnUIElement;

import org.eclipse.emf.ecore.resource.Resource;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.IEditorDescriptor;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.internal.registry.EditorDescriptor;

/**
 * @author Martin Fluegge
 */
@SuppressWarnings("restriction")
public class EditorDescriptionHelper
{
  public static String getEditorIdForDawnEditor(String resourceName)
  {
    IEditorDescriptor editorDescriptor = getEditorDescriptorForDawnEditor(resourceName);
    if (editorDescriptor != null)
    {
      return editorDescriptor.getId();
    }
    return null;
  }

  public static IEditorDescriptor getEditorDescriptorForDawnEditor(String resourceName)
  {
    IEditorDescriptor[] editors = PlatformUI.getWorkbench().getEditorRegistry().getEditors(resourceName);

    for (IEditorDescriptor editorDescriptor : editors)
    {
      EditorDescriptor des = (EditorDescriptor)editorDescriptor;

      try
      {
        IEditorPart editor = des.createEditor();
        if (editor instanceof IDawnUIElement)
        {
          return editorDescriptor;
        }
      }
      catch (CoreException ex)
      {
        throw new RuntimeException(ex);
      }
    }
    return null;
  }

  public static IEditorDescriptor getEditorDescriptorFromFirstEditor(String resourceName)
  {
    IEditorDescriptor[] editors = PlatformUI.getWorkbench().getEditorRegistry().getEditors(resourceName);
    if (editors.length > 0)
    {
      return editors[0];
    }

    return null;
  }

  public static IEditorDescriptor getEditorDescriptorForDawnEditor(Resource resource)
  {
    return getEditorDescriptorForDawnEditor(resource.getURI().lastSegment());
  }

  public static Image getImageForEditor(String resourceName)
  {
    IEditorDescriptor editorDescriptor = getEditorDescriptorForDawnEditor(resourceName);

    if (editorDescriptor == null)
    {
      editorDescriptor = getEditorDescriptorFromFirstEditor(resourceName);
    }

    if (editorDescriptor != null)
    {
      return editorDescriptor.getImageDescriptor().createImage();
    }

    return null;
  }
}

Back to the top