Skip to content

Commit

Permalink
[JENKINS-31162] @jglick's comments were addressed (partially)
Browse files Browse the repository at this point in the history
  • Loading branch information
recena committed Mar 21, 2016
1 parent 3c17558 commit 74b58dc
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 136 deletions.
2 changes: 1 addition & 1 deletion core/src/main/java/hudson/model/FreeStyleProject.java
Expand Up @@ -81,7 +81,7 @@ public String getDescription() {

@Override
public String getCategoryId() {
return "itemcategory-standaloneprojects";
return "standaloneprojects";
}

@Override
Expand Down
7 changes: 3 additions & 4 deletions core/src/main/java/hudson/model/ItemGroupMixIn.java
Expand Up @@ -32,7 +32,6 @@
import jenkins.model.item_category.Categories;
import jenkins.model.item_category.Category;
import jenkins.model.item_category.ItemCategory;
import jenkins.model.item_category.ItemCategoryConfigurator;
import jenkins.model.Jenkins;
import jenkins.util.xml.XMLUtils;
import org.acegisecurity.Authentication;
Expand Down Expand Up @@ -344,9 +343,9 @@ public synchronized TopLevelItem createProject( TopLevelItemDescriptor type, Str
*/
public static Categories getCategories(Authentication a, ItemGroup c) {
Categories categories = new Categories();
int weight = ItemCategory.MIN_WEIGHT;
int weight = 0;
for (TopLevelItemDescriptor descriptor : DescriptorVisibilityFilter.apply(c, Items.all(a, c))) {
ItemCategory ic = ItemCategoryConfigurator.getCategory(descriptor);
ItemCategory ic = ItemCategory.getCategory(descriptor);
Map<String, Serializable> metadata = new HashMap<String, Serializable>();

// Information about Item.
Expand All @@ -362,7 +361,7 @@ public static Categories getCategories(Authentication a, ItemGroup c) {
} else {
List<Map<String, Serializable>> temp = new ArrayList<Map<String, Serializable>>();
temp.add(metadata);
category = new Category(ic.getId(), ic.getDisplayName(), ic.getDescription(), ic.getWeight(), ic.getMinToShow(), temp);
category = new Category(ic.getId(), ic.getDisplayName(), ic.getDescription(), ic.getWeight() , ic.getMinToShow(), temp);
categories.getItems().add(category);
}
}
Expand Down
44 changes: 31 additions & 13 deletions core/src/main/java/hudson/model/TopLevelItemDescriptor.java
Expand Up @@ -29,6 +29,7 @@
import org.acegisecurity.AccessDeniedException;
import org.apache.commons.jelly.Script;
import org.apache.commons.jelly.XMLOutput;
import org.apache.commons.lang.StringUtils;
import org.kohsuke.stapler.MetaClass;
import org.kohsuke.stapler.Stapler;
import org.kohsuke.stapler.StaplerRequest;
Expand Down Expand Up @@ -130,25 +131,32 @@ public String getDisplayName() {
* A description of this kind of item type. This description can contain HTML code but it is recommend to use text plain
* in order to avoid how it should be represented.
*
* @return A string, an empty string by default.
* This method should be called in a thread where Stapler is associated, but it will return an empty string.
*
* @return A string, by default the value from newInstanceDetail view is taken.
*
* @since TODO
*/
@Nonnull
public String getDescription() {
try {
WebApp webapp = WebApp.getCurrent();
MetaClass meta = webapp.getMetaClass(this);
Script s = meta.loadTearOff(JellyClassTearOff.class).findScript("newInstanceDetail");
if (s == null) {
Stapler stapler = Stapler.getCurrent();
if (stapler != null) {
try {
WebApp webapp = WebApp.getCurrent();
MetaClass meta = webapp.getMetaClass(this);
Script s = meta.loadTearOff(JellyClassTearOff.class).findScript("newInstanceDetail");
if (s == null) {
return "";
}
DefaultScriptInvoker dsi = new DefaultScriptInvoker();
StringWriter sw = new StringWriter();
XMLOutput xml = dsi.createXMLOutput(sw, true);
dsi.invokeScript(Stapler.getCurrentRequest(), Stapler.getCurrentResponse(), s, this, xml);
return sw.toString();
} catch (Exception e) {
return "";
}
DefaultScriptInvoker dsi = new DefaultScriptInvoker();
StringWriter sw = new StringWriter();
XMLOutput xml = dsi.createXMLOutput(sw, true);
dsi.invokeScript(Stapler.getCurrentRequest(), Stapler.getCurrentResponse(), s, this, xml);
return sw.toString();
} catch (Exception e) {
} else {
return "";
}
}
Expand All @@ -166,6 +174,10 @@ public String getCategoryId() {
}

/**
* Represents a file path pattern to get the Item icon in different sizes.
*
* @return A string or null if it is not defined.
*
* @since TODO
*/
@CheckForNull
Expand All @@ -174,11 +186,17 @@ public String getIconFilePathPattern() {
}

/**
* An icon file path associated to a specific size.
*
* @param size A string with values that represent the common sizes: 16x16, 24x24, 32x32 or 48x48
*
* @return A string or null if it is not defined.
*
* @since TODO
*/
@CheckForNull
public String getIconFilePath(String size) {
if (getIconFilePathPattern().isEmpty()) {
if (!StringUtils.isBlank(getIconFilePathPattern())) {
return getIconFilePathPattern().replace(":size", size);
}
return null;
Expand Down
91 changes: 43 additions & 48 deletions core/src/main/java/jenkins/model/item_category/ItemCategory.java
@@ -1,23 +1,32 @@
package jenkins.model.item_category;

import hudson.Extension;
import hudson.ExtensionList;
import hudson.ExtensionPoint;
import hudson.model.ModelObject;
import hudson.model.TopLevelItemDescriptor;
import jenkins.model.Messages;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import java.util.Collection;

/**
* A category for {@link hudson.model.Item}s.
*
* @since TODO
*/
public abstract class ItemCategory implements ModelObject, ExtensionPoint {

public static int MIN_WEIGHT = 0;

public static int MIN_TOSHOW = 1;

/**
* Identifier, e.g. "category-id-standaloneprojects", etc.
* Helpful to set the order.
*/
private int weight = 1;

/**
* Identifier, e.g. "standaloneprojects", etc.
*
* @return the identifier
*/
Expand All @@ -30,77 +39,62 @@ public abstract class ItemCategory implements ModelObject, ExtensionPoint {
*/
public abstract String getDescription();

/**
* Helpful to set the order.
*
* @return the weight
*/
public abstract int getWeight();

/**
* Minimum number required to show the category.
*
* @return the minimum items required
*/
public abstract int getMinToShow();

protected void setWeight(int weight) {
this.weight = weight;
}

/**
* The default {@link ItemCategory}, if an item doesn't belong anywhere else, this is where it goes by default.
* @return A integer with the weight.
*/
@Extension
public static final class UncategorizedCategory extends ItemCategory {

@Override
public String getId() {
return "itemcategory-uncategorized";
}

@Override
public String getDescription() {
return Messages.ItemCategory_Uncategorized_Description();
}

@Override
public String getDisplayName() {
return Messages.ItemCategory_Uncategorized_DisplayName();
}

@Override
public int getWeight() {
return ItemCategory.MIN_WEIGHT;
}
public int getWeight() {
return weight;
}

@Override
public int getMinToShow() {
return ItemCategory.MIN_TOSHOW;
/**
* A {@link ItemCategory} associated to this {@link TopLevelItemDescriptor}.
*
* @return A {@link ItemCategory}, if not found, {@link ItemCategory.UncategorizedCategory} is returned
*/
@Nonnull
public static ItemCategory getCategory(TopLevelItemDescriptor descriptor) {
int weight = 1;
ExtensionList<ItemCategory> categories = ExtensionList.lookup(ItemCategory.class);
for (ItemCategory category : categories) {
if (category.getId().equals(descriptor.getCategoryId())) {
category.setWeight(categories.size() - weight);
return category;
}
weight++;
}

return new UncategorizedCategory();
}

/**
* A generic {@link ItemCategory}
* The default {@link ItemCategory}, if an item doesn't belong anywhere else, this is where it goes by default.
*/
@Extension
public static final class StandaloneProjectCategory extends ItemCategory {
@Extension(ordinal = Integer.MIN_VALUE)
public static final class UncategorizedCategory extends ItemCategory {

@Override
public String getId() {
return "itemcategory-standaloneprojects";
return "uncategorized";
}

@Override
public String getDescription() {
return Messages.ItemCategory_StandaloneProjects_Description();
return Messages.ItemCategory_Uncategorized_Description();
}

@Override
public String getDisplayName() {
return Messages.ItemCategory_StandaloneProjects_DisplayName();
}

@Override
public int getWeight() {
return ItemCategory.MIN_WEIGHT;
return Messages.ItemCategory_Uncategorized_DisplayName();
}

@Override
Expand All @@ -109,4 +103,5 @@ public int getMinToShow() {
}

}

}

This file was deleted.

@@ -0,0 +1,31 @@
package jenkins.model.item_category;

import hudson.Extension;
import jenkins.model.Messages;

/**
* A generic {@link ItemCategory}
*/
@Extension(ordinal = Integer.MIN_VALUE + 1)
public class StandaloneProjectsCategory extends ItemCategory {

@Override
public String getId() {
return "standaloneprojects";
}

@Override
public String getDescription() {
return Messages.ItemCategory_StandaloneProjects_Description();
}

@Override
public String getDisplayName() {
return Messages.ItemCategory_StandaloneProjects_DisplayName();
}

@Override
public int getMinToShow() {
return ItemCategory.MIN_TOSHOW;
}
}

0 comments on commit 74b58dc

Please sign in to comment.