File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\hudson\model\ViewGroupMixIn.java.html

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2011, CloudBees, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24 package hudson.model;
25
26 import hudson.model.ItemGroupMixIn;
27 import hudson.model.View;
28 import hudson.model.ViewGroup;
29 import org.kohsuke.stapler.export.Exported;
30
31 import java.io.IOException;
32 import java.util.ArrayList;
33 import java.util.Collection;
34 import java.util.Collections;
35 import java.util.Iterator;
36 import java.util.List;
37
38 /**
39 * Implements {@link ViewGroup} to be used as a "mix-in".
40 * Not meant for a consumption from outside {@link ViewGroup}s.
41 *
42 * <h2>How to use this class</h2>
43 * <ol>
44 * <li>
45 * Create three data fields in your class:
46 * <pre>
47 * private String primaryView;
48 * private CopyOnWriteArrayList&lt;View> views;
49 * private ViewsTabBar viewsTabBar;
50 * </pre>
51 * <li>
52 * Define a transient field and store ViewGroupMixIn subype, then wire up getters and setters:
53 * <pre>
54 * private transient ViewGroupMixIn = new ViewGroupMixIn() {
55 * List&lt;View> views() { return views; }
56 * ...
57 * }
58 * </pre>
59 * @author Kohsuke Kawaguchi
60 * @see ItemGroupMixIn
61 */
62 public abstract class ViewGroupMixIn {
63 private final ViewGroup owner;
64
65 /**
66 * Returns all the views. This list must be concurrently iterable.
67 */
68 protected abstract List<View> views();
69 protected abstract String primaryView();
70 protected abstract void primaryView(String newName);
71
72 protected ViewGroupMixIn(ViewGroup owner) {
73 this.owner = owner;
74 }
75
76 public void addView(View v) throws IOException {
77 v.owner = owner;
78 views().add(v);
79 owner.save();
80 }
81
82 public boolean canDelete(View view) {
83 return !view.isDefault(); // Cannot delete primary view
84 }
85
86 public synchronized void deleteView(View view) throws IOException {
87 if (views().size() <= 1)
88 throw new IllegalStateException("Cannot delete last view");
89 views().remove(view);
90 owner.save();
91 }
92
93 public View getView(String name) {
94 for (View v : views()) {
95 if(v.getViewName().equals(name))
96 return v;
97 }
98 if (name != null && !name.equals(primaryView())) {
99 // Fallback to subview of primary view if it is a ViewGroup
100 View pv = getPrimaryView();
101 if (pv instanceof ViewGroup)
102 return ((ViewGroup)pv).getView(name);
103 }
104 return null;
105 }
106
107 /**
108 * Gets the read-only list of all {@link View}s.
109 */
110 @Exported
111 public Collection<View> getViews() {
112 List<View> orig = views();
113 List<View> copy = new ArrayList<View>(orig.size());
114 for (View v : orig) {
115 if (v.hasPermission(View.READ))
116 copy.add(v);
117 }
118 Collections.sort(copy, View.SORTER);
119 return copy;
120 }
121
122 /**
123 * Returns the primary {@link View} that renders the top-page of Hudson.
124 */
125 @Exported
126 public View getPrimaryView() {
127 View v = getView(primaryView());
128 if(v==null) // fallback
129 v = views().get(0);
130 return v;
131 }
132
133 public void onViewRenamed(View view, String oldName, String newName) {
134 // If this view was the default view, change reference
135 if (oldName.equals(primaryView())) {
136 primaryView(newName);
137 }
138 }
139 }
140