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 java.lang.String.valueOf;
019    import static org.fest.swing.format.SwingIntEnums.SELECTION_MODES;
020    import static org.fest.util.Strings.concat;
021    import static org.fest.util.Strings.quote;
022    
023    import java.awt.Component;
024    import java.util.ArrayList;
025    import java.util.List;
026    
027    import javax.swing.JList;
028    import javax.swing.ListModel;
029    
030    import org.fest.util.Arrays;
031    
032    /**
033     * Understands a formatter for <code>{@link JList}</code>s.
034     *
035     * @author Yvonne Wang
036     */
037    public class JListFormatter extends ComponentFormatterTemplate {
038    
039      /**
040       * Returns the <code>String</code> representation of the given <code>{@link Component}</code>, which should be a
041       * <code>{@link JList}</code> (or subclass.)
042       * @param c the given <code>Component</code>.
043       * @return the <code>String</code> representation of the given <code>JList</code>.
044       */
045      protected String doFormat(Component c) {
046        JList list = (JList)c;
047        return concat(
048            list.getClass().getName(), "[",
049            "name=",           quote(list.getName()),                        ", ",
050            "selectedValues=", Arrays.format(list.getSelectedValues()),      ", ",
051            "contents=",       Arrays.format(contentsOf(list)),              ", ",
052            "selectionMode=",  SELECTION_MODES.get(list.getSelectionMode()), ", ",
053            "enabled=",        valueOf(list.isEnabled()),                    ", ",
054            "visible=",        valueOf(list.isVisible()),                    ", ",
055            "showing=",        valueOf(list.isShowing()),
056            "]"
057        );
058      }
059    
060      private Object[] contentsOf(JList list) {
061        List<Object> contents = new ArrayList<Object>();
062        ListModel model = list.getModel();
063        int size = model.getSize();
064        for (int i = 0; i < size; i++) contents.add(model.getElementAt(i));
065        return contents.toArray();
066      }
067    
068      /**
069       * Indicates that this formatter supports <code>{@link JList}</code> only.
070       * @return <code>JList.class</code>.
071       */
072      public Class<? extends Component> targetType() {
073        return JList.class;
074      }
075    }