Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 45d49092c31c1ce475e5c891cfbf2f44c2eef3f5 (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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*******************************************************************************
 * Copyright (c) 2008-2010 Sonatype, 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:
 *      Sonatype, Inc. - initial API and implementation
 *******************************************************************************/

package org.eclipse.m2e.core.ui.internal.lifecycle;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtension;
import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.IExtensionRegistry;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Platform;
import org.eclipse.m2e.core.MavenPlugin;
import org.eclipse.m2e.core.core.IMavenConstants;
import org.eclipse.m2e.core.core.MavenLogger;
import org.eclipse.m2e.core.project.IMavenProjectFacade;
import org.eclipse.m2e.core.project.IProjectConfigurationManager;
import org.eclipse.m2e.core.project.MavenProjectManager;
import org.eclipse.m2e.core.project.ResolverConfiguration;
import org.eclipse.m2e.core.project.configurator.ILifecycleMapping;
import org.eclipse.swt.widgets.Shell;


/**
 * LifecycleMappingPropertyPageFactory
 * 
 * @author dyocum
 */
public class LifecycleMappingPropertyPageFactory {

  public static final String EXTENSION_LIFECYCLE_MAPPING_PROPERTY_PAGE = IMavenConstants.PLUGIN_ID + ".lifecycleMappingPropertyPage"; //$NON-NLS-1$

  private static final String ATTR_LIFECYCLE_MAPPING_ID = "lifecycleMappingId"; //$NON-NLS-1$

  private static final String ATTR_LIFECYCLE_PROP_NAME = "name"; //$NON-NLS-1$

  private static final String ATTR_LIFECYCLE_PROP_ID = "id"; //$NON-NLS-1$

  private static final String ELEMENT_LIFECYCLE_MAPPING_PROPERTY_PAGE = "lifecycleMappingPropertyPage"; //$NON-NLS-1$

  private static LifecycleMappingPropertyPageFactory factory;

  private Map<String, ILifecyclePropertyPage> pageMap;

  public static LifecycleMappingPropertyPageFactory getFactory() {
    if(factory == null) {
      factory = new LifecycleMappingPropertyPageFactory();
      factory.buildFactory();
    }
    return factory;
  }

  /**
   * Get a particular lifecycle property page, set in the project to use for the lifecycle mapping, set the Shell for
   * displaying dialogs.
   * 
   * @param id
   * @param project
   * @param shell
   * @return
   */
  public ILifecyclePropertyPage getPageForId(String id, IProject project, Shell shell) {
    if(id == null){
      //for the no-op (empty) lifecycle mapping, use that page
      id = "NULL"; //$NON-NLS-1$
    }
    ILifecyclePropertyPage page = getFactory().pageMap.get(id);
    if(page == null){
      return null;
    }
    page.setProject(project);
    page.setShell(shell);
    return page;
  }
  
  public ILifecyclePropertyPage getPage(String id){
    return getFactory().pageMap.get(id);
  }

  public void buildFactory() {
    pageMap = new HashMap<String, ILifecyclePropertyPage>();
    IExtensionRegistry registry = Platform.getExtensionRegistry();
    IExtensionPoint configuratorsExtensionPoint = registry.getExtensionPoint(EXTENSION_LIFECYCLE_MAPPING_PROPERTY_PAGE);
    if(configuratorsExtensionPoint != null) {
      IExtension[] configuratorExtensions = configuratorsExtensionPoint.getExtensions();
      for(IExtension extension : configuratorExtensions) {
        IConfigurationElement[] elements = extension.getConfigurationElements();
        for(IConfigurationElement element : elements) {
          if(element.getName().equals(ELEMENT_LIFECYCLE_MAPPING_PROPERTY_PAGE)) {
            try {
              Object o = element.createExecutableExtension("class"); //$NON-NLS-1$
              ILifecyclePropertyPage propPage = (ILifecyclePropertyPage) o;
              String id = element.getAttribute(ATTR_LIFECYCLE_MAPPING_ID);

              propPage.setLifecycleMappingId(id);
              String name = element.getAttribute(ATTR_LIFECYCLE_PROP_NAME);
              propPage.setName(name);

              String pageId = element.getAttribute(ATTR_LIFECYCLE_PROP_ID);
              if(pageId != null) {
                propPage.setPageId(pageId);
              }
              pageMap.put(id, propPage);
            } catch(CoreException ex) {
              MavenLogger.log(ex);
            }
          }
        }
      }
    }
  }

  public static IMavenProjectFacade getProjectFacade(IProject project) {
    MavenProjectManager projectManager = MavenPlugin.getDefault().getMavenProjectManager();
    return projectManager.create(project, new NullProgressMonitor());
  }

  public static ResolverConfiguration getResolverConfiguration(IProject project) {
    MavenProjectManager projectManager = MavenPlugin.getDefault().getMavenProjectManager();
    return projectManager.getResolverConfiguration(project);
  }

  public static ILifecycleMapping getLifecycleMapping(IProject project) throws CoreException {
    IMavenProjectFacade facade = getProjectFacade(project);
    ILifecycleMapping lifecycleMapping = null;
    IProjectConfigurationManager configurationManager = MavenPlugin.getDefault().getProjectConfigurationManager();
    lifecycleMapping = configurationManager.getLifecycleMapping(facade);
    return lifecycleMapping;
  }
}

Back to the top