001: /*
002: * WbTextCellEditor.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.Color;
015: import java.awt.Component;
016: import java.awt.Font;
017: import java.awt.Frame;
018: import java.awt.event.MouseEvent;
019: import java.awt.event.MouseListener;
020: import java.util.EventObject;
021:
022: import javax.swing.DefaultCellEditor;
023: import javax.swing.JTable;
024: import javax.swing.JTextField;
025: import javax.swing.SwingUtilities;
026: import javax.swing.event.DocumentEvent;
027: import javax.swing.event.DocumentListener;
028:
029: import workbench.gui.WbSwingUtilities;
030: import workbench.resource.ResourceMgr;
031:
032: /**
033: *
034: * @author support@sql-workbench.net
035: */
036: public class WbTextCellEditor extends DefaultCellEditor implements
037: MouseListener, DocumentListener {
038: private JTextField textField;
039: private WbTable parentTable;
040: private Color defaultBackground;
041: private boolean changed = false;
042:
043: public static final WbTextCellEditor createInstance() {
044: return createInstance(null);
045: }
046:
047: public static final WbTextCellEditor createInstance(WbTable parent) {
048: JTextField field = new JTextField();
049: WbTextCellEditor editor = new WbTextCellEditor(parent, field);
050: return editor;
051: }
052:
053: public WbTextCellEditor(WbTable parent, JTextField field) {
054: super (field);
055: defaultBackground = field.getBackground();
056: this .parentTable = parent;
057: this .textField = field;
058: this .textField.setBorder(WbSwingUtilities.EMPTY_BORDER);
059: this .textField.addMouseListener(this );
060: this .textField
061: .addMouseListener(new TextComponentMouseListener());
062: this .textField.getDocument().addDocumentListener(this );
063: super .addCellEditorListener(parent);
064: }
065:
066: public String getText() {
067: return this .textField.getText();
068: }
069:
070: public void setFont(Font aFont) {
071: this .textField.setFont(aFont);
072: }
073:
074: public Color getDefaultBackground() {
075: return defaultBackground;
076: }
077:
078: public void requestFocus() {
079: this .textField.requestFocusInWindow();
080: }
081:
082: public void selectAll() {
083: this .textField.selectAll();
084: }
085:
086: public Component getTableCellEditorComponent(JTable table,
087: Object value, boolean isSelected, int row, int column) {
088: Component result = super .getTableCellEditorComponent(table,
089: value, isSelected, row, column);
090: textField.selectAll();
091: this .changed = false;
092: return result;
093: }
094:
095: public void setBackground(Color c) {
096: this .textField.setBackground(c);
097: }
098:
099: public boolean shouldSelectCell(EventObject anEvent) {
100: boolean shouldSelect = super .shouldSelectCell(anEvent);
101: if (shouldSelect) {
102: this .textField.selectAll();
103: }
104: return shouldSelect;
105: }
106:
107: public void mouseClicked(java.awt.event.MouseEvent evt) {
108: if (evt.getClickCount() == 2
109: && evt.getButton() == MouseEvent.BUTTON1) {
110: this .openEditWindow();
111: }
112: }
113:
114: public void mouseEntered(java.awt.event.MouseEvent mouseEvent) {
115: }
116:
117: public void mouseExited(java.awt.event.MouseEvent mouseEvent) {
118: }
119:
120: public void mousePressed(java.awt.event.MouseEvent mouseEvent) {
121: }
122:
123: public void mouseReleased(java.awt.event.MouseEvent mouseEvent) {
124: }
125:
126: public void cancelCellEditing() {
127: super .cancelCellEditing();
128: fireEditingCanceled();
129: }
130:
131: public boolean stopCellEditing() {
132: boolean result = super .stopCellEditing();
133: if (result) {
134: fireEditingStopped();
135: }
136: return result;
137: }
138:
139: public void openEditWindow() {
140: if (this .parentTable == null) {
141: Frame owner = (Frame) SwingUtilities
142: .getWindowAncestor(this .textField);
143: String title = ResourceMgr.getString("TxtEditWindowTitle");
144: String value = this .textField.getText();
145: EditWindow w = new EditWindow(owner, title, value);
146:
147: try {
148: w.setVisible(true);
149: if (!w.isCancelled()) {
150: this .textField.setText(w.getText());
151: }
152: } finally {
153: w.dispose();
154: }
155: } else {
156: this .parentTable.openEditWindow();
157: }
158: }
159:
160: public boolean isModified() {
161: return this .changed;
162: }
163:
164: public void insertUpdate(DocumentEvent arg0) {
165: this .changed = true;
166: }
167:
168: public void removeUpdate(DocumentEvent arg0) {
169: this .changed = true;
170: }
171:
172: public void changedUpdate(DocumentEvent arg0) {
173: this .changed = true;
174: }
175: }
|