2 * Copyright 2000-2010 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
.refactoring
.extractMethod
;
18 import com
.intellij
.ui
.ScrollPaneFactory
;
19 import com
.intellij
.ui
.TableUtil
;
20 import com
.intellij
.ui
.UIBundle
;
21 import com
.intellij
.util
.ui
.Table
;
22 import org
.jetbrains
.annotations
.NonNls
;
25 import javax
.swing
.event
.ListSelectionEvent
;
26 import javax
.swing
.event
.ListSelectionListener
;
27 import javax
.swing
.table
.AbstractTableModel
;
28 import javax
.swing
.table
.DefaultTableCellRenderer
;
29 import javax
.swing
.table
.TableCellEditor
;
31 import java
.awt
.event
.ActionEvent
;
32 import java
.awt
.event
.ActionListener
;
33 import java
.awt
.event
.KeyEvent
;
37 * It`s a modified copy of com.intellij.refactoring.util.AbstractParameterTablePanel
39 public abstract class AbstractParameterTablePanel
extends JPanel
{
40 private AbstractVariableData
[] myVariableData
;
42 private Table myTable
;
43 private MyTableModel myTableModel
;
44 private JButton myUpButton
;
45 private JButton myDownButton
;
46 private final ExtractMethodValidator myValidator
;
48 protected abstract void updateSignature();
50 protected abstract void doEnterAction();
52 protected abstract void doCancelAction();
54 public void setVariableData(AbstractVariableData
[] variableData
) {
55 myVariableData
= variableData
;
58 public AbstractParameterTablePanel(final ExtractMethodValidator validator
) {
59 super(new BorderLayout());
60 myValidator
= validator
;
64 myTableModel
= new MyTableModel();
65 myTable
= new Table(myTableModel
);
66 DefaultCellEditor defaultEditor
= (DefaultCellEditor
)myTable
.getDefaultEditor(Object
.class);
67 defaultEditor
.setClickCountToStart(1);
69 myTable
.setTableHeader(null);
70 myTable
.getSelectionModel().setSelectionMode(ListSelectionModel
.SINGLE_SELECTION
);
71 myTable
.getColumnModel().getColumn(MyTableModel
.CHECKMARK_COLUMN
).setMaxWidth(new JCheckBox().getPreferredSize().width
);
72 myTable
.getColumnModel().getColumn(MyTableModel
.PARAMETER_NAME_COLUMN
).setCellRenderer(new DefaultTableCellRenderer() {
73 public Component
getTableCellRendererComponent(JTable table
, Object value
, boolean isSelected
, boolean hasFocus
, int row
, int column
) {
74 super.getTableCellRendererComponent(table
, value
, isSelected
, hasFocus
, row
, column
);
75 AbstractVariableData data
= myVariableData
[row
];
82 myTable
.setPreferredScrollableViewportSize(new Dimension(250, myTable
.getRowHeight() * 5));
83 myTable
.setShowGrid(false);
84 myTable
.setIntercellSpacing(new Dimension(0, 0));
85 @NonNls final InputMap inputMap
= myTable
.getInputMap();
86 inputMap
.put(KeyStroke
.getKeyStroke(KeyEvent
.VK_SPACE
, 0), "enable_disable");
87 @NonNls final ActionMap actionMap
= myTable
.getActionMap();
88 actionMap
.put("enable_disable", new AbstractAction() {
89 public void actionPerformed(ActionEvent e
) {
90 if (myTable
.isEditing()) return;
91 int[] rows
= myTable
.getSelectedRows();
92 if (rows
.length
> 0) {
93 boolean valueToBeSet
= false;
94 for (int row
: rows
) {
95 if (!myVariableData
[row
].passAsParameter
) {
100 for (int row
: rows
) {
101 myVariableData
[row
].passAsParameter
= valueToBeSet
;
103 myTableModel
.fireTableRowsUpdated(rows
[0], rows
[rows
.length
- 1]);
104 TableUtil
.selectRows(myTable
, rows
);
108 // F2 should edit the name
109 inputMap
.put(KeyStroke
.getKeyStroke(KeyEvent
.VK_F2
, 0), "edit_parameter_name");
110 actionMap
.put("edit_parameter_name", new AbstractAction() {
111 public void actionPerformed(ActionEvent e
) {
112 if (!myTable
.isEditing()) {
113 int row
= myTable
.getSelectedRow();
114 if (row
>= 0 && row
< myTableModel
.getRowCount()) {
115 TableUtil
.editCellAt(myTable
, row
, MyTableModel
.PARAMETER_NAME_COLUMN
);
121 // make ENTER work when the table has focus
122 inputMap
.put(KeyStroke
.getKeyStroke(KeyEvent
.VK_ENTER
, 0), "invokeImpl");
123 actionMap
.put("invokeImpl", new AbstractAction() {
124 public void actionPerformed(ActionEvent e
) {
125 TableCellEditor editor
= myTable
.getCellEditor();
126 if (editor
!= null) {
127 editor
.stopCellEditing();
135 // make ESCAPE work when the table has focus
136 actionMap
.put("doCancel", new AbstractAction() {
137 public void actionPerformed(ActionEvent e
) {
138 TableCellEditor editor
= myTable
.getCellEditor();
139 if (editor
!= null) {
140 editor
.stopCellEditing();
148 JPanel listPanel
= new JPanel(new BorderLayout());
149 JScrollPane scrollPane
= ScrollPaneFactory
.createScrollPane(myTable
);
150 listPanel
.add(scrollPane
, BorderLayout
.CENTER
);
151 listPanel
.setBorder(BorderFactory
.createEmptyBorder(4, 4, 4, 4));
152 add(listPanel
, BorderLayout
.CENTER
);
154 JPanel buttonsPanel
= new JPanel();
155 buttonsPanel
.setBorder(BorderFactory
.createEmptyBorder(4, 4, 4, 4));
156 add(buttonsPanel
, BorderLayout
.EAST
);
158 buttonsPanel
.setLayout(new GridBagLayout());
159 GridBagConstraints gbConstraints
= new GridBagConstraints();
160 gbConstraints
.gridwidth
= GridBagConstraints
.REMAINDER
;
161 gbConstraints
.fill
= GridBagConstraints
.HORIZONTAL
;
162 gbConstraints
.insets
= new Insets(2, 4, 2, 4);
164 myUpButton
= new JButton();
165 myUpButton
.setText(UIBundle
.message("row.move.up"));
166 myUpButton
.setDefaultCapable(false);
167 buttonsPanel
.add(myUpButton
, gbConstraints
);
169 myDownButton
= new JButton();
170 myDownButton
.setText(UIBundle
.message("row.move.down"));
171 myDownButton
.setDefaultCapable(false);
172 buttonsPanel
.add(myDownButton
, gbConstraints
);
174 gbConstraints
.weighty
= 1;
175 buttonsPanel
.add(new JPanel(), gbConstraints
);
177 myUpButton
.addActionListener(new ActionListener() {
178 public void actionPerformed(ActionEvent e
) {
179 if (myTable
.isEditing()) {
180 final boolean isStopped
= myTable
.getCellEditor().stopCellEditing();
181 if (!isStopped
) return;
183 moveSelectedItem(-1);
185 myTable
.requestFocus();
189 myDownButton
.addActionListener(new ActionListener() {
190 public void actionPerformed(ActionEvent e
) {
191 if (myTable
.isEditing()) {
192 final boolean isStopped
= myTable
.getCellEditor().stopCellEditing();
193 if (!isStopped
) return;
195 moveSelectedItem(+1);
197 myTable
.requestFocus();
201 myTable
.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
202 public void valueChanged(ListSelectionEvent e
) {
206 if (myVariableData
.length
<= 1) {
207 myUpButton
.setEnabled(false);
208 myDownButton
.setEnabled(false);
211 myTable
.getSelectionModel().setSelectionInterval(0, 0);
216 private void updateMoveButtons() {
217 int row
= myTable
.getSelectedRow();
218 if (0 <= row
&& row
< myVariableData
.length
) {
219 myUpButton
.setEnabled(row
> 0);
220 myDownButton
.setEnabled(row
< myVariableData
.length
- 1);
223 myUpButton
.setEnabled(false);
224 myDownButton
.setEnabled(false);
228 private void moveSelectedItem(int moveIncrement
) {
229 int row
= myTable
.getSelectedRow();
230 if (row
< 0 || row
>= myVariableData
.length
) return;
231 int targetRow
= row
+ moveIncrement
;
232 if (targetRow
< 0 || targetRow
>= myVariableData
.length
) return;
234 AbstractVariableData currentItem
= myVariableData
[row
];
235 myVariableData
[row
] = myVariableData
[targetRow
];
236 myVariableData
[targetRow
] = currentItem
;
238 myTableModel
.fireTableRowsUpdated(Math
.min(targetRow
, row
), Math
.max(targetRow
, row
));
239 myTable
.getSelectionModel().setSelectionInterval(targetRow
, targetRow
);
242 public void setEnabled(boolean enabled
) {
243 myTable
.setEnabled(enabled
);
245 myUpButton
.setEnabled(false);
246 myDownButton
.setEnabled(false);
251 super.setEnabled(enabled
);
254 private class MyTableModel
extends AbstractTableModel
{
255 public static final int CHECKMARK_COLUMN
= 0;
256 public static final int PARAMETER_NAME_COLUMN
= 1;
258 public int getRowCount() {
259 return myVariableData
.length
;
262 public int getColumnCount() {
266 public Object
getValueAt(int rowIndex
, int columnIndex
) {
267 switch (columnIndex
) {
268 case CHECKMARK_COLUMN
: {
269 return myVariableData
[rowIndex
].passAsParameter ? Boolean
.TRUE
: Boolean
.FALSE
;
271 case PARAMETER_NAME_COLUMN
: {
272 return myVariableData
[rowIndex
].name
;
279 public void setValueAt(Object aValue
, int rowIndex
, int columnIndex
) {
280 switch (columnIndex
) {
281 case CHECKMARK_COLUMN
: {
282 myVariableData
[rowIndex
].passAsParameter
= ((Boolean
)aValue
).booleanValue();
283 fireTableRowsUpdated(rowIndex
, rowIndex
);
284 myTable
.getSelectionModel().setSelectionInterval(rowIndex
, rowIndex
);
288 case PARAMETER_NAME_COLUMN
: {
289 AbstractVariableData data
= myVariableData
[rowIndex
];
290 String name
= (String
)aValue
;
291 if (myValidator
.isValidName(name
)) {
300 public boolean isCellEditable(int rowIndex
, int columnIndex
) {
301 switch (columnIndex
) {
302 case CHECKMARK_COLUMN
:
304 case PARAMETER_NAME_COLUMN
:
305 return isEnabled() && myVariableData
[rowIndex
].passAsParameter
;
311 public Class
getColumnClass(int columnIndex
) {
312 if (columnIndex
== CHECKMARK_COLUMN
) {
313 return Boolean
.class;
315 return super.getColumnClass(columnIndex
);