Author: tchemit Date: 2013-11-25 16:54:51 +0100 (Mon, 25 Nov 2013) New Revision: 2755 Url: http://nuiton.org/projects/jaxx/repository/revisions/2755 Log: refs #2929: Introduce some coordinate editors (still need some improvements) Added: trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/gis/DmdCoordinateConverter.java trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/gis/DmsCoordinateConverter.java trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/gis/MaskFormatterFromConverter.java trunk/jaxx-widgets/src/main/resources/META-INF/ trunk/jaxx-widgets/src/main/resources/META-INF/services/ trunk/jaxx-widgets/src/main/resources/META-INF/services/org.apache.commons.beanutils.Converter trunk/jaxx-widgets/src/test/java/jaxx/runtime/swing/editor/gis/DmdCoordinateConverterTest.java trunk/jaxx-widgets/src/test/java/jaxx/runtime/swing/editor/gis/DmsCoordinateConverterTest.java trunk/jaxx-widgets/src/test/java/jaxx/runtime/swing/editor/gis/DmsCoordinateTest.java Modified: trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/gis/DmdCoordinate.java trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/gis/DmdCoordinateEditorHandler.java trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/gis/DmdCoordinateEditorModel.java trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/gis/DmsCoordinate.java trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/gis/DmsCoordinateEditorHandler.java trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/gis/DmsCoordinateEditorModel.java trunk/jaxx-widgets/src/main/resources/i18n/jaxx-widgets_en_GB.properties trunk/jaxx-widgets/src/main/resources/i18n/jaxx-widgets_es_ES.properties trunk/jaxx-widgets/src/main/resources/i18n/jaxx-widgets_fr_FR.properties trunk/jaxx-widgets/src/test/java/jaxx/runtime/swing/editor/gis/DmdCoordinateTest.java Modified: trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/gis/DmdCoordinate.java =================================================================== --- trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/gis/DmdCoordinate.java 2013-11-24 21:52:35 UTC (rev 2754) +++ trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/gis/DmdCoordinate.java 2013-11-25 15:54:51 UTC (rev 2755) @@ -26,6 +26,8 @@ import org.jdesktop.beans.AbstractSerializableBean; +import java.util.regex.Pattern; + /** * Geo coordinate in degree decimal, minute format. * <p/> @@ -38,7 +40,11 @@ private static final long serialVersionUID = 1L; + public static final String COORDINATE_STRING_PATTERN = "%s%s°%s'%s"; + public static final Pattern COORDINATE_PATTERN = + Pattern.compile("(.*)°(.*)'(.*)"); + public static final String PROPERTY_SIGN = "sign"; public static final String PROPERTY_DEGREE = "degree"; @@ -191,6 +197,21 @@ if (d == 0) { d = null; } + + if (d != null || m != null || dc != null) { + + // fill with 0 other null values + // if one value is not null, then put zero everywhere + if (d == null) { + d = 0; + } + if (m == null) { + m = 0; + } + if (dc == null) { + dc = 0; + } + } } degree = d; Added: trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/gis/DmdCoordinateConverter.java =================================================================== --- trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/gis/DmdCoordinateConverter.java (rev 0) +++ trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/gis/DmdCoordinateConverter.java 2013-11-25 15:54:51 UTC (rev 2755) @@ -0,0 +1,139 @@ +package jaxx.runtime.swing.editor.gis; + +/* + * #%L + * JAXX :: Widgets + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2008 - 2013 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% + */ + +import org.apache.commons.beanutils.ConversionException; +import org.apache.commons.beanutils.Converter; +import org.apache.commons.lang3.StringUtils; + +import java.util.regex.Matcher; + +import static org.nuiton.i18n.I18n._; + +/** + * Created on 11/25/13. + * + * @author Tony Chemit <chemit@codelutin.com> + * @since 2.6 + */ +public class DmdCoordinateConverter implements Converter { + + protected boolean useSign; + + protected boolean forLongitude; + + public boolean isUseSign() { + return useSign; + } + + public void setUseSign(boolean useSign) { + this.useSign = useSign; + } + + public void setForLongitude(boolean forLongitude) { + this.forLongitude = forLongitude; + } + + @Override + public Object convert(Class aClass, Object value) { + + Object result = null; + + if (value != null) { + + if (!isEnabled(aClass)) { + throw new ConversionException( + _("jaxx.error.no.convertor.coordinateDmd", value)); + } + + 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) ? 0 : Integer.valueOf(degresStr); + Integer minutes = minutesStr.isEmpty() ? 0 : Integer.valueOf(minutesStr); + Integer decimal = decimalesStr.isEmpty() ? 0 : Integer.valueOf(decimalesStr); + + boolean signed = degresStr.contains("-"); + result = + DmdCoordinate.valueOf(signed, + Math.abs(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 result; + } + + protected boolean isEnabled(Class<?> aClass) { + return String.class.isAssignableFrom(aClass) || + DmdCoordinate.class.isAssignableFrom(aClass); + } + + public Class<?> getType() { + return DmdCoordinate.class; + } + +} + Property changes on: trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/gis/DmdCoordinateConverter.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Modified: trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/gis/DmdCoordinateEditorHandler.java =================================================================== --- trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/gis/DmdCoordinateEditorHandler.java 2013-11-24 21:52:35 UTC (rev 2754) +++ trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/gis/DmdCoordinateEditorHandler.java 2013-11-25 15:54:51 UTC (rev 2755) @@ -26,7 +26,6 @@ import com.google.common.base.Preconditions; import jaxx.runtime.swing.editor.bean.BeanUIUtil; -import org.apache.commons.lang3.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -38,8 +37,6 @@ import java.beans.PropertyChangeListener; import java.lang.reflect.Method; import java.text.ParseException; -import java.util.regex.Matcher; -import java.util.regex.Pattern; /** * Created on 10/16/13. @@ -53,8 +50,6 @@ private static final Log log = LogFactory.getLog(DmdCoordinateEditorHandler.class); - protected static final Pattern VALUE_PATTERN = Pattern.compile("(.*)°(.*)'(.*)"); - private final DmdCoordinateEditor ui; protected Method signMutator; @@ -65,16 +60,17 @@ protected Method decimalMutator; - protected DefaultFormatterFactory unsignedFactory; - - protected DefaultFormatterFactory signedFactory; - protected boolean valueIsAdjusting; protected boolean valueModelIsAdjusting; + private final DmdCoordinateConverter converter; + public DmdCoordinateEditorHandler(DmdCoordinateEditor ui) { this.ui = ui; + + // can't use the one from ConverterUtil since we deal with some internal states + this.converter = new DmdCoordinateConverter(); } public void init() { @@ -87,6 +83,8 @@ Preconditions.checkNotNull(model.getPropertyMinute(), "could not find propertyMinute in " + ui); Preconditions.checkNotNull(model.getPropertyDecimal(), "could not find propertyDecimal in " + ui); + converter.setForLongitude(model.isLongitudeEditor()); + Object bean = model.getBean(); signMutator = BeanUIUtil.getMutator(bean, model.getPropertySign()); Preconditions.checkNotNull(signMutator, "could not find mutator for " + model.getPropertySign()); @@ -100,68 +98,48 @@ decimalMutator = BeanUIUtil.getMutator(bean, model.getPropertyDecimal()); Preconditions.checkNotNull(decimalMutator, "could not find mutator for " + model.getPropertyDecimal()); - MaskFormatter unsignedFormatter; + MaskFormatter maskFormatter; try { String pattern = model.getMaskFormatterPattern(); - unsignedFormatter = new MaskFormatter(pattern); - unsignedFormatter.setValidCharacters(" 01234567890"); + maskFormatter = new MaskFormatterFromConverter<DmdCoordinate>( + DmdCoordinate.class, pattern, converter); + maskFormatter.setValidCharacters(" 01234567890"); } catch (ParseException e) { // can't happen here throw new RuntimeException(e); } - unsignedFactory = new DefaultFormatterFactory(unsignedFormatter); - MaskFormatter signedFormatter; - try { + DefaultFormatterFactory formatterFactory = + new DefaultFormatterFactory(maskFormatter); - String pattern = model.getMaskFormatterPattern(); - signedFormatter = new MaskFormatter("-" + pattern); - signedFormatter.setValidCharacters(" 01234567890"); - } catch (ParseException e) { - // can't happen here - throw new RuntimeException(e); - } - signedFactory = new DefaultFormatterFactory(signedFormatter); - JFormattedTextField editor = ui.getEditor(); - editor.setFormatterFactory(unsignedFactory); + editor.setFormatterFactory(formatterFactory); editor.setFocusLostBehavior(JFormattedTextField.COMMIT); // When editor changes his value, propagate it to model editor.addPropertyChangeListener("value", new PropertyChangeListener() { @Override public void propertyChange(PropertyChangeEvent evt) { - String newValue = (String) evt.getNewValue(); + DmdCoordinate newValue = (DmdCoordinate) evt.getNewValue(); if (log.isDebugEnabled()) { log.debug("Value has changed: " + newValue); } - DmdCoordinate value = null; - if (newValue != null) { + model.setValue(newValue); + } + }); - Matcher matcher = VALUE_PATTERN.matcher(newValue); - - 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) ? 0 : Integer.valueOf(degresStr); - Integer minutes = minutesStr.isEmpty() ? 0 : Integer.valueOf(minutesStr); - Integer decimal = decimalesStr.isEmpty() ? 0 : Integer.valueOf(decimalesStr); - - boolean signed = degre < 0; - value = - DmdCoordinate.valueOf(signed, - Math.abs(degre), - minutes, - decimal); + // When longitudeEditor property changed, propagate to the converter + model.addPropertyChangeListener( + DmdCoordinateEditorModel.PROPERTY_LONGITUDE_EDITOR, + new PropertyChangeListener() { + @Override + public void propertyChange(PropertyChangeEvent evt) { + Boolean newValue = (Boolean) evt.getNewValue(); + converter.setForLongitude(newValue != null && newValue); } } - model.setValue(value); - } - }); + ); // When model sign changed, let's push it back in bean model.addPropertyChangeListener( @@ -191,40 +169,10 @@ return; } - String signStr = ""; - String degreeStr = ""; - String minuteStr = ""; - String decimalStr = ""; - - if (value != null) { - - boolean sign = value.isSign(); - signStr = sign ? "-" : ""; - - Integer degree = value.getDegree(); - degreeStr = degree == null ? "" : degree.toString(); - - Integer minute = value.getMinute(); - minuteStr = minute == null ? "" : minute.toString(); - - Integer decimal = value.getDecimal(); - decimalStr = decimal == null ? "" : decimal.toString(); - } - - DmdCoordinateEditorModel model = ui.getModel(); - - String stringPattern = model.getStringPattern(); - String valueStr = String.format( - stringPattern, - signStr, - StringUtils.leftPad(degreeStr, model.isLongitudeEditor() ? 3 : 2, ' '), - StringUtils.leftPad(minuteStr, 2, ' '), - StringUtils.leftPad(decimalStr, 2, ' ')); - valueIsAdjusting = !pushToModel; try { - ui.getEditor().setValue(valueStr); + ui.getEditor().setValue(value); } finally { valueIsAdjusting = false; } @@ -235,7 +183,7 @@ setValue(null, true); // use back unsigned format - ui.getEditor().setFormatterFactory(unsignedFactory); + converter.setUseSign(false); } public void onKeyReleased(KeyEvent e) { @@ -253,71 +201,44 @@ if (keyChar == '-') { // try to switch unsigned to signed + boolean useSign = converter.isUseSign(); - if (unsignedFactory == source.getFormatterFactory()) { + if (useSign) { - // switch to signed if (log.isDebugEnabled()) { - log.debug("Switch to signed"); + log.debug("Switch to unsigned"); } - newFactory = signedFactory; + converter.setUseSign(false); - try { - source.commitEdit(); - } catch (ParseException e1) { - // ignore it - } + source.setText(source.getText().substring(1).trim()); - newValue = "-" + source.getValue(); - + // remove a sign + caretPosition--; } else { - // switch to unsigned - + // switch to signed if (log.isDebugEnabled()) { - log.debug("Switch to unsigned"); + log.debug("Switch to signed"); } - newFactory = unsignedFactory; + converter.setUseSign(true); - try { - source.commitEdit(); - } catch (ParseException e1) { - // ignore it - } + source.setText("-" + source.getText()); - newValue = ((String) source.getValue()).substring(1); + // add a sign + caretPosition++; } - } else { + try { source.commitEdit(); } catch (ParseException e1) { // ignore it + if (log.isErrorEnabled()) { + log.error("Could not commit edit of " + source.getText(), e1); + } } if (log.isDebugEnabled()) { log.debug("Key pressed: newValue " + source.getValue()); } - } - - if (newFactory != null) { - - // consume event (prevent any side-effects) e.consume(); - - if (log.isDebugEnabled()) { - log.debug("Key pressed: newValue " + newValue); - } - source.setFormatterFactory(newFactory); - source.setValue(newValue); - - if (unsignedFactory == newFactory) { - - // remove a sign - caretPosition--; - } else { - - // add a sign - caretPosition++; - } - source.setCaretPosition(caretPosition); } } Modified: trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/gis/DmdCoordinateEditorModel.java =================================================================== --- trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/gis/DmdCoordinateEditorModel.java 2013-11-24 21:52:35 UTC (rev 2754) +++ trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/gis/DmdCoordinateEditorModel.java 2013-11-25 15:54:51 UTC (rev 2755) @@ -145,7 +145,7 @@ } public String getStringPattern() { - String pattern = "%s%s°%s'%s"; + String pattern = COORDINATE_STRING_PATTERN; return pattern; } } Modified: trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/gis/DmsCoordinate.java =================================================================== --- trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/gis/DmsCoordinate.java 2013-11-24 21:52:35 UTC (rev 2754) +++ trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/gis/DmsCoordinate.java 2013-11-25 15:54:51 UTC (rev 2755) @@ -26,6 +26,8 @@ import org.jdesktop.beans.AbstractSerializableBean; +import java.util.regex.Pattern; + /** * Geo coordinate in degree, minute, second format. * <p/> @@ -36,6 +38,11 @@ */ public class DmsCoordinate extends AbstractSerializableBean { + public static final String COORDINATE_STRING_PATTERN = "%s%s°%s'%s''"; + + public static final Pattern COORDINATE_PATTERN = + Pattern.compile("(.*)°(.*)'(.*)''"); + private static final long serialVersionUID = 1L; public static final String PROPERTY_SIGN = "sign"; @@ -205,11 +212,26 @@ if (s == 0) { s = null; } + + if (d != null || m != null || s != null) { + + // if one value is not null, then put zero everywhere + if (d == null) { + d = 0; + } + if (m == null) { + m = 0; + } + if (s == null) { + s = 0; + } + } } - if (d != null && d == 0) { - d = null; - } +// if (d != null && d == 0) { +// d = null; +// } + degree = d; minute = m; second = s; Added: trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/gis/DmsCoordinateConverter.java =================================================================== --- trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/gis/DmsCoordinateConverter.java (rev 0) +++ trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/gis/DmsCoordinateConverter.java 2013-11-25 15:54:51 UTC (rev 2755) @@ -0,0 +1,138 @@ +package jaxx.runtime.swing.editor.gis; + +/* + * #%L + * JAXX :: Widgets + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2008 - 2013 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% + */ + +import org.apache.commons.beanutils.ConversionException; +import org.apache.commons.beanutils.Converter; +import org.apache.commons.lang3.StringUtils; + +import java.util.regex.Matcher; + +import static org.nuiton.i18n.I18n._; + +/** + * Created on 11/25/13. + * + * @author Tony Chemit <chemit@codelutin.com> + * @since 2.6 + */ +public class DmsCoordinateConverter implements Converter { + + protected boolean useSign; + + protected boolean forLongitude; + + public boolean isUseSign() { + return useSign; + } + + public void setUseSign(boolean useSign) { + this.useSign = useSign; + } + + public void setForLongitude(boolean forLongitude) { + this.forLongitude = forLongitude; + } + + @Override + public Object convert(Class aClass, Object value) { + + Object result = null; + + if (value != null) { + + if (!isEnabled(aClass)) { + throw new ConversionException( + _("jaxx.error.no.convertor.coordinateDmd", value)); + } + + if (aClass.equals(value.getClass())) { + + // same class, no convertion to do + result = value; + } else if (value instanceof String) { + + // String to Value + + Matcher matcher = DmsCoordinate.COORDINATE_PATTERN.matcher((String) value); + + if (matcher.matches()) { + + String degresStr = matcher.group(1).replaceAll("\\s", ""); + String minutesStr = matcher.group(2).replaceAll("\\s", ""); + String secondsStr = matcher.group(3).replaceAll("\\s", ""); + + Integer degre = degresStr.isEmpty() || "-".equals(degresStr) ? 0 : Integer.valueOf(degresStr); + Integer minutes = minutesStr.isEmpty() ? 0 : Integer.valueOf(minutesStr); + Integer seconds = secondsStr.isEmpty() ? 0 : Integer.valueOf(secondsStr); + + boolean signed = degresStr.contains("-"); + result = DmsCoordinate.valueOf(signed, + Math.abs(degre), + minutes, + seconds); + } + + } else if (value instanceof DmsCoordinate) { + + // Value to String + + DmsCoordinate coordinate = (DmsCoordinate) 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 second = coordinate.getSecond(); + String secondStr = second == null ? "" : second.toString(); + + result = String.format( + DmsCoordinate.COORDINATE_STRING_PATTERN, + signStr, + StringUtils.leftPad(degreeStr, forLongitude ? 3 : 2, ' '), + StringUtils.leftPad(minuteStr, 2, ' '), + StringUtils.leftPad(secondStr, 2, ' ')); + } + } + return result; + + } + + protected boolean isEnabled(Class<?> aClass) { + return String.class.isAssignableFrom(aClass) || + DmsCoordinate.class.isAssignableFrom(aClass); + } + + public Class<?> getType() { + return DmsCoordinate.class; + } + +} + Property changes on: trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/gis/DmsCoordinateConverter.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Modified: trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/gis/DmsCoordinateEditorHandler.java =================================================================== --- trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/gis/DmsCoordinateEditorHandler.java 2013-11-24 21:52:35 UTC (rev 2754) +++ trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/gis/DmsCoordinateEditorHandler.java 2013-11-25 15:54:51 UTC (rev 2755) @@ -26,7 +26,6 @@ import com.google.common.base.Preconditions; import jaxx.runtime.swing.editor.bean.BeanUIUtil; -import org.apache.commons.lang3.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -38,8 +37,6 @@ import java.beans.PropertyChangeListener; import java.lang.reflect.Method; import java.text.ParseException; -import java.util.regex.Matcher; -import java.util.regex.Pattern; /** * Created on 10/16/13. @@ -53,9 +50,6 @@ private static final Log log = LogFactory.getLog(DmsCoordinateEditorHandler.class); - protected static final Pattern VALUE_PATTERN = - Pattern.compile("(.*)°(.*)'(.*)''"); - private final DmsCoordinateEditor ui; protected Method signMutator; @@ -66,16 +60,17 @@ protected Method secondMutator; - protected DefaultFormatterFactory unsignedFactory; - - protected DefaultFormatterFactory signedFactory; - protected boolean valueIsAdjusting; protected boolean valueModelIsAdjusting; + private final DmsCoordinateConverter converter; + public DmsCoordinateEditorHandler(DmsCoordinateEditor ui) { this.ui = ui; + + // can't use the one from ConverterUtil since we deal with some internal states + this.converter = new DmsCoordinateConverter(); } public void init() { @@ -101,68 +96,49 @@ secondMutator = BeanUIUtil.getMutator(bean, model.getPropertySecond()); Preconditions.checkNotNull(secondMutator, "could not find mutator for " + model.getPropertySecond()); - MaskFormatter unsignedFormatter; - try { + converter.setForLongitude(model.isLongitudeEditor()); - String pattern = model.getMaskFormatterPattern(); - unsignedFormatter = new MaskFormatter(pattern); - unsignedFormatter.setValidCharacters(" 01234567890"); - } catch (ParseException e) { - // can't happen here - throw new RuntimeException(e); - } - unsignedFactory = new DefaultFormatterFactory(unsignedFormatter); - - MaskFormatter signedFormatter; + MaskFormatter maskFormatter; try { String pattern = model.getMaskFormatterPattern(); - signedFormatter = new MaskFormatter("-" + pattern); - signedFormatter.setValidCharacters(" 01234567890"); + maskFormatter = new MaskFormatterFromConverter<DmsCoordinate>( + DmsCoordinate.class, + pattern, converter); + maskFormatter.setValidCharacters(" 01234567890"); } catch (ParseException e) { // can't happen here throw new RuntimeException(e); } - signedFactory = new DefaultFormatterFactory(signedFormatter); + DefaultFormatterFactory formatterFactory = new DefaultFormatterFactory(maskFormatter); JFormattedTextField editor = ui.getEditor(); - editor.setFormatterFactory(unsignedFactory); + editor.setFormatterFactory(formatterFactory); editor.setFocusLostBehavior(JFormattedTextField.COMMIT); // When editor changes his value, propagate it to model editor.addPropertyChangeListener("value", new PropertyChangeListener() { @Override public void propertyChange(PropertyChangeEvent evt) { - String newValue = (String) evt.getNewValue(); + DmsCoordinate newValue = (DmsCoordinate) evt.getNewValue(); if (log.isDebugEnabled()) { log.debug("Value has changed: " + newValue); } - DmsCoordinate value = null; - if (newValue != null) { + model.setValue(newValue); + } + }); - Matcher matcher = VALUE_PATTERN.matcher(newValue); - - if (matcher.matches()) { - - String degresStr = matcher.group(1).replaceAll("\\s", ""); - String minutesStr = matcher.group(2).replaceAll("\\s", ""); - String secondsStr = matcher.group(3).replaceAll("\\s", ""); - - Integer degre = degresStr.isEmpty() || "-".equals(degresStr) ? 0 : Integer.valueOf(degresStr); - Integer minutes = minutesStr.isEmpty() ? 0 : Integer.valueOf(minutesStr); - Integer seconds = secondsStr.isEmpty() ? 0 : Integer.valueOf(secondsStr); - - boolean signed = degre < 0; - value = - DmsCoordinate.valueOf(signed, - Math.abs(degre), - minutes, - seconds); + // When longitudeEditor property changed, propagate to the converter + model.addPropertyChangeListener( + DmsCoordinateEditorModel.PROPERTY_LONGITUDE_EDITOR, + new PropertyChangeListener() { + @Override + public void propertyChange(PropertyChangeEvent evt) { + Boolean newValue = (Boolean) evt.getNewValue(); + converter.setForLongitude(newValue != null && newValue); } } - model.setValue(value); - } - }); + ); // When model sign changed, let's push it back in bean model.addPropertyChangeListener( @@ -192,38 +168,10 @@ return; } - String signStr = ""; - String degreeStr = ""; - String minuteStr = ""; - String secondStr = ""; - if (value != null) { - - boolean sign = value.isSign(); - signStr = sign ? "-" : ""; - - Integer degree = value.getDegree(); - degreeStr = degree == null ? "" : degree.toString(); - - Integer minute = value.getMinute(); - minuteStr = minute == null ? "" : minute.toString(); - - Integer second = value.getSecond(); - secondStr = second == null ? "" : second.toString(); - } - - DmsCoordinateEditorModel model = ui.getModel(); - String stringPattern = model.getStringPattern(); - String valueStr = String.format( - stringPattern, - signStr, - StringUtils.leftPad(degreeStr, model.isLongitudeEditor() ? 3 : 2, ' '), - StringUtils.leftPad(minuteStr, 2, ' '), - StringUtils.leftPad(secondStr, 2, ' ')); - valueIsAdjusting = !pushToModel; try { - ui.getEditor().setValue(valueStr); + ui.getEditor().setValue(value); } finally { valueIsAdjusting = false; } @@ -234,7 +182,7 @@ setValue(null, true); // use back unsigned format - ui.getEditor().setFormatterFactory(unsignedFactory); + converter.setUseSign(false); } public void onKeyReleased(KeyEvent e) { @@ -247,77 +195,55 @@ log.debug("Key pressed: " + keyChar + " (caret position: " + caretPosition + ")"); } - String newValue = null; - DefaultFormatterFactory newFactory = null; + boolean needConsume = false; if (keyChar == '-') { // try to switch unsigned to signed - if (unsignedFactory == source.getFormatterFactory()) { + boolean useSign = converter.isUseSign(); - // switch to signed + if (useSign) { + if (log.isDebugEnabled()) { - log.debug("Switch to signed"); + log.debug("Switch to unsigned"); } - newFactory = signedFactory; + converter.setUseSign(false); - try { - source.commitEdit(); - } catch (ParseException e1) { - // ignore it - } + source.setText(source.getText().substring(1).trim()); - newValue = "-" + source.getValue(); - + // remove a sign + caretPosition--; } else { - // switch to unsigned - + // switch to signed if (log.isDebugEnabled()) { - log.debug("Switch to unsigned"); + log.debug("Switch to signed"); } - newFactory = unsignedFactory; + converter.setUseSign(true); - try { - source.commitEdit(); - } catch (ParseException e1) { - // ignore it - } + source.setText("-" + source.getText()); - newValue = ((String) source.getValue()).substring(1); + // add a sign + caretPosition++; } - } else { - try { - source.commitEdit(); - } catch (ParseException e1) { - // ignore it - } - if (log.isDebugEnabled()) { - log.debug("Key pressed: newValue " + source.getValue()); - } + + needConsume = useSign != converter.isUseSign(); } - if (newFactory != null) { - - // consume event (prevent any side-effects) - e.consume(); - - - if (log.isDebugEnabled()) { - log.debug("Key pressed: newValue " + newValue); + try { + source.commitEdit(); + } catch (ParseException e1) { + // ignore it + if (log.isErrorEnabled()) { + log.error("Could not commit edit of " + source.getText(), e1); } - source.setFormatterFactory(newFactory); - source.setValue(newValue); + } - if (unsignedFactory == newFactory) { + if (log.isDebugEnabled()) { + log.debug("Key pressed: newValue " + source.getValue()); + } - // remove a sign - caretPosition--; - } else { - - // add a sign - caretPosition++; - } - + if (needConsume) { + e.consume(); source.setCaretPosition(caretPosition); } } Modified: trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/gis/DmsCoordinateEditorModel.java =================================================================== --- trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/gis/DmsCoordinateEditorModel.java 2013-11-24 21:52:35 UTC (rev 2754) +++ trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/gis/DmsCoordinateEditorModel.java 2013-11-25 15:54:51 UTC (rev 2755) @@ -145,7 +145,6 @@ } public String getStringPattern() { - String pattern = "%s%s°%s'%s''"; - return pattern; + return COORDINATE_STRING_PATTERN; } } Added: trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/gis/MaskFormatterFromConverter.java =================================================================== --- trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/gis/MaskFormatterFromConverter.java (rev 0) +++ trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/gis/MaskFormatterFromConverter.java 2013-11-25 15:54:51 UTC (rev 2755) @@ -0,0 +1,71 @@ +package jaxx.runtime.swing.editor.gis; + +/* + * #%L + * JAXX :: Widgets + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2008 - 2013 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% + */ + +import org.apache.commons.beanutils.Converter; + +import javax.swing.text.MaskFormatter; +import java.text.ParseException; + +/** + * Created on 11/25/13. + * + * @author Tony Chemit <chemit@codelutin.com> + * @since 2.6 + */ +public class MaskFormatterFromConverter<O> extends MaskFormatter { + + private static final long serialVersionUID = 1L; + + private final Converter converter; + + private final Class<O> type; + + public MaskFormatterFromConverter(Class<O> type, + String pattern, + Converter converter) throws ParseException { + super(pattern); + this.type = type; + this.converter = converter; + } + + @Override + public String valueToString(Object value) throws ParseException { + return (String) converter.convert(String.class, value); + } + + @Override + public Object stringToValue(String value) throws ParseException { + return converter.convert(type, value); + } + + protected Converter getConverter() { + return converter; + } + + protected Class<O> getType() { + return type; + } +} Property changes on: trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/gis/MaskFormatterFromConverter.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Added: trunk/jaxx-widgets/src/main/resources/META-INF/services/org.apache.commons.beanutils.Converter =================================================================== --- trunk/jaxx-widgets/src/main/resources/META-INF/services/org.apache.commons.beanutils.Converter (rev 0) +++ trunk/jaxx-widgets/src/main/resources/META-INF/services/org.apache.commons.beanutils.Converter 2013-11-25 15:54:51 UTC (rev 2755) @@ -0,0 +1,2 @@ +jaxx.runtime.swing.editor.gis.DmsCoordinateConverter +jaxx.runtime.swing.editor.gis.DmdCoordinateConverter \ No newline at end of file Property changes on: trunk/jaxx-widgets/src/main/resources/META-INF/services/org.apache.commons.beanutils.Converter ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Modified: trunk/jaxx-widgets/src/main/resources/i18n/jaxx-widgets_en_GB.properties =================================================================== --- trunk/jaxx-widgets/src/main/resources/i18n/jaxx-widgets_en_GB.properties 2013-11-24 21:52:35 UTC (rev 2754) +++ trunk/jaxx-widgets/src/main/resources/i18n/jaxx-widgets_en_GB.properties 2013-11-25 15:54:51 UTC (rev 2755) @@ -78,6 +78,8 @@ i18neditor.popup.title=Change language i18neditor.selected=Selected language \: %1$s i18neditor.unselected=Select this langage \: %1$s +jaxx.error.no.convertor.coordinateDmd=Impossible de convert form (or to) a DMD coordinate (incoming value\: %s) +jaxx.error.no.convertor.coordinateDms=Impossible de convert form (or to) a DMS coordinate (incoming value\: %s) memorywidget.memory=%d/%dMb numbereditor..=. numbereditor.0=0 Modified: trunk/jaxx-widgets/src/main/resources/i18n/jaxx-widgets_es_ES.properties =================================================================== --- trunk/jaxx-widgets/src/main/resources/i18n/jaxx-widgets_es_ES.properties 2013-11-24 21:52:35 UTC (rev 2754) +++ trunk/jaxx-widgets/src/main/resources/i18n/jaxx-widgets_es_ES.properties 2013-11-25 15:54:51 UTC (rev 2755) @@ -78,6 +78,8 @@ i18neditor.popup.title=Cambiar idioma i18neditor.selected=Idioma usado actualmente \: %1$s i18neditor.unselected=Para usar este idioma \: %1$s +jaxx.error.no.convertor.coordinateDmd= +jaxx.error.no.convertor.coordinateDms= memorywidget.memory=%d/%dMo numbereditor..=. numbereditor.0=0 Modified: trunk/jaxx-widgets/src/main/resources/i18n/jaxx-widgets_fr_FR.properties =================================================================== --- trunk/jaxx-widgets/src/main/resources/i18n/jaxx-widgets_fr_FR.properties 2013-11-24 21:52:35 UTC (rev 2754) +++ trunk/jaxx-widgets/src/main/resources/i18n/jaxx-widgets_fr_FR.properties 2013-11-25 15:54:51 UTC (rev 2755) @@ -78,6 +78,8 @@ i18neditor.popup.title=Changer de langue i18neditor.selected=Langue actuellement utilisée \: %1$s i18neditor.unselected=Pour utiliser cette langue \: %1$s +jaxx.error.no.convertor.coordinateDmd=Impossible de convertir en (ou depuis) une coordonée DMD depuis la valeur %s +jaxx.error.no.convertor.coordinateDms=Impossible de convertir en (ou depuis) une coordonée DMS depuis la valeur %s memorywidget.memory=%d/%dMo numbereditor..=. numbereditor.0=0 Added: trunk/jaxx-widgets/src/test/java/jaxx/runtime/swing/editor/gis/DmdCoordinateConverterTest.java =================================================================== --- trunk/jaxx-widgets/src/test/java/jaxx/runtime/swing/editor/gis/DmdCoordinateConverterTest.java (rev 0) +++ trunk/jaxx-widgets/src/test/java/jaxx/runtime/swing/editor/gis/DmdCoordinateConverterTest.java 2013-11-25 15:54:51 UTC (rev 2755) @@ -0,0 +1,93 @@ +package jaxx.runtime.swing.editor.gis; + +/* + * #%L + * JAXX :: Widgets + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2008 - 2013 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% + */ + +import org.junit.Assert; +import org.junit.Test; +import org.nuiton.util.converter.ConverterUtil; + +/** + * Created on 11/25/13. + * + * @author Tony Chemit <chemit@codelutin.com> + * @since 2.6 + */ +public class DmdCoordinateConverterTest { + + + @Test + public void testConvert() throws Exception { + + ConverterUtil.initConverters(); + + testConversion(false, "- 0° 0'44", DmdCoordinate.valueOf(-0.007333f)); + testConversion(false, " 0° 0'44", DmdCoordinate.valueOf(0.007333f)); + testConversion(false, " 0°39'99", DmdCoordinate.valueOf(false, 0, 39, 99)); + + testConversion(true, "- 0° 0'44", DmdCoordinate.valueOf(-0.007333f)); + testConversion(true, " 0° 0'44", DmdCoordinate.valueOf(0.007333f)); + testConversion(true, " 0°39'99", DmdCoordinate.valueOf(false, 0, 39, 99)); + } + + protected void testConversion(boolean forLongitude, String expectedString, DmdCoordinate expectedCoordinate) { + + DmdCoordinateConverter converter = (DmdCoordinateConverter) ConverterUtil.getConverter(DmdCoordinate.class); + converter.setForLongitude(forLongitude); + Assert.assertNotNull(converter); + + String actualStr; + DmdCoordinate actualCoordinate; + + // String -> DmdCoordinate + actualCoordinate = (DmdCoordinate) converter.convert(DmdCoordinate.class, expectedString); + Assert.assertNotNull(actualCoordinate); + Assert.assertEquals(actualCoordinate.isSign(), expectedCoordinate.isSign()); + Assert.assertEquals(actualCoordinate.getDegree(), expectedCoordinate.getDegree()); + Assert.assertEquals(actualCoordinate.getMinute(), expectedCoordinate.getMinute()); + Assert.assertEquals(actualCoordinate.getDecimal(), expectedCoordinate.getDecimal()); + + // DmdCoordinate -> String + + actualStr = (String) converter.convert(String.class, expectedCoordinate); + + Assert.assertNotNull(actualStr); + Assert.assertEquals(expectedString, actualStr); + + // String -> String + + actualStr = (String) converter.convert(String.class, expectedString); + + Assert.assertNotNull(actualStr); + Assert.assertEquals(expectedString, actualStr); + + // DmdCoordinate -> DmdCoordinate + actualCoordinate = (DmdCoordinate) converter.convert(DmdCoordinate.class, expectedCoordinate); + Assert.assertNotNull(actualCoordinate); + Assert.assertEquals(actualCoordinate.isSign(), expectedCoordinate.isSign()); + Assert.assertEquals(actualCoordinate.getDegree(), expectedCoordinate.getDegree()); + Assert.assertEquals(actualCoordinate.getMinute(), expectedCoordinate.getMinute()); + Assert.assertEquals(actualCoordinate.getDecimal(), expectedCoordinate.getDecimal()); + } +} Property changes on: trunk/jaxx-widgets/src/test/java/jaxx/runtime/swing/editor/gis/DmdCoordinateConverterTest.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Modified: trunk/jaxx-widgets/src/test/java/jaxx/runtime/swing/editor/gis/DmdCoordinateTest.java =================================================================== --- trunk/jaxx-widgets/src/test/java/jaxx/runtime/swing/editor/gis/DmdCoordinateTest.java 2013-11-24 21:52:35 UTC (rev 2754) +++ trunk/jaxx-widgets/src/test/java/jaxx/runtime/swing/editor/gis/DmdCoordinateTest.java 2013-11-25 15:54:51 UTC (rev 2755) @@ -39,34 +39,22 @@ public void testFromDecimal() throws Exception { { - DmdCoordinate actual = DmdCoordinate.empty(); + DmdCoordinate coordinate = DmdCoordinate.empty(); - actual.fromDecimal(42.7f); + coordinate.fromDecimal(42.7f); - Integer expectedDegree = 42; - Integer expectedMinute = 42; - Integer expectedDecimal = null; - - Assert.assertEquals(expectedDegree, actual.getDegree()); - Assert.assertEquals(expectedMinute, actual.getMinute()); - Assert.assertEquals(expectedDecimal, actual.getDecimal()); + assertDmdCoordinate(coordinate, false, 42,42,0); } { - DmdCoordinate actual = DmdCoordinate.empty(); + DmdCoordinate coordinate = DmdCoordinate.empty(); float decimalExcepted = 42.707f; - actual.fromDecimal(decimalExcepted); + coordinate.fromDecimal(decimalExcepted); - Integer expectedDegree = 42; - Integer expectedMinute = 42; - Integer expectedDecimal = 42; + assertDmdCoordinate(coordinate, false, 42,42,42); - Assert.assertEquals(expectedDegree, actual.getDegree()); - Assert.assertEquals(expectedMinute, actual.getMinute()); - Assert.assertEquals(expectedDecimal, actual.getDecimal()); - - Float decimal = actual.toDecimal(); + Float decimal = coordinate.toDecimal(); Assert.assertEquals(decimalExcepted, decimal, 0.001); } } @@ -75,32 +63,26 @@ public void testToDecimal() throws Exception { { - DmdCoordinate component = DmdCoordinate.empty(); - component.setDegree(42); - component.setMinute(42); - component.setDecimal(42); - Float actual = component.toDecimal(); + DmdCoordinate coordinate = DmdCoordinate.empty(); + coordinate.setDegree(42); + coordinate.setMinute(42); + coordinate.setDecimal(42); + Float actual = coordinate.toDecimal(); Float expected = 42.707f; Assert.assertEquals(expected, actual, 0.001); } { - DmdCoordinate component = DmdCoordinate.empty(); - component.setDegree(12); - component.setMinute(12); - Float actual = component.toDecimal(); + DmdCoordinate coordinate = DmdCoordinate.empty(); + coordinate.setDegree(12); + coordinate.setMinute(12); + Float actual = coordinate.toDecimal(); Float expected = 12.2f; Assert.assertEquals(expected, actual, 0.0001); - component.fromDecimal(expected); + coordinate.fromDecimal(expected); - Integer expectedDegree = 12; - Integer expectedMinute = 12; - Integer expectedDecimal = null; - - Assert.assertEquals(expectedDegree, component.getDegree()); - Assert.assertEquals(expectedMinute, component.getMinute()); - Assert.assertEquals(expectedDecimal, component.getDecimal()); + assertDmdCoordinate(coordinate, false, 12,12,0); } { @@ -114,13 +96,35 @@ component.fromDecimal(expected); - Integer expectedDegree = 12; - Integer expectedMinute = 12; - Integer expectedDecimal = 20; + assertDmdCoordinate(component, false, 12,12,20); - Assert.assertEquals(expectedDegree, component.getDegree()); - Assert.assertEquals(expectedMinute, component.getMinute()); - Assert.assertEquals(expectedDecimal, component.getDecimal()); } } + + @Test + public void testValueOf() throws Exception { + + DmdCoordinate coordinate = DmdCoordinate.valueOf(-0.007333f); + Assert.assertNotNull(coordinate); + Assert.assertTrue(coordinate.isDegreeNull()); + Assert.assertTrue(coordinate.isMinuteNull()); + + Assert.assertTrue(coordinate.isDegreeValid(true)); + Assert.assertTrue(coordinate.isMinuteValid()); + Assert.assertTrue(coordinate.isDecimalValid()); + + assertDmdCoordinate(coordinate, true, 0, 0, 44); + } + + public static void assertDmdCoordinate(DmdCoordinate coordinate, + boolean expectedSign, + Integer expectedDegree, + Integer expectedMinute, + Integer expectedDecimal) { + Assert.assertNotNull(coordinate); + Assert.assertEquals(expectedSign, coordinate.isSign()); + Assert.assertEquals(expectedDegree, coordinate.getDegree()); + Assert.assertEquals(expectedMinute, coordinate.getMinute()); + Assert.assertEquals(expectedDecimal, coordinate.getDecimal()); + } } Added: trunk/jaxx-widgets/src/test/java/jaxx/runtime/swing/editor/gis/DmsCoordinateConverterTest.java =================================================================== --- trunk/jaxx-widgets/src/test/java/jaxx/runtime/swing/editor/gis/DmsCoordinateConverterTest.java (rev 0) +++ trunk/jaxx-widgets/src/test/java/jaxx/runtime/swing/editor/gis/DmsCoordinateConverterTest.java 2013-11-25 15:54:51 UTC (rev 2755) @@ -0,0 +1,93 @@ +package jaxx.runtime.swing.editor.gis; + +/* + * #%L + * JAXX :: Widgets + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2008 - 2013 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% + */ + +import org.junit.Assert; +import org.junit.Test; +import org.nuiton.util.converter.ConverterUtil; + +/** + * Created on 11/25/13. + * + * @author Tony Chemit <chemit@codelutin.com> + * @since 2.6 + */ +public class DmsCoordinateConverterTest { + + + @Test + public void testConvert() throws Exception { + + ConverterUtil.initConverters(); + + testConversion(false, "- 0° 0'26''", DmsCoordinate.valueOf(-0.007333f)); + testConversion(false, " 0° 0'26''", DmsCoordinate.valueOf(0.007333f)); + testConversion(false, " 0°39'59''", DmsCoordinate.valueOf(false, 0, 39, 59)); + + testConversion(true, "- 0° 0'26''", DmsCoordinate.valueOf(-0.007333f)); + testConversion(true, " 0° 0'26''", DmsCoordinate.valueOf(0.007333f)); + testConversion(true, " 0°39'59''", DmsCoordinate.valueOf(false, 0, 39, 59)); + } + + protected void testConversion(boolean forLongitude, String expectedString, DmsCoordinate expectedCoordinate) { + + DmsCoordinateConverter converter = (DmsCoordinateConverter) ConverterUtil.getConverter(DmsCoordinate.class); + converter.setForLongitude(forLongitude); + Assert.assertNotNull(converter); + + String actualStr; + DmsCoordinate actualCoordinate; + + // String -> DmsCoordinate + actualCoordinate = (DmsCoordinate) converter.convert(DmsCoordinate.class, expectedString); + Assert.assertNotNull(actualCoordinate); + Assert.assertEquals(actualCoordinate.isSign(), expectedCoordinate.isSign()); + Assert.assertEquals(actualCoordinate.getDegree(), expectedCoordinate.getDegree()); + Assert.assertEquals(actualCoordinate.getMinute(), expectedCoordinate.getMinute()); + Assert.assertEquals(actualCoordinate.getSecond(), expectedCoordinate.getSecond()); + + // DmsCoordinate -> String + + actualStr = (String) converter.convert(String.class, expectedCoordinate); + + Assert.assertNotNull(actualStr); + Assert.assertEquals(expectedString, actualStr); + + // String -> String + + actualStr = (String) converter.convert(String.class, expectedString); + + Assert.assertNotNull(actualStr); + Assert.assertEquals(expectedString, actualStr); + + // DmsCoordinate -> DmsCoordinate + actualCoordinate = (DmsCoordinate) converter.convert(DmsCoordinate.class, expectedCoordinate); + Assert.assertNotNull(actualCoordinate); + Assert.assertEquals(actualCoordinate.isSign(), expectedCoordinate.isSign()); + Assert.assertEquals(actualCoordinate.getDegree(), expectedCoordinate.getDegree()); + Assert.assertEquals(actualCoordinate.getMinute(), expectedCoordinate.getMinute()); + Assert.assertEquals(actualCoordinate.getSecond(), expectedCoordinate.getSecond()); + } +} Property changes on: trunk/jaxx-widgets/src/test/java/jaxx/runtime/swing/editor/gis/DmsCoordinateConverterTest.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Added: trunk/jaxx-widgets/src/test/java/jaxx/runtime/swing/editor/gis/DmsCoordinateTest.java =================================================================== --- trunk/jaxx-widgets/src/test/java/jaxx/runtime/swing/editor/gis/DmsCoordinateTest.java (rev 0) +++ trunk/jaxx-widgets/src/test/java/jaxx/runtime/swing/editor/gis/DmsCoordinateTest.java 2013-11-25 15:54:51 UTC (rev 2755) @@ -0,0 +1,133 @@ +package jaxx.runtime.swing.editor.gis; + +/* + * #%L + * JAXX :: Widgets + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2008 - 2013 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% + */ + +import org.junit.Assert; +import org.junit.Test; + +/** + * Created on 11/25/13. + * + * @author Tony Chemit <chemit@codelutin.com> + * @since 2.6 + */ +public class DmsCoordinateTest { + + @Test + public void testFromDecimal() throws Exception { + + { + DmsCoordinate actual = DmsCoordinate.empty(); + + actual.fromDecimal(42.7f); + + assertDmsCoordinate(actual, false, 42, 42, 0); + } + + { + DmsCoordinate actual = DmsCoordinate.empty(); + + float decimalExcepted = 42.711f; + actual.fromDecimal(decimalExcepted); + + assertDmsCoordinate(actual, false, 42, 42, 39); + + Float decimal = actual.toDecimal(); + Assert.assertEquals(decimalExcepted, decimal, 0.001); + } + } + + @Test + public void testToDecimal() throws Exception { + + { + DmsCoordinate coordinate = DmsCoordinate.empty(); + coordinate.setDegree(42); + coordinate.setMinute(42); + coordinate.setSecond(42); + Float floatValue = coordinate.toDecimal(); + Float expected = 42.711f; + Assert.assertEquals(expected, floatValue, 0.001); + } + + { + DmsCoordinate coordinate = DmsCoordinate.empty(); + coordinate.setDegree(12); + coordinate.setMinute(12); + + Float floatValue = coordinate.toDecimal(); + Float expected = 12.2001f; + Assert.assertEquals(expected, floatValue, 0.001); + + coordinate.fromDecimal(expected); + assertDmsCoordinate(coordinate, false, 12, 12, 0); + + } + + { + DmsCoordinate coordinate = DmsCoordinate.empty(); + coordinate.setDegree(12); + coordinate.setMinute(12); + coordinate.setSecond(20); + Float floatValue = coordinate.toDecimal(); + Float expected = 12.20569f; + Assert.assertEquals(expected, floatValue, 0.001); + + coordinate.fromDecimal(expected); + + assertDmsCoordinate(coordinate, false, 12, 12, 20); + } + } + + + @Test + public void testValueOf() throws Exception { + + DmsCoordinate coordinate = DmsCoordinate.valueOf(-0.007333f); + Assert.assertNotNull(coordinate); + Assert.assertTrue(coordinate.isDegreeNull()); + Assert.assertTrue(coordinate.isMinuteNull()); + Assert.assertFalse(coordinate.isSecondNull()); + + + Assert.assertTrue(coordinate.isDegreeValid(true)); + Assert.assertTrue(coordinate.isMinuteValid()); + Assert.assertTrue(coordinate.isSecondValid()); + + assertDmsCoordinate(coordinate, true, 0, 0, 26); + } + + public static void assertDmsCoordinate(DmsCoordinate coordinate, + boolean expectedSign, + Integer expectedDegree, + Integer expectedMinute, + Integer expectedSecond) { + Assert.assertNotNull(coordinate); + Assert.assertEquals(expectedSign, coordinate.isSign()); + Assert.assertEquals(expectedDegree, coordinate.getDegree()); + Assert.assertEquals(expectedMinute, coordinate.getMinute()); + Assert.assertEquals(expectedSecond, coordinate.getSecond()); + } +} Property changes on: trunk/jaxx-widgets/src/test/java/jaxx/runtime/swing/editor/gis/DmsCoordinateTest.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native