Search in sources :

Example 1 with BeanMonitor

use of org.apache.pivot.beans.BeanMonitor in project pivot by apache.

the class ComponentPropertyInspectorSkin method sourceChanged.

@Override
public void sourceChanged(ComponentInspector componentInspector, Component previousSource) {
    Component source = componentInspector.getSource();
    clearControls();
    Form.SectionSequence sections = form.getSections();
    sections.remove(0, sections.getLength());
    if (previousSource != null) {
        beanMonitor.getPropertyChangeListeners().remove(propertyChangeListener);
    }
    if (source == null) {
        beanMonitor = null;
    } else {
        beanMonitor = new BeanMonitor(source);
        beanMonitor.getPropertyChangeListeners().add(propertyChangeListener);
        Class<?> sourceType = source.getClass();
        HashMap<Class<?>, List<String>> declaringClassPartitions = new HashMap<>(classComparator);
        // Partition the properties by their declaring class
        BeanAdapter beanAdapter = new BeanAdapter(source);
        for (String propertyName : beanAdapter) {
            if (beanMonitor.isNotifying(propertyName) && !beanAdapter.isReadOnly(propertyName)) {
                Method method = BeanAdapter.getGetterMethod(sourceType, propertyName);
                Class<?> declaringClass = method.getDeclaringClass();
                List<String> propertyNames = declaringClassPartitions.get(declaringClass);
                if (propertyNames == null) {
                    propertyNames = new ArrayList<>(nameComparator);
                    declaringClassPartitions.put(declaringClass, propertyNames);
                }
                propertyNames.add(propertyName);
            }
        }
        // Add the controls
        for (Class<?> declaringClass : declaringClassPartitions) {
            Form.Section section = new Form.Section();
            section.setHeading(declaringClass.getSimpleName());
            sections.add(section);
            List<String> propertyNames = declaringClassPartitions.get(declaringClass);
            for (String propertyName : propertyNames) {
                Class<?> type = beanAdapter.getType(propertyName);
                addControl(beanAdapter, propertyName, type, section);
            }
        }
    }
}
Also used : BeanMonitor(org.apache.pivot.beans.BeanMonitor) Form(org.apache.pivot.wtk.Form) HashMap(org.apache.pivot.collections.HashMap) Method(java.lang.reflect.Method) List(org.apache.pivot.collections.List) ArrayList(org.apache.pivot.collections.ArrayList) BeanAdapter(org.apache.pivot.beans.BeanAdapter) Component(org.apache.pivot.wtk.Component)

Aggregations

Method (java.lang.reflect.Method)1 BeanAdapter (org.apache.pivot.beans.BeanAdapter)1 BeanMonitor (org.apache.pivot.beans.BeanMonitor)1 ArrayList (org.apache.pivot.collections.ArrayList)1 HashMap (org.apache.pivot.collections.HashMap)1 List (org.apache.pivot.collections.List)1 Component (org.apache.pivot.wtk.Component)1 Form (org.apache.pivot.wtk.Form)1