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

com.rapiddweller.common.ui.swing.delegate.PropertyTextArea 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.StringUtil;
import com.rapiddweller.common.bean.ObservableBean;
import com.rapiddweller.common.converter.ToStringConverter;

import javax.swing.JTextArea;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

/**
 * {@link JTextArea} implementation that serves as delegate of a property of a JavaBean object.
 * Created: 22.08.2010 07:29:11
 *
 * @author Volker Bergmann
 * @since 0.5.13
 */
public class PropertyTextArea extends JTextArea {

  /**
   * uid for serialization
   */
  private static final long serialVersionUID = 4917700588809725874L;

  // attributes ------------------------------------------------------------------------------------------------------

  private final Object bean;
  private final String propertyName;

  private final ToStringConverter toStringConverter;
  /**
   * The Locked.
   */
  boolean locked;

  // constructor -----------------------------------------------------------------------------------------------------

  /**
   * Instantiates a new Property text area.
   *
   * @param bean         the bean
   * @param propertyName the property name
   */
  public PropertyTextArea(Object bean, String propertyName) {
    this.bean = bean;
    this.propertyName = propertyName;
    this.toStringConverter = new ToStringConverter();
    this.setLineWrap(true);
    this.setWrapStyleWord(true);
    this.setRows(5);
    this.locked = true;
    Listener listener = new Listener();
    if (bean instanceof ObservableBean) {
      ((ObservableBean) bean).addPropertyChangeListener(propertyName, listener);
    }
    this.getDocument().addDocumentListener(listener);
    this.locked = false;
    refresh();
  }

  // event handlers --------------------------------------------------------------------------------------------------

  /**
   * reads the current property value and writes it to the text field.
   */
  void refresh() {
    if (!locked) {
      locked = true;
      Object propertyValue = BeanUtil.getPropertyValue(bean, propertyName);
      String text = toStringConverter.convert(propertyValue);
      text = StringUtil.escape(text);
      if (!getText().equals(text)) {
        setText(text);
      }
      locked = false;
    }
  }

  /**
   * writes the current text field content to the property.
   */
  void update() {
    if (!locked) {
      locked = true;
      Document document = getDocument();
      String text;
      try {
        text = document.getText(0, document.getLength());
      } catch (BadLocationException e) {
        throw new RuntimeException(e);
      }
      text = StringUtil.unescape(text);
      if (!text.equals(BeanUtil.getPropertyValue(bean, propertyName))) {
        BeanUtil.setPropertyValue(bean, propertyName, text);
      }
      locked = false;
    }
  }

  /**
   * The type Listener.
   */
  class Listener implements PropertyChangeListener, DocumentListener {

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

    @Override
    public void changedUpdate(DocumentEvent evt) {
      update();
    }

    @Override
    public void insertUpdate(DocumentEvent evt) {
      update();
    }

    @Override
    public void removeUpdate(DocumentEvent evt) {
      update();
    }

  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy