提交 72e1ea35 编写于 作者: K Kohsuke Kawaguchi

extracted the commonality into ViewGroupMixIn, much like we have...

extracted the commonality into ViewGroupMixIn, much like we have ItemGroupMixIn. This makes reuse in plugins easier, too.
上级 04ca8b61
......@@ -439,6 +439,13 @@ public class Hudson extends Node implements ItemGroup<TopLevelItem>, StaplerProx
*/
private volatile String primaryView;
private transient final ViewGroupMixIn viewGroupMixIn = new ViewGroupMixIn(this) {
protected List<View> views() { return views; }
protected String primaryView() { return primaryView; }
protected void primaryView(String name) { primaryView=name; }
};
private transient final FingerprintMap fingerprintMap = new FingerprintMap();
/**
......@@ -1328,7 +1335,7 @@ public class Hudson extends Node implements ItemGroup<TopLevelItem>, StaplerProx
* no need to search recursively.
*/
public List<Project> getProjects() {
return Util.createSubList(items.values(),Project.class);
return Util.createSubList(items.values(), Project.class);
}
/**
......@@ -1352,17 +1359,7 @@ public class Hudson extends Node implements ItemGroup<TopLevelItem>, StaplerProx
}
public synchronized View getView(String name) {
for (View v : views) {
if(v.getViewName().equals(name))
return v;
}
if (name != null && !name.equals(primaryView)) {
// Fallback to subview of primary view if it is a ViewGroup
View pv = getPrimaryView();
if (pv instanceof ViewGroup)
return ((ViewGroup)pv).getView(name);
}
return null;
return viewGroupMixIn.getView(name);
}
/**
......@@ -1370,26 +1367,31 @@ public class Hudson extends Node implements ItemGroup<TopLevelItem>, StaplerProx
*/
@Exported
public synchronized Collection<View> getViews() {
List<View> copy = new ArrayList<View>(views);
Collections.sort(copy, View.SORTER);
return copy;
return viewGroupMixIn.getViews();
}
public void addView(View v) throws IOException {
v.owner = this;
views.add(v);
save();
viewGroupMixIn.addView(v);
}
public boolean canDelete(View view) {
return !view.isDefault(); // Cannot delete primary view
return viewGroupMixIn.canDelete(view);
}
public synchronized void deleteView(View view) throws IOException {
if (views.size() <= 1)
throw new IllegalStateException("Cannot delete last view");
views.remove(view);
save();
viewGroupMixIn.deleteView(view);
}
public void onViewRenamed(View view, String oldName, String newName) {
viewGroupMixIn.onViewRenamed(view,oldName,newName);
}
/**
* Returns the primary {@link View} that renders the top-page of Hudson.
*/
@Exported
public View getPrimaryView() {
return viewGroupMixIn.getPrimaryView();
}
public ViewsTabBar getViewsTabBar() {
......@@ -1738,13 +1740,6 @@ public class Hudson extends Node implements ItemGroup<TopLevelItem>, StaplerProx
return "";
}
public void onViewRenamed(View view, String oldName, String newName) {
// If this view was the default view, change reference
if (oldName.equals(primaryView)) {
primaryView = newName;
}
}
@Override
public SearchIndexBuilder makeSearchIndex() {
return super.makeSearchIndex()
......@@ -1766,17 +1761,6 @@ public class Hudson extends Node implements ItemGroup<TopLevelItem>, StaplerProx
});
}
/**
* Returns the primary {@link View} that renders the top-page of Hudson.
*/
@Exported
public View getPrimaryView() {
View v = getView(primaryView);
if(v==null) // fallback
v = views.get(0);
return v;
}
public String getUrlChildPrefix() {
return "job";
}
......
......@@ -45,6 +45,7 @@ import java.util.Map;
* implementations. Not meant for a consumption from outside {@link ItemGroup}s.
*
* @author Kohsuke Kawaguchi
* @see ViewGroupMixIn
*/
public abstract class ItemGroupMixIn {
/**
......
......@@ -34,13 +34,9 @@ import hudson.views.ViewsTabBar;
import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
......@@ -60,23 +56,40 @@ import org.kohsuke.stapler.StaplerResponse;
* @author Tom Huybrechts
*/
public class MyViewsProperty extends UserProperty implements ViewGroup, Action {
private static final Logger log = Logger.getLogger(MyViewsProperty.class.getName());
private String primaryViewName;
/**
* Always hold at least one view.
*/
private CopyOnWriteArrayList<View> views = new CopyOnWriteArrayList<View>();
private transient ViewGroupMixIn viewGroupMixIn;
@DataBoundConstructor
public MyViewsProperty(String primaryViewName) {
this.primaryViewName = primaryViewName;
}
private MyViewsProperty() {
views.add(new AllView(Messages.Hudson_ViewName(), this));
primaryViewName = views.get(0).getViewName();
readResolve();
}
public Object readResolve() {
if (views == null)
// this shouldn't happen, but an error in 1.319 meant the last view could be deleted
views = new CopyOnWriteArrayList<View>();
if (views.isEmpty())
// preserve the non-empty invariant
views.add(new AllView(Messages.Hudson_ViewName(), this));
viewGroupMixIn = new ViewGroupMixIn(this) {
protected List<View> views() { return views; }
protected String primaryView() { return primaryViewName; }
protected void primaryView(String name) { primaryViewName=name; }
};
return this;
}
public String getPrimaryViewName() {
......@@ -101,58 +114,31 @@ public class MyViewsProperty extends UserProperty implements ViewGroup, Action {
}
public Collection<View> getViews() {
List<View> copy = new ArrayList<View>(views);
Collections.sort(copy, View.SORTER);
return copy;
return viewGroupMixIn.getViews();
}
public View getView(String name) {
for (View v : views) {
if (v.getViewName().equals(name)) {
return v;
}
}
return null;
return viewGroupMixIn.getView(name);
}
public boolean canDelete(View view) {
return views.size() > 1; // Cannot delete last view
return viewGroupMixIn.canDelete(view);
}
public void deleteView(View view) throws IOException {
if (views.size() <= 1) {
throw new IllegalStateException("Cannot delete last view");
}
views.remove(view);
if (view.getViewName().equals(primaryViewName)) {
primaryViewName = views.get(0).getViewName();
}
save();
viewGroupMixIn.deleteView(view);
}
public void onViewRenamed(View view, String oldName, String newName) {
if (primaryViewName.equals(oldName)) {
primaryViewName = newName;
try {
save();
} catch (IOException ex) {
log.log(Level.SEVERE, "error while saving user " + user.getId(), ex);
}
}
viewGroupMixIn.onViewRenamed(view,oldName,newName);
}
public void addView(View view) throws IOException {
views.add(view);
save();
viewGroupMixIn.addView(view);
}
public View getPrimaryView() {
if (primaryViewName != null) {
View view = getView(primaryViewName);
if (view != null) return view;
}
return views.get(0);
return viewGroupMixIn.getPrimaryView();
}
public HttpResponse doIndex() {
......@@ -230,17 +216,6 @@ public class MyViewsProperty extends UserProperty implements ViewGroup, Action {
req.bindJSON(this, form);
return this;
}
public Object readResolve() {
if (views == null)
// this shouldn't happen, but an error in 1.319 meant the last view could be deleted
views = new CopyOnWriteArrayList<View>();
if (views.isEmpty())
// preserve the non-empty invariant
views.add(new AllView(Messages.Hudson_ViewName(), this));
return this;
}
public ViewsTabBar getViewsTabBar() {
return Hudson.getInstance().getViewsTabBar();
......
......@@ -50,6 +50,7 @@ import java.util.concurrent.CopyOnWriteArrayList;
*
* <p>
* Until this class is sufficiently stable, there's no need for l10n.
* TODO: use ViewGroupMixIn
*
* @author Kohsuke Kawaguchi
*/
......
/*
* The MIT License
*
* Copyright (c) 2011, 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 hudson.model;
import hudson.model.ItemGroupMixIn;
import hudson.model.View;
import hudson.model.ViewGroup;
import org.kohsuke.stapler.export.Exported;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
/**
* Implements {@link ViewGroup} to be used as a "mix-in".
* Not meant for a consumption from outside {@link ViewGroup}s.
*
* <h2>How to use this class</h2>
* <ol>
* <li>
* Create three data fields in your class:
* <pre>
* private String primaryView;
* private CopyOnWriteArrayList&lt;View> views;
* private ViewsTabBar viewsTabBar;
* </pre>
* <li>
* Define a transient field and store ViewGroupMixIn subype, then wire up getters and setters:
* <pre>
* private transient ViewGroupMixIn = new ViewGroupMixIn() {
* List&lt;View> views() { return views; }
* ...
* }
* </pre>
* @author Kohsuke Kawaguchi
* @see ItemGroupMixIn
*/
public abstract class ViewGroupMixIn {
private final ViewGroup owner;
protected abstract List<View> views();
protected abstract String primaryView();
protected abstract void primaryView(String newName);
protected ViewGroupMixIn(ViewGroup owner) {
this.owner = owner;
}
public void addView(View v) throws IOException {
v.owner = owner;
views().add(v);
owner.save();
}
public boolean canDelete(View view) {
return !view.isDefault(); // Cannot delete primary view
}
public synchronized void deleteView(View view) throws IOException {
if (views().size() <= 1)
throw new IllegalStateException("Cannot delete last view");
views().remove(view);
owner.save();
}
public synchronized View getView(String name) {
for (View v : views()) {
if(v.getViewName().equals(name))
return v;
}
if (name != null && !name.equals(primaryView())) {
// Fallback to subview of primary view if it is a ViewGroup
View pv = getPrimaryView();
if (pv instanceof ViewGroup)
return ((ViewGroup)pv).getView(name);
}
return null;
}
/**
* Gets the read-only list of all {@link View}s.
*/
@Exported
public synchronized Collection<View> getViews() {
List<View> copy = new ArrayList<View>(views());
Collections.sort(copy, View.SORTER);
return copy;
}
/**
* Returns the primary {@link View} that renders the top-page of Hudson.
*/
@Exported
public View getPrimaryView() {
View v = getView(primaryView());
if(v==null) // fallback
v = views().get(0);
return v;
}
public void onViewRenamed(View view, String oldName, String newName) {
// If this view was the default view, change reference
if (oldName.equals(primaryView())) {
primaryView(newName);
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册