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

com.anrisoftware.prefdialog.fields.buttongroup.ButtonGroupField Maven / Gradle / Ivy

/*
 * Copyright 2013-2015 Erwin Müller 
 *
 * This file is part of prefdialog-corefields.
 *
 * prefdialog-corefields is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or (at your
 * option) any later version.
 *
 * prefdialog-corefields is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with prefdialog-corefields. If not, see .
 */
package com.anrisoftware.prefdialog.fields.buttongroup;

import java.beans.PropertyVetoException;
import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.inject.Inject;
import javax.swing.AbstractListModel;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.ListModel;

import org.apache.commons.lang3.ArrayUtils;

import com.anrisoftware.globalpom.reflection.annotations.AnnotationAccess;
import com.anrisoftware.globalpom.reflection.annotations.AnnotationAccessFactory;
import com.anrisoftware.prefdialog.annotations.ButtonGroup;
import com.anrisoftware.prefdialog.annotations.HorizontalAlignment;
import com.anrisoftware.prefdialog.core.AbstractTitleField;
import com.google.inject.assistedinject.Assisted;

/**
 * Field with a group of buttons.
 * 
 * @author Erwin Mueller, [email protected]
 * @since 3.0
 */
@SuppressWarnings("serial")
public class ButtonGroupField extends AbstractTitleField {

    private static final String HORIZONTAL_ALIGNMENT_ELEMENT = "horizontalAlignment";

    private static final Class ANNOTATION_CLASS = ButtonGroup.class;

    private final ButtonGroupFieldLogger log;

    private final ButtonsGroupPanel buttonsGroupPanel;

    private final ButtonsRowPanel buttonsRowPanel;

    private AnnotationAccess fieldAnnotation;

    /**
     * @see ButtonGroupFieldFactory#create(Object, String)
     */
    @Inject
    ButtonGroupField(ButtonGroupFieldLogger logger,
            ButtonsGroupPanel buttonsGroupPanel,
            ButtonsRowPanel buttonsRowPanel, @Assisted Object parentObject,
            @Assisted String fieldName) {
        super(buttonsGroupPanel, parentObject, fieldName);
        this.log = logger;
        this.buttonsGroupPanel = buttonsGroupPanel;
        this.buttonsRowPanel = buttonsRowPanel;
        setup();
    }

    private void setup() {
        buttonsGroupPanel.setButtonsRowPanel(buttonsRowPanel);
    }

    @Inject
    void setBeanAccessFactory(AnnotationAccessFactory annotationAccessFactory) {
        this.fieldAnnotation = annotationAccessFactory.create(ANNOTATION_CLASS,
                getAccessibleObject());
        setupHorizontalAlignment();
    }

    @Override
    public void setName(String name) {
        super.setName(name);
        buttonsGroupPanel.setName(name);
        buttonsRowPanel.setName(name);
    }

    @Override
    public void setValue(Object value) throws PropertyVetoException {
        super.setValue(value);
        setupButtons();
    }

    @Override
    public void restoreInput() throws PropertyVetoException {
    }

    /**
     * Sets the horizontal alignment of the button group.
     * 
     * @param alignment
     *            the {@link HorizontalAlignment}.
     */
    public void setHorizontalAlignment(HorizontalAlignment alignment) {
        buttonsGroupPanel.setHorizontalAlignment(alignment);
        log.horizontalAlignmentSet(this, alignment);
    }

    /**
     * Returns the horizontal alignment of the button group.
     * 
     * @return the {@link HorizontalAlignment}.
     */
    public HorizontalAlignment getHorizontalAlignment() {
        return buttonsGroupPanel.getHorizontalAlignment();
    }

    /**
     * Returns the model that contains the button actions.
     * 
     * @return the {@link ListModel} of {@link Action} actions.
     */
    public ListModel getModel() {
        return buttonsRowPanel.getModel();
    }

    /**
     * Returns the button in this buttons row panel with the specified index.
     * 
     * @param index
     *            the index.
     * 
     * @return the {@link JButton} with the index.
     * 
     * @throws IndexOutOfBoundsException
     *             if the index is out of range.
     */
    public JButton getButton(int index) {
        return buttonsRowPanel.getButton(index);
    }

    private void setupHorizontalAlignment() {
        HorizontalAlignment alignment = fieldAnnotation
                .getValue(HORIZONTAL_ALIGNMENT_ELEMENT);
        setHorizontalAlignment(alignment);
    }

    private void setupButtons() {
        List actions = asList(getValue());
        AbstractListModel model = createModel(actions);
        buttonsRowPanel.setModel(model);
    }

    private AbstractListModel createModel(final List actions) {
        return new AbstractListModel() {

            @Override
            public int getSize() {
                return actions.size();
            }

            @Override
            public Action getElementAt(int i) {
                return actions.get(i);
            }
        };
    }

    @SuppressWarnings("unchecked")
    private List asList(Object value) {
        if (ArrayUtils.isSameType(value, new Action[0])) {
            return Arrays.asList((Action[]) value);
        } else if (value instanceof Iterable) {
            return createList((Iterable) value);
        }
        throw log.valueNotList(value);
    }

    private List createList(Iterable it) {
        List list = new ArrayList();
        for (Action action : it) {
            list.add(action);
        }
        return list;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy