2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com
.intellij
.ui
;
18 import com
.intellij
.openapi
.ui
.FixedSizeButton
;
21 import javax
.swing
.table
.TableCellEditor
;
23 import java
.awt
.event
.*;
24 import java
.io
.Serializable
;
25 import java
.util
.EventObject
;
27 public class TableCellEditorWithButton
extends AbstractCellEditor
implements TableCellEditor
{
29 protected final MyComponent myComponent
;
30 protected final EditorDelegate delegate
;
31 protected final int myClickCountToStart
= 1;
33 public TableCellEditorWithButton() {
34 myComponent
= new MyComponent();
35 delegate
= new EditorDelegate() {
36 public void setValue(Object value
) {
37 myComponent
.getTextField().setText((value
!= null) ? value
.toString() : "");
40 public Object
getCellEditorValue() {
41 return myComponent
.getTextField().getText();
44 myComponent
.getTextField().addActionListener(delegate
);
47 public JButton
getButton() {
48 return myComponent
.getButton();
51 public JTextField
getTextField() {
52 return myComponent
.getTextField();
55 public Object
getCellEditorValue() {
56 return delegate
.getCellEditorValue();
59 public boolean isCellEditable(EventObject anEvent
) {
60 return delegate
.isCellEditable(anEvent
);
63 public boolean shouldSelectCell(EventObject anEvent
) {
64 return delegate
.shouldSelectCell(anEvent
);
67 public boolean stopCellEditing() {
68 return delegate
.stopCellEditing();
71 public void cancelCellEditing() {
72 delegate
.cancelCellEditing();
75 public Component
getTableCellEditorComponent(JTable table
, Object value
, boolean isSelected
, int row
, int column
) {
76 delegate
.setValue(value
);
80 protected class EditorDelegate
implements ActionListener
, ItemListener
, Serializable
{
82 protected Object value
;
84 public Object
getCellEditorValue() {
88 public void setValue(Object value
) {
92 public boolean isCellEditable(EventObject anEvent
) {
93 if (anEvent
instanceof MouseEvent
) {
94 return ((MouseEvent
)anEvent
).getClickCount() >= myClickCountToStart
;
99 public boolean shouldSelectCell(EventObject anEvent
) {
103 public boolean stopCellEditing() {
104 fireEditingStopped();
108 public void cancelCellEditing() {
109 fireEditingCanceled();
112 public void actionPerformed(ActionEvent e
) {
113 TableCellEditorWithButton
.this.stopCellEditing();
116 public void itemStateChanged(ItemEvent e
) {
117 TableCellEditorWithButton
.this.stopCellEditing();
121 private class MyComponent
extends JPanel
{
122 private final JTextField myTextField
;
123 private final FixedSizeButton myButton
;
125 public MyComponent() {
126 super(new GridBagLayout());
128 myTextField
= new JTextField();
129 myButton
= new FixedSizeButton(myTextField
);
131 add(myTextField
, new GridBagConstraints(0, 0, 1, 1, 1, 0, GridBagConstraints
.WEST
, GridBagConstraints
.HORIZONTAL
, new Insets(0, 0, 0, 0), 0, 0));
132 add(myButton
, new GridBagConstraints(1, 0, 1, 1, 0, 0, GridBagConstraints
.WEST
, GridBagConstraints
.NONE
, new Insets(0, 0, 0, 0), 0, 0));
135 public FixedSizeButton
getButton() {
139 public JTextField
getTextField() {
143 public boolean requestDefaultFocus() {
144 myTextField
.requestFocus();