Skip to content

Commit

Permalink
[JENKINS-31162] A new description field has been added for Items
Browse files Browse the repository at this point in the history
  • Loading branch information
recena committed Mar 14, 2016
1 parent ac72888 commit 2356791
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
2 changes: 2 additions & 0 deletions core/src/main/java/hudson/model/ItemGroupMixIn.java
Expand Up @@ -348,10 +348,12 @@ public static Categories getCategories(Authentication a, ItemGroup c) {
ItemCategory ic = ItemCategoryConfigurator.getCategory(descriptor);
Map<String, Serializable> metadata = new HashMap<String, Serializable>();

// Information about Item.
metadata.put("class", descriptor.clazz.getName());
metadata.put("iconClassName", "item-icon-" + descriptor.clazz.getName().substring(descriptor.clazz.getName().lastIndexOf(".") + 1).toLowerCase());
metadata.put("weight", ItemCategoryConfigurator.getWeight(descriptor));
metadata.put("name", descriptor.getDisplayName());
metadata.put("description", ItemCategoryConfigurator.getDescription(descriptor));

Category category = categories.getItem(ic.getId());
if (category != null) {
Expand Down
Expand Up @@ -56,12 +56,12 @@ public static ItemCategory getCategory(@Nonnull TopLevelItemDescriptor descripto
protected abstract Integer getWeightFor(@Nonnull TopLevelItemDescriptor descriptor);

/**
* Finds the category specified by the first mapper.
* If none can be found {@link ItemCategory.Default} is returned.
* Finds the weight specified by the first configurator.
* If none can be found {@link Integer#MIN_VALUE} is returned. {@see DefaultConfigurator#getWeightFor}.
*
* @param descriptor the item to categorize.
*
* @return the category
* @return the weight
*/
@Nonnull
public static Integer getWeight(@Nonnull TopLevelItemDescriptor descriptor) {
Expand All @@ -71,15 +71,44 @@ public static Integer getWeight(@Nonnull TopLevelItemDescriptor descriptor) {
return weight;
}
}
throw new IllegalStateException("At least, must exist the category: " + ItemCategory.Default.class);
throw new IllegalStateException("At least, a default value must exist");
}

/**
* Provides the description for the requested item or null if this configurator doesn't have one.
*
* @param descriptor the item to categorize
*
* @return A {@link ItemCategory} or null
*/
@CheckForNull
protected abstract String getDescriptionFor(@Nonnull TopLevelItemDescriptor descriptor);

/**
* Finds the weight specified by the first configurator.
* If none can be found a empty string is returned. {@see DefaultConfigurator#getDescriptionFor}.
*
* @param descriptor the item to categorize.
*
* @return A {@link ItemCategory}
*/
@Nonnull
public static String getDescription(@Nonnull TopLevelItemDescriptor descriptor) {
for (ItemCategoryConfigurator m : all()) {
String description = m.getDescriptionFor(descriptor);
if (description != null) {
return description;
}
}
throw new IllegalStateException("At least, a default value must exist");
}

public static Collection<ItemCategoryConfigurator> all() {
return ExtensionList.lookup(ItemCategoryConfigurator.class);
}

/**
* Mapper implementation with the lowest ordinal that simply returns {@link ItemCategory.Default}.
* Default configurator with the lowest ordinal that simply returns {@link ItemCategory.Default}.
*/
@Extension(ordinal = Integer.MIN_VALUE)
public static final class DefaultConfigurator extends ItemCategoryConfigurator {
Expand All @@ -96,5 +125,11 @@ public Integer getWeightFor(@Nonnull TopLevelItemDescriptor descriptor) {
return Integer.MIN_VALUE;
}

@Nonnull
@Override
public String getDescriptionFor(@Nonnull TopLevelItemDescriptor descriptor) {
return "";
}

}
}

0 comments on commit 2356791

Please sign in to comment.