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

nl.cloudfarming.client.util.swing.DisabledItemsComboBox Maven / Gradle / Ivy

There is a newer version: 13.03-beta
Show newest version
/**
 * Copyright (C) 2010-2012 Agrosense [email protected]
 *
 * Licensed under the Eclipse Public License - v 1.0 (the "License"); you may
 * not use this file except in compliance with the License. You may obtain a
 * copy of the License at
 *
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package nl.cloudfarming.client.util.swing;

import java.awt.Component;
import java.util.HashSet;
import java.util.Set;
import javax.swing.ComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JList;
import javax.swing.UIManager;
import javax.swing.plaf.basic.BasicComboBoxRenderer;

/**
 *
 * @author merijn
 */
public class DisabledItemsComboBox extends JComboBox {

    private Set disabled_items = new HashSet();

    public DisabledItemsComboBox() {
        super();
        super.setRenderer(new DisabledItemsRenderer());
    }

    public DisabledItemsComboBox(ComboBoxModel boxModel) {
        super(boxModel);
        super.setRenderer(new DisabledItemsRenderer());
    }

    public void addItem(T anObject, boolean disabled) {
        super.addItem(anObject);
        if (disabled) {
            disabled_items.add(getItemCount() - 1);
        }
    }

    public void setDisabled(int index) {
        disabled_items.add(index);
    }

    public void setDisabled(T anObject) {
        for (int i = 0; i < getItemCount(); i++) {
            if (anObject.equals(getItemAt(i))) {
                disabled_items.add(i);
            }
        }
    }

    @Override
    public void removeAllItems() {
        super.removeAllItems();
        disabled_items = new HashSet();
    }

    @Override
    public void removeItemAt(final int anIndex) {
        super.removeItemAt(anIndex);
        disabled_items.remove(anIndex);
    }

    @Override
    public void removeItem(final Object anObject) {
        for (int i = 0; i <= getItemCount(); i++) {
            if (getItemAt(i) == anObject) {
                disabled_items.remove(i);
            }
        }
        super.removeItem(anObject);
    }

    @Override
    public void setSelectedIndex(int index) {
        if (!disabled_items.contains(index)) {
            super.setSelectedIndex(index);
        }
    }

    private class DisabledItemsRenderer extends BasicComboBoxRenderer {

        @Override
        public Component getListCellRendererComponent(JList list,
                Object value,
                int index,
                boolean isSelected,
                boolean cellHasFocus) {

            if (isSelected) {
                setBackground(list.getSelectionBackground());
                setForeground(list.getSelectionForeground());
            } else {
                setBackground(list.getBackground());
                setForeground(list.getForeground());
            }
            if (disabled_items.contains(index)) {
                setBackground(list.getBackground());
                setForeground(UIManager.getColor("Label.disabledForeground"));
            }
            setFont(list.getFont());
            setText((value == null) ? "" : value.toString());
            return this;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy