Source Code Cross Referenced for Slider.java in  » Testing » UISpec4J » org » uispec4j » 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 » Testing » UISpec4J » org.uispec4j 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package org.uispec4j;
002:
003:        import junit.framework.Assert;
004:        import org.uispec4j.assertion.Assertion;
005:        import org.uispec4j.utils.ComponentUtils;
006:        import org.uispec4j.utils.Utils;
007:
008:        import javax.swing.JComponent;
009:        import javax.swing.JSlider;
010:        import java.awt.Component;
011:        import java.util.Dictionary;
012:        import java.util.Enumeration;
013:        import java.util.TreeMap;
014:
015:        /**
016:         * Wrapper for JSlider components.<p/>
017:         * This class provides means for checking the contents and the current position of the knob,
018:         * changing the position, etc.
019:         */
020:        public class Slider extends AbstractUIComponent {
021:            public static final String TYPE_NAME = "slider";
022:            public static final Class[] SWING_CLASSES = { JSlider.class };
023:
024:            private JSlider jSlider;
025:            private int precision = 2;
026:
027:            public Slider(JSlider jSlider) {
028:                this .jSlider = jSlider;
029:            }
030:
031:            public Component getAwtComponent() {
032:                return jSlider;
033:            }
034:
035:            public String getDescriptionTypeName() {
036:                return TYPE_NAME;
037:            }
038:
039:            /**
040:             * Checks the slider labels in order.
041:             */
042:            public Assertion labelsEqual(final String[] expected) {
043:                return new Assertion() {
044:                    public void check() throws Exception {
045:                        TreeMap sortedTree = getSortedTree();
046:                        Utils
047:                                .assertEquals(expected, sortedTree.values()
048:                                        .toArray(
049:                                                new Object[sortedTree.values()
050:                                                        .size()]));
051:                    }
052:                };
053:            }
054:
055:            /**
056:             * Moves the knob at the specified label position
057:             */
058:            public void setPosition(String label) throws ItemNotFoundException {
059:                int index = getIndexForLabel(label);
060:                if (index == -1) {
061:                    throw new ItemNotFoundException("No label '" + label
062:                            + "' has been found");
063:                }
064:                jSlider.setValue(index);
065:            }
066:
067:            /**
068:             * Checks that the current position corresponds to the specified label
069:             */
070:            public Assertion positionEquals(final String expectedLabel) {
071:                return new Assertion() {
072:                    public void check() throws Exception {
073:                        Assert.assertEquals(expectedLabel, getCurrentLabel());
074:                    }
075:                };
076:            }
077:
078:            /**
079:             * Checks the knob position as a percentage (0-100) of the available range.
080:             * The actual completion must be equal to the given value plus or minus the slider precision.
081:             *
082:             * @param expectedValue an int between 0 and 100, or -1 if the status is undeterminate
083:             * @see #setPrecision(int)
084:             */
085:            public Assertion relativePositionEquals(final int expectedValue) {
086:                return new Assertion() {
087:                    public void check() throws Exception {
088:                        int relativePosition = getRelativePosition();
089:                        Assert
090:                                .assertTrue("Expected " + expectedValue
091:                                        + " but was " + relativePosition,
092:                                        isRoughlyEqual(expectedValue,
093:                                                relativePosition));
094:                    }
095:                };
096:            }
097:
098:            /**
099:             * Sets the precision for the relative position check. This precision is the greatest difference
100:             * allowed between the actual and expected position values (both are integers between 0
101:             * and 100).<p/>
102:             * The default precision is 2.
103:             *
104:             * @see #relativePositionEquals(int)
105:             */
106:            public void setPrecision(int value) {
107:                this .precision = value;
108:            }
109:
110:            private int getRelativePosition() {
111:                int minimum = jSlider.getMinimum();
112:                int value = jSlider.getValue() - minimum;
113:                int length = jSlider.getMaximum() - minimum;
114:                return value * 100 / length;
115:            }
116:
117:            private boolean isRoughlyEqual(int actualValue, int expectedValue) {
118:                return Math.abs(actualValue - expectedValue) <= precision;
119:            }
120:
121:            private int getIndexForLabel(String label) {
122:                Dictionary dictionary = jSlider.getLabelTable();
123:                for (Enumeration indices = dictionary.keys(); indices
124:                        .hasMoreElements();) {
125:                    Integer indice = (Integer) indices.nextElement();
126:                    JComponent component = (JComponent) dictionary.get(indice);
127:                    if (label
128:                            .equals(ComponentUtils.getDisplayedName(component))) {
129:                        return indice.intValue();
130:                    }
131:                }
132:                return -1;
133:            }
134:
135:            private String getCurrentLabel() {
136:                int value = jSlider.getValue();
137:                Dictionary dictionary = jSlider.getLabelTable();
138:                for (Enumeration indices = dictionary.keys(); indices
139:                        .hasMoreElements();) {
140:                    Integer indice = (Integer) indices.nextElement();
141:                    JComponent component = (JComponent) dictionary.get(indice);
142:                    if (indice.intValue() == value) {
143:                        return ComponentUtils.getDisplayedName(component);
144:                    }
145:                }
146:                return null;
147:            }
148:
149:            private TreeMap getSortedTree() {
150:                Dictionary dictionary = jSlider.getLabelTable();
151:                TreeMap treeMap = new TreeMap();
152:                for (Enumeration indices = dictionary.keys(); indices
153:                        .hasMoreElements();) {
154:                    Integer indice = (Integer) indices.nextElement();
155:                    JComponent component = (JComponent) dictionary.get(indice);
156:                    treeMap.put(indice, ComponentUtils
157:                            .getDisplayedName(component));
158:                }
159:                return treeMap;
160:            }
161:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.