01 /*
02 * Copyright 2010-2013 the original author or authors.
03 *
04 * Licensed under the Apache License, Version 2.0 (the "License");
05 * you may not use this file except in compliance with the License.
06 * You may obtain a copy of the License at
07 *
08 * http://www.apache.org/licenses/LICENSE-2.0
09 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.codehaus.griffon.runtime.core;
17
18 import griffon.core.GriffonApplication;
19 import griffon.core.GriffonModel;
20 import griffon.core.GriffonModelClass;
21 import griffon.util.GriffonClassUtils;
22 import groovy.lang.Closure;
23
24 import java.util.Arrays;
25 import java.util.LinkedHashSet;
26 import java.util.Set;
27
28 /**
29 * @author Andres Almiray
30 * @since 0.9.1
31 */
32 public class DefaultGriffonModelClass extends DefaultGriffonClass implements GriffonModelClass {
33 protected final Set<String> propertiesCache = new LinkedHashSet<String>();
34 private static final Set<String> BINDABLE_PROPERTIES = new LinkedHashSet<String>(
35 Arrays.asList("propertyChangeListeners", "vetoableChangeListeners"));
36
37 public DefaultGriffonModelClass(GriffonApplication app, Class<?> clazz) {
38 super(app, clazz, TYPE, TRAILING);
39 }
40
41 public void resetCaches() {
42 super.resetCaches();
43 propertiesCache.clear();
44 }
45
46 public String[] getPropertyNames() {
47 if (propertiesCache.isEmpty()) {
48 for (String propertyName : getPropertiesWithFields()) {
49 if (!propertiesCache.contains(propertyName) &&
50 !GriffonClassUtils.isEventHandler(propertyName) &&
51 getPropertyValue(propertyName, Closure.class) == null &&
52 !STANDARD_PROPERTIES.contains(propertyName) &&
53 !BINDABLE_PROPERTIES.contains(propertyName)) {
54 propertiesCache.add(propertyName);
55 }
56 }
57 }
58
59 return propertiesCache.toArray(new String[propertiesCache.size()]);
60 }
61
62 public void setModelPropertyValue(GriffonModel model, String propertyName, Object value) {
63 getMetaClass().setProperty(model, propertyName, value);
64 }
65
66 public Object getModelPropertyValue(GriffonModel model, String propertyName) {
67 return getMetaClass().getProperty(model, propertyName);
68 }
69 }
|