Commit cf5a0dcd authored by Nico Mack's avatar Nico Mack

Refactored BundleBasedCallback into TULIP core package

parent 11a8ff5e
/**
* 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.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("Required callback {} was not found in available callbacks!", callbackAttribute); //$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