Source Code Cross Referenced for RatingsPage.java in  » J2EE » wicket » wicket » examples » ajax » builtin » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » J2EE » wicket » wicket.examples.ajax.builtin 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package wicket.examples.ajax.builtin;
002:
003:        import java.io.Serializable;
004:
005:        import wicket.ResourceReference;
006:        import wicket.ajax.AjaxRequestTarget;
007:        import wicket.extensions.rating.RatingPanel;
008:        import wicket.markup.html.link.Link;
009:        import wicket.model.IModel;
010:        import wicket.model.Model;
011:        import wicket.model.PropertyModel;
012:
013:        /**
014:         * Demo page for the rating component.
015:         * 
016:         * @author Martijn Dashorst
017:         */
018:        public class RatingsPage extends BasePage {
019:            /**
020:             * Star image for no selected star
021:             */
022:            public static final ResourceReference WICKETSTAR0 = new ResourceReference(
023:                    RatingsPage.class, "WicketStar0.png");
024:
025:            /**
026:             * Star image for selected star
027:             */
028:            public static final ResourceReference WICKETSTAR1 = new ResourceReference(
029:                    RatingsPage.class, "WicketStar1.png");
030:
031:            /** For serialization. */
032:            private static final long serialVersionUID = 1L;
033:
034:            /**
035:             * Link to reset the ratings.
036:             */
037:            private final class ResetRatingLink extends Link {
038:                /** For serialization. */
039:                private static final long serialVersionUID = 1L;
040:
041:                /**
042:                 * Constructor.
043:                 * 
044:                 * @param id
045:                 *            component id
046:                 * @param object
047:                 *            the model to reset.
048:                 */
049:                public ResetRatingLink(String id, IModel object) {
050:                    super (id, object);
051:                }
052:
053:                /**
054:                 * @see Link#onClick()
055:                 */
056:                public void onClick() {
057:                    RatingModel rating = (RatingModel) getModelObject();
058:                    rating.nrOfVotes = 0;
059:                    rating.rating = 0;
060:                    rating.sumOfRatings = 0;
061:                }
062:            }
063:
064:            /**
065:             * Rating model for storing the ratings, typically this comes from a
066:             * database.
067:             */
068:            public static class RatingModel implements  Serializable {
069:                private int nrOfVotes = 0;
070:                private int sumOfRatings = 0;
071:                private double rating = 0;
072:
073:                /**
074:                 * Returns whether the star should be rendered active.
075:                 * 
076:                 * @param star
077:                 *            the number of the star
078:                 * @return true when the star is active
079:                 */
080:                public boolean isActive(int star) {
081:                    return star < ((int) (rating + 0.5));
082:                }
083:
084:                /**
085:                 * Gets the number of cast votes.
086:                 * 
087:                 * @return the number of cast votes.
088:                 */
089:                public Integer getNrOfVotes() {
090:                    return new Integer(nrOfVotes);
091:                }
092:
093:                /**
094:                 * Adds the vote from the user to the total of votes, and calculates the
095:                 * rating.
096:                 * 
097:                 * @param nrOfStars
098:                 *            the number of stars the user has cast
099:                 */
100:                public void addRating(int nrOfStars) {
101:                    nrOfVotes++;
102:                    sumOfRatings += nrOfStars;
103:                    rating = sumOfRatings / (1.0 * nrOfVotes);
104:                }
105:
106:                /**
107:                 * Gets the rating.
108:                 * 
109:                 * @return the rating
110:                 */
111:                public Double getRating() {
112:                    return new Double(rating);
113:                }
114:
115:                /**
116:                 * Returns the sum of the ratings.
117:                 * 
118:                 * @return the sum of the ratings.
119:                 */
120:                public int getSumOfRatings() {
121:                    return sumOfRatings;
122:                }
123:            }
124:
125:            /**
126:             * static models for the ratings, not thread safe, but in this case, we
127:             * don't care.
128:             */
129:            private static RatingModel rating1 = new RatingModel();
130:
131:            /**
132:             * static model for the ratings, not thread safe, but in this case, we don't
133:             * care.
134:             */
135:            private static RatingModel rating2 = new RatingModel();
136:
137:            /**
138:             * keeps track whether the user has already voted on this page, comes
139:             * typically from the database, or is stored in a cookie on the client side.
140:             */
141:            private Boolean hasVoted = Boolean.FALSE;
142:
143:            /**
144:             * Constructor.
145:             */
146:            public RatingsPage() {
147:                add(new RatingPanel("rating1", new PropertyModel(rating1,
148:                        "rating"), 5, new PropertyModel(rating1, "nrOfVotes"),
149:                        true) {
150:                    protected boolean onIsStarActive(int star) {
151:                        return RatingsPage.rating1.isActive(star);
152:                    }
153:
154:                    protected void onRated(int rating, AjaxRequestTarget target) {
155:                        RatingsPage.rating1.addRating(rating);
156:                    }
157:                });
158:                add(new RatingPanel("rating2", new PropertyModel(rating2,
159:                        "rating"), new Model(new Integer(5)),
160:                        new PropertyModel(rating2, "nrOfVotes"),
161:                        new PropertyModel(this , "hasVoted"), true) {
162:                    protected String getActiveStarUrl(int iteration) {
163:                        return getRequestCycle().urlFor(WICKETSTAR1).toString();
164:                    }
165:
166:                    protected String getInactiveStarUrl(int iteration) {
167:                        return getRequestCycle().urlFor(WICKETSTAR0).toString();
168:                    }
169:
170:                    protected boolean onIsStarActive(int star) {
171:                        return RatingsPage.rating2.isActive(star);
172:                    }
173:
174:                    protected void onRated(int rating, AjaxRequestTarget target) {
175:                        // make sure the user can't vote again
176:                        hasVoted = Boolean.TRUE;
177:                        RatingsPage.rating2.addRating(rating);
178:                    }
179:                });
180:                add(new ResetRatingLink("reset1", new Model(rating1)));
181:                add(new ResetRatingLink("reset2", new Model(rating2)));
182:            }
183:
184:            /**
185:             * Getter for the hasVoted flag.
186:             * 
187:             * @return <code>true</code> when the user has already voted.
188:             */
189:            public Boolean getHasVoted() {
190:                return hasVoted;
191:            }
192:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.