001: package org.gui4j.core.swing;
002:
003: import java.awt.Color;
004: import java.awt.Component;
005: import java.awt.Font;
006: import java.awt.event.FocusEvent;
007: import java.util.Map;
008:
009: import javax.swing.DefaultCellEditor;
010: import javax.swing.JTable;
011: import javax.swing.JTextField;
012: import javax.swing.JWindow;
013: import javax.swing.border.Border;
014: import javax.swing.border.LineBorder;
015: import javax.swing.text.AttributeSet;
016: import javax.swing.text.BadLocationException;
017: import javax.swing.text.PlainDocument;
018:
019: import org.gui4j.Gui4jCallBase;
020: import org.gui4j.core.Gui4jCall;
021:
022: public class Gui4jCellEditor extends DefaultCellEditor {
023:
024: // infos for notification callback of temporary edit value
025: private Gui4jCallBase mNotifyCallBase;
026: private Gui4jCall mNotifyCall;
027: private Map mNotifyParams;
028: private Object mNotifyParamsValueKey;
029:
030: public static Gui4jCellEditor createTextEditor(Font font,
031: boolean withBorder) {
032: NotificationDocument document = new NotificationDocument();
033: CellEditorTextField textField = new CellEditorTextField(
034: document, withBorder);
035: textField.setBorder(new LineBorder(Color.black));
036: textField.setFont(font);
037: Gui4jCellEditor editor = new Gui4jCellEditor(textField);
038: textField.cellEditor = editor;
039: document.setCellEditor(editor);
040: return editor;
041: }
042:
043: public Component getTableCellEditorComponent(JTable table,
044: Object value, boolean isSelected, int row, int column) {
045: Component c = super .getTableCellEditorComponent(table, value,
046: isSelected, row, column);
047: JTextField textField = (JTextField) c;
048: textField.selectAll();
049: return c;
050: }
051:
052: public void setNotificationCallback(Gui4jCallBase gui4jController,
053: Gui4jCall notifyTempValue, Map params, Object paramsValueKey) {
054: this .mNotifyCallBase = gui4jController;
055: this .mNotifyCall = notifyTempValue;
056: this .mNotifyParams = params;
057: this .mNotifyParamsValueKey = paramsValueKey;
058: }
059:
060: /**
061: * Method called by an edit component to notify the cell editor about the
062: * current temporary (non-committed) value of the editing component.
063: *
064: * @param value
065: */
066: public void notifyTempValue(String value) {
067: // use call back method to pass value on to client controller
068: if (mNotifyCall != null) {
069: mNotifyParams.put(mNotifyParamsValueKey, value);
070: mNotifyCall.getValue(mNotifyCallBase, mNotifyParams, null);
071: }
072: }
073:
074: /**
075: * Constructor for CellEditor.
076: *
077: * @param textField
078: */
079: private Gui4jCellEditor(JTextField textField) {
080: super (textField);
081: }
082:
083: public static final class CellEditorTextField extends JTextField {
084: protected javax.swing.CellEditor cellEditor;
085: private final boolean withBorder;
086:
087: public CellEditorTextField(boolean withBorder) {
088: this (null, withBorder);
089: }
090:
091: public CellEditorTextField(NotificationDocument document,
092: boolean withBorder) {
093: super (document, null, 0);
094: this .withBorder = withBorder;
095: }
096:
097: public void setBorder(Border border) {
098: if (withBorder) {
099: super .setBorder(border);
100: }
101: }
102:
103: public javax.swing.CellEditor getCellEditor() {
104: return cellEditor;
105: }
106:
107: public void setCellEditor(javax.swing.CellEditor cellEditor) {
108: this .cellEditor = cellEditor;
109: }
110:
111: protected void processFocusEvent(FocusEvent e) {
112: super .processFocusEvent(e);
113: if (e.getID() == FocusEvent.FOCUS_LOST
114: && !(e.getOppositeComponent() instanceof JWindow)) {
115: if (cellEditor != null) {
116: cellEditor.stopCellEditing();
117: }
118: }
119: }
120: }
121:
122: private static final class NotificationDocument extends
123: PlainDocument {
124:
125: private Gui4jCellEditor cellEditor;
126:
127: public NotificationDocument() {
128: super ();
129: }
130:
131: public void setCellEditor(Gui4jCellEditor cellEditor) {
132: this .cellEditor = cellEditor;
133: }
134:
135: public void insertString(int offs, String str, AttributeSet a)
136: throws BadLocationException {
137: super .insertString(offs, str, a);
138: notifyCellEditor();
139: }
140:
141: public void remove(int offs, int len)
142: throws BadLocationException {
143: super .remove(offs, len);
144: notifyCellEditor();
145: }
146:
147: private void notifyCellEditor() throws BadLocationException {
148: // notify cell editor of current temporary (non-committed) value
149: if (cellEditor != null) {
150: String value = getText(0, getLength());
151: cellEditor.notifyTempValue(value);
152: }
153: }
154:
155: }
156:
157: }
|