/* * 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.shapeAdjustment; import net.sf.jame.contextfree.ContextFreeRegistry; import net.sf.jame.contextfree.renderer.support.CFModification; import net.sf.jame.contextfree.shapeAdjustment.extension.ShapeAdjustmentExtensionConfig; import net.sf.jame.contextfree.shapeAdjustment.extension.ShapeAdjustmentExtensionRuntime; 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 ShapeAdjustmentRuntimeElement extends RuntimeElement { private ShapeAdjustmentConfigElement shapeAdjustmentElement; private ShapeAdjustmentExtensionRuntime<?> extensionRuntime; private ExtensionListener extensionListener; /** * Constructs a new ShapeAdjustmentRuntimeElement. * * @param registry * @param ShapeAdjustmentRuntimeElementElement */ public ShapeAdjustmentRuntimeElement(final ShapeAdjustmentConfigElement shapeAdjustmentElement) { if (shapeAdjustmentElement == null) { throw new IllegalArgumentException("shapeAdjustmentElement is null"); } this.shapeAdjustmentElement = shapeAdjustmentElement; createRuntime(shapeAdjustmentElement.getExtensionReference()); extensionListener = new ExtensionListener(); shapeAdjustmentElement.getExtensionElement().addChangeListener(extensionListener); } /** * @see net.sf.jame.core.config.RuntimeElement#dispose() */ @Override public void dispose() { if ((shapeAdjustmentElement != null) && (extensionListener != null)) { shapeAdjustmentElement.getExtensionElement().removeChangeListener(extensionListener); } extensionListener = null; if (extensionRuntime != null) { extensionRuntime.dispose(); extensionRuntime = null; } shapeAdjustmentElement = null; super.dispose(); } /** * @see net.sf.jame.core.config.RuntimeElement#isChanged() */ @Override public boolean isChanged() { boolean shapeAdjustmentChanged = false; shapeAdjustmentChanged |= (extensionRuntime != null) && extensionRuntime.isChanged(); return super.isChanged() || shapeAdjustmentChanged; } @SuppressWarnings("unchecked") private void createRuntime(final ConfigurableExtensionReference<ShapeAdjustmentExtensionConfig> reference) { try { if (reference != null) { final ShapeAdjustmentExtensionRuntime extensionRuntime = ContextFreeRegistry.getInstance().getShapeAdjustmentExtension(reference.getExtensionId()).createExtensionRuntime(); final ShapeAdjustmentExtensionConfig 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 ShapeAdjustmentExtensionRuntimeRuntime */ public ShapeAdjustmentExtensionRuntime<?> getExtensionRuntime() { return extensionRuntime; } private void setExtensionRuntime(final ShapeAdjustmentExtensionRuntime<?> 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<ShapeAdjustmentExtensionConfig>) e.getParams()[0]); fireChanged(); break; } default: { break; } } } } public void apply(CFModification mod) { if (extensionRuntime != null) { extensionRuntime.apply(mod); } } public boolean isSizeChange() { if (extensionRuntime != null) { return extensionRuntime.isSizeChange(); } return false; } public float getSize() { if (extensionRuntime != null) { return extensionRuntime.getSize(); } return 1; } }