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