Skip to content

Commit

Permalink
[FIXED JENKINS-38960] Deprecate getIconFilePathPattern and switch to …
Browse files Browse the repository at this point in the history
…IconSpec
  • Loading branch information
stephenc committed Oct 13, 2016
1 parent b62ad15 commit bd834fe
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
13 changes: 13 additions & 0 deletions core/src/main/java/hudson/model/FreeStyleProject.java
Expand Up @@ -25,6 +25,8 @@

import hudson.Extension;
import jenkins.model.Jenkins;
import org.jenkins.ui.icon.Icon;
import org.jenkins.ui.icon.IconSet;
import org.jenkinsci.Symbol;
import jenkins.model.item_category.StandaloneProjectsCategory;
import org.kohsuke.accmod.Restricted;
Expand Down Expand Up @@ -97,5 +99,16 @@ public String getIconFilePathPattern() {
return (Jenkins.RESOURCE_PATH + "/images/:size/freestyleproject.png").replaceFirst("^/", "");
}

@Override
public String getIconClassName() {
return "icon-freestyle-project";
}

static {
IconSet.icons.addIcon(new Icon("icon-freestyle-project icon-sm", "16x16/freestyleproject.png", Icon.ICON_SMALL_STYLE));
IconSet.icons.addIcon(new Icon("icon-freestyle-project icon-md", "24x24/freestyleproject.png", Icon.ICON_MEDIUM_STYLE));
IconSet.icons.addIcon(new Icon("icon-freestyle-project icon-lg", "32x32/freestyleproject.png", Icon.ICON_LARGE_STYLE));
IconSet.icons.addIcon(new Icon("icon-freestyle-project icon-xlg", "48x48/freestyleproject.png", Icon.ICON_XLARGE_STYLE));
}
}
}
37 changes: 36 additions & 1 deletion core/src/main/java/hudson/model/TopLevelItemDescriptor.java
Expand Up @@ -30,6 +30,9 @@
import org.apache.commons.jelly.Script;
import org.apache.commons.jelly.XMLOutput;
import org.apache.commons.lang.StringUtils;
import org.jenkins.ui.icon.Icon;
import org.jenkins.ui.icon.IconSet;
import org.jenkins.ui.icon.IconSpec;
import org.kohsuke.stapler.MetaClass;
import org.kohsuke.stapler.Stapler;
import org.kohsuke.stapler.StaplerRequest;
Expand All @@ -48,7 +51,7 @@
*
* @author Kohsuke Kawaguchi
*/
public abstract class TopLevelItemDescriptor extends Descriptor<TopLevelItem> {
public abstract class TopLevelItemDescriptor extends Descriptor<TopLevelItem> implements IconSpec {

private static final Logger LOGGER = Logger.getLogger(TopLevelItemDescriptor.class.getName());

Expand Down Expand Up @@ -189,6 +192,7 @@ public String getCategoryId() {
* @return A string or null if it is not defined.
*
* @since 2.0
* @deprecated prefer {@link #getIconClassName()}
*/
@CheckForNull
public String getIconFilePathPattern() {
Expand All @@ -203,6 +207,7 @@ public String getIconFilePathPattern() {
* @return A string or null if it is not defined.
*
* @since 2.0
* @deprecated prefer {@link #getIconClassName()}
*/
@CheckForNull
public String getIconFilePath(String size) {
Expand All @@ -212,6 +217,36 @@ public String getIconFilePath(String size) {
return null;
}

/**
* Get the Item's Icon class specification e.g. 'icon-notepad'.
* <p/>
* Note: do <strong>NOT</strong> include icon size specifications (such as 'icon-sm').
*
* @return The Icon class specification e.g. 'icon-notepad'.
*/
@Override
public String getIconClassName() {
// Oh the fun of somebody adding a legacy way of referencing images into 2.0 code
String pattern = getIconFilePathPattern();
if (pattern != null) {
// here we go with the dance of the IconSet's
String path = pattern.replace(":size", "24x24"); // we'll strip the icon-md to get the class name
if (path.indexOf('/') == -1) {
// this one is easy... too easy... also will never happen
return IconSet.toNormalizedIconNameClass(path);
}
if (Jenkins.RESOURCE_PATH.length() > 0 && path.startsWith(Jenkins.RESOURCE_PATH)) {
// will to live falling
path = path.substring(Jenkins.RESOURCE_PATH.length());
}
Icon icon = IconSet.icons.getIconByUrl(path);
if (icon != null) {
return icon.getClassSpec().replaceAll("\\s*icon-md\\s*", " ").replaceAll("\\s+", " ");
}
}
return null;
}

/**
* @deprecated since 2007-01-19.
* This is not a valid operation for {@link Item}s.
Expand Down
1 change: 1 addition & 0 deletions core/src/main/java/hudson/model/View.java
Expand Up @@ -1058,6 +1058,7 @@ public Categories doItemCategories(StaplerRequest req, StaplerResponse rsp) thro
metadata.put("displayName", descriptor.getDisplayName());
metadata.put("description", descriptor.getDescription());
metadata.put("iconFilePathPattern", descriptor.getIconFilePathPattern());
metadata.put("iconClassName", descriptor.getIconClassName());

Category category = categories.getItem(ic.getId());
if (category != null) {
Expand Down

0 comments on commit bd834fe

Please sign in to comment.