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
51   161   16   5.1
12   99   0.31   10
10     1.6  
1    
1.4% of code in this file is excluded from these metrics.
 
  Formatting       Line # 40 51 1.4% 16 0 100% 1.0
 
No Tests
 
1    /*
2    * Created on Sep 16, 2007
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 @2007-2010 the original author or authors.
15    */
16    package org.fest.swing.format;
17   
18    import static org.fest.swing.edt.GuiActionRunner.execute;
19    import static org.fest.util.Strings.*;
20   
21    import java.awt.*;
22    import java.util.concurrent.ConcurrentHashMap;
23    import java.util.concurrent.ConcurrentMap;
24    import java.util.logging.Logger;
25   
26    import javax.swing.*;
27    import javax.swing.text.JTextComponent;
28   
29    import org.fest.swing.annotation.RunsInCurrentThread;
30    import org.fest.swing.annotation.RunsInEDT;
31    import org.fest.swing.edt.GuiQuery;
32    import org.fest.util.VisibleForTesting;
33   
34    /**
35    * Understands utility methods related to formatting.
36    *
37    * @author Alex Ruiz
38    * @author Yvonne Wang
39    */
 
40    public class Formatting {
41   
42   
43    private static final String MAXIMUM = "maximum";
44   
45    private static final String MINIMUM = "minimum";
46   
47    private static final String NULL_COMPONENT_MESSAGE = "Null Component";
48   
49    private static final String ENABLED = "enabled";
50    private static final String NAME = "name";
51    private static final String SHOWING = "showing";
52    private static final String TEXT = "text";
53    private static final String TITLE = "title";
54    private static final String VALUE = "value";
55    private static final String VISIBLE = "visible";
56   
57    private static final ConcurrentMap<Class<?>, ComponentFormatter> FORMATTERS = new ConcurrentHashMap<Class<?>, ComponentFormatter>();
58   
59    private static Logger logger = Logger.getLogger(Formatting.class.getName());
60   
 
61  322 toggle static {
62  322 register(instrospect(AbstractButton.class, NAME, TEXT, "selected", ENABLED, VISIBLE, SHOWING));
63  322 register(instrospect(Dialog.class, NAME, TITLE, ENABLED, "modal", VISIBLE, SHOWING));
64  322 register(instrospect(Frame.class, NAME, TITLE, ENABLED, VISIBLE, SHOWING));
65  322 register(new JComboBoxFormatter());
66  322 register(instrospect(JButton.class, NAME, TEXT, ENABLED, VISIBLE, SHOWING));
67  322 register(new JFileChooserFormatter());
68  322 register(instrospect(JLabel.class, NAME, TEXT, ENABLED, VISIBLE, SHOWING));
69  322 register(empty(JLayeredPane.class));
70  322 register(new JListFormatter());
71  322 register(empty(JMenuBar.class));
72  322 register(new JOptionPaneFormatter());
73  322 register(nameOnly(JPanel.class));
74  322 register(instrospect(JPopupMenu.class, NAME, "label", ENABLED, VISIBLE, SHOWING));
75  322 register(instrospect(JProgressBar.class, NAME, VALUE, MINIMUM, MAXIMUM, "string", "stringPainted", ENABLED, VISIBLE, SHOWING));
76  322 register(empty(JRootPane.class));
77  322 register(instrospect(JScrollBar.class, NAME, VALUE, "blockIncrement", MINIMUM, MAXIMUM, ENABLED, VISIBLE, SHOWING));
78  322 register(instrospect(JScrollPane.class, NAME, ENABLED, VISIBLE, SHOWING));
79  322 register(instrospect(JSlider.class, NAME, VALUE, MINIMUM, MAXIMUM, ENABLED, VISIBLE, SHOWING));
80  322 register(instrospect(JSpinner.class, NAME, VALUE, ENABLED, VISIBLE, SHOWING));
81  322 register(new JTabbedPaneFormatter());
82  322 register(new JTableFormatter());
83  322 register(nameOnly(JToolBar.class));
84  322 register(instrospect(JPasswordField.class, NAME, ENABLED, VISIBLE, SHOWING));
85  322 register(instrospect(JTextComponent.class, NAME, TEXT, ENABLED, VISIBLE, SHOWING));
86  322 register(new JTreeFormatter());
87    }
88   
 
89  4186 toggle private static ComponentFormatter instrospect(Class<? extends Component> targetType, String...propertyNames) {
90  4186 return new IntrospectionComponentFormatter(targetType, propertyNames);
91    }
92   
 
93  966 toggle private static ComponentFormatter empty(Class<? extends Component> targetType) {
94  966 return new IntrospectionComponentFormatter(targetType);
95    }
96   
 
97  644 toggle private static ComponentFormatter nameOnly(Class<? extends Component> targetType) {
98  644 return new IntrospectionComponentFormatter(targetType, NAME);
99    }
100   
101    /**
102    * Registers the given formatter, replacing any other one previously registered for the same supported component type.
103    * @param formatter the formatter to register.
104    */
 
105  8052 toggle public static void register(ComponentFormatter formatter) {
106  8052 Class<?> key = formatter.targetType();
107  8052 ComponentFormatter previous = FORMATTERS.put(key, formatter);
108  8052 if (previous != null)
109  2 logger.info(
110    concat("Replaced formatter ", previous, " with ", formatter, " for the type ", key.getName()));
111    }
112   
 
113  9 toggle @VisibleForTesting
114    static ComponentFormatter formatter(Class<?> type) {
115  9 return FORMATTERS.get(type);
116    }
117   
118    /**
119    * Returns a <code>String</code> representation of the given <code>{@link Component}</code>. This method is invoked in
120    * the event dispatch thread.
121    * @param c the given <code>Component</code>.
122    * @return a <code>String</code> representation of the given <code>Component</code>.
123    */
 
124  1 toggle @RunsInEDT
125    public static String inEdtFormat(final Component c) {
126  1 return execute(new GuiQuery<String>() {
 
127  1 toggle protected String executeInEDT() {
128  1 return format(c);
129    }
130    });
131    }
132   
133    /**
134    * Returns a <code>String</code> representation of the given <code>{@link Component}</code>.
135    * <p>
136    * <b>Note:</b> This method is <b>not</b> guaranteed to be executed in the event dispatch thread (EDT.) Clients are
137    * responsible for calling this method from the EDT.
138    * </p>
139    * @param c the given <code>Component</code>.
140    * @return a <code>String</code> representation of the given <code>Component</code>.
141    */
 
142  43680 toggle @RunsInCurrentThread
143    public static String format(Component c) {
144  2 if (c == null) return NULL_COMPONENT_MESSAGE;
145  43678 ComponentFormatter formatter = formatterFor(c.getClass());
146  41059 if (formatter != null) return formatter.format(c);
147  2619 String name = c.getName();
148  2612 if (isEmpty(name)) return c.toString();
149  7 return concat(c.getClass().getName(), "[name=", quote(name), "]");
150    }
151   
 
152  71126 toggle private static ComponentFormatter formatterFor(Class<?> type) {
153  71126 ComponentFormatter formatter = FORMATTERS.get(type);
154  41059 if (formatter != null) return formatter;
155  30067 Class<?> superType = type.getSuperclass();
156  27448 if (superType != null) return formatterFor(superType);
157  2619 return null;
158    }
159   
 
160    toggle private Formatting() {}
161    }