/*
 * Copyright (c) Josh Reed, 2009.
 *
 * 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.andrill.coretools.ui.widget.swing;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.text.JTextComponent;

import org.andrill.coretools.model.edit.EditableProperty;
import org.andrill.coretools.ui.widget.AbstractWidget;

/**
 * A widget for editing text.
 * 
 * @author Josh Reed (jareed@andrill.org)
 */
public class TextWidget extends AbstractWidget implements FocusListener, ActionListener {
	protected JTextComponent component = null;

	/**
	 * Create a new TextWidget.
	 * 
	 * @param p
	 *            the editable property.
	 * @param readOnly
	 *            the read only flag.
	 */
	public TextWidget(final EditableProperty p, final boolean readOnly) {
		super(p, readOnly);
	}

	public void actionPerformed(final ActionEvent e) {
		fireChange();
	}

	public void focusGained(final FocusEvent e) {
		// ignored
	}

	public void focusLost(final FocusEvent e) {
		fireChange();
	}

	@Override
	protected JTextComponent getEditableUI() {
		if (component == null) {
			if (TEXTAREA_TYPE.equalsIgnoreCase(property.getWidgetType())) {
				JTextArea widget = new JTextArea(property.getValue(), 3, 40);
				widget.setLineWrap(true);
				widget.setWrapStyleWord(true);
				component = widget;
			} else {
				JTextField widget = new JTextField(property.getValue(), 10);
				widget.addActionListener(this);
				component = widget;
			}
			component.setBorder(BorderFactory.createEtchedBorder());
			component.setInputVerifier(new PropertyInputVerifier(property));
			component.addFocusListener(this);
		}
		return component;
	}

	@Override
	protected Object getReadOnlyUI() {
		if (TEXTAREA_TYPE.equalsIgnoreCase(property.getWidgetType())) {
			JTextArea widget = new JTextArea(property.getValue(), 1, 40);
			widget.setLineWrap(true);
			widget.setWrapStyleWord(true);
			widget.setEditable(false);
			widget.setBackground(new JLabel().getBackground());
			widget.setBorder(null);
			return widget;
		} else {
			return new JLabel(property.getValue());
		}
	}

	@Override
	protected String getWidgetValue() {
		String value = (component == null) ? null : component.getText();
		if ((value != null) && "".equals(value.trim())) {
			value = null;
		}
		return value;
	}

	/**
	 * {@inheritDoc}
	 */
	public void setValue(final String value) {
		if (component != null) {
			component.setText(value);
		}
	}
}

@@ -906,16 +906,99 @@
 tField;%0A
+import javax.swing.event.DocumentEvent;%0Aimport javax.swing.event.DocumentListener;%0A
 import j
@@ -1029,16 +1029,16 @@
 ponent;%0A
-
 %0Aimport 
@@ -1315,16 +1315,34 @@
 Listener
+, DocumentListener
  %7B%0A%09prot
@@ -1823,24 +1823,223 @@
 Change();%0A%09%7D
+%0A%09%0A%09public void insertUpdate(DocumentEvent e) %7B%0A%09%09fireChange();%0A%09%7D%0A%09%0A%09public void removeUpdate(DocumentEvent e) %7B%0A%09%09fireChange();%0A%09%7D%0A%09%0A%09public void changedUpdate(DocumentEvent e) %7B%0A%09%09fireChange();%0A%09%7D
 %0A%0A%09@Override
@@ -2177,16 +2177,16 @@
 e())) %7B%0A
-
 %09%09%09%09JTex
@@ -2239,24 +2239,76 @@
 (), 3, 40);%0A
+%09%09%09%09widget.getDocument().addDocumentListener(this);%0A
 %09%09%09%09widget.s

Update description text on any edit (previously updated only on focus change)