Commit 7870e77f authored by Nico Mack's avatar Nico Mack

Added support for colour nuances in BootstrappingUtils

Added SwitchablePair class
Moved Template parsing into TangibleApplication
parent b0fa3b10
......@@ -62,6 +62,7 @@ FADE_WITH_HANDLE_NODE=fadeWithHandle
FADING_ENABLED_NODE=fadingEnabled
FILL_COLOUR_ELEMENT=fillColour
FILL_COLOUR_NODE=fillColour
FILL_COLOUR_NUANCE_NODE=fillColourNuance
FONT_NODE=font
FONT_SIZE_NODE=fontSize
FULLSCREEN_NODE=fullScreen
......@@ -169,6 +170,7 @@ START_NODE=start
STATE_NODE=state
STOP_NODE=stop
STROKE_COLOUR_ELEMENT=strokeColour
STROKE_COLOUR_NUANCE_NODE=strokeColourNuance
STROKE_WIDTH_NODE=strokeWidth
STYLE_NODE=style
STYLERULE_NODE=rule
......
......@@ -24,6 +24,7 @@ import lu.list.itis.dkd.dbc.annotation.NonNullByDefault;
import lu.list.itis.dkd.dbc.annotation.Nullable;
import lu.list.itis.dkd.tui.adapter.TangibleObject;
import lu.list.itis.dkd.tui.adapter.TuiAdapter;
import lu.list.itis.dkd.tui.bootstrapping.TemplateBootstrapper;
import lu.list.itis.dkd.tui.exception.BuildException;
import lu.list.itis.dkd.tui.utility.GlobalContext;
import lu.list.itis.dkd.tui.utility.IdMapper;
......@@ -32,6 +33,7 @@ import lu.list.itis.dkd.tui.utility.PropertiesFetcher;
import com.google.common.base.Strings;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import org.jdom2.located.LocatedJDOMFactory;
......@@ -43,6 +45,8 @@ import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Vector;
......@@ -91,6 +95,8 @@ public abstract class TangibleApplication {
protected static StageManager stageManager;
protected Map<String, Element> templates;
/**
* Constructor initialising several basic properties.
*
......@@ -114,7 +120,7 @@ public abstract class TangibleApplication {
bootstrap = parseBootstrapDocument(bootstrapURI);
// setupAnimation();
templates = TemplateBootstrapper.loadTemplates(bootstrap);
stageManager = new StageManager(properties, bootstrap);
stageManager.show();
......@@ -289,4 +295,12 @@ public abstract class TangibleApplication {
public Collection<TangibleObject> getActiveObjects() {
return new Vector<>(adapter.getActiveObjects());
}
/**
* @return
*/
public Map<String, Element> getTemplates() {
return new HashMap<>(this.templates);
}
}
\ No newline at end of file
......@@ -75,7 +75,8 @@ public class BootstrappingUtils {
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 Pattern DIMENSION_PATTERN = Pattern.compile("([+-]?\\d+\\.?\\d*)\\s*(px|mm)?", Pattern.CASE_INSENSITIVE); //$NON-NLS-1$
private static final Pattern NUANCE_PATTERN = Pattern.compile("([+-]?\\d+\\.?\\d*)\\s*(%?)", Pattern.CASE_INSENSITIVE); //$NON-NLS-1$
private static final String VAR_TEMPLATE = "${{}}"; //$NON-NLS-1$
......@@ -88,6 +89,9 @@ public class BootstrappingUtils {
private static final String BOLD = "bold"; //$NON-NLS-1$
private static final String ITALIC = "italic"; //$NON-NLS-1$
private static final String PERCENTS = "%"; //$NON-NLS-1$
private static final String NORMALIZED = ""; //$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$
......@@ -110,6 +114,7 @@ public class BootstrappingUtils {
private static final String NON_BOOLEAN_ATTRIBUTE_TEMPLATE = "Value provide for {} attribute must be a boolean. Specified value {} could not be interpreted as such!"; //$NON-NLS-1$
private static final String NON_ANGLE_NODE_TEMPLATE = "Value provide for {} node must be an angle matching the pattern {}. Specified value {} could not be interpreted as such!"; //$NON-NLS-1$
private static final String NON_NUANCE_NODE_TEMPLATE = "Value provide for {} node must be a nuance matching the pattern {}. Specified value {} could not be interpreted as such!"; //$NON-NLS-1$
private static final String UNKOWN_UNIT_TEMPLATE = "{} is not a know unit value!"; //$NON-NLS-1$
private static final Map<String, Integer> FONT_STYLES = new HashMap<>();
......@@ -363,6 +368,67 @@ public class BootstrappingUtils {
return value;
}
// ---------------------------------------------------------------------------
/**
* @param rootElement
* @param childName
* @param optional
* @param defaultAngle
* @param context
* @param targetUnit
* @return
* @throws BuildException
*/
// ---------------------------------------------------------------------------
private static double getContentAsNuance(Element rootElement, String childName, boolean optional, Double defaultNuance, BootstrapContext context, String targetUnit) throws BuildException {
double value = 0;
String contentAsString = Strings.nullToEmpty(getContent(rootElement, childName, context));
if (contentAsString.length() > 0) {
Matcher nuanceMatcher = NUANCE_PATTERN.matcher(contentAsString);
if (nuanceMatcher.matches()) {
String nuance = nuanceMatcher.group(1);
String unit = nuanceMatcher.group(2);
if (Strings.isNullOrEmpty(unit)) {
Element nuanceElement = rootElement.getChild(childName);
if (nuanceElement != null) {
unit = getAttributeAsString(nuanceElement, Externalization.UNIT_NODE, OPTIONAL, targetUnit);
}
} else {
unit = unit.toLowerCase();
}
try {
value = parseDouble(nuance);
} catch (NumberFormatException exception) {
logAndThrowBuildException(new BuildException(StringUtils.build(NON_DECIMAL_NODE_TEMPLATE, childName, contentAsString)), rootElement, childName);
}
if (!targetUnit.equals(unit)) {
switch (unit) {
case PERCENTS:
value = (value / 100d);
break;
case NORMALIZED:
value = value * 100;
break;
default:
logAndThrowBuildException(new BuildException(StringUtils.build(UNKOWN_UNIT_TEMPLATE, unit)), rootElement, childName);
}
}
} else {
logAndThrowBuildException(new BuildException(StringUtils.build(NON_NUANCE_NODE_TEMPLATE, childName, NUANCE_PATTERN, contentAsString)), rootElement, childName);
}
} else {
if (optional)
value = defaultNuance;
else {
logAndThrowBuildException(new BuildException(StringUtils.build(MANDATORY_NODE_TEMPLATE, childName)), rootElement, childName);
}
}
return value;
}
// ---------------------------------------------------------------------------
// ***************************************************************************
// * Class Body
......@@ -811,23 +877,10 @@ public class BootstrappingUtils {
Element nuanceNode = (childName != null) ? rootElement.getChild(childName) : rootElement;
if (nuanceNode != null) {
nuance = new ColorNuance();
int alpha = getContentAsInteger(rootElement, Externalization.ALPHA_NODE, BootstrappingUtils.OPTIONAL, 0, context);
int hue = getContentAsInteger(rootElement, Externalization.HUE_NODE, BootstrappingUtils.OPTIONAL, 0, context);
int saturation = getContentAsInteger(rootElement, Externalization.SATURATION_NODE, BootstrappingUtils.OPTIONAL, 0, context);
int brightness = getContentAsInteger(rootElement, Externalization.BRIGHTNESS_NODE, BootstrappingUtils.OPTIONAL, 0, context);
if (alpha != 0) {
nuance.setAlpha((double) alpha / TWO_POWER_EIGHT);
}
if (hue != 0) {
nuance.setHue((double) hue / TWO_POWER_EIGHT);
}
if (saturation != 0) {
nuance.setSaturation((double) saturation / TWO_POWER_EIGHT);
}
if (brightness != 0) {
nuance.setBrightness((double) brightness / TWO_POWER_EIGHT);
}
nuance.setAlpha(getContentAsNuance(nuanceNode, Externalization.ALPHA_NODE, BootstrappingUtils.OPTIONAL, 0d, context, NORMALIZED));
nuance.setHue(getContentAsNuance(nuanceNode, Externalization.HUE_NODE, BootstrappingUtils.OPTIONAL, 0d, context, NORMALIZED));
nuance.setSaturation(getContentAsNuance(nuanceNode, Externalization.SATURATION_NODE, BootstrappingUtils.OPTIONAL, 0d, context, NORMALIZED));
nuance.setBrightness(getContentAsNuance(nuanceNode, Externalization.BRIGHTNESS_NODE, BootstrappingUtils.OPTIONAL, 0d, context, NORMALIZED));
} else {
if (!optional) {
logAndThrowBuildException(new BuildException(StringUtils.build(MANDATORY_NODE_TEMPLATE, childName)), rootElement, childName);
......
......@@ -97,6 +97,7 @@ public class Externalization extends NLS {
public static String FADING_ENABLED_NODE;
public static String FILL_COLOUR_ELEMENT;
public static String FILL_COLOUR_NODE;
public static String FILL_COLOUR_NUANCE_NODE;
public static String FONT_NODE;
public static String FONT_SIZE_NODE;
public static String FULLSCREEN_NODE;
......@@ -204,6 +205,7 @@ public class Externalization extends NLS {
public static String STATE_NODE;
public static String STOP_NODE;
public static String STROKE_COLOUR_ELEMENT;
public static String STROKE_COLOUR_NUANCE_NODE;
public static String STROKE_WIDTH_NODE;
public static String STYLE_NODE;
public static String STYLERULE_NODE;
......
/**
* Copyright Luxembourg Institute of Science and Technology, 2018. All rights reserved.
*
* This file is part of TULIP.
*
* TULIP 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, version 3 of the
* License.
*
* TULIP 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 Lesser
* General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with TULIP. If
* not, see <http://www.gnu.org/licenses/lgpl-3.0.html>.
*/
package lu.list.itis.dkd.tui.utility;
/**
* Class referring the global tangible context of the interface, offering methods to retrieve, for
* example, objects and cursors as identified by the interface.
*
* @author Nico Mack [nico.mack@list.lu]
* @since 2.5
* @version 1.0.0
* @param <T>
*/
// ***************************************************************************
// * Class Definition and Members *
// ***************************************************************************
public class SwitchablePair<T> {
private boolean switched;
private T first;
private T second;
// ---------------------------------------------------------------------------
// ***************************************************************************
// * Constructor(s)
// ***************************************************************************
// ---------------------------------------------------------------------------
/**
* @param first
* @param second
*/
// ---------------------------------------------------------------------------
public SwitchablePair(T first, T second) {
this.switched = false;
this.first = first;
this.second = second;
}
// ---------------------------------------------------------------------------
// ***************************************************************************
// * Class Body
// ***************************************************************************
// ---------------------------------------------------------------------------
/**
* @return
*/
// ---------------------------------------------------------------------------
public T getFirst() {
return first;
}
// ---------------------------------------------------------------------------
/**
* @param newFirst
*/
// ---------------------------------------------------------------------------
public void setFirst(T newFirst) {
this.first = newFirst;
}
// ---------------------------------------------------------------------------
/**
* @return
*/
// ---------------------------------------------------------------------------
public T getSecond() {
return second;
}
// ---------------------------------------------------------------------------
/**
* @param newSecond
*/
// ---------------------------------------------------------------------------
public void setSecond(T newSecond) {
this.second = newSecond;
}
// ---------------------------------------------------------------------------
/**
* @return
*/
// ---------------------------------------------------------------------------
public boolean isSwitched() {
return switched;
}
// ---------------------------------------------------------------------------
/**
* @param switchIt
*/
// ---------------------------------------------------------------------------
public void setSwitched(boolean switchIt) {
this.switched = switchIt;
}
// ---------------------------------------------------------------------------
/**
* @return
*/
// ---------------------------------------------------------------------------
public T getCurrent() {
return (switched) ? first : second;
}
// ---------------------------------------------------------------------------
// ***************************************************************************
// * End of Class
// ***************************************************************************
// ---------------------------------------------------------------------------
}
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