All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jboss.as.console.client.widgets.forms.FormMetaData Maven / Gradle / Ivy

Go to download

Bundles the core AS7 console as a GWT module. Includes minor customizations to support extensions.

There is a newer version: 0.7.0.Final
Show newest version
/*
 * JBoss, Home of Professional Open Source
 * Copyright 2011 Red Hat Inc. and/or its affiliates and other contributors
 * as indicated by the @author tags. All rights reserved.
 * See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * This copyrighted material is made available to anyone wishing to use,
 * modify, copy, or redistribute it subject to the terms and conditions
 * of the GNU Lesser General Public License, v. 2.1.
 * This program is distributed in the hope that it will be useful, but WITHOUT A
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
 * You should have received a copy of the GNU Lesser General Public License,
 * v.2.1 along with this distribution; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA  02110-1301, USA.
 */
package org.jboss.as.console.client.widgets.forms;

import org.jboss.as.console.client.Console;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Aggregator for PropertyBinding instances.  Allows searching and grouping.
 *
 * @author Stan Silvert [email protected] (C) 2011 Red Hat Inc.
 */
public class FormMetaData {
    public static final String DEFAULT_TAB = Console.CONSTANTS.common_label_attributes();
    public static final String CUSTOM_TAB = "CUSTOM";

    private Comparator orderComparator = new Comparator() {
        @Override
        public int compare(PropertyBinding item1, PropertyBinding item2) {
            return item1.getOrder() - item2.getOrder();
        }
    };

    private List baseAttributes = new ArrayList();
    private Map> groupedAttributes = new LinkedHashMap>();
    private Map> tabbedAttributes = new LinkedHashMap>();
    private boolean isFlattened = false;
    private Class type;

    public FormMetaData(Class type, List propertyMetaData) {
        this.type = type;

        // Sort the input, this means that everything will be sorted appropriately
        // regardless of where it appears (including tabs)
        Collections.sort(propertyMetaData, orderComparator);

        // make sure default is first
        tabbedAttributes.put(DEFAULT_TAB, new ArrayList());

        for (PropertyBinding binding : propertyMetaData) {
           String subgroup = binding.getSubgroup();

           if ("".equals(subgroup)) {
               baseAttributes.add(binding);
           } else {
               List subgroupData = groupedAttributes.get(subgroup);
               if (subgroupData == null) {
                   subgroupData = new ArrayList();
                   groupedAttributes.put(subgroup, subgroupData);
               }
               subgroupData.add(binding);
           }

            List tabData = tabbedAttributes.get(binding.getTabName());
            if (tabData == null) {
                tabData = new ArrayList();
                tabbedAttributes.put(binding.getTabName(), tabData);
            }

            if(!CUSTOM_TAB.equals(binding.getTabName())) // items in CUSTOM_TAB will be skipped and need to be provided manually
                tabData.add(binding);

           
           if (binding.isFlattened()) isFlattened = true;
        }

        if (tabbedAttributes.get(DEFAULT_TAB).isEmpty()) tabbedAttributes.remove(DEFAULT_TAB);

        doGroupCheck();
    }

    // make sure all grouped attributes are on the same tab
    private void doGroupCheck() {
        if (!hasTabs()) return;

        for (String groupName : getGroupNames()) {
            String tabName = getGroupedAttribtes(groupName).get(0).getTabName();
            for (PropertyBinding propBinding : getGroupedAttribtes(groupName)) {
                if (!tabName.equals(propBinding.getTabName())) {
                    throw new RuntimeException("FormItem " + propBinding.getJavaName() + " must be on the same tab with all members of its subgroup.");
                }
            }
        }
    }

    public List getBaseAttributes() {
        return Collections.unmodifiableList(this.baseAttributes);
    }

    public void setGroupedAttributes(String groupName, List attributes) {
        groupedAttributes.put(groupName, attributes);
    }

    public List getGroupedAttribtes(String groupName) {
        return groupedAttributes.get(groupName);
    }

    /**
     * Returns all group names.  Calling iterator() on the returned Set
     * will give you an iterator that maintains the names in the order they were added.
     *
     * @return The group names.
     */
    public Set getGroupNames() {
        return groupedAttributes.keySet();
    }

    /**
     * Returns true if the Form contains one or more attributes that are accessed as
     * sub-attributes.
     *
     * @return true if the structure has been flattened, false otherwise.
     */
    public boolean isFlattened() {
        return this.isFlattened;
    }

    public boolean hasTabs() {
        return this.tabbedAttributes.size() > 1;
    }

    public Map> getTabbedAttributes() {
        return this.tabbedAttributes;
    }

    /**
     * Find a PropertyBinding with the given bean (Java Bean Name) property.
     * @param beanPropName The name of the bean property.
     * @return The PropertyBinding
     * @throws IllegalArgumentException if the PropertyBinding is not found.
     */
    public PropertyBinding findAttribute(String beanPropName) {
        for (PropertyBinding attrib : baseAttributes) {
            if (attrib.getJavaName().equals(beanPropName)) return attrib;
        }

        for (Map.Entry> entry : groupedAttributes.entrySet()) {
            for (PropertyBinding attrib : entry.getValue()) {
                if (attrib.getJavaName().equals(beanPropName)) return attrib;
            }
        }

        throw new IllegalArgumentException("Unknown Attribute with beanPropName name " + beanPropName);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy