Clover Coverage Report - FEST Swing 1.2
Coverage timestamp: Tue Jun 1 2010 15:19:25 PDT
../../../../img/srcFileCovDistChart10.png 0% of files have more coverage
18   81   12   3
6   49   0.67   6
6     2  
1    
3.2% of code in this file is excluded from these metrics.
 
  JFileChooserSelectFileTask       Line # 39 18 3.2% 12 0 100% 1.0
 
No Tests
 
1    /*
2    * Created on Aug 8, 2008
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5    * in compliance with the License. You may obtain a copy of the License at
6    *
7    * http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software distributed under the License
10    * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11    * or implied. See the License for the specific language governing permissions and limitations under
12    * the License.
13    *
14    * Copyright @2008-2010 the original author or authors.
15    */
16    package org.fest.swing.driver;
17   
18    import static javax.swing.JFileChooser.DIRECTORIES_ONLY;
19    import static javax.swing.JFileChooser.FILES_ONLY;
20    import static org.fest.swing.driver.ComponentStateValidator.validateIsEnabledAndShowing;
21    import static org.fest.swing.edt.GuiActionRunner.execute;
22    import static org.fest.swing.format.Formatting.format;
23    import static org.fest.util.Strings.concat;
24   
25    import java.io.File;
26   
27    import javax.swing.JFileChooser;
28   
29    import org.fest.swing.annotation.RunsInCurrentThread;
30    import org.fest.swing.annotation.RunsInEDT;
31    import org.fest.swing.edt.GuiTask;
32   
33    /**
34    * Understands a task that selects a file in a <code>{@link JFileChooser}</code>. This task is executed in the event
35    * dispatch thread.
36    *
37    * @author Alex Ruiz
38    */
 
39    final class JFileChooserSelectFileTask {
40   
 
41  6 toggle @RunsInEDT
42    static void validateAndSelectFile(final JFileChooser fileChooser, final File file) {
43  6 execute(new GuiTask() {
 
44  6 toggle protected void executeInEDT() {
45  6 validateIsEnabledAndShowing(fileChooser);
46  4 validateFileToChoose(fileChooser, file);
47  2 fileChooser.setSelectedFile(file);
48    }
49    });
50    }
51   
 
52  6 toggle @RunsInEDT
53    static void validateAndSelectFiles(final JFileChooser fileChooser, final File[] files) {
54  6 execute(new GuiTask() {
 
55  6 toggle protected void executeInEDT() {
56  6 validateIsEnabledAndShowing(fileChooser);
57  5 if (files.length > 1 && !fileChooser.isMultiSelectionEnabled())
58  1 throw new IllegalStateException(
59    concat("Expecting file chooser ", format(fileChooser), " to handle multiple selection"));
60  6 for (File file : files) validateFileToChoose(fileChooser, file);
61  2 fileChooser.setSelectedFiles(files);
62    }
63    });
64    }
65   
 
66  10 toggle @RunsInCurrentThread
67    private static void validateFileToChoose(JFileChooser fileChooser, File file) {
68  10 int mode = fileChooser.getFileSelectionMode();
69  10 boolean isFolder = file.isDirectory();
70  10 if (mode == FILES_ONLY && isFolder)
71  2 throw cannotSelectFile(file, "the file chooser cannot open directories");
72  8 if (mode == DIRECTORIES_ONLY && !isFolder)
73  2 throw cannotSelectFile(file, "the file chooser can only open directories");
74    }
75   
 
76  4 toggle private static IllegalArgumentException cannotSelectFile(File file, String reason) {
77  4 return new IllegalArgumentException(concat("Unable to select file ", file, ": ", reason));
78    }
79   
 
80    toggle private JFileChooserSelectFileTask() {}
81    }