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 javax.swing.JOptionPane.*;
020 import static org.fest.util.Strings.concat;
021 import static org.fest.util.Strings.quote;
022
023 import java.awt.Component;
024
025 import javax.swing.JOptionPane;
026
027 /**
028 * Understands a formatter for <code>{@link JOptionPane}</code>s.
029 *
030 * @author Alex Ruiz
031 */
032 public class JOptionPaneFormatter extends ComponentFormatterTemplate {
033
034 private static final IntEnum MESSAGE_TYPES = new IntEnum();
035 static {
036 MESSAGE_TYPES.put(ERROR_MESSAGE, "ERROR_MESSAGE")
037 .put(INFORMATION_MESSAGE, "INFORMATION_MESSAGE")
038 .put(WARNING_MESSAGE, "WARNING_MESSAGE")
039 .put(QUESTION_MESSAGE, "QUESTION_MESSAGE")
040 .put(PLAIN_MESSAGE, "PLAIN_MESSAGE");
041 }
042
043 private static final IntEnum OPTION_TYPES = new IntEnum();
044 static {
045 OPTION_TYPES.put(DEFAULT_OPTION, "DEFAULT_OPTION")
046 .put(YES_NO_OPTION, "YES_NO_OPTION")
047 .put(YES_NO_CANCEL_OPTION, "YES_NO_CANCEL_OPTION")
048 .put(QUESTION_MESSAGE, "QUESTION_MESSAGE")
049 .put(OK_CANCEL_OPTION, "OK_CANCEL_OPTION");
050 }
051
052 /**
053 * Returns the <code>String</code> representation of the given <code>{@link Component}</code>, which should be a
054 * <code>{@link JOptionPane}</code> (or subclass.)
055 * @param c the given <code>Component</code>.
056 * @return the <code>String</code> representation of the given <code>JOptionPane</code>.
057 */
058 protected String doFormat(Component c) {
059 JOptionPane optionPane = (JOptionPane)c;
060 return concat(
061 optionPane.getClass().getName(), "[",
062 "message=", quote(optionPane.getMessage()), ", ",
063 "messageType=", MESSAGE_TYPES.get(optionPane.getMessageType()), ", ",
064 "optionType=", OPTION_TYPES.get(optionPane.getOptionType()), ", ",
065 "enabled=", valueOf(optionPane.isEnabled()), ", ",
066 "visible=", valueOf(optionPane.isVisible()), ", ",
067 "showing=", valueOf(optionPane.isShowing()),
068 "]"
069 );
070 }
071
072 /**
073 * Indicates that this formatter supports <code>{@link JOptionPane}</code> only.
074 * @return <code>JOptionPane.class</code>.
075 */
076 public Class<? extends Component> targetType() {
077 return JOptionPane.class;
078 }
079 }