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