001package ball.beans;
002/*-
003 * ##########################################################################
004 * Utilities
005 * $Id: PropertyDescriptorsTableModel.java 6118 2020-06-04 19:31:45Z 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 - 2020 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.util.Arrays;
029import lombok.ToString;
030
031import static org.apache.commons.lang3.StringUtils.EMPTY;
032
033/**
034 * {@code BeanInfo} {@link PropertyDescriptor properties}
035 * {@link javax.swing.table.TableModel} implementation.
036 *
037 * @author {@link.uri mailto:ball@hcf.dev Allen D. Ball}
038 * @version $Revision: 6118 $
039 */
040@ToString
041public class PropertyDescriptorsTableModel
042             extends ArrayListTableModel<PropertyDescriptor> {
043    private static final long serialVersionUID = -4799510974827528198L;
044
045    private static final String R = "R";
046    private static final String W = "W";
047
048    /**
049     * Sole constructor.
050     *
051     * @param   rows            The {@link PropertyDescriptor}s.
052     */
053    public PropertyDescriptorsTableModel(PropertyDescriptor[] rows) {
054        super(Arrays.asList(rows),
055              "Name", "Mode", "Type",
056              "isHidden", "isBound", "isConstrained");
057    }
058
059    @Override
060    protected Object getValueAt(PropertyDescriptor row, int x) {
061        Object value = null;
062
063        switch (x) {
064        case 0:
065        default:
066            value = row.getName();
067            break;
068
069        case 1:
070            value = getMode(row);
071            break;
072
073        case 2:
074            if (row.getReadMethod() != null) {
075                value = row.getReadMethod().getGenericReturnType();
076            } else if (row.getWriteMethod() != null) {
077                value = row.getWriteMethod().getGenericParameterTypes()[0];
078            } else {
079                value = row.getPropertyType();
080            }
081            break;
082
083        case 3:
084            value = row.isHidden();
085            break;
086
087        case 4:
088            value = row.isBound();
089            break;
090
091        case 5:
092            value = row.isConstrained();
093            break;
094        }
095
096        return value;
097    }
098
099    private String getMode(PropertyDescriptor descriptor) {
100        String mode =
101            getMode(descriptor.getReadMethod(), descriptor.getWriteMethod());
102
103        if (descriptor instanceof IndexedPropertyDescriptor) {
104            mode += "[";
105            mode += getMode((IndexedPropertyDescriptor) descriptor);
106            mode += "]";
107        }
108
109        return mode;
110    }
111
112    private String getMode(IndexedPropertyDescriptor descriptor) {
113        return getMode(descriptor.getIndexedReadMethod(),
114                       descriptor.getIndexedWriteMethod());
115    }
116
117    private String getMode(Method read, Method write) {
118        return ((read != null) ? R : EMPTY) + ((write != null) ? W : EMPTY);
119    }
120}