001 /*
002 * Copyright 2008-2017 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 griffon.builder.pivot
017
018 import org.apache.pivot.collections.Dictionary
019 import org.apache.pivot.collections.List as PivotList
020 import org.apache.pivot.collections.Map as PivotMap
021 import org.apache.pivot.collections.Sequence
022 import org.apache.pivot.collections.Set as PivotSet
023 import org.apache.pivot.util.ListenerList
024 import org.apache.pivot.wtk.Component
025 import org.apache.pivot.wtk.Form
026 import org.apache.pivot.wtk.TabPane
027 import org.apache.pivot.wtk.text.Span
028
029 import static griffon.util.GriffonNameUtils.getSetterName
030
031 /**
032 * @author Andres Almiray
033 */
034 final class PivotUtils {
035 private PivotUtils() {
036 // prevent instantiation
037 }
038
039 private static final String ENHANCED = "_ENHANCED_METACLASS_"
040
041 static boolean hasBeenEnhanced(Class klass) {
042 MetaClassRegistry mcr = GroovySystem.metaClassRegistry
043 MetaClass mc = mcr.getMetaClass(klass)
044 if (!(mc instanceof ExpandoMetaClass)) return false
045 return mc.hasMetaProperty(ENHANCED)
046 }
047
048 static void enhance(Class klass, Map enhancedMethods) {
049 MetaClassRegistry mcr = GroovySystem.metaClassRegistry
050 MetaClass mc = mcr.getMetaClass(klass)
051 boolean init = false
052 if (!(mc instanceof ExpandoMetaClass) ||
053 (mc instanceof ExpandoMetaClass && !mc.isModified())) {
054 mcr.removeMetaClass klass
055 mc = new ExpandoMetaClass(klass)
056 init = true
057 }
058 // if mc is an EMC that was initialized previously
059 // with additional methods/properties and it does
060 // not allow modifications after init, then the next
061 // block will throw an exception
062 enhancedMethods.each { k, v ->
063 if (mc.getMetaMethod(k) == null) {
064 mc.registerInstanceMethod(k, v)
065 }
066 }
067 mc.registerBeanProperty(ENHANCED, true)
068 if (init) {
069 mc.initialize()
070 mcr.setMetaClass(klass, mc)
071 }
072 }
073
074 static enhanceClasses() {
075 enhance(Sequence, [
076 iterator: {-> new FixedIterator(delegate, true) },
077 size: {-> delegate.getLength() },
078 getAt: { i -> delegate.get(i) },
079 putAt: { i, e -> delegate.update(i, e) }
080 ])
081 enhance(ListenerList, [
082 leftShift: { e -> delegate.add(e) }
083 ])
084 enhance(PivotList, [
085 leftShift: { e -> delegate.add(e) }
086 ])
087 enhance(PivotSet, [
088 leftShift: { e -> delegate.add(e) },
089 size: {-> delegate.getCount() }
090 ])
091 enhance(Dictionary, [
092 putAt: { k, v -> delegate.put(k, v) },
093 getAt: { k -> delegate.get(k) }
094 ])
095 enhance(PivotMap, [
096 size: {-> delegate.getCount() }
097 ])
098 enhance(Span, [
099 asRange: {-> new IntRange(delegate.start, delegate.end) },
100 size: {-> delegate.getLength() }
101 ])
102 enhance(Component, [
103 setFormLabel: { label -> Form.setLabel(delegate, label ? label.toString() : null) },
104 setFormFlag: { flag -> flag ? Form.setFlag(delegate, flag) : Form.setFlag(delegate, (Form.Flag) null) },
105 setTabLabel: { label -> TabPane.setLabel(delegate, label ? label.toString() : null) },
106 ])
107 }
108
109 static void setBeanProperty(String propertyName, value, bean) {
110 try {
111 // use setter method instead of property access
112 // workaround to multiple property setters & single property getter
113 String setter = getSetterName(propertyName)
114 bean."$setter"(value)
115 } catch (MissingPropertyException mpe) {
116 bean.styles.put(propertyName, value)
117 }
118 }
119 }
|