KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > gui > controlx > version > VersionListModel


1 /**
2  *
3  * Magnolia and its source-code is licensed under the LGPL.
4  * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
5  * When copying, adapting, or redistributing this document in keeping with the guidelines above,
6  * you are required to provide proper attribution to obinary.
7  * If you reproduce or distribute the document without making any substantive modifications to its content,
8  * please use the following attribution line:
9  *
10  * Copyright 1993-2006 obinary Ltd. (http://www.obinary.com) All rights reserved.
11  *
12  */

13 package info.magnolia.cms.gui.controlx.version;
14
15 import info.magnolia.cms.core.Content;
16 import info.magnolia.cms.gui.controlx.list.AbstractListModel;
17 import info.magnolia.cms.gui.query.SearchQuery;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.Collection JavaDoc;
21 import java.util.List JavaDoc;
22
23 import javax.jcr.RepositoryException;
24 import javax.jcr.version.Version;
25 import javax.jcr.version.VersionHistory;
26 import javax.jcr.version.VersionIterator;
27
28
29 /**
30  * @author Sameer Charles $Id:VersionListModel.java 2544 2006-04-04 12:47:32Z philipp $
31  */

32 public class VersionListModel extends AbstractListModel {
33
34     /**
35      * versioned node
36      */

37     private Content content;
38
39     /**
40      * defines how many versions this model will return in maximum
41      */

42     private int max = -1;
43
44     /**
45      * search query to be used by sub implementation
46      */

47     protected SearchQuery query;
48
49     /**
50      * @param content
51      * @param max
52      */

53     public VersionListModel(Content content, int max) {
54         this.content = content;
55         this.max = max;
56     }
57
58     /**
59      * constructor
60      */

61     public VersionListModel(Content content) {
62         this.content = content;
63     }
64
65     /**
66      * get all versions
67      * @return all versions in a collection
68      */

69     protected Collection JavaDoc getResult() throws RepositoryException {
70         List JavaDoc allVersions = new ArrayList JavaDoc();
71
72         VersionHistory versionHistory = this.content.getVersionHistory();
73         if (versionHistory == null) {
74             return allVersions;
75         }
76
77         VersionIterator iterator = versionHistory.getAllVersions();
78         // skip root version, its safe here since this list is only meant for presentation
79
// and there is always a root version
80
iterator.nextVersion();
81         while (iterator.hasNext()) {
82             Version version = iterator.nextVersion();
83             allVersions.add(this.content.getVersionedContent(version));
84         }
85         if (max != -1) {
86             while(allVersions.size() > max){
87                 allVersions.remove(0);
88             }
89         }
90         return allVersions;
91     }
92
93     /**
94      * @return the max
95      */

96     public int getMax() {
97         return this.max;
98     }
99
100     /**
101      * @param max the max to set
102      */

103     public void setMax(int max) {
104         this.max = max;
105     }
106
107     /**
108      * @return the content
109      */

110     public Content getContent() {
111         return this.content;
112     }
113
114     /**
115      * @param content the content to set
116      */

117     public void setContent(Content content) {
118         this.content = content;
119     }
120 }
121
Popular Tags