001: /**
002: * L2FProd.com Common Components 7.3 License.
003: *
004: * Copyright 2005-2007 L2FProd.com
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */package com.l2fprod.common.propertysheet;
018:
019: import java.awt.Component;
020: import java.awt.event.ActionEvent;
021: import java.awt.event.ActionListener;
022: import java.awt.event.FocusEvent;
023: import java.awt.event.FocusListener;
024: import java.awt.event.KeyEvent;
025: import java.awt.event.MouseEvent;
026: import java.beans.PropertyChangeEvent;
027: import java.beans.PropertyChangeListener;
028: import java.beans.PropertyEditor;
029: import java.util.EventObject;
030:
031: import javax.swing.AbstractCellEditor;
032: import javax.swing.JComponent;
033: import javax.swing.JTable;
034: import javax.swing.JTextField;
035: import javax.swing.JTree;
036: import javax.swing.KeyStroke;
037: import javax.swing.SwingUtilities;
038: import javax.swing.table.TableCellEditor;
039: import javax.swing.tree.TreeCellEditor;
040:
041: /**
042: * Allows to use any PropertyEditor as a Table or Tree cell editor. <br>
043: */
044: public class CellEditorAdapter extends AbstractCellEditor implements
045: TableCellEditor, TreeCellEditor {
046:
047: protected PropertyEditor editor;
048: protected int clickCountToStart = 1;
049:
050: class CommitEditing implements ActionListener {
051: public void actionPerformed(ActionEvent e) {
052: stopCellEditing();
053: }
054: }
055:
056: class CancelEditing implements ActionListener {
057: public void actionPerformed(ActionEvent e) {
058: CellEditorAdapter.this .cancelCellEditing();
059: }
060: }
061:
062: /**
063: * Select all text when focus gained, deselect when focus lost.
064: */
065: class SelectOnFocus implements FocusListener {
066: public void focusGained(final FocusEvent e) {
067: if (!(e.getSource() instanceof JTextField))
068: return;
069: SwingUtilities.invokeLater(new Runnable() {
070: public void run() {
071: ((JTextField) e.getSource()).selectAll();
072: }
073: });
074: }
075:
076: public void focusLost(final FocusEvent e) {
077: if (!(e.getSource() instanceof JTextField))
078: return;
079: SwingUtilities.invokeLater(new Runnable() {
080: public void run() {
081: ((JTextField) e.getSource()).select(0, 0);
082: }
083: });
084: }
085: }
086:
087: public CellEditorAdapter(PropertyEditor editor) {
088: this .editor = editor;
089: Component component = editor.getCustomEditor();
090: if (component instanceof JTextField) {
091: JTextField field = (JTextField) component;
092: field.addFocusListener(new SelectOnFocus());
093: field.addActionListener(new CommitEditing());
094: field.registerKeyboardAction(new CancelEditing(), KeyStroke
095: .getKeyStroke(KeyEvent.VK_ESCAPE, 0),
096: JComponent.WHEN_FOCUSED);
097: }
098:
099: // when the editor notifies a change, commit the changes
100: editor.addPropertyChangeListener(new PropertyChangeListener() {
101: public void propertyChange(PropertyChangeEvent evt) {
102: stopCellEditing();
103: }
104: });
105: }
106:
107: public Component getTreeCellEditorComponent(JTree tree,
108: Object value, boolean selected, boolean expanded,
109: boolean leaf, int row) {
110: return getEditor(value);
111: }
112:
113: public Component getTableCellEditorComponent(JTable table,
114: Object value, boolean selected, int row, int column) {
115: return getEditor(value);
116: }
117:
118: public void setClickCountToStart(int count) {
119: clickCountToStart = count;
120: }
121:
122: public int getClickCountToStart() {
123: return clickCountToStart;
124: }
125:
126: public Object getCellEditorValue() {
127: return editor.getValue();
128: }
129:
130: public boolean isCellEditable(EventObject event) {
131: if (event instanceof MouseEvent) {
132: return ((MouseEvent) event).getClickCount() >= clickCountToStart;
133: }
134: return true;
135: }
136:
137: public boolean shouldSelectCell(EventObject event) {
138: return true;
139: }
140:
141: public boolean stopCellEditing() {
142: fireEditingStopped();
143: return true;
144: }
145:
146: public void cancelCellEditing() {
147: fireEditingCanceled();
148: }
149:
150: private Component getEditor(Object value) {
151: editor.setValue(value);
152:
153: final Component cellEditor = editor.getCustomEditor();
154:
155: // request focus later so the editor can be used to enter value as soon as
156: // made visible
157: SwingUtilities.invokeLater(new Runnable() {
158: public void run() {
159: cellEditor.requestFocus();
160: }
161: });
162:
163: return cellEditor;
164: }
165:
166: }
|