Commit 108b9a45 authored by Nico Mack's avatar Nico Mack

Added StackedCallback class

parent 8c9da833
......@@ -15,7 +15,6 @@ 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;
......@@ -109,12 +108,12 @@ public class BundleBasedCallback implements BootstrapCallback {
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$
LOGGER.error("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$
LOGGER.error("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);
......
/**
* 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.Templating;
import lu.list.itis.dkd.tui.widget.BaseWidget;
import com.jgoodies.common.base.Preconditions;
import org.jdom2.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* @author Nico Mack [nico.mack@list.lu]
* @since 1.5
* @version 1.5.0
*/
// ***************************************************************************
// * Class Definition and Members *
// ***************************************************************************
public class StackedCallback implements BootstrapCallback {
private Integer widgetId;
private boolean repeating;
private List<BootstrapCallback> stackedCallbacks;
private Iterator<BootstrapCallback> callbackIterator;
private BootstrapCallback callback;
// ***************************************************************************
// * Constants *
// ***************************************************************************
private static final Logger LOGGER = LoggerFactory.getLogger(StackedCallback.class.getName());
// ---------------------------------------------------------------------------
// ***************************************************************************
// * Constructor(s) *
// ***************************************************************************
// ---------------------------------------------------------------------------
public StackedCallback(Integer widgetId, List<BootstrapCallback> callbacks) throws BuildException {
this.widgetId = widgetId;
this.stackedCallbacks = new ArrayList<>();
this.stackedCallbacks.addAll(callbacks);
this.callbackIterator = this.stackedCallbacks.iterator();
}
// ---------------------------------------------------------------------------
// ***************************************************************************
// * 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);
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);
if (repeating) {
if (this.callbackIterator.hasNext()) {
this.callback = this.callbackIterator.next();
} else {
LOGGER.error("Ran out of callbacks!"); //$NON-NLS-1$
}
}
} catch (BuildException exception) {
LOGGER.error("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;
}
}
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