Commit 0ce9998f authored by Nico Mack's avatar Nico Mack

Implementation of BundleBasedCallback

parent be5a940d
/**
* Copyright Luxembourg Institute of Science and Technology, 2017. All rights reserved. If you wish
* to use this code for any purpose, please contact the author(s).
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
* WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package lu.list.itis.dkd.tui.bootstrapping;
import lu.list.itis.dkd.tui.exception.BuildException;
import lu.list.itis.dkd.tui.utility.Externalization;
import lu.list.itis.dkd.tui.utility.StringUtils;
import lu.list.itis.dkd.tui.utility.Templating;
import lu.list.itis.dkd.tui.widget.BaseWidget;
import com.google.common.base.Strings;
import com.jgoodies.common.base.Preconditions;
import org.jdom2.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
/**
* @author Nico Mack [nico.mack@list.lu]
* @since 1.5
* @version 1.5.0
*/
// ***************************************************************************
// * Class Definition and Members *
// ***************************************************************************
public class BundleBasedCallback implements BootstrapCallback {
private Integer widgetId;
private boolean repeating;
private Map<String, BootstrapCallback> bundleCallbacks;
private BootstrapCallback callback;
private BootstrapCallback defaultCallback;
// ***************************************************************************
// * Constants *
// ***************************************************************************
private static final Logger LOGGER = LoggerFactory.getLogger(BundleBasedCallback.class.getName());
// ---------------------------------------------------------------------------
// ***************************************************************************
// * Constructor(s) *
// ***************************************************************************
// ---------------------------------------------------------------------------
public BundleBasedCallback(Integer widgetId, Map<String, BootstrapCallback> callbacks) throws BuildException {
this.widgetId = widgetId;
this.bundleCallbacks = new ConcurrentHashMap<>();
this.bundleCallbacks.putAll(callbacks);
}
// ---------------------------------------------------------------------------
// ***************************************************************************
// * Class Body *
// ***************************************************************************
// ---------------------------------------------------------------------------
public List<BaseWidget> buildWidgets(Element template, BootstrapContext context) throws BuildException {
Preconditions.checkArgument(context != null, "Context MUST not be null!"); //$NON-NLS-1$
assert context != null;
List<BaseWidget> widgets = null;
context.setProperty(Templating.HANDLE_ID_PROPERTY, widgetId);
for (Entry<String, BootstrapCallback> callbackEntry : this.bundleCallbacks.entrySet()) {
context.setProperty(callbackEntry.getKey(), callbackEntry.getValue());
}
widgets = TangibleObjectBootstrapper.buildWidgetFromTemplate(template, context, this);
return widgets;
}
// ---------------------------------------------------------------------------
/** {@inheritDoc} */
// ---------------------------------------------------------------------------
@Override
public BootstrapContext initialize(Element node, BootstrapContext context) {
try {
this.callback = null;
repeating = BootstrappingUtils.getAttributeAsBoolean(node, Externalization.REPEAT_ATTRIBUTE, BootstrappingUtils.OPTIONAL, false);
String callbackAttribute = BootstrappingUtils.getAttributeAsString(node, Externalization.CALLBACK_ATTRIBUTE, BootstrappingUtils.OPTIONAL, null);
if (repeating) {
if (!Strings.isNullOrEmpty(callbackAttribute)) {
if (context.hasProperty(callbackAttribute)) {
this.callback = (BootstrapCallback) context.getProperty(callbackAttribute);
} else {
LOGGER.error(StringUtils.build("Required callback {} was not found in available callbacks!", callbackAttribute)); //$NON-NLS-1$
}
}
}
} catch (BuildException exception) {
LOGGER.error(StringUtils.build("An error occured while trying to retrieve {} attribute!", Externalization.CALLBACK_ATTRIBUTE), exception); //$NON-NLS-1$
}
if (this.callback != null) {
context = this.callback.initialize(node, context);
}
return context;
}
// ---------------------------------------------------------------------------
/** {@inheritDoc} */
// ---------------------------------------------------------------------------
@Override
public BootstrapContext preInstantiation(Element node, BootstrapContext context) {
return (this.callback != null) ? this.callback.preInstantiation(node, context) : context;
}
// ---------------------------------------------------------------------------
/** {@inheritDoc} */
// ---------------------------------------------------------------------------
@Override
public BootstrapContext postInstantiation(Element node, BootstrapContext context) {
return (this.callback != null) ? this.callback.postInstantiation(node, context) : context;
}
// ---------------------------------------------------------------------------
/** {@inheritDoc} */
// ---------------------------------------------------------------------------
@Override
public BootstrapContext next(BootstrapContext context) {
return (this.callback != null) ? this.callback.next(context) : context;
}
// ---------------------------------------------------------------------------
/** {@inheritDoc} */
// ---------------------------------------------------------------------------
@Override
public BootstrapContext reset(BootstrapContext context) {
return (this.callback != null) ? this.callback.reset(context) : context;
}
// ---------------------------------------------------------------------------
/** {@inheritDoc} */
@Override
public BootstrapContext finalize(BootstrapContext context) {
if (this.callback != null) {
context = this.callback.finalize(context);
this.callback = null;
}
return context;
}
}
......@@ -41,19 +41,20 @@ import java.util.Map;
// * Class Definition and Members *
// ***************************************************************************
public class DisplayWidgetBootstrapper implements BootstrapCallback {
public class VariableBasedCallback implements BootstrapCallback {
private List<Variable<?>> variables;
private Iterator<Variable<?>> variableIterator;
private Variable<?> variable;
private List<Color> variableColours;
private Integer index;
private Integer widgetId;
// ***************************************************************************
// * Constants *
// ***************************************************************************
private static final Logger LOGGER = LoggerFactory.getLogger(DisplayWidgetBootstrapper.class.getName());
private static final Logger LOGGER = LoggerFactory.getLogger(VariableBasedCallback.class.getName());
// ---------------------------------------------------------------------------
// ***************************************************************************
......@@ -61,7 +62,8 @@ public class DisplayWidgetBootstrapper implements BootstrapCallback {
// ***************************************************************************
// ---------------------------------------------------------------------------
public DisplayWidgetBootstrapper(List<Variable<?>> variables) {
public VariableBasedCallback(Integer widgetId, List<Variable<?>> variables) {
this.widgetId = widgetId;
this.variables = variables;
this.variableColours = ColorFactory.makeRainbowColours(variables.size());
}
......@@ -72,41 +74,49 @@ public class DisplayWidgetBootstrapper implements BootstrapCallback {
// ***************************************************************************
// ---------------------------------------------------------------------------
public List<BaseWidget> buildWidgetsFromVariables(Element template, Integer widgetId, BootstrapContext context, BootstrapCallback callback) throws BuildException {
public List<BaseWidget> buildWidgets(Element template, BootstrapContext context, BootstrapCallback callback) throws BuildException {
List<BaseWidget> widgets = null;
Map<String, Variable<?>> systemVariables = new HashMap<>();
index = 0;
context.setProperty(Templating.HANDLE_ID_PROPERTY, widgetId);
context.setProperty(Templating.NUMBER_OF_VARIABLES_PROPERTY, variables.size());
this.initialize(null, context);
for (Variable<?> systemVariable : variables) {
systemVariables.put(systemVariable.getName(), systemVariable);
}
variableIterator = variables.iterator();
if (variableIterator.hasNext()) {
this.variable = variableIterator.next();
widgets = TangibleObjectBootstrapper.buildWidgetFromTemplate(template, context, this);
widgets = TangibleObjectBootstrapper.buildWidgetFromTemplate(template, context, this);
for (BaseWidget widget : widgets) {
List<Variable<?>> connected;
if (widget.getClass().isAssignableFrom(VariableBased.class)) {
VariableBased display = (VariableBased) widget;
connected = display.connectWithSystemVariables(systemVariables);
for (BaseWidget widget : widgets) {
List<Variable<?>> connected;
if (widget.getClass().isAssignableFrom(VariableBased.class)) {
VariableBased display = (VariableBased) widget;
connected = display.connectWithSystemVariables(systemVariables);
if (widget.getClass().isAssignableFrom(InputChangeListener.class)) {
InputChangeListener listener = (InputChangeListener) widget;
for (Variable<?> connectedVariable : connected)
connectedVariable.addListener(listener);
}
if (widget.getClass().isAssignableFrom(InputChangeListener.class)) {
InputChangeListener listener = (InputChangeListener) widget;
for (Variable<?> connectedVariable : connected)
connectedVariable.addListener(listener);
}
}
}
return widgets;
}
// ---------------------------------------------------------------------------
@Override
public BootstrapContext initialize(Element node, BootstrapContext context) {
index = 0;
context.setProperty(Templating.HANDLE_ID_PROPERTY, widgetId);
context.setProperty(Templating.NUMBER_OF_VARIABLES_PROPERTY, variables.size());
variableIterator = variables.iterator();
if (variableIterator.hasNext()) {
this.variable = variableIterator.next();
} else {
LOGGER.warn("Encountered a Widget without any variables. Won't build Widget! ID of offending Widget is {}", widgetId); //$NON-NLS-1$
}
return widgets;
return context;
}
// ---------------------------------------------------------------------------
......@@ -164,4 +174,9 @@ public class DisplayWidgetBootstrapper implements BootstrapCallback {
}
// ---------------------------------------------------------------------------
/** {@inheritDoc} */
@Override
public BootstrapContext finalize(BootstrapContext context) {
return reset(context);
}
}
......@@ -41,8 +41,9 @@ import java.util.Map;
// * Class Definition and Members *
// ***************************************************************************
public class VectorBasedBootstrapper implements BootstrapCallback {
public class VectorBasedCallback implements BootstrapCallback {
private Integer widgetId;
private List<VectorVariable<?>> variables;
private List<Color> indexColours;
private Integer index;
......@@ -52,7 +53,7 @@ public class VectorBasedBootstrapper implements BootstrapCallback {
// * Constants *
// ***************************************************************************
private static final Logger LOGGER = LoggerFactory.getLogger(VectorBasedBootstrapper.class.getName());
private static final Logger LOGGER = LoggerFactory.getLogger(VectorBasedCallback.class.getName());
// ---------------------------------------------------------------------------
// ***************************************************************************
......@@ -60,9 +61,10 @@ public class VectorBasedBootstrapper implements BootstrapCallback {
// ***************************************************************************
// ---------------------------------------------------------------------------
public VectorBasedBootstrapper(List<VectorVariable<?>> variables) {
public VectorBasedCallback(Integer widgetId, List<VectorVariable<?>> variables) {
size = -1;
this.widgetId = widgetId;
this.variables = variables;
for (VectorVariable<?> variable : variables) {
if (size < 0)
......@@ -81,13 +83,15 @@ public class VectorBasedBootstrapper implements BootstrapCallback {
// ***************************************************************************
// ---------------------------------------------------------------------------
public List<BaseWidget> buildWidgetsFromVariables(Element template, Integer widgetId, BootstrapContext context, BootstrapCallback callback) throws BuildException {
public List<BaseWidget> buildWidgets(Element template, BootstrapContext context, BootstrapCallback callback) throws BuildException {
List<BaseWidget> widgets = null;
Map<String, Variable<?>> systemVariables = new HashMap<>();
index = 0;
context.setProperty(Templating.HANDLE_ID_PROPERTY, widgetId);
context.setProperty(Templating.SIZE_OF_VECTORS_PROPERTY, size);
/*
* index = 0; context.setProperty(Templating.HANDLE_ID_PROPERTY, widgetId);
* context.setProperty(Templating.SIZE_OF_VECTORS_PROPERTY, size);
*/
this.initialize(null, context);
for (Variable<?> systemVariable : variables) {
systemVariables.put(systemVariable.getName(), systemVariable);
......@@ -117,6 +121,16 @@ public class VectorBasedBootstrapper implements BootstrapCallback {
// ---------------------------------------------------------------------------
@Override
public BootstrapContext initialize(Element node, BootstrapContext context) {
index = 0;
context.setProperty(Templating.HANDLE_ID_PROPERTY, widgetId);
context.setProperty(Templating.SIZE_OF_VECTORS_PROPERTY, size);
return context;
}
// ---------------------------------------------------------------------------
/** {@inheritDoc} */
@Override
public BootstrapContext preInstantiation(Element node, BootstrapContext context) {
......@@ -159,4 +173,10 @@ public class VectorBasedBootstrapper implements BootstrapCallback {
// ---------------------------------------------------------------------------
/** {@inheritDoc} */
@Override
public BootstrapContext finalize(BootstrapContext context) {
return reset(context);
}
}
......@@ -136,7 +136,7 @@ public class Leader {
// Build Landing Line
int quadrant = PolarCoordinateHelper.getQuadrant(leaderAngle);
int quadrant = AngleUtils.getQuadrant(leaderAngle);
boolean flipped = ((quadrant == 1) || (quadrant == 2));
if (landingLineLength > 0) {
......
......@@ -64,6 +64,10 @@ public class VariableHtmlBox extends HtmlBox implements VariableBased, InputChan
if (!Strings.isNullOrEmpty(this.htmlTemplate)) {
identifiers = Interpolator.extractVariableIdentifiers(this.htmlTemplate);
if (identifiers.isEmpty()) {
this.htmlContent = this.htmlTemplate;
this.setInformation(this.htmlContent);
}
}
}
......
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