Commit df3066ab authored by Nico Mack's avatar Nico Mack

Added a method for retrieving and converting dimension from scenarios

parent 271349d8
......@@ -17,9 +17,14 @@
package lu.list.itis.dkd.tui.bootstrapping;
import lu.list.itis.dkd.tui.exception.BuildException;
import lu.list.itis.dkd.tui.utility.CameraCoordinates;
import lu.list.itis.dkd.tui.utility.ColorFactory;
import lu.list.itis.dkd.tui.utility.CoordinateState;
import lu.list.itis.dkd.tui.utility.Externalization;
import lu.list.itis.dkd.tui.utility.Point;
import lu.list.itis.dkd.tui.utility.ScreenCoordinates;
import lu.list.itis.dkd.tui.utility.StringUtils;
import lu.list.itis.dkd.tui.utility.TableCoordinates;
import com.google.common.base.Strings;
......@@ -64,6 +69,7 @@ public class BootstrappingUtils {
// Pattern.CASE_INSENSITIVE); //$NON-NLS-1$
private static final Pattern BOOTSTRAP_VAR_PATTERN = Pattern.compile("(\\${1,2})\\{([a-z0-9\\-_]+)\\}", Pattern.CASE_INSENSITIVE); //$NON-NLS-1$
private static final Pattern ANGLE_PATTERN = Pattern.compile("([+-]?\\d+.?\\d*)\\s*(deg|rad)??", Pattern.CASE_INSENSITIVE); //$NON-NLS-1$
private static final Pattern DIMENSION_PATTERN = Pattern.compile("([+-]?\\d+.?\\d*)\\s*(px|mm)??", Pattern.CASE_INSENSITIVE); //$NON-NLS-1$
private static final String VAR_TEMPLATE = "${{}}"; //$NON-NLS-1$
......@@ -72,6 +78,10 @@ public class BootstrappingUtils {
private static final String DEGREES = "deg"; //$NON-NLS-1$
private static final String RADIANS = "rad"; //$NON-NLS-1$
private static final String PIXELS = "px"; //$NON-NLS-1$
private static final String MILLIMETERS = "mm"; //$NON-NLS-1$
private static final String CAMERA = ""; //$NON-NLS-1$
// ---------------------------------------------------------------------------
// ***************************************************************************
// * Primitives
......@@ -669,6 +679,51 @@ public class BootstrappingUtils {
return getContentAsDegrees(rootElement, childName, optional, defaultAngle, null);
}
// ---------------------------------------------------------------------------
/**
* @param rootElement
* @param childName
* @param optional
* @param defaultValue
* @return
* @throws BuildException
*/
// ---------------------------------------------------------------------------
public static double getDimensionAsScreenCoordinates(Element rootElement, String childName, boolean optional, Double defaultValue) throws BuildException {
return getContentAsDimension(rootElement, childName, optional, defaultValue, null, ScreenCoordinates.class);
}
// ---------------------------------------------------------------------------
/**
* @param rootElement
* @param childName
* @param optional
* @param defaultValue
* @return
* @throws BuildException
*/
// ---------------------------------------------------------------------------
public static double getDimensionAsTableCoordinates(Element rootElement, String childName, boolean optional, Double defaultValue) throws BuildException {
return getContentAsDimension(rootElement, childName, optional, defaultValue, null, TableCoordinates.class);
}
// ---------------------------------------------------------------------------
/**
* @param rootElement
* @param childName
* @param optional
* @param defaultValue
* @return
* @throws BuildException
*/
// ---------------------------------------------------------------------------
public static double getDimensionAsCameraCoordinates(Element rootElement, String childName, boolean optional, Double defaultValue) throws BuildException {
return getContentAsDimension(rootElement, childName, optional, defaultValue, null, CameraCoordinates.class);
}
// ---------------------------------------------------------------------------
/**
* @param rootElement
......@@ -749,4 +804,63 @@ public class BootstrappingUtils {
return value;
}
// ---------------------------------------------------------------------------
/**
* @param rootElement
* @param childName
* @param optional
* @param defaultValue
* @param context
* @param targetStateClass
* @return
* @throws BuildException
*/
// ---------------------------------------------------------------------------
public static double getContentAsDimension(Element rootElement, String childName, boolean optional, Double defaultValue, BootstrapContext context, Class<? extends CoordinateState> targetStateClass) throws BuildException {
double value = 0;
Point dimension;
String contentAsString = Strings.nullToEmpty(getContent(rootElement, childName, context));
if (contentAsString.length() > 0) {
Matcher dimensionMatcher = DIMENSION_PATTERN.matcher(contentAsString);
if (dimensionMatcher.matches()) {
String dimensionAsString = dimensionMatcher.group(1);
String unit = dimensionMatcher.group(2);
unit = (Strings.isNullOrEmpty(unit)) ? CAMERA : unit.toLowerCase(); // $NON-NLS-1$
try {
value = Double.parseDouble(dimensionAsString);
} catch (NumberFormatException exception) {
throw new BuildException("Value provide for " + childName + " node must be a decimal number. Specified value " + dimensionAsString + " could not be interpreted as such!"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
}
switch (unit) {
case CAMERA:
dimension = new Point(CameraCoordinates.class);
break;
case MILLIMETERS:
dimension = new Point(TableCoordinates.class);
break;
case PIXELS:
dimension = new Point(ScreenCoordinates.class);
break;
default:
dimension = new Point(ScreenCoordinates.class);
}
dimension.setLocation(value, value);
dimension.toCoordinates(targetStateClass);
value = dimension.getX();
}
} else {
if (optional)
value = defaultValue;
else
throw new BuildException("Node " + childName + " is mandatory and MUST be provided!"); //$NON-NLS-1$//$NON-NLS-2$
}
return value;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment