| /** |
| * Copyright (c) 2011, 2015 - Lunifera GmbH (Gross Enzersdorf, Austria), Loetz GmbH&Co.KG (69115 Heidelberg, Germany) |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License 2.0 |
| * which accompanies this distribution, and is available at |
| * https://www.eclipse.org/legal/epl-2.0/ |
| * |
| * SPDX-License-Identifier: EPL-2.0 |
| * |
| * Contributors: |
| * Florian Pirchner - Initial implementation |
| */ |
| package org.eclipse.osbp.runtime.common.annotations; |
| |
| import java.beans.PropertyChangeEvent; |
| import java.beans.PropertyChangeListener; |
| import java.io.Serializable; |
| |
| import org.slf4j.Logger; |
| import org.slf4j.LoggerFactory; |
| |
| /** |
| * A property change listener which may be added to several dto instances. It |
| * sets the dirty flag on a dto and keeps an additional dirty flag for the whole |
| * structure, where the adapter was added. |
| */ |
| public class DirtyStateAdapter implements PropertyChangeListener, Serializable { |
| |
| private static final long serialVersionUID = 3286204198033608643L; |
| |
| /** The Constant LOGGER. */ |
| private static final Logger LOGGER = LoggerFactory |
| .getLogger(DirtyStateAdapter.class); |
| |
| /** The dirty. */ |
| private boolean dirty; |
| |
| /** The active. */ |
| private boolean active; |
| |
| /* (non-Javadoc) |
| * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent) |
| */ |
| @Override |
| public void propertyChange(PropertyChangeEvent event) { |
| if (!active) { |
| return; |
| } |
| Object object = event.getSource(); |
| Class<?> objClass = object.getClass(); |
| try { |
| if (DtoUtils.isDisposeField(objClass, event.getPropertyName())) { |
| return; |
| } |
| // dirty field not forwarded to handle dirty, if already dirty |
| if (DtoUtils.isDirtyField(objClass, event.getPropertyName()) && DtoUtils.isDirty(object)) { |
| return; |
| } |
| } catch (SecurityException ignoreIt) { |
| LOGGER.error("{}", ignoreIt); |
| } catch (IllegalAccessException ignoreIt) { |
| LOGGER.error("{}", ignoreIt); |
| } |
| |
| handleDirty(event); |
| } |
| |
| /** |
| * True, if the adapter handles notifications. |
| * |
| * @return true, if is active |
| */ |
| public boolean isActive() { |
| return active; |
| } |
| |
| /** |
| * True, if the adapter handles notifications. |
| * |
| * @param active the new active |
| */ |
| public void setActive(boolean active) { |
| this.active = active; |
| } |
| |
| /** |
| * Sets the dirty flag. |
| * |
| * @param event the event |
| */ |
| protected void handleDirty(PropertyChangeEvent event) { |
| Object object = event.getSource(); |
| // set the dirty flag to true if available |
| DtoUtils.invokeDirtySetter(object, true); |
| |
| dirty = true; |
| } |
| |
| /** |
| * Returns true, if the dto structure became dirty. |
| * |
| * @return true, if is dirty |
| */ |
| public boolean isDirty() { |
| return dirty; |
| } |
| |
| } |