Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 50742999a7a67d44646831333da9234a84dd1e15 (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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/*******************************************************************************
 * 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.editor.composites;

import java.util.Collection;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.model.DependencyManagement;
import org.apache.maven.model.InputLocation;
import org.apache.maven.project.MavenProject;
import org.eclipse.jface.viewers.IColorProvider;
import org.eclipse.jface.viewers.IDecoration;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.StyledString;
import org.eclipse.m2e.core.MavenImages;
import org.eclipse.m2e.core.MavenPlugin;
import org.eclipse.m2e.core.project.IMavenProjectFacade;
import org.eclipse.m2e.core.project.MavenProjectManager;
import org.eclipse.m2e.editor.MavenEditorImages;
import org.eclipse.m2e.editor.internal.Messages;
import org.eclipse.m2e.editor.pom.MavenPomEditor;
import org.eclipse.m2e.editor.pom.ValueProvider;
import org.eclipse.m2e.model.edit.pom.Dependency;
import org.eclipse.m2e.model.edit.pom.Exclusion;
import org.eclipse.m2e.model.edit.pom.Extension;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;
import org.eclipse.jface.viewers.DelegatingStyledCellLabelProvider;


/**
 * Label provider for Dependency, Exclusion and Extension elements
 * 
 * @author Eugene Kuleshov
 */
public class DependencyLabelProvider extends LabelProvider implements IColorProvider, DelegatingStyledCellLabelProvider.IStyledLabelProvider {

  private MavenPomEditor pomEditor;

  private boolean showGroupId = false;

  private final boolean showManagedOverlay;

  private ValueProvider<org.eclipse.m2e.model.edit.pom.DependencyManagement> managementProvider;
  
  public DependencyLabelProvider() {
    this(false);
  }

  public DependencyLabelProvider(boolean showManagedOverlay) {
    super();
    this.showManagedOverlay = showManagedOverlay;
  }

  public void setPomEditor(MavenPomEditor pomEditor, ValueProvider<org.eclipse.m2e.model.edit.pom.DependencyManagement> dependencyManagementProvider) {
    assert pomEditor != null;
    assert managementProvider != null;
    this.pomEditor = pomEditor;
    this.managementProvider = dependencyManagementProvider;
  }
  
  public void setShowGroupId(boolean showGroupId) {
    this.showGroupId = showGroupId;
  }

  // IColorProvider
  
  public Color getForeground(Object element) {
    if (element instanceof org.apache.maven.model.Dependency) {
      //mkleint: let's just assume all maven Dependency instances are inherited
      return Display.getDefault().getSystemColor(SWT.COLOR_DARK_GRAY);
    }
    return null;
  }
  
  public Color getBackground(Object element) {
    return null;
  }
  
  private String findManagedVersion(Dependency dep) {
    if (pomEditor != null) {
      MavenProject mp = pomEditor.getMavenProject();
      String version = null;
      if(mp != null) {
        String id = mp.getGroupId() + ":" + mp.getArtifactId() + ":" + mp.getVersion();
        DependencyManagement dm = mp.getDependencyManagement();
        if(dm != null) {
          for(org.apache.maven.model.Dependency d : dm.getDependencies()) {
            if(d.getGroupId().equals(dep.getGroupId()) && d.getArtifactId().equals(dep.getArtifactId())) {
              //based on location, try finding a match in the live Model
              InputLocation location = d.getLocation("artifactId");
              if (location != null) {
                if (id.equals(location.getSource().getModelId())) {
                  version = d.getVersion();
                  break;
                }
              }
              return d.getVersion();
            }
          }
        }
      }
      org.eclipse.m2e.model.edit.pom.DependencyManagement dm = managementProvider.getValue();
      for (Dependency modelDep : dm.getDependencies()) {
        String modelGroupId = modelDep.getGroupId();
        String modelArtifactId = modelDep.getArtifactId();
        String modelVersion = modelDep.getVersion();
        if (modelGroupId != null && modelGroupId.equals(dep.getGroupId()) && 
            modelArtifactId != null && modelArtifactId.equals(dep.getArtifactId())) {
          if (version != null && (modelVersion == null || modelVersion.contains("${"))) {
            //prefer the resolved version to the model one if the model version as expressions..
            return version;
          }
          return modelVersion;
        }
      }
    }
    return null;
  }

  public StyledString getStyledText(Object element) {
    if(element instanceof Dependency) {
      StyledString ss = new StyledString(getText(element));
      Dependency dep = (Dependency) element;
      String version = findManagedVersion(dep);
      if (version != null) {
        ss.append(NLS.bind(Messages.DependencyLabelProvider_0, version), StyledString.DECORATIONS_STYLER);
      }
      return ss;
    }
    return new StyledString(getText(element));
  }
 
  // LabelProvider
  
  @Override
  public String getText(Object element) {
    if(element instanceof Dependency) {
      Dependency dependency = (Dependency) element;
      return getText(dependency.getGroupId(), dependency.getArtifactId(), dependency.getVersion(), //
          dependency.getClassifier(), dependency.getType(), dependency.getScope());
    } else if (element instanceof org.apache.maven.model.Dependency) {
      org.apache.maven.model.Dependency dependency = (org.apache.maven.model.Dependency) element;
      return getText(dependency.getGroupId(), dependency.getArtifactId(), dependency.getVersion(),
          dependency.getClassifier(), dependency.getType(), dependency.getScope());
    } else if(element instanceof Exclusion) {
      Exclusion exclusion = (Exclusion) element;
      return getText(exclusion.getGroupId(), exclusion.getArtifactId(), null, null, null, null);
    } else if(element instanceof Extension) {
      Extension extension = (Extension) element;
      return getText(extension.getGroupId(), extension.getArtifactId(), extension.getVersion(), null, null, null);
    }
    return super.getText(element);
  }

  @Override
  public Image getImage(Object element) {
    if(element instanceof Dependency) {
      Dependency dependency = (Dependency) element;
      boolean isManaged = showManagedOverlay && findManagedVersion(dependency) != null;
      return getImage(dependency.getGroupId(), dependency.getArtifactId(), dependency.getVersion(), isManaged);
    } else if (element instanceof org.apache.maven.model.Dependency) {
      //mkleint: all MavenDependency instances are inherited
      return MavenEditorImages.IMG_INHERITED;
    }else if(element instanceof Exclusion) {
      Exclusion exclusion = (Exclusion) element;
      return getImage(exclusion.getGroupId(), exclusion.getArtifactId(), null, false);
    } else if(element instanceof Extension) {
      Extension extension = (Extension) element;
      return getImage(extension.getGroupId(), extension.getArtifactId(), extension.getVersion(), false);
    }

    return null;
  }

  private Image getImage(String groupId, String artifactId, String version, boolean isManaged) {
    // XXX need to resolve actual dependencies (i.e. inheritance, dependency management or properties)
    // XXX need to handle version ranges
    
    if((version == null || version.indexOf("${") > -1) && pomEditor != null) { //$NON-NLS-1$
        MavenProject mavenProject = pomEditor.getMavenProject();
        if(mavenProject != null) {
          Artifact artifact = mavenProject.getArtifactMap().get(groupId + ":" + artifactId); //$NON-NLS-1$
          if(artifact!=null) {
            version = artifact.getVersion();
          }
          if(version==null || version.indexOf("${") > -1) { //$NON-NLS-1$
            Collection<Artifact> artifacts = mavenProject.getManagedVersionMap().values();
            for(Artifact a : artifacts) {
              if(a.getGroupId().equals(groupId) && a.getArtifactId().equals(artifactId)) {
                version = a.getVersion();
                break;
              }
            }
          }
        }
    }
    
    if(groupId != null && artifactId != null && version != null) {
      MavenProjectManager projectManager = MavenPlugin.getDefault().getMavenProjectManager();
      IMavenProjectFacade projectFacade = projectManager.getMavenProject(groupId, artifactId, version);
      if(projectFacade != null) {
        return isManaged ? MavenImages.getOverlayImage(MavenImages.PATH_PROJECT, MavenImages.PATH_LOCK, IDecoration.BOTTOM_LEFT) : MavenEditorImages.IMG_PROJECT;
      }
    } 
    return isManaged ? MavenImages.getOverlayImage(MavenImages.PATH_JAR, MavenImages.PATH_LOCK, IDecoration.BOTTOM_LEFT) : MavenEditorImages.IMG_JAR;
  }

  private String getText(String groupId, String artifactId, String version, String classifier, String type, String scope) {
    StringBuilder sb = new StringBuilder();

    if(showGroupId) {
      sb.append(isEmpty(groupId) ? "?" : groupId).append(" : "); //$NON-NLS-1$ //$NON-NLS-2$
    }
    sb.append(isEmpty(artifactId) ? "?" : artifactId); //$NON-NLS-1$

    if(!isEmpty(version)) {
      sb.append(" : ").append(version); //$NON-NLS-1$
    }

    if(!isEmpty(classifier)) {
      sb.append(" : ").append(classifier); //$NON-NLS-1$
    }

    if(!isEmpty(type)) {
      sb.append(" : ").append(type); //$NON-NLS-1$
    }

    if(!isEmpty(scope)) {
      sb.append(" [").append(scope).append(']'); //$NON-NLS-1$
    }

    return sb.toString();
  }

  private boolean isEmpty(String s) {
    return s == null || s.trim().length() == 0;
  }



}

Back to the top