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

com.cedarsoft.spring.rcp.binding.LabelBinding Maven / Gradle / Ivy

package com.cedarsoft.spring.rcp.binding;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.springframework.binding.form.FormModel;
import org.springframework.richclient.form.binding.support.CustomBinding;
import org.springframework.richclient.selection.binding.support.LabelProvider;

import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.text.JTextComponent;
import java.awt.Color;

/**
 * Binding for a label
 */
public class LabelBinding extends CustomBinding {
  @NotNull
  private final JComponent control;

  @Nullable
  private final LabelProvider labelProvider;
  @Nullable
  private final Color defaultForeground;

  /**
   * Creates a new label binding
   *
   * @param formModel           the form model
   * @param formPropertyPath    the form property path
   * @param control             the control
   * @param requiredSourceClass thre required source class
   * @param labelProvider       the label provider
   */
  public LabelBinding( @NotNull FormModel formModel, @NotNull String formPropertyPath, @NotNull JComponent control, @Nullable Class requiredSourceClass, @Nullable LabelProvider labelProvider ) {
    super( formModel, formPropertyPath, requiredSourceClass );
    this.control = control;
    defaultForeground = this.control.getForeground();
    this.labelProvider = labelProvider;
  }

  @Override
  protected void valueModelChanged( Object newValue ) {
    setText( newValue );
    control.setForeground( getForeground( newValue ) );
  }

  private void setText( @Nullable Object newValue ) {
    JComponent component;
    if ( control instanceof JScrollPane ) {
      component = ( JComponent ) ( ( JScrollPane ) control ).getViewport().getView();
    } else {
      component = control;
    }

    if ( component instanceof JTextComponent ) {
      ( ( JTextComponent ) component ).setText( format( newValue ) );
    } else {
      ( ( JLabel ) component ).setText( format( newValue ) );
    }
  }

  @Nullable
  private Color getForeground( @Nullable Object newValue ) {
    if ( labelProvider != null && labelProvider instanceof ColoredLabelProvider ) {
      Color customColor = ( ( ColoredLabelProvider ) labelProvider ).getForeground( newValue );
      if ( customColor == null ) {
        return defaultForeground;
      } else {
        return customColor;
      }
    }

    return defaultForeground;
  }

  @NotNull
  private String format( @Nullable Object newValue ) {
    if ( labelProvider != null ) {
      return labelProvider.getLabel( newValue );
    }
    return String.valueOf( newValue );
  }

  @Override
  protected JComponent doBindControl() {
    valueModelChanged( getValue() );
    return control;
  }

  @Override
  protected void readOnlyChanged() {
  }

  @Override
  protected void enabledChanged() {
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy