001 /*
002 * Created on Dec 23, 2007
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005 * in compliance with the License. You may obtain a copy of the License at
006 *
007 * http://www.apache.org/licenses/LICENSE-2.0
008 *
009 * Unless required by applicable law or agreed to in writing, software distributed under the License
010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
011 * or implied. See the License for the specific language governing permissions and limitations under
012 * the License.
013 *
014 * Copyright @2007-2010 the original author or authors.
015 */
016 package org.fest.swing.format;
017
018 import static org.fest.util.Strings.concat;
019
020 import java.awt.Component;
021
022 /**
023 * Understands a template for implementations of <code>{@link ComponentFormatter}</code>.
024 *
025 * @author Yvonne Wang
026 */
027 public abstract class ComponentFormatterTemplate implements ComponentFormatter {
028
029 /**
030 * Returns the <code>String</code> representation of the given <code>{@link Component}</code>.
031 * @param c the given <code>Component</code>.
032 * @return the <code>String</code> representation of the given <code>Component</code>.
033 * @throws NullPointerException if the given <code>Component</code> is <code>null</code>.
034 * @throws IllegalArgumentException if the type of the given <code>Component</code> is not supported by this
035 * formatter.
036 */
037 public final String format(Component c) {
038 validateTypeOf(c);
039 return doFormat(c);
040 }
041
042 /**
043 * Returns the <code>String</code> representation of the given <code>{@link Component}</code>.
044 * @param c the given <code>Component</code>.
045 * @return the <code>String</code> representation of the given <code>Component</code>.
046 */
047 protected abstract String doFormat(Component c);
048
049 private void validateTypeOf(Component c) {
050 if (c == null) throw new NullPointerException("The component should not be null");
051 if (!targetType().isAssignableFrom(c.getClass()))
052 throw new IllegalArgumentException(concat("This formatter only supports components of type ", targetType().getName()));
053 }
054
055
056 }