001: /*
002: * Copyright (C) 2005 Jeff Tassin
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018:
019: package com.jeta.swingbuilder.gui.properties.editors;
020:
021: import java.awt.BorderLayout;
022: import java.awt.Component;
023: import java.awt.event.KeyEvent;
024:
025: import javax.swing.JPanel;
026: import javax.swing.JTextArea;
027: import javax.swing.UIManager;
028: import javax.swing.text.AttributeSet;
029: import javax.swing.text.BadLocationException;
030: import javax.swing.text.PlainDocument;
031:
032: import com.jeta.open.gui.framework.JETADialog;
033: import com.jeta.open.gui.utils.JETAToolbox;
034: import com.jeta.open.i18n.I18N;
035: import com.jeta.swingbuilder.gui.components.text.TextPropertyView;
036: import com.jeta.swingbuilder.gui.properties.JETAPropertyEditor;
037:
038: public class StringEditor extends JETAPropertyEditor {
039: private JPanel m_panel;
040: private EditorTextArea m_field = new EditorTextArea();
041: private boolean m_field_is_null = true;
042:
043: public StringEditor() {
044: m_panel = new JPanel();
045: m_panel.setLayout(new BorderLayout());
046:
047: m_field.setRows(1);
048: m_field.setLineWrap(false);
049: m_field.setWrapStyleWord(false);
050: m_field.setBorder(javax.swing.BorderFactory.createEmptyBorder(
051: 2, 2, 2, 0));
052:
053: m_panel.add(m_field, BorderLayout.CENTER);
054:
055: m_panel.setBackground(UIManager.getColor("Table.background"));
056:
057: /**
058: * We need to trap document changes so that we can differentiate between
059: * a NULL string and a zero length string.
060: */
061: m_field.setDocument(new PlainDocument() {
062: public void insertString(int offs, String str,
063: AttributeSet a) throws BadLocationException {
064: super .insertString(offs, str, a);
065: if (str != null && m_field_is_null) {
066: m_field_is_null = false;
067: }
068: }
069: });
070: }
071:
072: public Component getCustomEditor() {
073: return m_panel;
074: }
075:
076: /**
077: * Invokes a dialog used to update the property
078: */
079: public void invokePropertyDialog(Component comp) {
080: TextPropertyView view = new TextPropertyView(
081: (String) getValue());
082: JETADialog dlg = (JETADialog) JETAToolbox.createDialog(
083: JETADialog.class, comp, true);
084: dlg.setPrimaryPanel(view);
085: dlg.setTitle(I18N.getLocalizedMessage("Set Text Property"));
086: dlg.setInitialFocusComponent((javax.swing.JComponent) view
087: .getComponentByName(TextPropertyView.ID_TEXT_AREA));
088: dlg.setSize(dlg.getPreferredSize());
089: dlg.showCenter();
090: if (dlg.isOk()) {
091: m_field.setText(view.getText());
092: setValue(view.getText());
093: }
094: }
095:
096: /**
097: * @return true if this editor supports custom editing inline in the
098: * property table. Property types such as the Java primitives and
099: * Strings support inline editing.
100: */
101: public boolean supportsInlineEditing() {
102: return true;
103: }
104:
105: /**
106: *
107: */
108: public boolean supportsCustomEditor() {
109: return true;
110: }
111:
112: public void setValue(Object value) {
113: super .setValue(value);
114: if (value != null) {
115: m_field.setText(value.toString());
116: }
117: }
118:
119: public Object getValue() {
120: Object value = super .getValue();
121: /**
122: * Need to differentiate between NULL strings and zero length strings
123: */
124: if (value == null && m_field.getText().length() == 0
125: && m_field_is_null) {
126: return null;
127: }
128: return m_field.getText();
129: }
130:
131: /**
132: * Specialization of JTextArea that we need for this component
133: *
134: * @author Jeff Tassin
135: */
136: public class EditorTextArea extends JTextArea {
137: /**
138: * Intercept commands to set field to null
139: */
140: public void processKeyEvent(KeyEvent evt) {
141: if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
142: setValue(getText());
143: } else
144: super.processKeyEvent(evt);
145: }
146: }
147:
148: }
|