Source Code Cross Referenced for ListBox.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 junit.framework.AssertionFailedError;
005:        import org.uispec4j.assertion.Assertion;
006:        import org.uispec4j.finder.FinderUtils;
007:        import org.uispec4j.finder.StringMatcher;
008:        import org.uispec4j.utils.ArrayUtils;
009:        import org.uispec4j.utils.KeyUtils;
010:
011:        import javax.swing.*;
012:        import java.awt.*;
013:        import java.util.ArrayList;
014:        import java.util.List;
015:        import java.util.Arrays;
016:
017:        /**
018:         * Wrapper for JList components.</p>
019:         * This class provides means for checking the contents and selection of the list,
020:         * changing the selection, etc. For all these methods, when using String values,
021:         * the means of retrieving a String representation of the displayed values can be customized
022:         * using the {@link #setCellValueConverter(ListBoxCellValueConverter)} method and providing
023:         * a new {@link ListBoxCellValueConverter} implementation.
024:         * A {@link DefaultListBoxCellValueConverter} is set up by default.
025:         */
026:        public class ListBox extends AbstractUIComponent {
027:            public static final String TYPE_NAME = "listBox";
028:            public static final Class[] SWING_CLASSES = { JList.class };
029:
030:            private JList jList;
031:            private ListBoxCellValueConverter cellValueConverter = new DefaultListBoxCellValueConverter();
032:
033:            public ListBox(JList list) {
034:                this .jList = list;
035:            }
036:
037:            public String getDescriptionTypeName() {
038:                return TYPE_NAME;
039:            }
040:
041:            public Component getAwtComponent() {
042:                return jList;
043:            }
044:
045:            public void setCellValueConverter(
046:                    ListBoxCellValueConverter cellValueConverter) {
047:                this .cellValueConverter = cellValueConverter;
048:            }
049:
050:            public Assertion isEmpty() {
051:                return new Assertion() {
052:                    public void check() {
053:                        if (getSize() != 0) {
054:                            Assert.fail("List should be empty but contains: "
055:                                    + ArrayUtils.toString(getContent()));
056:                        }
057:                    }
058:                };
059:            }
060:
061:            public Assertion contentEquals(final String[] displayedValues) {
062:                return new Assertion() {
063:                    public void check() {
064:                        ArrayUtils.assertEquals(displayedValues, getContent());
065:                    }
066:                };
067:            }
068:
069:            public Assertion contains(String item) {
070:                return contains(new String[] { item });
071:            }
072:
073:            public Assertion contains(final String[] items) {
074:                return new Assertion() {
075:                    public void check() throws Exception {
076:                        List content = Arrays.asList(getContent());
077:                        for (int i = 0; i < items.length; i++) {
078:                            String item = items[i];
079:                            if (!content.contains(item)) {
080:                                throw new AssertionFailedError("Item '" + item
081:                                        + "' not found - actual content:"
082:                                        + content);
083:                            }
084:                        }
085:                    }
086:                };
087:            }
088:
089:            public void selectIndex(int index) {
090:                jList.setSelectedIndex(index);
091:            }
092:
093:            public void selectIndices(int[] indices) {
094:                jList.getSelectionModel().setValueIsAdjusting(true);
095:                jList.setSelectedIndices(indices);
096:                jList.getSelectionModel().setValueIsAdjusting(false);
097:            }
098:
099:            public void select(String[] values) {
100:                int[] indices = new int[values.length];
101:                for (int i = 0; i < values.length; i++) {
102:                    indices[i] = getIndexForString(values[i]);
103:                }
104:                selectIndices(indices);
105:            }
106:
107:            public void select(String value) {
108:                int index = getIndexForString(value);
109:                if (index == -1) {
110:                    throw new AssertionFailedError("Item '" + value
111:                            + "' not found in "
112:                            + ArrayUtils.toString(getContent()));
113:                }
114:                jList.setSelectedIndex(index);
115:            }
116:
117:            public void clearSelection() {
118:                jList.clearSelection();
119:            }
120:
121:            public int getSize() {
122:                return jList.getModel().getSize();
123:            }
124:
125:            public void doubleClick() {
126:                Mouse.doubleClick(this );
127:            }
128:
129:            public Assertion selectionIsEmpty() {
130:                return new Assertion() {
131:                    public void check() {
132:                        if (jList.getSelectedIndices().length != 0) {
133:                            String[] names = getSelectedItemNames();
134:                            Assert.fail("Selection should be empty but is: "
135:                                    + ArrayUtils.toString(names));
136:                        }
137:                    }
138:                };
139:            }
140:
141:            public Assertion selectionEquals(final String item) {
142:                return selectionEquals(new String[] { item });
143:            }
144:
145:            public Assertion selectionEquals(final String[] items) {
146:                return new Assertion() {
147:                    public void check() {
148:                        ArrayUtils.assertEquals(items, getSelectedItemNames());
149:                    }
150:                };
151:            }
152:
153:            public void pressKey(Key key) {
154:                KeyUtils.pressKey(jList, key);
155:            }
156:
157:            private String[] getContent() {
158:                String[] names = new String[jList.getModel().getSize()];
159:                for (int i = 0, max = jList.getModel().getSize(); i < max; i++) {
160:                    names[i] = getRenderedValue(i);
161:                }
162:                return names;
163:            }
164:
165:            private String[] getSelectedItemNames() {
166:                int[] selectedIndices = jList.getSelectedIndices();
167:                String[] names = new String[selectedIndices.length];
168:                for (int i = 0; i < selectedIndices.length; i++) {
169:                    names[i] = getRenderedValue(selectedIndices[i]);
170:                }
171:                return names;
172:            }
173:
174:            private String getRenderedValue(int index) {
175:                return cellValueConverter.getValue(index, getComponent(index),
176:                        jList.getModel().getElementAt(index));
177:            }
178:
179:            private Component getComponent(int index) {
180:                ListCellRenderer renderer = jList.getCellRenderer();
181:                return renderer.getListCellRendererComponent(jList, jList
182:                        .getModel().getElementAt(index), index, false, false);
183:            }
184:
185:            private int getIndexForString(String searchedValue) {
186:                StringMatcher[] matchers = FinderUtils
187:                        .getMatchers(searchedValue);
188:                for (int i = 0; i < matchers.length; i++) {
189:                    StringMatcher matcher = matchers[i];
190:                    List indexes = new ArrayList();
191:                    for (int listIndex = 0, max = jList.getModel().getSize(); listIndex < max; listIndex++) {
192:                        String renderedValue = getRenderedValue(listIndex);
193:                        if (matcher.matches(renderedValue)) {
194:                            indexes.add(new Integer(listIndex));
195:                        }
196:                    }
197:                    if (indexes.size() == 1) {
198:                        return ((Integer) indexes.get(0)).intValue();
199:                    }
200:                    if (indexes.size() > 1) {
201:                        String[] items = new String[indexes.size()];
202:                        for (int j = 0; j < items.length; j++) {
203:                            items[j] = getRenderedValue(j);
204:                        }
205:                        throw new ItemAmbiguityException(searchedValue, items);
206:                    }
207:                }
208:                return -1;
209:            }
210:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.