001    /*
002     * Created on Dec 24, 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 java.lang.String.valueOf;
019    import static org.fest.util.Strings.concat;
020    import static org.fest.util.Strings.quote;
021    
022    import java.awt.Component;
023    
024    import javax.swing.JTabbedPane;
025    
026    import org.fest.util.Arrays;
027    
028    /**
029     * Understands a formatter for <code>{@link JTabbedPane}</code>s.
030     *
031     * @author Alex Ruiz
032     */
033    public class JTabbedPaneFormatter extends ComponentFormatterTemplate {
034    
035      private static final String NO_SELECTION = "<No selection>";
036      
037      /**
038       * Returns the <code>String</code> representation of the given <code>{@link Component}</code>, which should be a
039       * <code>{@link JTabbedPane}</code> (or subclass.)
040       * @param c the given <code>Component</code>.
041       * @return the <code>String</code> representation of the given <code>JTabbedPane</code>.
042       */
043      protected String doFormat(Component c) {
044        JTabbedPane tabbedPane = (JTabbedPane)c;
045        return concat(
046            tabbedPane.getClass().getName(), "[",
047            "name=",             quote(tabbedPane.getName()),            ", ",
048            "selectedTabIndex=", valueOf(tabbedPane.getSelectedIndex()), ", ",
049            "selectedTabTitle=", selectedTab(tabbedPane),                ", ",
050            "tabCount=",         valueOf(tabbedPane.getTabCount()),      ", ",
051            "tabTitles=",        Arrays.format(tabTitles(tabbedPane)),   ", ",
052            "enabled=",          valueOf(tabbedPane.isEnabled()),        ", ",
053            "visible=",          valueOf(tabbedPane.isVisible()),        ", ",
054            "showing=",          valueOf(tabbedPane.isShowing()),
055            "]"
056        );
057      }
058    
059      private String selectedTab(JTabbedPane tabbedPane) {
060        if (tabbedPane.getTabCount() == 0) return NO_SELECTION;
061        int index = tabbedPane.getSelectedIndex();
062        if (index == -1) return NO_SELECTION;
063        return quote(tabbedPane.getTitleAt(index));
064      }
065    
066      private String[] tabTitles(JTabbedPane tabbedPane) {
067        int count = tabbedPane.getTabCount();
068        if (count == 0) return new String[0];
069        String[] titles = new String[count];
070        for (int i = 0; i < count; i++) titles[i] = tabbedPane.getTitleAt(i);
071        return titles;
072      }
073    
074      /**
075       * Indicates that this formatter supports <code>{@link JTabbedPane}</code> only.
076       * @return <code>JTabbedPane.class</code>.
077       */
078      public Class<? extends Component> targetType() {
079        return JTabbedPane.class;
080      }
081    }