001package ball.beans;
002/*-
003 * ##########################################################################
004 * Utilities
005 * $Id: PropertyDescriptorsTableModel.java 7334 2021-01-12 22:57:41Z ball $
006 * $HeadURL: svn+ssh://svn.hcf.dev/var/spool/scm/repository.svn/ball-util/trunk/src/main/java/ball/beans/PropertyDescriptorsTableModel.java $
007 * %%
008 * Copyright (C) 2008 - 2021 Allen D. Ball
009 * %%
010 * Licensed under the Apache License, Version 2.0 (the "License");
011 * you may not use this file except in compliance with the License.
012 * You may obtain a copy of the License at
013 *
014 *      http://www.apache.org/licenses/LICENSE-2.0
015 *
016 * Unless required by applicable law or agreed to in writing, software
017 * distributed under the License is distributed on an "AS IS" BASIS,
018 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
019 * See the License for the specific language governing permissions and
020 * limitations under the License.
021 * ##########################################################################
022 */
023import ball.swing.table.ArrayListTableModel;
024import java.beans.BeanInfo;
025import java.beans.IndexedPropertyDescriptor;
026import java.beans.PropertyDescriptor;
027import java.lang.reflect.Method;
028import java.lang.reflect.Type;
029import java.util.Arrays;
030import lombok.ToString;
031
032import static org.apache.commons.lang3.StringUtils.EMPTY;
033
034/**
035 * {@code BeanInfo} {@link PropertyDescriptor properties}
036 * {@link javax.swing.table.TableModel} implementation.
037 *
038 * @author {@link.uri mailto:ball@hcf.dev Allen D. Ball}
039 * @version $Revision: 7334 $
040 */
041@ToString
042public class PropertyDescriptorsTableModel
043             extends ArrayListTableModel<PropertyDescriptor> {
044    private static final long serialVersionUID = -4799510974827528198L;
045
046    private static final String R = "R";
047    private static final String W = "W";
048
049    /**
050     * Sole constructor.
051     *
052     * @param   rows            The {@link PropertyDescriptor}s.
053     */
054    public PropertyDescriptorsTableModel(PropertyDescriptor[] rows) {
055        super(Arrays.asList(rows),
056              "Name", "Mode", "Type",
057              "isHidden", "isBound", "isConstrained");
058    }
059
060    @Override
061    protected Object getValueAt(PropertyDescriptor row, int x) {
062        Object value = null;
063
064        switch (x) {
065        case 0:
066        default:
067            value = row.getName();
068            break;
069
070        case 1:
071            value = getMode(row);
072            break;
073
074        case 2:
075            value = getPropertyType(row);
076            break;
077
078        case 3:
079            value = row.isHidden();
080            break;
081
082        case 4:
083            value = row.isBound();
084            break;
085
086        case 5:
087            value = row.isConstrained();
088            break;
089        }
090
091        return value;
092    }
093
094    private String getMode(PropertyDescriptor descriptor) {
095        String mode =
096            getMode(descriptor.getReadMethod(), descriptor.getWriteMethod());
097
098        if (descriptor instanceof IndexedPropertyDescriptor) {
099            mode += "[";
100            mode += getMode((IndexedPropertyDescriptor) descriptor);
101            mode += "]";
102        }
103
104        return mode;
105    }
106
107    private String getMode(IndexedPropertyDescriptor descriptor) {
108        return getMode(descriptor.getIndexedReadMethod(),
109                       descriptor.getIndexedWriteMethod());
110    }
111
112    private String getMode(Method read, Method write) {
113        return ((read != null) ? R : EMPTY) + ((write != null) ? W : EMPTY);
114    }
115
116    private Type getPropertyType(PropertyDescriptor descriptor) {
117        Type type = null;
118
119        if (descriptor instanceof IndexedPropertyDescriptor) {
120            type = getPropertyType((IndexedPropertyDescriptor) descriptor);
121        } else if (descriptor.getReadMethod() != null) {
122            type = descriptor.getReadMethod().getGenericReturnType();
123        } else if (descriptor.getWriteMethod() != null) {
124            type = descriptor.getWriteMethod().getGenericParameterTypes()[0];
125        } else {
126            type = descriptor.getPropertyType();
127        }
128
129        return type;
130    }
131
132    private Type getPropertyType(IndexedPropertyDescriptor descriptor) {
133        Type type = null;
134
135        if (descriptor.getIndexedReadMethod() != null) {
136            type = descriptor.getIndexedReadMethod().getGenericReturnType();
137        } else if (descriptor.getIndexedWriteMethod() != null) {
138            type = descriptor.getIndexedWriteMethod().getGenericParameterTypes()[1];
139        } else {
140            type = descriptor.getIndexedPropertyType();
141        }
142
143        return type;
144    }
145}