KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > hudson > model > ListView


1 package hudson.model;
2
3 import hudson.Util;
4 import org.kohsuke.stapler.StaplerRequest;
5 import org.kohsuke.stapler.StaplerResponse;
6
7 import javax.servlet.ServletException JavaDoc;
8 import java.io.IOException JavaDoc;
9 import java.util.Arrays JavaDoc;
10 import java.util.List JavaDoc;
11 import java.util.Set JavaDoc;
12 import java.util.TreeSet JavaDoc;
13
14 /**
15  * Displays {@link Job}s in a flat list view.
16  *
17  * @author Kohsuke Kawaguchi
18  */

19 public class ListView extends View {
20
21     private final Hudson owner;
22
23     /**
24      * List of job names. This is what gets serialized.
25      */

26     /*package*/ final Set JavaDoc<String JavaDoc> jobNames = new TreeSet JavaDoc<String JavaDoc>();
27
28     /**
29      * PluginName of this view.
30      */

31     private String JavaDoc name;
32
33     /**
34      * Message displayed in the view page.
35      */

36     private String JavaDoc description;
37
38
39     public ListView(Hudson owner, String JavaDoc name) {
40         this.name = name;
41         this.owner = owner;
42     }
43
44     /**
45      * Returns a read-only view of all {@link Job}s in this view.
46      *
47      * <p>
48      * This method returns a separate copy each time to avoid
49      * concurrent modification issue.
50      */

51     public synchronized List JavaDoc<TopLevelItem> getItems() {
52         TopLevelItem[] items = new TopLevelItem[jobNames.size()];
53         int i=0;
54         for (String JavaDoc name : jobNames)
55             items[i++] = owner.getItem(name);
56         return Arrays.asList(items);
57     }
58
59     public TopLevelItem getJob(String JavaDoc name) {
60         return owner.getItem(name);
61     }
62
63     public boolean contains(TopLevelItem item) {
64         return jobNames.contains(item.getName());
65     }
66
67     public String JavaDoc getViewName() {
68         return name;
69     }
70
71     public String JavaDoc getDescription() {
72         return description;
73     }
74
75     public String JavaDoc getDisplayName() {
76         return name;
77     }
78
79     public Item doCreateItem(StaplerRequest req, StaplerResponse rsp) throws IOException JavaDoc, ServletException JavaDoc {
80         if(!Hudson.adminCheck(req,rsp))
81             return null;
82
83         Item item = owner.doCreateItem(req, rsp);
84         if(item!=null) {
85             jobNames.add(item.getName());
86             owner.save();
87         }
88         return item;
89     }
90
91     public String JavaDoc getUrl() {
92         return "view/"+name+'/';
93     }
94
95     /**
96      * Accepts submission from the configuration page.
97      */

98     public synchronized void doConfigSubmit( StaplerRequest req, StaplerResponse rsp ) throws IOException JavaDoc {
99         if(!Hudson.adminCheck(req,rsp))
100             return;
101
102         req.setCharacterEncoding("UTF-8");
103         
104         jobNames.clear();
105         for (TopLevelItem item : owner.getItems()) {
106             if(req.getParameter(item.getName())!=null)
107                 jobNames.add(item.getName());
108         }
109
110         description = Util.nullify(req.getParameter("description"));
111
112         owner.save();
113
114         rsp.sendRedirect(".");
115     }
116
117     /**
118      * Accepts the new description.
119      */

120     public synchronized void doSubmitDescription( StaplerRequest req, StaplerResponse rsp ) throws IOException JavaDoc, ServletException JavaDoc {
121         if(!Hudson.adminCheck(req,rsp))
122             return;
123
124         req.setCharacterEncoding("UTF-8");
125         description = req.getParameter("description");
126         owner.save();
127         rsp.sendRedirect("."); // go to the top page
128
}
129
130     /**
131      * Deletes this view.
132      */

133     public synchronized void doDoDelete( StaplerRequest req, StaplerResponse rsp ) throws IOException JavaDoc {
134         if(!Hudson.adminCheck(req,rsp))
135             return;
136
137         owner.deleteView(this);
138         rsp.sendRedirect2(req.getContextPath()+"/");
139     }
140 }
141
Popular Tags