001: /*
002: * WbCellEditor.java
003: *
004: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
005: *
006: * Copyright 2002-2008, Thomas Kellerer
007: * No part of this code maybe reused without the permission of the author
008: *
009: * To contact the author please send an email to: support@sql-workbench.net
010: *
011: */
012: package workbench.gui.components;
013:
014: import java.awt.Component;
015: import java.awt.EventQueue;
016: import java.awt.Font;
017: import java.awt.event.ActionEvent;
018: import java.awt.event.InputEvent;
019: import java.awt.event.KeyEvent;
020: import java.awt.event.MouseEvent;
021: import java.awt.event.MouseListener;
022: import java.util.Collections;
023: import java.util.EventObject;
024: import javax.swing.AbstractAction;
025: import javax.swing.AbstractCellEditor;
026: import javax.swing.InputMap;
027: import javax.swing.JComponent;
028: import javax.swing.JScrollPane;
029: import javax.swing.JTable;
030: import javax.swing.JTextArea;
031: import javax.swing.KeyStroke;
032: import javax.swing.table.TableCellEditor;
033: import workbench.gui.WbSwingUtilities;
034: import workbench.gui.WbSwingUtilities;
035:
036: public class WbCellEditor extends AbstractCellEditor implements
037: TableCellEditor, MouseListener {
038: private TextAreaEditor editor;
039: private WbTable parentTable;
040: private JScrollPane scroll;
041:
042: public WbCellEditor(WbTable parent) {
043: parentTable = parent;
044: editor = new TextAreaEditor();
045: setDefaultCopyPasteKeys(editor);
046: setFont(parent.getFont());
047: scroll = new TextAreaScrollPane(editor);
048: scroll
049: .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
050: scroll
051: .setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
052: editor.setLineWrap(false);
053: editor.setWrapStyleWord(true);
054: editor.setBorder(WbSwingUtilities.EMPTY_BORDER);
055: scroll.setBorder(WbSwingUtilities.EMPTY_BORDER);
056: editor.addMouseListener(new TextComponentMouseListener());
057: editor.addMouseListener(this );
058: }
059:
060: protected void setDefaultCopyPasteKeys(JComponent editor) {
061: InputMap im = editor.getInputMap();
062: KeyStroke ks = KeyStroke.getKeyStroke(KeyEvent.VK_C,
063: InputEvent.CTRL_MASK);
064: KeyStroke ksnew = KeyStroke.getKeyStroke(KeyEvent.VK_INSERT,
065: InputEvent.CTRL_MASK);
066:
067: Object cmd = im.get(ks);
068: im.put(ksnew, cmd);
069:
070: ks = KeyStroke
071: .getKeyStroke(KeyEvent.VK_V, InputEvent.CTRL_MASK);
072: ksnew = KeyStroke.getKeyStroke(KeyEvent.VK_INSERT,
073: InputEvent.SHIFT_MASK);
074:
075: cmd = im.get(ks);
076: im.put(ksnew, cmd);
077:
078: ks = KeyStroke
079: .getKeyStroke(KeyEvent.VK_X, InputEvent.CTRL_MASK);
080: ksnew = KeyStroke.getKeyStroke(KeyEvent.VK_DELETE,
081: InputEvent.SHIFT_MASK);
082:
083: cmd = im.get(ks);
084: im.put(ksnew, cmd);
085: }
086:
087: public void setFont(Font aFont) {
088: this .editor.setFont(aFont);
089: }
090:
091: public Component getComponent() {
092: return scroll;
093: }
094:
095: public Object getCellEditorValue() {
096: return editor.getText();
097: }
098:
099: public boolean isCellEditable(EventObject anEvent) {
100: boolean result = true;
101: if (anEvent instanceof MouseEvent) {
102: result = ((MouseEvent) anEvent).getClickCount() >= 2;
103: }
104: if (result) {
105: EventQueue.invokeLater(new Runnable() {
106: public void run() {
107: editor.requestFocus();
108: }
109: });
110: }
111: return result;
112: }
113:
114: public Component getTableCellEditorComponent(JTable table,
115: Object value, boolean isSelected, int row, int column) {
116: editor.setText((value != null) ? value.toString() : "");
117: return scroll;
118: }
119:
120: public boolean shouldSelectCell(EventObject anEvent) {
121: return true;
122: }
123:
124: public boolean isManagingFocus() {
125: return false;
126: }
127:
128: public void mouseClicked(MouseEvent evt) {
129: if (evt.getClickCount() == 2
130: && evt.getButton() == MouseEvent.BUTTON1
131: && this .parentTable != null) {
132: parentTable.openEditWindow();
133: }
134: }
135:
136: public void mousePressed(MouseEvent evt) {
137: }
138:
139: public void mouseReleased(MouseEvent evt) {
140: }
141:
142: public void mouseEntered(MouseEvent evt) {
143: }
144:
145: public void mouseExited(MouseEvent evt) {
146: }
147:
148: class TextAreaEditor extends JTextArea {
149: public TextAreaEditor() {
150: super ();
151: this .setFocusCycleRoot(false);
152: this .setFocusTraversalKeys(WHEN_FOCUSED,
153: Collections.EMPTY_SET);
154:
155: Object tabAction = this .getInputMap().get(
156: WbSwingUtilities.TAB);
157:
158: this .getInputMap().put(WbSwingUtilities.TAB,
159: "wb-do-nothing-at-all");
160:
161: if (tabAction != null) {
162: this .getInputMap().put(WbSwingUtilities.CTRL_TAB,
163: tabAction);
164: }
165:
166: Object enterAction = this .getInputMap().get(
167: WbSwingUtilities.ENTER);
168:
169: this .getInputMap().put(WbSwingUtilities.ENTER,
170: "wb-stop-editing");
171:
172: this .getActionMap().put("stopEditing",
173: new AbstractAction("wb-stop-editing") {
174: public void actionPerformed(ActionEvent e) {
175: stopCellEditing();
176: }
177: });
178: if (enterAction != null) {
179: this .getInputMap().put(WbSwingUtilities.CTRL_ENTER,
180: enterAction);
181: this .getInputMap().put(WbSwingUtilities.ALT_ENTER,
182: enterAction);
183: }
184: }
185:
186: public boolean isManagingFocus() {
187: return false;
188: }
189: }
190:
191: public class TextAreaScrollPane extends JScrollPane {
192: TextAreaEditor editor;
193:
194: public TextAreaScrollPane(TextAreaEditor content) {
195: super (content);
196: this .setFocusCycleRoot(false);
197: this .setFocusTraversalKeys(WHEN_FOCUSED,
198: Collections.EMPTY_SET);
199: editor = content;
200: }
201:
202: public boolean isManagingFocus() {
203: return false;
204: }
205:
206: public boolean requestFocus(boolean temp) {
207: return editor.requestFocus(temp);
208: }
209:
210: public void requestFocus() {
211: this .editor.requestFocus();
212: }
213:
214: public boolean requestFocusInWindow() {
215: return this .editor.requestFocusInWindow();
216: }
217:
218: public boolean requestFocusInWindow(boolean temp) {
219: return this.editor.requestFocusInWindow();
220: }
221: }
222:
223: }
|