All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jaxx.runtime.swing.editor.gis.DmdCoordinateConverter Maven / Gradle / Ivy

There is a newer version: 3.0-alpha-1
Show newest version
package jaxx.runtime.swing.editor.gis;

/*
 * #%L
 * JAXX :: Widgets
 * %%
 * Copyright (C) 2008 - 2014 Code Lutin, Tony Chemit
 * %%
 * 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
 * .
 * #L%
 */

import org.apache.commons.beanutils.ConversionException;
import org.apache.commons.lang3.StringUtils;
import org.nuiton.converter.NuitonConverter;

import java.util.regex.Matcher;

import static org.nuiton.i18n.I18n.t;

/**
 * Created on 11/25/13.
 *
 * @author Tony Chemit - [email protected]
 * @since 2.6
 */
public class DmdCoordinateConverter implements NuitonConverter {

    protected boolean useSign;

    protected boolean forLongitude;

    public void setUseSign(boolean useSign) {
        this.useSign = useSign;
    }

    public void setForLongitude(boolean forLongitude) {
        this.forLongitude = forLongitude;
    }

    @Override
    public  T convert(Class aClass, Object value) {

        if (!isEnabled(aClass)) {
            throw new ConversionException(
                    t("jaxx.error.no.convertor.coordinateDmd", value));
        }

        Object result = null;

        if (value == null) {

            if (aClass.equals(String.class)) {

                result = String.format(
                        DmdCoordinate.COORDINATE_STRING_PATTERN,
                        useSign ? "-" : "",
                        StringUtils.leftPad("", forLongitude ? 3 : 2, ' '),
                        StringUtils.leftPad("", 2, ' '),
                        StringUtils.leftPad("", 2, ' '));
            }
        } else {

            if (aClass.equals(value.getClass())) {

                // same class, no convertion to do
                result = value;
            } else if (value instanceof String) {

                // String to Value

                Matcher matcher = DmdCoordinate.COORDINATE_PATTERN.matcher((String) value);

                if (matcher.matches()) {

                    String degresStr = matcher.group(1).replaceAll("\\s", "");
                    String minutesStr = matcher.group(2).replaceAll("\\s", "");
                    String decimalesStr = matcher.group(3).replaceAll("\\s", "");

                    Integer degre = degresStr.isEmpty() || "-".equals(degresStr) ? null : Math.abs(Integer.valueOf(degresStr));
                    Integer minutes = minutesStr.isEmpty() ? null : Integer.valueOf(minutesStr);
                    Integer decimal = decimalesStr.isEmpty() ? null : Integer.valueOf(decimalesStr);

                    boolean signed = degresStr.contains("-");
                    result = DmdCoordinate.valueOf(signed,
                                                   degre,
                                                   minutes,
                                                   decimal);
                }

            } else if (value instanceof DmdCoordinate) {

                // Value to String

                DmdCoordinate coordinate = (DmdCoordinate) value;

                boolean sign = coordinate.isSign();
                String signStr = sign ? "-" : "";

                Integer degree = coordinate.getDegree();
                String degreeStr = degree == null ? "" : degree.toString();

                Integer minute = coordinate.getMinute();
                String minuteStr = minute == null ? "" : minute.toString();

                Integer decimal = coordinate.getDecimal();
                String decimalStr = decimal == null ? "" : decimal.toString();

                result = String.format(
                        DmdCoordinate.COORDINATE_STRING_PATTERN,
                        signStr,
                        StringUtils.leftPad(degreeStr, forLongitude ? 3 : 2, ' '),
                        StringUtils.leftPad(minuteStr, 2, ' '),
                        StringUtils.leftPad(decimalStr, 2, ' '));
            }
        }
        return aClass.cast(result);
    }

    protected boolean isEnabled(Class aClass) {
        return String.class.isAssignableFrom(aClass) ||
               DmdCoordinate.class.isAssignableFrom(aClass);
    }

    @Override
    public Class getType() {
        return DmdCoordinate.class;
    }

}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy