Author: sletellier Date: 2012-07-06 12:07:04 +0200 (Fri, 06 Jul 2012) New Revision: 2365 Url: http://nuiton.org/repositories/revision/nuiton-utils/2365 Log: - fixes #2174 : Enable using KeyStroke as config option. Added: trunk/nuiton-utils/src/main/java/org/nuiton/util/converter/KeyStrokeConverter.java Modified: trunk/nuiton-utils/src/main/java/org/nuiton/util/ApplicationConfig.java Modified: trunk/nuiton-utils/src/main/java/org/nuiton/util/ApplicationConfig.java =================================================================== --- trunk/nuiton-utils/src/main/java/org/nuiton/util/ApplicationConfig.java 2012-06-11 13:22:52 UTC (rev 2364) +++ trunk/nuiton-utils/src/main/java/org/nuiton/util/ApplicationConfig.java 2012-07-06 10:07:04 UTC (rev 2365) @@ -26,13 +26,6 @@ package org.nuiton.util; -import org.apache.commons.beanutils.ConstructorUtils; -import org.apache.commons.collections.EnumerationUtils; -import org.apache.commons.lang3.ArrayUtils; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.nuiton.util.converter.ConverterUtil; - import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; import java.beans.PropertyChangeSupport; @@ -71,6 +64,13 @@ import java.util.Map; import java.util.Properties; import java.util.Set; +import javax.swing.KeyStroke; +import org.apache.commons.beanutils.ConstructorUtils; +import org.apache.commons.collections.EnumerationUtils; +import org.apache.commons.lang3.ArrayUtils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.nuiton.util.converter.ConverterUtil; import static org.nuiton.i18n.I18n._; @@ -1602,7 +1602,19 @@ return result; } + /** + * Get option value as {@link KeyStroke}. + * + * @param key the option's key + * @return value as {@link KeyStroke}. + * @since 2.5.1 + */ + public KeyStroke getOptionAsKeyStroke(String key) { + KeyStroke result = getOption(KeyStroke.class, key); + return result; + } + /** * Get all options from configuration. * Added: trunk/nuiton-utils/src/main/java/org/nuiton/util/converter/KeyStrokeConverter.java =================================================================== --- trunk/nuiton-utils/src/main/java/org/nuiton/util/converter/KeyStrokeConverter.java (rev 0) +++ trunk/nuiton-utils/src/main/java/org/nuiton/util/converter/KeyStrokeConverter.java 2012-07-06 10:07:04 UTC (rev 2365) @@ -0,0 +1,74 @@ +/* + * #%L + * Nuiton Utils :: Nuiton Utils + * $Id:$ + * $HeadURL:$ + * %% + * Copyright (C) 2004 - 2012 CodeLutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ +package org.nuiton.util.converter; + +import javax.swing.KeyStroke; +import org.apache.commons.beanutils.ConversionException; +import org.apache.commons.beanutils.Converter; +import org.apache.commons.logging.Log; + +import static org.apache.commons.logging.LogFactory.getLog; +import static org.nuiton.i18n.I18n._; + +/** + * Used to convert a {@link String} to {@link KeyStroke}. + * + * @author sletellier <letellier@codelutin.com> + * @since 2.5.1 + */ +public class KeyStrokeConverter implements Converter { + + + /** Logger. */ + static Log log = getLog(KeyStrokeConverter.class); + + @Override + public Object convert(Class aClass, Object value) { + if (value == null) { + throw new ConversionException( + _("nuitonutil.error.convertor.noValue", this)); + } + if (isEnabled(aClass)) { + Object result; + if (isEnabled(value.getClass())) { + result = value; + return result; + } + if (value instanceof String) { + result = KeyStroke.getKeyStroke((String) value); + return result; + } + } + throw new ConversionException( + _("nuitonutil.error.no.convertor", aClass.getName(), value)); + } + + protected boolean isEnabled(Class<?> aClass) { + return KeyStroke.class.equals(aClass); + } + + public Class<?> getType() { + return KeyStroke.class; + } +}