DefaultGriffonModelClass.java
01 /*
02  * Copyright 2010-2012 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 griffon.util.GriffonNameUtils;
23 import groovy.lang.Closure;
24 import groovy.lang.MetaProperty;
25 
26 import java.util.Arrays;
27 import java.util.LinkedHashSet;
28 import java.util.Set;
29 
30 /**
31  @author Andres Almiray
32  @since 0.9.1
33  */
34 public class DefaultGriffonModelClass extends DefaultGriffonClass implements GriffonModelClass {
35     protected final Set<String> propertiesCache = new LinkedHashSet<String>();
36     private static final Set<String> BINDABLE_PROPERTIES = new LinkedHashSet<String>(
37             Arrays.asList("propertyChangeListeners""vetoableChangeListeners"));
38 
39     public DefaultGriffonModelClass(GriffonApplication app, Class<?> clazz) {
40         super(app, clazz, TYPE, TRAILING);
41     }
42 
43     public void resetCaches() {
44         super.resetCaches();
45         propertiesCache.clear();
46     }
47 
48     public String[] getPropertyNames() {
49         if (propertiesCache.isEmpty()) {
50             for (String propertyName : getPropertiesWithFields()) {
51                 if (!propertiesCache.contains(propertyName&&
52                         !GriffonClassUtils.isEventHandler(propertyName&&
53                         getPropertyValue(propertyName, Closure.class== null &&
54                         !STANDARD_PROPERTIES.contains(propertyName&&
55                         !BINDABLE_PROPERTIES.contains(propertyName)) {
56                     propertiesCache.add(propertyName);
57                 }
58             }
59             for (MetaProperty p : getMetaProperties()) {
60                 String propertyName = p.getName();
61                 if (GriffonClassUtils.isGetter(p, true)) {
62                     propertyName = GriffonNameUtils.uncapitalize(propertyName.substring(3));
63                 }
64                 if (!propertiesCache.contains(propertyName&&
65                         !GriffonClassUtils.isEventHandler(propertyName&&
66                         !isClosureMetaProperty(p&&
67                         !STANDARD_PROPERTIES.contains(propertyName&&
68                         !BINDABLE_PROPERTIES.contains(propertyName)) {
69                     propertiesCache.add(propertyName);
70                 }
71             }
72         }
73 
74         return propertiesCache.toArray(new String[propertiesCache.size()]);
75     }
76 
77     public void setModelPropertyValue(GriffonModel model, String propertyName, Object value) {
78         getMetaClass().setProperty(model, propertyName, value);
79     }
80 
81     public Object getModelPropertyValue(GriffonModel model, String propertyName) {
82         return getMetaClass().getProperty(model, propertyName);
83     }
84 }