/* * JAME 6.2.1 * http://jame.sourceforge.net * * Copyright 2001, 2016 Andrea Medeghini * * This file is part of JAME. * * JAME is an application for creating fractals and other graphics artifacts. * * JAME is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * JAME 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with JAME. If not, see <http://www.gnu.org/licenses/>. * */ package net.sf.jame.contextfree.pathAdjustment; import net.sf.jame.contextfree.ContextFreeRegistry; import net.sf.jame.contextfree.pathAdjustment.extension.PathAdjustmentExtensionConfig; import net.sf.jame.contextfree.pathAdjustment.extension.PathAdjustmentExtensionRuntime; import net.sf.jame.contextfree.renderer.support.CFModification; import net.sf.jame.core.common.ExtensionReferenceElement; import net.sf.jame.core.config.RuntimeElement; import net.sf.jame.core.config.ValueChangeEvent; import net.sf.jame.core.config.ValueChangeListener; import net.sf.jame.core.extension.ConfigurableExtensionReference; import net.sf.jame.core.extension.ExtensionException; import net.sf.jame.core.extension.ExtensionNotFoundException; /** * @author Andrea Medeghini */ public class PathAdjustmentRuntimeElement extends RuntimeElement { private PathAdjustmentConfigElement pathAdjustmentElement; private PathAdjustmentExtensionRuntime<?> extensionRuntime; private ExtensionListener extensionListener; /** * Constructs a new PathAdjustmentRuntimeElement. * * @param registry * @param PathAdjustmentRuntimeElementElement */ public PathAdjustmentRuntimeElement(final PathAdjustmentConfigElement pathAdjustmentElement) { if (pathAdjustmentElement == null) { throw new IllegalArgumentException("pathAdjustmentElement is null"); } this.pathAdjustmentElement = pathAdjustmentElement; createRuntime(pathAdjustmentElement.getExtensionReference()); extensionListener = new ExtensionListener(); pathAdjustmentElement.getExtensionElement().addChangeListener(extensionListener); } /** * @see net.sf.jame.core.config.RuntimeElement#dispose() */ @Override public void dispose() { if ((pathAdjustmentElement != null) && (extensionListener != null)) { pathAdjustmentElement.getExtensionElement().removeChangeListener(extensionListener); } extensionListener = null; if (extensionRuntime != null) { extensionRuntime.dispose(); extensionRuntime = null; } pathAdjustmentElement = null; super.dispose(); } /** * @see net.sf.jame.core.config.RuntimeElement#isChanged() */ @Override public boolean isChanged() { boolean pathAdjustmentChanged = false; pathAdjustmentChanged |= (extensionRuntime != null) && extensionRuntime.isChanged(); return super.isChanged() || pathAdjustmentChanged; } @SuppressWarnings("unchecked") private void createRuntime(final ConfigurableExtensionReference<PathAdjustmentExtensionConfig> reference) { try { if (reference != null) { final PathAdjustmentExtensionRuntime extensionRuntime = ContextFreeRegistry.getInstance().getPathAdjustmentExtension(reference.getExtensionId()).createExtensionRuntime(); final PathAdjustmentExtensionConfig extensionConfig = reference.getExtensionConfig(); extensionRuntime.setConfig(extensionConfig); setExtensionRuntime(extensionRuntime); } else { setExtensionRuntime(null); } } catch (final ExtensionNotFoundException e) { e.printStackTrace(); } catch (final ExtensionException e) { e.printStackTrace(); } } /** * @return the PathAdjustmentExtensionRuntimeRuntime */ public PathAdjustmentExtensionRuntime<?> getExtensionRuntime() { return extensionRuntime; } private void setExtensionRuntime(final PathAdjustmentExtensionRuntime<?> extensionRuntime) { if (this.extensionRuntime != null) { this.extensionRuntime.dispose(); } this.extensionRuntime = extensionRuntime; } private class ExtensionListener implements ValueChangeListener { /** * @see net.sf.jame.core.config.ValueChangeListener#valueChanged(net.sf.jame.core.config.ValueChangeEvent) */ @SuppressWarnings("unchecked") public void valueChanged(final ValueChangeEvent e) { switch (e.getEventType()) { case ExtensionReferenceElement.EXTENSION_REFERENCE_CHANGED: { createRuntime((ConfigurableExtensionReference<PathAdjustmentExtensionConfig>) e.getParams()[0]); fireChanged(); break; } default: { break; } } } } public void apply(CFModification mod) { if (extensionRuntime != null) { extensionRuntime.apply(mod); } } }