001 /*
002 * SPDX-License-Identifier: Apache-2.0
003 *
004 * Copyright 2008-2017 the original author or authors.
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018 package org.codehaus.griffon.runtime.core.artifact;
019
020
021 import griffon.core.GriffonApplication;
022 import griffon.core.artifact.GriffonModel;
023 import griffon.core.artifact.GriffonModelClass;
024
025 import javax.annotation.Nonnull;
026 import javax.annotation.Nullable;
027 import javax.inject.Inject;
028 import java.beans.PropertyChangeEvent;
029 import java.beans.PropertyChangeListener;
030 import java.beans.PropertyChangeSupport;
031 import java.beans.PropertyVetoException;
032 import java.beans.VetoableChangeListener;
033 import java.beans.VetoableChangeSupport;
034
035 import static griffon.util.GriffonNameUtils.requireNonBlank;
036 import static java.util.Objects.requireNonNull;
037
038 /**
039 * Base implementation of the GriffonModel interface.
040 *
041 * @author Andres Almiray
042 * @since 2.0.0
043 */
044 public abstract class AbstractGriffonModel extends AbstractGriffonMvcArtifact implements GriffonModel {
045 private static final String ERROR_EVENT_NULL = "Argument 'event' must not be null";
046 private static final String ERROR_PROPERTY_NAME_BLANK = "Argument 'propertyName' must not be blank";
047 protected final PropertyChangeSupport pcs;
048 protected final VetoableChangeSupport vcs;
049
050 public AbstractGriffonModel() {
051 pcs = new PropertyChangeSupport(this);
052 vcs = new VetoableChangeSupport(this);
053 }
054
055 /**
056 * Creates a new instance of this class.
057 *
058 * @param application the GriffonApplication that holds this artifact.
059 * @deprecated Griffon prefers field injection over constructor injector for artifacts as of 2.1.0
060 */
061 @Inject
062 @Deprecated
063 public AbstractGriffonModel(@Nonnull GriffonApplication application) {
064 super(application);
065 pcs = new PropertyChangeSupport(this);
066 vcs = new VetoableChangeSupport(this);
067 }
068
069 @Nonnull
070 @Override
071 protected String getArtifactType() {
072 return GriffonModelClass.TYPE;
073 }
074
075 @Override
076 public void addVetoableChangeListener(@Nullable VetoableChangeListener listener) {
077 vcs.addVetoableChangeListener(listener);
078 }
079
080 @Override
081 public void addVetoableChangeListener(@Nullable String propertyName, @Nullable VetoableChangeListener listener) {
082 vcs.addVetoableChangeListener(propertyName, listener);
083 }
084
085 @Override
086 public void removeVetoableChangeListener(@Nullable VetoableChangeListener listener) {
087 vcs.removeVetoableChangeListener(listener);
088 }
089
090 @Override
091 public void removeVetoableChangeListener(@Nullable String propertyName, @Nullable VetoableChangeListener listener) {
092 vcs.removeVetoableChangeListener(propertyName, listener);
093 }
094
095 @Nonnull
096 @Override
097 public VetoableChangeListener[] getVetoableChangeListeners() {
098 return vcs.getVetoableChangeListeners();
099 }
100
101 @Nonnull
102 @Override
103 public VetoableChangeListener[] getVetoableChangeListeners(@Nullable String propertyName) {
104 return vcs.getVetoableChangeListeners(propertyName);
105 }
106
107 @Override
108 public void addPropertyChangeListener(@Nullable PropertyChangeListener listener) {
109 pcs.addPropertyChangeListener(listener);
110 }
111
112 @Override
113 public void addPropertyChangeListener(@Nullable String propertyName, @Nullable PropertyChangeListener listener) {
114 pcs.addPropertyChangeListener(propertyName, listener);
115 }
116
117 @Override
118 public void removePropertyChangeListener(@Nullable PropertyChangeListener listener) {
119 pcs.removePropertyChangeListener(listener);
120 }
121
122 @Override
123 public void removePropertyChangeListener(@Nullable String propertyName, @Nullable PropertyChangeListener listener) {
124 pcs.removePropertyChangeListener(propertyName, listener);
125 }
126
127 @Nonnull
128 @Override
129 public PropertyChangeListener[] getPropertyChangeListeners() {
130 return pcs.getPropertyChangeListeners();
131 }
132
133 @Nonnull
134 @Override
135 public PropertyChangeListener[] getPropertyChangeListeners(@Nullable String propertyName) {
136 return pcs.getPropertyChangeListeners(propertyName);
137 }
138
139 protected void firePropertyChange(@Nonnull PropertyChangeEvent event) {
140 pcs.firePropertyChange(requireNonNull(event, ERROR_EVENT_NULL));
141 }
142
143 protected void firePropertyChange(@Nonnull String propertyName, @Nullable Object oldValue, @Nullable Object newValue) {
144 pcs.firePropertyChange(requireNonBlank(propertyName, ERROR_PROPERTY_NAME_BLANK), oldValue, newValue);
145 }
146
147 protected void fireVetoableChange(@Nonnull PropertyChangeEvent event) throws PropertyVetoException {
148 vcs.fireVetoableChange(requireNonNull(event, ERROR_EVENT_NULL));
149 }
150
151 protected void fireVetoableChange(@Nonnull String propertyName, @Nullable Object oldValue, @Nullable Object newValue) throws PropertyVetoException {
152 vcs.fireVetoableChange(requireNonBlank(propertyName, ERROR_PROPERTY_NAME_BLANK), oldValue, newValue);
153 }
154 }
|