001 /*
002 * Copyright 2008-2014 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.*;
027
028 import static griffon.util.GriffonNameUtils.requireNonBlank;
029 import static java.util.Objects.requireNonNull;
030
031 /**
032 * Base implementation of the GriffonModel interface.
033 *
034 * @author Andres Almiray
035 * @since 2.0.0
036 */
037 public abstract class AbstractGriffonModel extends AbstractGriffonMvcArtifact implements GriffonModel {
038 private static final String ERROR_EVENT_NULL = "Argument 'event' must not be null";
039 private static final String ERROR_PROPERTY_NAME_BLANK = "Argument 'propertyName' must not be blank";
040 protected final PropertyChangeSupport pcs;
041 protected final VetoableChangeSupport vcs;
042
043 @Inject
044 public AbstractGriffonModel(@Nonnull GriffonApplication application) {
045 super(application);
046 pcs = new PropertyChangeSupport(this);
047 vcs = new VetoableChangeSupport(this);
048 }
049
050 @Nonnull
051 @Override
052 protected String getArtifactType() {
053 return GriffonModelClass.TYPE;
054 }
055
056 @Override
057 public void addVetoableChangeListener(@Nullable VetoableChangeListener listener) {
058 vcs.addVetoableChangeListener(listener);
059 }
060
061 @Override
062 public void addVetoableChangeListener(@Nullable String propertyName, @Nullable VetoableChangeListener listener) {
063 vcs.addVetoableChangeListener(propertyName, listener);
064 }
065
066 @Override
067 public void removeVetoableChangeListener(@Nullable VetoableChangeListener listener) {
068 vcs.removeVetoableChangeListener(listener);
069 }
070
071 @Override
072 public void removeVetoableChangeListener(@Nullable String propertyName, @Nullable VetoableChangeListener listener) {
073 vcs.removeVetoableChangeListener(propertyName, listener);
074 }
075
076 @Nonnull
077 @Override
078 public VetoableChangeListener[] getVetoableChangeListeners() {
079 return vcs.getVetoableChangeListeners();
080 }
081
082 @Nonnull
083 @Override
084 public VetoableChangeListener[] getVetoableChangeListeners(@Nullable String propertyName) {
085 return vcs.getVetoableChangeListeners(propertyName);
086 }
087
088 @Override
089 public void addPropertyChangeListener(@Nullable PropertyChangeListener listener) {
090 pcs.addPropertyChangeListener(listener);
091 }
092
093 @Override
094 public void addPropertyChangeListener(@Nullable String propertyName, @Nullable PropertyChangeListener listener) {
095 pcs.addPropertyChangeListener(propertyName, listener);
096 }
097
098 @Override
099 public void removePropertyChangeListener(@Nullable PropertyChangeListener listener) {
100 pcs.removePropertyChangeListener(listener);
101 }
102
103 @Override
104 public void removePropertyChangeListener(@Nullable String propertyName, @Nullable PropertyChangeListener listener) {
105 pcs.removePropertyChangeListener(propertyName, listener);
106 }
107
108 @Nonnull
109 @Override
110 public PropertyChangeListener[] getPropertyChangeListeners() {
111 return pcs.getPropertyChangeListeners();
112 }
113
114 @Nonnull
115 @Override
116 public PropertyChangeListener[] getPropertyChangeListeners(@Nullable String propertyName) {
117 return pcs.getPropertyChangeListeners(propertyName);
118 }
119
120 protected void firePropertyChange(@Nonnull PropertyChangeEvent event) {
121 pcs.firePropertyChange(requireNonNull(event, ERROR_EVENT_NULL));
122 }
123
124 protected void firePropertyChange(@Nonnull String propertyName, @Nullable Object oldValue, @Nullable Object newValue) {
125 pcs.firePropertyChange(requireNonBlank(propertyName, ERROR_PROPERTY_NAME_BLANK), oldValue, newValue);
126 }
127
128 protected void fireVetoableChange(@Nonnull PropertyChangeEvent event) throws PropertyVetoException {
129 vcs.fireVetoableChange(requireNonNull(event, ERROR_EVENT_NULL));
130 }
131
132 protected void fireVetoableChange(@Nonnull String propertyName, @Nullable Object oldValue, @Nullable Object newValue) throws PropertyVetoException {
133 vcs.fireVetoableChange(requireNonBlank(propertyName, ERROR_PROPERTY_NAME_BLANK), oldValue, newValue);
134 }
135 }
|