001: /*
002: * Javu WingS - Lightweight Java Component Set
003: * Copyright (c) 2005-2007 Krzysztof A. Sadlocha
004: * e-mail: ksadlocha@programics.com
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: */
020:
021: package com.javujavu.javux.wings.item;
022:
023: import java.awt.event.ActionEvent;
024: import java.awt.event.ActionListener;
025: import java.awt.event.FocusEvent;
026: import java.awt.event.FocusListener;
027: import java.awt.event.ItemEvent;
028: import java.awt.event.ItemListener;
029: import java.awt.event.KeyEvent;
030: import com.javujavu.javux.wings.ShortcutAdapter;
031: import com.javujavu.javux.wings.WingCombo;
032: import com.javujavu.javux.wings.WingComponent;
033: import com.javujavu.javux.wings.WingSpinner;
034: import com.javujavu.javux.wings.WingTextField;
035:
036: /**
037: * This class implements <code>ItemEditor</code>
038: * <br><br>
039: * <b>This class is thread safe.</b>
040: **/
041: public class DefaultItemEditor implements ItemEditor, FocusListener,
042: ItemListener, ActionListener {
043: protected ItemOwner owner;
044: protected WingComponent editor;
045:
046: protected final ShortcutAdapter enter = new ShortcutAdapter(
047: KeyEvent.VK_ENTER, 0, this );
048: protected final ShortcutAdapter esc = new ShortcutAdapter(
049: KeyEvent.VK_ESCAPE, 0, this );
050:
051: public WingComponent getComponent(ItemOwner owner, Object value,
052: Object context) {
053: if (value instanceof ItemEditor) {
054: return ((ItemEditor) value).getComponent(owner, value,
055: context);
056: }
057: if (value instanceof String) {
058: WingTextField editor = getTextFieldEditor(owner);
059: editor.setText(value.toString());
060: return editor;
061: }
062: if (value instanceof Integer) {
063: WingSpinner editor = getSpinEditor(owner);
064: editor.setValue(((Integer) value).intValue());
065: return editor;
066: }
067: if (value instanceof Boolean) {
068: owner.commitEdit(new Boolean(!((Boolean) value)
069: .booleanValue()));
070: }
071: return null;
072: }
073:
074: public WingTextField getTextFieldEditor(ItemOwner owner) {
075: WingTextField editor = new WingTextField();
076: editor.addFocusListener(this );
077: editor.addShortcut(enter);
078: editor.addShortcut(esc);
079: this .editor = editor;
080: this .owner = owner;
081: return editor;
082: }
083:
084: public WingCombo getComboEditor(ItemOwner owner, boolean editable) {
085: WingCombo editor = new WingCombo(editable);
086: editor.addFocusListener(this );
087: editor.addShortcut(enter);
088: editor.addShortcut(esc);
089: editor.addItemListener(this );
090: this .editor = editor;
091: this .owner = owner;
092: return editor;
093: }
094:
095: public WingSpinner getSpinEditor(ItemOwner owner) {
096: WingSpinner editor = new WingSpinner();
097: editor.addFocusListener(this );
098: editor.addShortcut(enter);
099: editor.addShortcut(esc);
100: this .editor = editor;
101: this .owner = owner;
102: return editor;
103: }
104:
105: public Object getCommitedValue() {
106: WingComponent editor = this .editor;
107: if (editor instanceof WingTextField) {
108: return ((WingTextField) editor).getText();
109: } else if (editor instanceof WingCombo) {
110: return ((WingCombo) editor).getSelectedItem();
111: } else if (editor instanceof WingSpinner) {
112: return new Integer(((WingSpinner) editor).getValue());
113: }
114: return null;
115: }
116:
117: public void focusGained(FocusEvent e) {
118: }
119:
120: public void focusLost(FocusEvent e) {
121: ItemOwner owner = this .owner;
122: this .owner = null;
123: if (owner == null)
124: return;
125: owner.removeEditor();
126: }
127:
128: public void actionPerformed(ActionEvent e) {
129: Object src = e.getSource();
130: ItemOwner owner = this .owner;
131: if (owner == null || (src != esc && src != enter))
132: return;
133: this .owner = null;
134:
135: ((WingComponent) owner).requestFocus();
136: Object value;
137: if (src == enter && (value = getCommitedValue()) != null) {
138: owner.commitEdit(value);
139: } else {
140: owner.removeEditor();
141: }
142: }
143:
144: public void itemStateChanged(ItemEvent e) {
145: ItemOwner owner = this .owner;
146: if (owner == null || e.getItem() == null)
147: return;
148: this .owner = null;
149: ((WingComponent) owner).requestFocus();
150: Object value = getCommitedValue();
151: if (value != null) {
152: owner.commitEdit(value);
153: } else
154: owner.removeEditor();
155: }
156: }
|