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
20   129   11   3.33
10   47   0.55   6
6     1.83  
1    
 
  LabelMatcher       Line # 36 20 0% 11 0 100% 1.0
 
No Tests
 
1    /*
2    * Created on Dec 2, 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.core;
17   
18    import static java.lang.String.valueOf;
19    import static org.fest.util.Objects.areEqual;
20    import static org.fest.util.Strings.*;
21   
22    import java.awt.Component;
23   
24    import javax.swing.JLabel;
25   
26    import org.fest.swing.annotation.RunsInCurrentThread;
27   
28    /**
29    * Understands <code>{@link java.awt.Component}</code> matching by the text of the associated
30    * <code>{@link JLabel}</code> and (optionally) by type.
31    * @see JLabel#getLabelFor()
32    * @see JLabel#setLabelFor(Component)
33    *
34    * @author Alex Ruiz
35    */
 
36    public class LabelMatcher extends AbstractComponentMatcher {
37   
38    private final String label;
39    private final Class<? extends Component> type;
40   
41    /**
42    * Creates a new <code>{@link LabelMatcher}</code>. The component to match does not have to be showing.
43    * @param label the text of the label associated to the component we are looking for.
44    * @throws NullPointerException if the given label is <code>null</code>.
45    * @throws IllegalArgumentException if the given label is empty.
46    */
 
47  6 toggle public LabelMatcher(String label) {
48  6 this(label, false);
49    }
50   
51    /**
52    * Creates a new <code>{@link LabelMatcher}</code>.
53    * @param label the text of the label associated to the component we are looking for.
54    * @param requireShowing indicates if the component to match should be showing or not.
55    * @throws NullPointerException if the given label is <code>null</code>.
56    * @throws IllegalArgumentException if the given label is empty.
57    */
 
58  13 toggle public LabelMatcher(String label, boolean requireShowing) {
59  13 this(label, Component.class, requireShowing);
60    }
61   
62    /**
63    * Creates a new <code>{@link LabelMatcher}</code>. The component to match does not have to be showing.
64    * @param label the text of the label associated to the component we are looking for.
65    * @param type the type of the component we are looking for.
66    * @throws NullPointerException if the given label is <code>null</code>.
67    * @throws IllegalArgumentException if the given label is empty.
68    * @throws NullPointerException if the given type is <code>null</code>.
69    */
 
70  1 toggle public LabelMatcher(String label, Class<? extends Component> type) {
71  1 this(label, type, false);
72    }
73   
74    /**
75    * Creates a new <code>{@link LabelMatcher}</code>.
76    * @param label the text of the label associated to the component we are looking for.
77    * @param type the type of the component we are looking for.
78    * @param requireShowing indicates if the component to match should be showing or not.
79    * @throws NullPointerException if the given label is <code>null</code>.
80    * @throws IllegalArgumentException if the given label is empty.
81    * @throws NullPointerException if the given type is <code>null</code>.
82    */
 
83  25 toggle public LabelMatcher(String label, Class<? extends Component> type, boolean requireShowing) {
84  25 super(requireShowing);
85  25 if (label == null)
86  1 throw new NullPointerException("The text of the label associated to the component to find should not be null");
87  24 if (isEmpty(label))
88  1 throw new IllegalArgumentException("The text of the label associated to the component to find should not be empty");
89  1 if (type == null) throw new NullPointerException("The type of component to find should not be null");
90  22 this.label = label;
91  22 this.type = type;
92    }
93   
94    /**
95    * Indicates whether the given <code>{@link java.awt.Component}</code> matches the criteria specified in this
96    * matcher:
97    * <ol>
98    * <li>the text of the <code>{@link JLabel}</code></li> attached to the component to look for matches the text
99    * specified in this matcher
100    * <li>the component to look for is of the type specified in this matcher (if specified)</li>
101    * <li>visibility of the given <code>{@link java.awt.Component}</code> matches the value specified in this matcher
102    * </li>
103    * </ol>
104    * <p>
105    * <b>Note:</b> This method is <b>not</b> guaranteed to be executed in the event dispatch thread (EDT.) Clients are
106    * responsible for calling this method from the EDT.
107    * </p>
108    * @return <code>true</code> if the name and visibility of the given <code>Component</code> matches the values
109    * specified in this matcher, <code>false</code> otherwise.
110    */
 
111  96 toggle @RunsInCurrentThread
112    public boolean matches(Component c) {
113  76 if (!(c instanceof JLabel)) return false;
114  20 JLabel labelForComponent = (JLabel)c;
115  10 if (!areEqual(labelForComponent.getText(), label)) return false;
116  10 Component labeled = labelForComponent.getLabelFor();
117  10 return type.isInstance(labeled) && requireShowingMatches(labeled);
118    }
119   
 
120  6 toggle @Override public String toString() {
121  6 return concat(
122    getClass().getName(), "[",
123    "label=", quote(label), ", ",
124    "type=", type.getName(), ", ",
125    "requireShowing=", valueOf(requireShowing()),
126    "]"
127    );
128    }
129    }