Skip to content
Permalink
Browse files

[JENKINS-39213] Add ability for computed folders to control the views…

… in the folder
  • Loading branch information...
stephenc committed Oct 24, 2016
1 parent 9c718a0 commit 3df543235f7882e8cbcf4e11a37f637380ff34ac
@@ -28,6 +28,8 @@
import com.cloudbees.hudson.plugins.folder.health.FolderHealthMetric;
import com.cloudbees.hudson.plugins.folder.health.FolderHealthMetricDescriptor;
import com.cloudbees.hudson.plugins.folder.icons.StockFolderIcon;
import com.cloudbees.hudson.plugins.folder.views.DefaultFolderViewHolder;
import com.cloudbees.hudson.plugins.folder.views.AbstractFolderViewHolder;
import hudson.AbortException;
import hudson.BulkChange;
import hudson.Util;
@@ -162,20 +164,25 @@ private static void scan(File d, int depth) {

private DescribableList<AbstractFolderProperty<?>,AbstractFolderPropertyDescriptor> properties;

private /*almost final*/ AbstractFolderViewHolder folderViews;

/**
* {@link View}s.
*/
private /*almost final*/ CopyOnWriteArrayList<View> views;
@Deprecated
private transient /*almost final*/ CopyOnWriteArrayList<View> views;

/**
* Currently active Views tab bar.
*/
private volatile ViewsTabBar viewsTabBar;
@Deprecated
private transient volatile ViewsTabBar viewsTabBar;

/**
* Name of the primary view.
*/
private volatile String primaryView;
@Deprecated
private transient volatile String primaryView;

private transient /*almost final*/ ViewGroupMixIn viewGroupMixIn;

@@ -198,37 +205,52 @@ protected void init() {
p.setOwner(this);
}
if (icon == null) {
icon = new StockFolderIcon();
icon = newDefaultFolderIcon();
}
icon.setOwner(this);

if (views == null) {
views = new CopyOnWriteArrayList<View>();
}
if (viewsTabBar == null) {
viewsTabBar = new DefaultViewsTabBar();

if (folderViews == null) {
if (views != null && !views.isEmpty()) {
folderViews = new DefaultFolderViewHolder(views, primaryView, viewsTabBar == null ? newDefaultViewsTabBar()
: viewsTabBar);
} else {
folderViews = newFolderViewHolder();
}
views = null;
primaryView = null;
viewsTabBar = null;
}
viewGroupMixIn = new ViewGroupMixIn(this) {
@Override
protected List<View> views() {
return views;
return folderViews.getViews();
}
@Override
protected String primaryView() {
return primaryView == null ? views.get(0).getViewName() : primaryView;
String primaryView = folderViews.getPrimaryView();
return primaryView == null ? folderViews.getViews().get(0).getViewName() : primaryView;
}
@Override
protected void primaryView(String name) {
primaryView = name;
folderViews.setPrimaryView(name);
}
};
if (views.isEmpty()) {
try {
initViews(views);
} catch (IOException e) {
LOGGER.log(Level.WARNING, "Failed to set up the initial view", e);
@Override
public void addView(View v) throws IOException {
if (folderViews.isViewsModifiable()) {
super.addView(v);
}
}
}
@Override
public boolean canDelete(View view) {
return folderViews.isViewsModifiable() && super.canDelete(view);
}
@Override
public synchronized void deleteView(View view) throws IOException {
if (folderViews.isViewsModifiable()) {
super.deleteView(view);
}
}
};
if (healthMetrics == null) {
List<FolderHealthMetric> metrics = new ArrayList<FolderHealthMetric>();
for (FolderHealthMetricDescriptor d : FolderHealthMetricDescriptor.all()) {
@@ -241,6 +263,24 @@ protected void primaryView(String name) {
}
}

protected DefaultViewsTabBar newDefaultViewsTabBar() {
return new DefaultViewsTabBar();
}

protected AbstractFolderViewHolder newFolderViewHolder() {
CopyOnWriteArrayList views = new CopyOnWriteArrayList<View>();
try {
initViews(views);
} catch (IOException e) {
LOGGER.log(Level.WARNING, "Failed to set up the initial view", e);
}
return new DefaultFolderViewHolder(views, null, newDefaultViewsTabBar());
}

protected FolderIcon newDefaultFolderIcon() {
return new StockFolderIcon();
}

protected void initViews(List<View> views) throws IOException {
AllView v = new AllView("All", this);
views.add(v);
@@ -401,14 +441,22 @@ public View getView(String name) {
return viewGroupMixIn.getViews();
}

public AbstractFolderViewHolder getFolderViews() {
return folderViews;
}

public void resetFolderViews() {
folderViews = newFolderViewHolder();
}

@Exported
@Override
public View getPrimaryView() {
return viewGroupMixIn.getPrimaryView();
}

public void setPrimaryView(View v) {
primaryView = v.getViewName();
folderViews.setPrimaryView(v.getViewName());
}

@Override
@@ -418,7 +466,7 @@ public void onViewRenamed(View view, String oldName, String newName) {

@Override
public ViewsTabBar getViewsTabBar() {
return viewsTabBar;
return folderViews.getTabBar();
}

@Override
@@ -494,6 +542,7 @@ public FormValidation doViewExistsCheck(@QueryParameter String value) {
}
}


/**
* Get the current health report for a folder.
*
@@ -611,7 +660,7 @@ public void onRenamed(TopLevelItem item, String oldName, String newName) throws
items.remove(oldName);
items.put(newName, (I) item);
// For compatibility with old views:
for (View v : views) {
for (View v : folderViews.getViews()) {
v.onJobRenamed(item, oldName, newName);
}
save();
@@ -623,7 +672,7 @@ public void onDeleted(TopLevelItem item) throws IOException {
ItemListener.fireOnDeleted(item);
items.remove(item.getName());
// For compatibility with old views:
for (View v : views) {
for (View v : folderViews.getViews()) {
v.onJobRenamed(item, item.getName(), null);
}
save();
@@ -694,12 +743,12 @@ public void doConfigSubmit(StaplerRequest req, StaplerResponse rsp) throws IOExc
try {
description = json.getString("description");
displayName = Util.fixEmpty(json.optString("displayNameOrNull"));
if (json.has("viewsTabBar")) {
viewsTabBar = req.bindJSON(ViewsTabBar.class, json.getJSONObject("viewsTabBar"));
if (folderViews.isTabBarModifiable() && json.has("viewsTabBar")) {
folderViews.setTabBar(req.bindJSON(ViewsTabBar.class, json.getJSONObject("viewsTabBar")));
}

if (json.has("primaryView")) {
primaryView = json.getString("primaryView");
if (folderViews.isPrimaryModifiable() && json.has("primaryView")) {
folderViews.setPrimaryView(json.getString("primaryView"));
}

properties.rebuild(req, json, getDescriptor().getPropertyDescriptors());
@@ -26,8 +26,11 @@

import com.cloudbees.hudson.plugins.folder.health.FolderHealthMetricDescriptor;
import hudson.model.TopLevelItemDescriptor;
import hudson.views.ViewsTabBar;
import hudson.views.ViewsTabBarDescriptor;
import java.util.ArrayList;
import java.util.List;
import jenkins.model.Jenkins;

/**
* Category of {@link AbstractFolder}.
@@ -72,4 +75,16 @@ public String getCategoryId() {
return r;
}

public boolean isIconConfigurable() {
return FolderIconDescriptor.all().size() > 1;
}

public boolean isTabBarConfigurable() {
return Jenkins.getActiveInstance().getDescriptorList(ViewsTabBar.class).size() > 1;
}

public boolean isLookAndFeelConfigurable(AbstractFolder<?> folder) {
return isIconConfigurable() || (isTabBarConfigurable() && folder.getFolderViews().isTabBarModifiable()) || (folder.getViews().size() > 1 && folder.getFolderViews().isPrimaryModifiable());
}

}
@@ -0,0 +1,130 @@
/*
* The MIT License
*
* Copyright 2016 CloudBees, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package com.cloudbees.hudson.plugins.folder.views;

import com.cloudbees.hudson.plugins.folder.AbstractFolder;
import edu.umd.cs.findbugs.annotations.CheckForNull;
import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.ExtensionPoint;
import hudson.model.View;
import hudson.views.ViewsTabBar;
import java.util.List;

/**
* Responsible for holding the view configuration of an {@link AbstractFolder}. Each {@link AbstractFolder} concrete
* type should define its view configuration holder by returning the implementaion from
* {@link AbstractFolder#newFolderViewHolder()}
*
* Use-cases:
* <ul>
* <li>
* Where the {@link AbstractFolder} permits the views to be configured by the user, use a
* {@link DefaultFolderViewHolder}
* </li>
* <li>
* Where the {@link AbstractFolder} has a fixed set of pre-configured views, the plugin can provide
* a custom implementation that returns the fixed set of views.
* </li>
* </ul>
*
* @since FIXME
*/
public abstract class AbstractFolderViewHolder implements ExtensionPoint {
/**
* Returns the list of views. If {@link #isViewsModifiable()} then this list is modifiable.
*
* @return the list of views.
*/
@NonNull
public abstract List<View> getViews();

/**
* Changes the list of {@link View}s. May be a no-op if {@link #isViewsModifiable()} returns {@code false}.
*
* @param views the new list of {@link View}s.
* @see #isViewsModifiable()
*/
public abstract void setViews(@NonNull List<? extends View> views);

/**
* Returns {@code true} if the list of views is modifiable.
*
* @return {@code true} if the list of views is modifiable.
*/
public boolean isViewsModifiable() {
return true;
}

/**
* Returns the {@link View#getViewName()} of the primary view or {@code null} if the first view should be primary.
*
* @return the {@link View#getViewName()} of the primary view or {@code null} if the first view should be primary.
*/
@CheckForNull
public abstract String getPrimaryView();

/**
* Changes the primary {@link View}. May be a no-op if {@link #isPrimaryModifiable()} returns {@code false}.
*
* @param name the {@link View#getViewName()} of the primary {@link View} of {@code null} to use the first view.
* @see #isPrimaryModifiable()
*/
public abstract void setPrimaryView(@CheckForNull String name);

/**
* Returns {@code true} if the primary {@link View} is modifiable.
*
* @return {@code true} if the primary {@link View} is modifiable.
*/
public boolean isPrimaryModifiable() {
return true;
}

/**
* Returns the {@link ViewsTabBar}.
*
* @return the {@link ViewsTabBar}.
*/
@NonNull
public abstract ViewsTabBar getTabBar();

/**
* Changes the {@link ViewsTabBar}. May be a no-op if {@link #isTabBarModifiable()} returns {@code false}.
*
* @param tabBar the new {@link ViewsTabBar}.
* @see #isTabBarModifiable()
*/
public abstract void setTabBar(@NonNull ViewsTabBar tabBar);

/**
* Returns {@code true} if the {@link ViewsTabBar} is modifiable.
*
* @return {@code true} if the {@link ViewsTabBar} is modifiable.
*/
public boolean isTabBarModifiable() {
return true;
}

}

0 comments on commit 3df5432

Please sign in to comment.
You can’t perform that action at this time.