org.nuiton.jaxx.widgets.gis.DmdCoordinateConverter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jaxx-widgets-gis Show documentation
Show all versions of jaxx-widgets-gis Show documentation
Gis widgets wrote with JAXX
package org.nuiton.jaxx.widgets.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.12
*/
public class DmdCoordinateConverter implements NuitonConverter {
protected boolean useSign;
protected boolean forLongitude;
protected String nullValue = "";
public void setUseSign(boolean useSign) {
this.useSign = useSign;
}
public void setForLongitude(boolean forLongitude) {
this.forLongitude = forLongitude;
}
public void setDisplayZeroWhenNull(boolean displayZeroWhenNull) {
this.nullValue = displayZeroWhenNull ? "0" : "";
}
@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(nullValue, forLongitude ? 3 : 2, ' '),
StringUtils.leftPad(nullValue, 2, ' '),
StringUtils.leftPad(nullValue, 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 ? nullValue : degree.toString();
Integer minute = coordinate.getMinute();
String minuteStr = minute == null ? nullValue : minute.toString();
Integer decimal = coordinate.getDecimal();
String decimalStr = decimal == null ? nullValue : 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;
}
}