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

org.databene.commons.ui.swing.delegate.PropertyTextArea Maven / Gradle / Ivy

Go to download

'databene commons' is an open source Java library by Volker Bergmann. It provides extensions to the Java core library by utility classes, abstract concepts and concrete implementations.

There is a newer version: 1.0.11
Show newest version
/*
 * Copyright (C) 2004-2014 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 org.databene.commons.ui.swing.delegate;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

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 org.databene.commons.BeanUtil;
import org.databene.commons.StringUtil;
import org.databene.commons.bean.ObservableBean;
import org.databene.commons.converter.ToStringConverter;

/**
 * {@link JTextArea} implementation that serves as delegate of a property of a JavaBean object.

* Created: 22.08.2010 07:29:11 * @since 0.5.13 * @author Volker Bergmann */ public class PropertyTextArea extends JTextArea { /** uid for serialization */ private static final long serialVersionUID = 4917700588809725874L; // attributes ------------------------------------------------------------------------------------------------------ private Object bean; private String propertyName; private ToStringConverter toStringConverter; boolean locked; // constructor ----------------------------------------------------------------------------------------------------- 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; } } 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