Skip to content

Commit db697ec

Browse files
committed
Init
1 parent c44796d commit db697ec

File tree

25 files changed

+922
-180
lines changed

25 files changed

+922
-180
lines changed

core/src/main/java/hudson/Functions.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@
170170
import jenkins.model.details.DetailFactory;
171171
import jenkins.model.details.DetailGroup;
172172
import jenkins.util.SystemProperties;
173+
import net.sf.json.JSONObject;
173174
import org.apache.commons.jelly.JellyContext;
174175
import org.apache.commons.jelly.JellyTagException;
175176
import org.apache.commons.jelly.Script;
@@ -2599,6 +2600,16 @@ public static String generateItemId() {
25992600
return String.valueOf(Math.floor(Math.random() * 3000));
26002601
}
26012602

2603+
/**
2604+
* Converts the given actions to a JSON object
2605+
*/
2606+
@Restricted(NoExternalUse.class)
2607+
public static String convertActionsToJson(List<Action> actions) {
2608+
ModelObjectWithContextMenu.ContextMenu contextMenu = new ModelObjectWithContextMenu.ContextMenu();
2609+
contextMenu.addAll(actions);
2610+
return JSONObject.fromObject(contextMenu).toString();
2611+
}
2612+
26022613
/**
26032614
* Returns a grouped list of Detail objects for the given Actionable object
26042615
*/

core/src/main/java/hudson/model/Action.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@
2626

2727
import edu.umd.cs.findbugs.annotations.CheckForNull;
2828
import hudson.Functions;
29+
import jenkins.model.menu.Group;
30+
import jenkins.model.menu.Semantic;
31+
import jenkins.model.menu.event.Event;
32+
import jenkins.model.menu.event.LinkEvent;
2933

3034
/**
3135
* Object that contributes additional information, behaviors, and UIs to {@link ModelObject}
@@ -137,6 +141,27 @@ public interface Action extends ModelObject {
137141
* null if this action object doesn't need to be bound to web
138142
* (when you do that, be sure to also return null from {@link #getIconFileName()}.
139143
* @see Functions#getActionUrl(String, Action)
144+
*
145+
* @deprecated
146+
* Override {@link #getEvent()} instead
140147
*/
141-
@CheckForNull String getUrlName();
148+
@CheckForNull default String getUrlName() {
149+
return null;
150+
}
151+
152+
default Group getGroup() {
153+
return Group.IN_MENU;
154+
}
155+
156+
default Event getEvent() {
157+
return LinkEvent.of(getUrlName());
158+
}
159+
160+
default Semantic getSemantic() {
161+
return null;
162+
}
163+
164+
default boolean isVisibleInContextMenu() {
165+
return true;
166+
}
142167
}

core/src/main/java/hudson/model/Actionable.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,20 @@
3030
import java.util.ArrayList;
3131
import java.util.Collection;
3232
import java.util.Collections;
33+
import java.util.Comparator;
3334
import java.util.List;
35+
import java.util.Objects;
3436
import java.util.Set;
3537
import java.util.concurrent.CopyOnWriteArrayList;
3638
import java.util.logging.Level;
3739
import java.util.logging.Logger;
40+
import java.util.stream.Collectors;
3841
import jenkins.model.ModelObjectWithContextMenu;
42+
import jenkins.model.Tab;
3943
import jenkins.model.TransientActionFactory;
4044
import jenkins.security.stapler.StaplerNotDispatchable;
45+
import org.apache.commons.lang.StringUtils;
46+
import org.jenkins.ui.icon.IconSpec;
4147
import org.kohsuke.stapler.StaplerRequest;
4248
import org.kohsuke.stapler.StaplerRequest2;
4349
import org.kohsuke.stapler.StaplerResponse;
@@ -113,6 +119,29 @@ public final List<? extends Action> getAllActions() {
113119
return Collections.unmodifiableList(_actions);
114120
}
115121

122+
/**
123+
* TODO
124+
* @since TODO
125+
*/
126+
public List<Action> getAppBarActions() {
127+
return getAllActions().stream()
128+
.filter(e -> !(e instanceof Tab))
129+
.filter(e -> {
130+
String icon = e.getIconFileName();
131+
132+
if (e instanceof IconSpec) {
133+
if (((IconSpec) e).getIconClassName() != null) {
134+
icon = ((IconSpec) e).getIconClassName();
135+
}
136+
}
137+
138+
return !StringUtils.isBlank(e.getDisplayName()) && !StringUtils.isBlank(icon);
139+
})
140+
.sorted(Comparator.comparingInt((Action e) -> e.getGroup().getOrder())
141+
.thenComparing(e -> Objects.requireNonNullElse(e.getDisplayName(), "")))
142+
.collect(Collectors.toUnmodifiableList());
143+
}
144+
116145
private <T> Collection<? extends Action> createFor(TransientActionFactory<T> taf) {
117146
try {
118147
Collection<? extends Action> result = taf.createFor(taf.type().cast(this));

0 commit comments

Comments
 (0)