Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[JENKINS-31162] New fields were added to the JSON file
  • Loading branch information
Manuel Recena committed Mar 11, 2016
1 parent f3b92f8 commit e140be8
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 11 deletions.
8 changes: 5 additions & 3 deletions core/src/main/java/hudson/model/ItemGroupMixIn.java
Expand Up @@ -349,19 +349,21 @@ public static Categories getCategories(Authentication a, ItemGroup c) {
boolean found = false;
while (i < categories.getItems().size() && !found) {
if (categories.getItems().get(i).getId() == ic.getId()) {
Map<String, String> metadata = new HashMap<String, String>();
Map<String, Object> metadata = new HashMap<String, Object>();
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));
categories.getItems().get(i).getItems().add(metadata);
found = true;
}
i++;
}
if (!found) {
Map<String, String> metadata = new HashMap<String, String>();
Map<String, Object> metadata = new HashMap<String, Object>();
metadata.put("class", descriptor.clazz.getName());
metadata.put("iconClassName", "item-icon-" + descriptor.clazz.getName().substring(descriptor.clazz.getName().lastIndexOf(".") + 1).toLowerCase());
List<Map<String, String>> temp = new ArrayList<Map<String, String>>();
metadata.put("weight", ItemCategoryConfigurator.getWeight(descriptor));
List<Map<String, Object>> temp = new ArrayList<Map<String, Object>>();
temp.add(metadata);
categories.getItems().add(new Category(ic.getId(), ic.getDisplayName(), ic.getDescription(),
ic.getIconClassName(), ic.getWeight(), temp));
Expand Down
6 changes: 3 additions & 3 deletions core/src/main/java/jenkins/model/ItemCategory/Category.java
Expand Up @@ -24,9 +24,9 @@ public class Category implements Serializable {

private int weight;

private List<Map<String, String>> items;
private List<Map<String, Object>> items;

public Category(String id, String name, String description, String iconClassName, int weight, List<Map<String, String>> items) {
public Category(String id, String name, String description, String iconClassName, int weight, List<Map<String, Object>> items) {
this.id= id;
this.name = name;
this.description = description;
Expand Down Expand Up @@ -61,7 +61,7 @@ public int getWeight() {
}

@Exported
public List<Map<String, String>> getItems() {
public List<Map<String, Object>> getItems() {
return items;
}

Expand Down
Expand Up @@ -15,23 +15,21 @@
*/
public abstract class ItemCategoryConfigurator implements ExtensionPoint {

public static final Logger LOGGER = Logger.getLogger(ItemCategoryConfigurator.class.getName());

/**
* Provides the category for the requested item or null if this mapper doesn't have one.
*
* @param descriptor the item to categorise
* @param descriptor the item to categorize
*
* @return the category or null
*/
@CheckForNull
protected abstract ItemCategory getCategoryFor(@Nonnull TopLevelItemDescriptor descriptor);

/**
* Finds the category specified by the first mapper.
* Finds the category specified by the first configurator.
* If none can be found {@link ItemCategory.Default} is returned.
*
* @param descriptor the item to categorise.
* @param descriptor the item to categorize.
*
* @return the category
*/
Expand All @@ -46,6 +44,35 @@ public static ItemCategory getCategory(@Nonnull TopLevelItemDescriptor descripto
throw new IllegalStateException();
}

/**
* Provides the weight for the requested item. Helpful to order a list.
*
* @param descriptor the item to categorize
*
* @return the weight or null
*/
@CheckForNull
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.
*
* @param descriptor the item to categorize.
*
* @return the category
*/
@Nonnull
public static Integer getWeight(@Nonnull TopLevelItemDescriptor descriptor) {
for (ItemCategoryConfigurator m : all()) {
Integer weight = m.getWeightFor(descriptor);
if (weight != null) {
return weight;
}
}
throw new IllegalStateException();
}

public static Collection<ItemCategoryConfigurator> all() {
return ExtensionList.lookup(ItemCategoryConfigurator.class);
}
Expand All @@ -56,9 +83,17 @@ public static Collection<ItemCategoryConfigurator> all() {
@Extension(ordinal = Integer.MIN_VALUE)
public static final class DefaultConfigurator extends ItemCategoryConfigurator {

@Nonnull
@Override
public ItemCategory getCategoryFor(@Nonnull TopLevelItemDescriptor descriptor) {
return ExtensionList.lookup(ItemCategory.Default.class).iterator().next();
}

@Nonnull
@Override
public Integer getWeightFor(@Nonnull TopLevelItemDescriptor descriptor) {
return Integer.MIN_VALUE;
}

}
}

0 comments on commit e140be8

Please sign in to comment.