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


001:        package org.uispec4j.interception;
002:
003:        import junit.framework.Assert;
004:        import junit.framework.AssertionFailedError;
005:        import org.uispec4j.Trigger;
006:        import org.uispec4j.Window;
007:        import org.uispec4j.utils.ComponentUtils;
008:        import org.uispec4j.utils.Utils;
009:
010:        import javax.swing.*;
011:        import java.awt.*;
012:        import java.io.File;
013:        import java.util.ArrayList;
014:        import java.util.Iterator;
015:        import java.util.List;
016:
017:        /**
018:         * WindowHandler implementation dedicated to file chooser dialogs.<p>
019:         * Sample usage:
020:         * <pre><code>
021:         * WindowInterceptor
022:         *   .run(openMenu.triggerClick())
023:         *   .process(FileChooserHandler.init()
024:         *            .titleEquals("Select a template file")
025:         *            .assertAcceptsFilesOnly()
026:         *            .select("/home/bond/file.txt"))
027:         *   .run();
028:         * </code></pre>
029:         *
030:         * @see <a href="http://www.uispec4j.org/interception.html">Intercepting windows</a>
031:         */
032:        public class FileChooserHandler {
033:            private DialogHandler handler = new DialogHandler();
034:
035:            /**
036:             * Creates a new interceptor with the associated {@link Trigger}
037:             */
038:            public static FileChooserHandler init() {
039:                return new FileChooserHandler();
040:            }
041:
042:            public FileChooserHandler assertIsOpenDialog() {
043:                handler.add(new DialogTypeFileChooserHandler(
044:                        JFileChooser.OPEN_DIALOG));
045:                return this ;
046:            }
047:
048:            public FileChooserHandler assertIsSaveDialog() {
049:                handler.add(new DialogTypeFileChooserHandler(
050:                        JFileChooser.SAVE_DIALOG));
051:                return this ;
052:            }
053:
054:            public FileChooserHandler assertCurrentDirEquals(
055:                    final File currentDir) {
056:                handler.add(new FileChooserInternalHandler() {
057:                    public void process(JFileChooser fileChooser) {
058:                        Assert.assertEquals("Unexpected current directory -",
059:                                currentDir, fileChooser.getCurrentDirectory());
060:                    }
061:                });
062:                return this ;
063:            }
064:
065:            public FileChooserHandler titleEquals(final String title) {
066:                handler.add(new FileChooserInternalHandler() {
067:                    public void process(JFileChooser fileChooser) {
068:                        Assert.assertEquals("Unexpected title -", title,
069:                                fileChooser.getDialogTitle());
070:                    }
071:                });
072:                return this ;
073:            }
074:
075:            public FileChooserHandler assertApplyButtonTextEquals(
076:                    final String text) {
077:                handler.add(new FileChooserInternalHandler() {
078:                    public void process(JFileChooser fileChooser) {
079:                        Assert.assertEquals("Unexpected apply button text -",
080:                                text, fileChooser.getApproveButtonText());
081:                    }
082:                });
083:                return this ;
084:            }
085:
086:            public FileChooserHandler assertAcceptsFilesOnly() {
087:                handler.add(new FileSelectionModeFileChooserHandler(
088:                        JFileChooser.FILES_ONLY));
089:                return this ;
090:            }
091:
092:            public FileChooserHandler assertAcceptsFilesAndDirectories() {
093:                handler.add(new FileSelectionModeFileChooserHandler(
094:                        JFileChooser.FILES_AND_DIRECTORIES));
095:                return this ;
096:            }
097:
098:            public FileChooserHandler assertAcceptsDirectoriesOnly() {
099:                handler.add(new FileSelectionModeFileChooserHandler(
100:                        JFileChooser.DIRECTORIES_ONLY));
101:                return this ;
102:            }
103:
104:            public FileChooserHandler assertMultiSelectionEnabled(
105:                    final boolean enabled) {
106:                handler.add(new FileChooserInternalHandler() {
107:                    public void process(JFileChooser fileChooser) {
108:                        boolean actual = fileChooser.isMultiSelectionEnabled();
109:                        if (actual != enabled) {
110:                            Assert.fail(actual ? "Multi selection is enabled."
111:                                    : "Multi selection is not enabled.");
112:                        }
113:                    }
114:                });
115:                return this ;
116:            }
117:
118:            public WindowHandler select(final File file) {
119:                handler.add(new FileChooserInternalHandler() {
120:                    public void process(JFileChooser fileChooser) {
121:                        fileChooser.setSelectedFile(file);
122:                    }
123:                });
124:                return handler;
125:            }
126:
127:            public WindowHandler select(final File[] files) {
128:                handler.add(new FileChooserInternalHandler() {
129:                    public void process(JFileChooser fileChooser) {
130:                        fileChooser.setSelectedFiles(files);
131:                    }
132:                });
133:                return handler;
134:            }
135:
136:            public WindowHandler select(String fileName) {
137:                return select(new File(fileName));
138:            }
139:
140:            public WindowHandler select(String[] fileNames) {
141:                File[] files = new File[fileNames.length];
142:                for (int i = 0; i < files.length; i++) {
143:                    files[i] = new File(fileNames[i]);
144:                }
145:                return select(files);
146:            }
147:
148:            /** Clicks on "Cancel". */
149:            public WindowHandler cancelSelection() {
150:                handler.add(new FileChooserInternalHandler() {
151:                    public void process(JFileChooser fileChooser) {
152:                        fileChooser.cancelSelection();
153:                    }
154:                });
155:                return handler;
156:            }
157:
158:            private static class DialogHandler extends WindowHandler {
159:                private List fileChooserHandlers = new ArrayList();
160:
161:                public DialogHandler() {
162:                    super ("FileChooserHandler");
163:                }
164:
165:                void add(FileChooserInternalHandler handler) {
166:                    fileChooserHandlers.add(handler);
167:                }
168:
169:                public Trigger process(final Window window) {
170:                    Component[] components = window
171:                            .getSwingComponents(JFileChooser.class);
172:                    if (components.length != 1) {
173:                        throw new AssertionFailedError(
174:                                "The shown window is not a file chooser - window content:"
175:                                        + Utils.LINE_SEPARATOR
176:                                        + window.getDescription());
177:                    }
178:                    JFileChooser fileChooser = (JFileChooser) components[0];
179:                    for (Iterator iterator = fileChooserHandlers.iterator(); iterator
180:                            .hasNext();) {
181:                        FileChooserInternalHandler handler = (FileChooserInternalHandler) iterator
182:                                .next();
183:                        handler.process(fileChooser);
184:                    }
185:                    fileChooser.approveSelection();
186:                    return new Trigger() {
187:                        public void run() throws Exception {
188:                            ComponentUtils.close(window);
189:                        }
190:                    };
191:                }
192:            }
193:
194:            private interface FileChooserInternalHandler {
195:                void process(JFileChooser fileChooser);
196:            }
197:
198:            private static class DialogTypeFileChooserHandler implements 
199:                    FileChooserInternalHandler {
200:                int expectedType;
201:
202:                public DialogTypeFileChooserHandler(int expectedType) {
203:                    this .expectedType = expectedType;
204:                }
205:
206:                public void process(JFileChooser fileChooser) {
207:                    if (fileChooser.getDialogType() != expectedType) {
208:                        Assert.fail(getChooserType(fileChooser));
209:                    }
210:                }
211:
212:                private String getChooserType(JFileChooser fileChooser) {
213:                    String message = "Chooser is in '";
214:                    switch (fileChooser.getDialogType()) {
215:                    case JFileChooser.OPEN_DIALOG:
216:                        message += "open";
217:                        break;
218:                    case JFileChooser.SAVE_DIALOG:
219:                        message += "save";
220:                        break;
221:                    default:
222:                        message += "custom";
223:                    }
224:                    message += "' mode";
225:                    return message;
226:                }
227:            }
228:
229:            private static class FileSelectionModeFileChooserHandler implements 
230:                    FileChooserInternalHandler {
231:                int expectedMode;
232:
233:                public FileSelectionModeFileChooserHandler(int expectedMode) {
234:                    this .expectedMode = expectedMode;
235:                }
236:
237:                public void process(JFileChooser fileChooser) {
238:                    int actualMode = fileChooser.getFileSelectionMode();
239:                    if (actualMode != expectedMode) {
240:                        Assert.fail(getMessage(actualMode));
241:                    }
242:                }
243:
244:                private String getMessage(int mode) {
245:                    String message = "The file chooser accepts ";
246:                    switch (mode) {
247:                    case JFileChooser.FILES_ONLY:
248:                        message += "files only.";
249:                        break;
250:                    case JFileChooser.DIRECTORIES_ONLY:
251:                        message += "directories only.";
252:                        break;
253:                    case JFileChooser.FILES_AND_DIRECTORIES:
254:                        message += "both files and directories.";
255:                    }
256:                    return message;
257:                }
258:            }
259:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.