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

com.rapiddweller.common.ui.swing.delegate.PropertyComboBox Maven / Gradle / Ivy

Go to download

'rapiddweller Common' is an open source Java library forked from Databene Commons by Volker Bergmann. It provides extensions to the Java core library by utility classes, abstract concepts and concrete implementations.

There is a newer version: 2.0.1-jdk-11
Show newest version
/*
 * Copyright (C) 2004-2015 Volker Bergmann ([email protected]).
 * All rights reserved.
 *
 * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
 * 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 com.rapiddweller.common.ui.swing.delegate;

import com.rapiddweller.common.BeanUtil;
import com.rapiddweller.common.NullSafeComparator;
import com.rapiddweller.common.bean.ObservableBean;
import com.rapiddweller.common.converter.ToStringConverter;
import com.rapiddweller.common.ui.I18NSupport;

import javax.swing.DefaultListCellRenderer;
import javax.swing.JComboBox;
import javax.swing.JList;
import javax.swing.event.ListDataEvent;
import javax.swing.event.ListDataListener;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

/**
 * {@link JComboBox} implementation that serves as delegate of a property of a JavaBean object.
 * Created at 01.12.2008 07:44:32
 *
 * @param  The item type of the {@link JComboBox}' elements
 * @author Volker Bergmann
 * @since 0.5.13
 */
public class PropertyComboBox extends JComboBox {

  private static final long serialVersionUID = -5039037135360506124L;

  private final Object bean;
  private final String propertyName;
  /**
   * The Locked.
   */
  boolean locked;


  /**
   * Instantiates a new Property combo box.
   *
   * @param bean         the bean
   * @param propertyName the property name
   * @param i18n         the 18 n
   * @param prefix       the prefix
   * @param values       the values
   */
  @SafeVarargs
  public PropertyComboBox(Object bean, String propertyName, I18NSupport i18n, String prefix, E... values) {
    super(values);
    this.bean = bean;
    this.propertyName = propertyName;
    this.locked = true;
    Listener listener = new Listener();
    this.addActionListener(listener);
    if (bean instanceof ObservableBean) {
      ((ObservableBean) bean).addPropertyChangeListener(propertyName, listener);
    }
    this.getModel().addListDataListener(listener);
    this.setRenderer(new Renderer(i18n, prefix));
    this.locked = false;
    refresh();
  }

  /**
   * reads the current property value and writes it to the text field.
   */
  void refresh() {
    if (!locked) {
      locked = true;
      Object propertyValue = BeanUtil.getPropertyValue(bean, propertyName);
      Object selectedItem = getSelectedItem();
      if (!NullSafeComparator.equals(selectedItem, propertyValue)) {
        setSelectedItem(propertyValue);
      }
      locked = false;
    }
  }

  /**
   * writes the current text field content to the property.
   */
  void update() {
    if (!locked) {
      locked = true;
      Object propertyValue = BeanUtil.getPropertyValue(bean, propertyName);
      Object selectedItem = getSelectedItem();
      if (!NullSafeComparator.equals(selectedItem, propertyValue)) {
        BeanUtil.setPropertyValue(bean, propertyName, selectedItem);
      }
      locked = false;
    }
  }

  /**
   * The type Listener.
   */
  class Listener implements PropertyChangeListener, ListDataListener, ActionListener {

    @Override
    public void propertyChange(PropertyChangeEvent evt) {
      refresh();
    }

    @Override
    public void contentsChanged(ListDataEvent evt) {
      update();
    }

    @Override
    public void intervalAdded(ListDataEvent evt) {
      update();
    }

    @Override
    public void intervalRemoved(ListDataEvent evt) {
      update();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
      update();
    }

  }

  /**
   * The type Renderer.
   */
  static final class Renderer extends DefaultListCellRenderer {

    private static final long serialVersionUID = 8358429951305253637L;
    private final ToStringConverter converter = new ToStringConverter();
    private final I18NSupport i18n;
    private final String prefix;

    /**
     * Instantiates a new Renderer.
     *
     * @param i18n   the 18 n
     * @param prefix the prefix
     */
    public Renderer(I18NSupport i18n, String prefix) {
      this.i18n = i18n;
      this.prefix = prefix;
    }

    @Override
    public Component getListCellRendererComponent(JList list, Object value,
                                                  int index, boolean isSelected, boolean cellHasFocus) {
      String text = (i18n != null ? i18n.getString(prefix + converter.convert(value)) : String.valueOf(value));
      return super.getListCellRendererComponent(list, text, index, isSelected, cellHasFocus);
    }
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy