All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
org.nuiton.jaxx.widgets.gis.DmdCoordinateConverter Maven / Gradle / Ivy
package org.nuiton.jaxx.widgets.gis;
/*
* #%L
* JAXX :: Widgets
* %%
* Copyright (C) 2008 - 2024 Code Lutin, Ultreia.io
* %%
* 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 com.google.auto.service.AutoService;
import org.apache.commons.beanutils.ConversionException;
import org.apache.commons.lang3.StringUtils;
import org.nuiton.converter.NuitonConverter;
import org.nuiton.jaxx.runtime.resources.UIResourcesProvider;
import java.util.regex.Matcher;
import static io.ultreia.java4all.i18n.I18n.t;
/**
* Created on 11/25/13.
*
* @author Tony Chemit - [email protected]
* @since 2.12
*/
@AutoService(NuitonConverter.class)
public class DmdCoordinateConverter implements NuitonConverter {
protected boolean useSign;
protected boolean forLongitude;
protected String nullValue = "";
protected char fillChar = ' ';
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" : "";
}
public void setFillWithZero(boolean fillWithZero) {
fillChar = fillWithZero ? '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);
if (decimalesStr.length() == 1 && decimal != null && decimal > 0) {
decimal *= 10;
}
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, nullValue.equals(degreeStr) ? ' ' : fillChar),
StringUtils.leftPad(minuteStr, 2, nullValue.equals(minuteStr) ? ' ' : fillChar),
StringUtils.leftPad(decimalStr, 2, nullValue.equals(decimalStr) ? ' ' : fillChar));
}
}
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;
}
}