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
.refactoring
.encapsulateFields
;
18 import com
.intellij
.openapi
.diagnostic
.Logger
;
19 import com
.intellij
.openapi
.help
.HelpManager
;
20 import com
.intellij
.openapi
.project
.Project
;
21 import com
.intellij
.openapi
.util
.IconLoader
;
22 import com
.intellij
.openapi
.util
.Iconable
;
23 import com
.intellij
.psi
.*;
24 import com
.intellij
.psi
.util
.PropertyUtil
;
25 import com
.intellij
.psi
.util
.PsiFormatUtil
;
26 import com
.intellij
.refactoring
.HelpID
;
27 import com
.intellij
.refactoring
.RefactoringBundle
;
28 import com
.intellij
.refactoring
.JavaRefactoringSettings
;
29 import com
.intellij
.refactoring
.ui
.DocCommentPanel
;
30 import com
.intellij
.refactoring
.ui
.RefactoringDialog
;
31 import com
.intellij
.refactoring
.util
.CommonRefactoringUtil
;
32 import com
.intellij
.refactoring
.util
.RefactoringMessageUtil
;
33 import com
.intellij
.ui
.*;
34 import com
.intellij
.util
.IconUtil
;
35 import com
.intellij
.util
.IncorrectOperationException
;
36 import com
.intellij
.util
.ui
.EmptyIcon
;
37 import com
.intellij
.util
.ui
.Table
;
38 import com
.intellij
.util
.ui
.UIUtil
;
39 import org
.jetbrains
.annotations
.NonNls
;
42 import javax
.swing
.border
.Border
;
43 import javax
.swing
.table
.AbstractTableModel
;
44 import javax
.swing
.table
.DefaultTableCellRenderer
;
45 import javax
.swing
.table
.TableCellEditor
;
46 import javax
.swing
.table
.TableColumnModel
;
48 import java
.awt
.event
.*;
51 public class EncapsulateFieldsDialog
extends RefactoringDialog
implements EncapsulateFieldsDescriptor
{
52 private static final Logger LOG
= Logger
.getInstance(
53 "#com.intellij.refactoring.encapsulateFields.EncapsulateFieldsDialog"
56 private static final int CHECKED_COLUMN
= 0;
57 private static final int FIELD_COLUMN
= 1;
58 private static final int GETTER_COLUMN
= 2;
59 private static final int SETTER_COLUMN
= 3;
61 private final Project myProject
;
62 private final PsiClass myClass
;
64 private final PsiField
[] myFields
;
65 private final boolean[] myCheckedMarks
;
66 private final boolean[] myFinalMarks
;
67 private final String
[] myFieldNames
;
68 private final String
[] myGetterNames
;
69 private final PsiMethod
[] myGetterPrototypes
;
70 private final String
[] mySetterNames
;
71 private final PsiMethod
[] mySetterPrototypes
;
73 private JTable myTable
;
74 private MyTableModel myTableModel
;
76 private final JCheckBox myCbEncapsulateGet
= new JCheckBox();
77 private final JCheckBox myCbEncapsulateSet
= new JCheckBox();
78 private final JCheckBox myCbUseAccessorsWhenAccessible
= new JCheckBox();
79 private final JRadioButton myRbFieldPrivate
= new JRadioButton();
80 private final JRadioButton myRbFieldProtected
= new JRadioButton();
81 private final JRadioButton myRbFieldPackageLocal
= new JRadioButton();
82 private final JRadioButton myRbFieldAsIs
= new JRadioButton();
83 private final JRadioButton myRbAccessorPublic
= new JRadioButton();
84 private final JRadioButton myRbAccessorProtected
= new JRadioButton();
85 private final JRadioButton myRbAccessorPrivate
= new JRadioButton();
86 private final JRadioButton myRbAccessorPackageLocal
= new JRadioButton();
87 private static final String REFACTORING_NAME
= RefactoringBundle
.message("encapsulate.fields.title");
88 private DocCommentPanel myJavadocPolicy
;
91 myCbEncapsulateGet
.setFocusable(false);
92 myCbEncapsulateSet
.setFocusable(false);
93 myCbUseAccessorsWhenAccessible
.setFocusable(false);
95 myRbAccessorPackageLocal
.setFocusable(false);
96 myRbAccessorPrivate
.setFocusable(false);
97 myRbAccessorProtected
.setFocusable(false);
98 myRbAccessorPublic
.setFocusable(false);
100 myRbFieldAsIs
.setFocusable(false);
101 myRbFieldPackageLocal
.setFocusable(false);
102 myRbFieldPrivate
.setFocusable(false);
103 myRbFieldProtected
.setFocusable(false);
106 public EncapsulateFieldsDialog(Project project
, PsiClass aClass
, final Set preselectedFields
) {
107 super(project
, true);
111 String title
= REFACTORING_NAME
;
112 String qName
= myClass
.getQualifiedName();
114 title
+= " - " + qName
;
118 myFields
= myClass
.getFields();
119 myFieldNames
= new String
[myFields
.length
];
120 myCheckedMarks
= new boolean[myFields
.length
];
121 myFinalMarks
= new boolean[myFields
.length
];
122 myGetterNames
= new String
[myFields
.length
];
123 mySetterNames
= new String
[myFields
.length
];
124 myGetterPrototypes
= new PsiMethod
[myFields
.length
];
125 mySetterPrototypes
= new PsiMethod
[myFields
.length
];
126 for (int idx
= 0; idx
< myFields
.length
; idx
++) {
127 PsiField field
= myFields
[idx
];
128 myCheckedMarks
[idx
] = preselectedFields
.contains(field
);
129 myFinalMarks
[idx
] = field
.hasModifierProperty(PsiModifier
.FINAL
);
131 PsiFormatUtil
.formatVariable(field
,
132 PsiFormatUtil
.SHOW_NAME
| PsiFormatUtil
.SHOW_TYPE
| PsiFormatUtil
.TYPE_AFTER
,
135 myGetterNames
[idx
] = PropertyUtil
.suggestGetterName(myProject
, field
);
136 mySetterNames
[idx
] = PropertyUtil
.suggestSetterName(myProject
, field
);
137 myGetterPrototypes
[idx
] = generateMethodPrototype(field
, myGetterNames
[idx
], true);
138 mySetterPrototypes
[idx
] = generateMethodPrototype(field
, mySetterNames
[idx
], false);
144 public PsiField
[] getSelectedFields() {
145 int[] rows
= getCheckedRows();
146 PsiField
[] selectedFields
= new PsiField
[rows
.length
];
147 for (int idx
= 0; idx
< rows
.length
; idx
++) {
148 selectedFields
[idx
] = myFields
[rows
[idx
]];
150 return selectedFields
;
153 public String
[] getGetterNames() {
154 int[] rows
= getCheckedRows();
155 String
[] selectedGetters
= new String
[rows
.length
];
156 for (int idx
= 0; idx
< rows
.length
; idx
++) {
157 selectedGetters
[idx
] = myGetterNames
[rows
[idx
]];
159 return selectedGetters
;
162 public String
[] getSetterNames() {
163 int[] rows
= getCheckedRows();
164 String
[] selectedSetters
= new String
[rows
.length
];
165 for (int idx
= 0; idx
< rows
.length
; idx
++) {
166 selectedSetters
[idx
] = mySetterNames
[rows
[idx
]];
168 return selectedSetters
;
171 public PsiMethod
[] getGetterPrototypes() {
172 if (isToEncapsulateGet()) {
173 int[] rows
= getCheckedRows();
174 PsiMethod
[] selectedGetters
= new PsiMethod
[rows
.length
];
175 for (int idx
= 0; idx
< rows
.length
; idx
++) {
176 selectedGetters
[idx
] = myGetterPrototypes
[rows
[idx
]];
178 return selectedGetters
;
184 public PsiMethod
[] getSetterPrototypes() {
185 if (isToEncapsulateSet()) {
186 int[] rows
= getCheckedRows();
187 PsiMethod
[] selectedSetters
= new PsiMethod
[rows
.length
];
188 for (int idx
= 0; idx
< rows
.length
; idx
++) {
189 selectedSetters
[idx
] = mySetterPrototypes
[rows
[idx
]];
191 return selectedSetters
;
197 public boolean isToEncapsulateGet() {
198 return myCbEncapsulateGet
.isSelected();
201 public boolean isToEncapsulateSet() {
202 return myCbEncapsulateSet
.isSelected();
205 public boolean isToUseAccessorsWhenAccessible() {
206 if (getFieldsVisibility() == null) {
210 return myCbUseAccessorsWhenAccessible
.isSelected();
214 public String
getFieldsVisibility() {
215 if (myRbFieldPrivate
.isSelected()) {
216 return PsiModifier
.PRIVATE
;
217 } else if (myRbFieldPackageLocal
.isSelected()) {
218 return PsiModifier
.PACKAGE_LOCAL
;
219 } else if (myRbFieldProtected
.isSelected()) {
220 return PsiModifier
.PROTECTED
;
221 } else if (myRbFieldAsIs
.isSelected()) {
224 LOG
.assertTrue(false);
229 public int getJavadocPolicy() {
230 return myJavadocPolicy
.getPolicy();
233 protected String
getDimensionServiceKey() {
234 return "#com.intellij.refactoring.encapsulateFields.EncalpsulateFieldsDialog";
238 public String
getAccessorsVisibility() {
239 if (myRbAccessorPublic
.isSelected()) {
240 return PsiModifier
.PUBLIC
;
241 } else if (myRbAccessorProtected
.isSelected()) {
242 return PsiModifier
.PROTECTED
;
243 } else if (myRbAccessorPackageLocal
.isSelected()) {
244 return PsiModifier
.PACKAGE_LOCAL
;
245 } else if (myRbAccessorPrivate
.isSelected()) {
246 return PsiModifier
.PRIVATE
;
248 LOG
.assertTrue(false);
253 protected JComponent
createCenterPanel() {
254 JPanel panel
= new JPanel(new BorderLayout());
255 panel
.add(createTable(), BorderLayout
.CENTER
);
257 myCbEncapsulateGet
.setText(RefactoringBundle
.message("encapsulate.fields.get.access.checkbox"));
258 myCbEncapsulateSet
.setText(RefactoringBundle
.message("encapsulate.fields.set.access.checkbox"));
259 myCbUseAccessorsWhenAccessible
.setText(RefactoringBundle
.message("encapsulate.fields.use.accessors.even.when.field.is.accessible.checkbox"));
260 myRbFieldPrivate
.setText(RefactoringBundle
.message("encapsulate.fields.private.radio"));
261 myRbFieldProtected
.setText(RefactoringBundle
.message("encapsulate.fields.protected.radio"));
262 myRbFieldPackageLocal
.setText(RefactoringBundle
.message("encapsulate.fields..package.local.radio"));
263 myRbFieldAsIs
.setText(RefactoringBundle
.getVisibilityAsIs());
264 myRbAccessorPublic
.setText(RefactoringBundle
.getVisibilityPublic());
265 myRbAccessorProtected
.setText(RefactoringBundle
.getVisibilityProtected());
266 myRbAccessorPrivate
.setText(RefactoringBundle
.getVisibilityPrivate());
267 myRbAccessorPackageLocal
.setText(RefactoringBundle
.getVisibilityPackageLocal());
269 ButtonGroup fieldGroup
= new ButtonGroup();
270 fieldGroup
.add(myRbFieldAsIs
);
271 fieldGroup
.add(myRbFieldPackageLocal
);
272 fieldGroup
.add(myRbFieldPrivate
);
273 fieldGroup
.add(myRbFieldProtected
);
275 ButtonGroup methodGroup
= new ButtonGroup();
276 methodGroup
.add(myRbAccessorPackageLocal
);
277 methodGroup
.add(myRbAccessorPrivate
);
278 methodGroup
.add(myRbAccessorProtected
);
279 methodGroup
.add(myRbAccessorPublic
);
281 myCbEncapsulateGet
.setSelected(true);
282 myCbEncapsulateSet
.setSelected(true);
283 ActionListener checkboxListener
= new ActionListener() {
284 public void actionPerformed(ActionEvent e
) {
285 if (myCbEncapsulateGet
.equals(e
.getSource())) {
286 if (!myCbEncapsulateGet
.isSelected()) {
287 myCbEncapsulateSet
.setSelected(true);
290 // myCbEncapsulateSet is the source
291 if (!myCbEncapsulateSet
.isSelected()) {
292 myCbEncapsulateGet
.setSelected(true);
295 int[] rows
= myTable
.getSelectedRows();
296 myTableModel
.fireTableDataChanged();
297 TableUtil
.selectRows(myTable
, rows
);
300 myCbEncapsulateGet
.addActionListener(checkboxListener
);
301 myCbEncapsulateSet
.addActionListener(checkboxListener
);
302 myRbFieldAsIs
.addItemListener(new ItemListener() {
303 public void itemStateChanged(ItemEvent e
) {
304 myCbUseAccessorsWhenAccessible
.setEnabled(!myRbFieldAsIs
.isSelected());
308 myCbUseAccessorsWhenAccessible
.setSelected(
309 JavaRefactoringSettings
.getInstance().ENCAPSULATE_FIELDS_USE_ACCESSORS_WHEN_ACCESSIBLE
312 myRbFieldPrivate
.setSelected(true);
313 myRbAccessorPublic
.setSelected(true);
315 Box leftBox
= Box
.createVerticalBox();
316 myCbEncapsulateGet
.setPreferredSize(myCbUseAccessorsWhenAccessible
.getPreferredSize());
317 leftBox
.add(myCbEncapsulateGet
);
318 leftBox
.add(myCbEncapsulateSet
);
319 leftBox
.add(Box
.createVerticalStrut(10));
320 leftBox
.add(myCbUseAccessorsWhenAccessible
);
321 JPanel leftPanel
= new JPanel(new BorderLayout());
322 leftPanel
.setBorder(IdeBorderFactory
.createTitledBorder(RefactoringBundle
.message("encapsulate.fields.encapsulate.border.title")));
323 leftPanel
.add(leftBox
, BorderLayout
.CENTER
);
324 leftPanel
.add(Box
.createHorizontalStrut(5), BorderLayout
.WEST
);
326 JPanel encapsulateBox
= new JPanel(new BorderLayout());
327 encapsulateBox
.add(leftPanel
, BorderLayout
.CENTER
);
328 myJavadocPolicy
= new DocCommentPanel("JavaDoc");
329 encapsulateBox
.add(myJavadocPolicy
, BorderLayout
.EAST
);
330 boolean hasJavadoc
= false;
331 for (PsiField field
: myFields
) {
332 if (field
.getDocComment() != null) {
337 myJavadocPolicy
.setVisible(hasJavadoc
);
339 Box fieldsBox
= Box
.createVerticalBox();
340 fieldsBox
.add(myRbFieldPrivate
);
341 fieldsBox
.add(myRbFieldPackageLocal
);
342 fieldsBox
.add(myRbFieldProtected
);
343 fieldsBox
.add(myRbFieldAsIs
);
344 JPanel fieldsVisibilityPanel
= new JPanel(new BorderLayout());
345 fieldsVisibilityPanel
.setBorder(IdeBorderFactory
.createTitledBorder(RefactoringBundle
.message("encapsulate.fields..encapsulated.fields.visibility.border.title")));
346 fieldsVisibilityPanel
.add(fieldsBox
, BorderLayout
.CENTER
);
347 fieldsVisibilityPanel
.add(Box
.createHorizontalStrut(5), BorderLayout
.WEST
);
349 Box methodsBox
= Box
.createVerticalBox();
350 methodsBox
.add(myRbAccessorPublic
);
351 methodsBox
.add(myRbAccessorProtected
);
352 methodsBox
.add(myRbAccessorPackageLocal
);
353 methodsBox
.add(myRbAccessorPrivate
);
354 JPanel methodsVisibilityPanel
= new JPanel(new BorderLayout());
355 methodsVisibilityPanel
.setBorder(IdeBorderFactory
.createTitledBorder(RefactoringBundle
.message("encapsulate.fields.accessors.visibility.border.title")));
356 methodsVisibilityPanel
.add(methodsBox
, BorderLayout
.CENTER
);
357 methodsVisibilityPanel
.add(Box
.createHorizontalStrut(5), BorderLayout
.WEST
);
359 Box visibilityBox
= Box
.createHorizontalBox();
360 visibilityBox
.add(fieldsVisibilityPanel
);
361 visibilityBox
.add(Box
.createHorizontalStrut(5));
362 visibilityBox
.add(methodsVisibilityPanel
);
364 Box box
= Box
.createVerticalBox();
365 box
.add(encapsulateBox
);
366 box
.add(Box
.createVerticalStrut(5));
367 box
.add(visibilityBox
);
369 JPanel boxPanel
= new JPanel(new BorderLayout());
370 boxPanel
.add(box
, BorderLayout
.CENTER
);
371 boxPanel
.add(Box
.createVerticalStrut(5), BorderLayout
.NORTH
);
372 panel
.add(boxPanel
, BorderLayout
.SOUTH
);
377 private JComponent
createTable() {
378 myTableModel
= new MyTableModel();
379 myTable
= new Table(myTableModel
);
380 myTable
.setSurrendersFocusOnKeystroke(true);
381 MyTableRenderer renderer
= new MyTableRenderer();
382 TableColumnModel columnModel
= myTable
.getColumnModel();
383 columnModel
.getColumn(CHECKED_COLUMN
).setCellRenderer(new BooleanTableCellRenderer());
384 columnModel
.getColumn(FIELD_COLUMN
).setCellRenderer(renderer
);
385 columnModel
.getColumn(GETTER_COLUMN
).setCellRenderer(renderer
);
386 columnModel
.getColumn(SETTER_COLUMN
).setCellRenderer(renderer
);
387 columnModel
.getColumn(CHECKED_COLUMN
).setMaxWidth(new JCheckBox().getPreferredSize().width
);
389 myTable
.setPreferredScrollableViewportSize(new Dimension(550, myTable
.getRowHeight() * 12));
390 myTable
.getSelectionModel().setSelectionMode(ListSelectionModel
.MULTIPLE_INTERVAL_SELECTION
);
392 JScrollPane scrollPane
= ScrollPaneFactory
.createScrollPane(myTable
);
393 // JLabel label = new JLabel("Fields to Encapsulate");
394 // CompTitledBorder titledBorder = new CompTitledBorder(label);
395 Border titledBorder
= IdeBorderFactory
.createTitledBorder(RefactoringBundle
.message("encapsulate.fields.fields.to.encapsulate.border.title"));
396 Border emptyBorder
= BorderFactory
.createEmptyBorder(0, 5, 5, 5);
397 Border border
= BorderFactory
.createCompoundBorder(titledBorder
, emptyBorder
);
398 scrollPane
.setBorder(border
);
399 // make ESC and ENTER work when focus is in the table
400 myTable
.registerKeyboardAction(new ActionListener() {
401 public void actionPerformed(ActionEvent e
) {
402 TableCellEditor editor
= myTable
.getCellEditor();
403 if (editor
!= null) {
404 editor
.cancelCellEditing();
409 }, KeyStroke
.getKeyStroke(KeyEvent
.VK_ESCAPE
, 0), JComponent
.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT
411 // make SPACE check/uncheck selected rows
412 @NonNls InputMap inputMap
= myTable
.getInputMap();
413 inputMap
.put(KeyStroke
.getKeyStroke(KeyEvent
.VK_SPACE
, 0), "enable_disable");
414 @NonNls ActionMap actionMap
= myTable
.getActionMap();
415 actionMap
.put("enable_disable", new AbstractAction() {
416 public void actionPerformed(ActionEvent e
) {
417 if (myTable
.isEditing()) return;
418 int[] rows
= myTable
.getSelectedRows();
419 if (rows
.length
> 0) {
420 boolean valueToBeSet
= false;
421 for (int row
: rows
) {
422 if (!myCheckedMarks
[row
]) {
427 for (int row
: rows
) {
428 myCheckedMarks
[row
] = valueToBeSet
;
430 myTableModel
.fireTableRowsUpdated(rows
[0], rows
[rows
.length
- 1]);
431 TableUtil
.selectRows(myTable
, rows
);
436 // make ENTER work when the table has focus
437 inputMap
.put(KeyStroke
.getKeyStroke(KeyEvent
.VK_ENTER
, 0), "invokeImpl");
438 actionMap
.put("invokeImpl", new AbstractAction() {
439 public void actionPerformed(ActionEvent e
) {
440 TableCellEditor editor
= myTable
.getCellEditor();
441 if (editor
!= null) {
442 editor
.stopCellEditing();
444 clickDefaultButton();
452 public JComponent
getPreferredFocusedComponent() {
456 protected void doAction() {
457 if (myTable
.isEditing()) {
458 TableCellEditor editor
= myTable
.getCellEditor();
459 if (editor
!= null) {
460 editor
.stopCellEditing();
463 String errorString
= validateData();
464 if (errorString
!= null) { // were errors
465 CommonRefactoringUtil
.showErrorMessage(REFACTORING_NAME
, errorString
, HelpID
.ENCAPSULATE_FIELDS
, myProject
);
469 if (getCheckedRows().length
== 0) {
470 CommonRefactoringUtil
.showErrorMessage(REFACTORING_NAME
, "Nothing found to encapsulate", HelpID
.ENCAPSULATE_FIELDS
, myProject
);
474 invokeRefactoring(new EncapsulateFieldsProcessor(myProject
, this));
475 JavaRefactoringSettings settings
= JavaRefactoringSettings
.getInstance();
476 settings
.ENCAPSULATE_FIELDS_USE_ACCESSORS_WHEN_ACCESSIBLE
= myCbUseAccessorsWhenAccessible
.isSelected();
480 * @return error string if errors were found, or null if everything is ok
482 private String
validateData() {
483 PsiManager manager
= PsiManager
.getInstance(myProject
);
484 for (int idx
= 0; idx
< myFields
.length
; idx
++) {
485 if (myCheckedMarks
[idx
]) {
487 if (isToEncapsulateGet()) {
488 name
= myGetterNames
[idx
];
489 if (!JavaPsiFacade
.getInstance(manager
.getProject()).getNameHelper().isIdentifier(name
)) {
490 return RefactoringMessageUtil
.getIncorrectIdentifierMessage(name
);
493 if (!myFinalMarks
[idx
] && isToEncapsulateSet()) {
494 name
= mySetterNames
[idx
];
495 if (!JavaPsiFacade
.getInstance(manager
.getProject()).getNameHelper().isIdentifier(name
)) {
496 return RefactoringMessageUtil
.getIncorrectIdentifierMessage(name
);
505 protected boolean areButtonsValid() {
506 return getCheckedRows().length
> 0;
509 private PsiMethod
generateMethodPrototype(PsiField field
, String methodName
, boolean isGetter
) {
510 PsiMethod prototype
= isGetter
511 ? PropertyUtil
.generateGetterPrototype(field
)
512 : PropertyUtil
.generateSetterPrototype(field
);
514 PsiElementFactory factory
= JavaPsiFacade
.getInstance(field
.getProject()).getElementFactory();
515 PsiIdentifier identifier
= factory
.createIdentifier(methodName
);
516 prototype
.getNameIdentifier().replace(identifier
);
517 //prototype.getModifierList().setModifierProperty(getAccessorsVisibility(), true);
519 } catch (IncorrectOperationException e
) {
524 private int[] getCheckedRows() {
526 for (boolean checkedMark
: myCheckedMarks
) {
531 int[] rows
= new int[count
];
533 for (int idx
= 0; idx
< myCheckedMarks
.length
; idx
++) {
534 if (myCheckedMarks
[idx
]) {
535 rows
[currentRow
++] = idx
;
541 protected void doHelpAction() {
542 HelpManager
.getInstance().invokeHelp(HelpID
.ENCAPSULATE_FIELDS
);
545 private class MyTableModel
extends AbstractTableModel
{
546 public int getColumnCount() {
550 public int getRowCount() {
551 return myFields
.length
;
554 public Class
getColumnClass(int columnIndex
) {
555 if (columnIndex
== CHECKED_COLUMN
) {
556 return Boolean
.class;
558 return super.getColumnClass(columnIndex
);
561 public Object
getValueAt(int rowIndex
, int columnIndex
) {
562 switch (columnIndex
) {
564 return myCheckedMarks
[rowIndex
] ? Boolean
.TRUE
: Boolean
.FALSE
;
566 return myFieldNames
[rowIndex
];
568 return myGetterNames
[rowIndex
];
570 return mySetterNames
[rowIndex
];
572 throw new RuntimeException("Incorrect column index");
576 public String
getColumnName(int column
) {
581 return RefactoringBundle
.message("encapsulate.fields.field.column.name");
583 return RefactoringBundle
.message("encapsulate.fields.getter.column.name");
585 return RefactoringBundle
.message("encapsulate.fields.setter.column.name");
587 throw new RuntimeException("Incorrect column index");
591 public boolean isCellEditable(int rowIndex
, int columnIndex
) {
592 if (columnIndex
== CHECKED_COLUMN
) return true;
593 if (myCheckedMarks
[rowIndex
]) {
594 if (columnIndex
== GETTER_COLUMN
&& myCbEncapsulateGet
.isSelected()) return true;
595 if (columnIndex
== SETTER_COLUMN
) {
596 if (!myFinalMarks
[rowIndex
] && myCbEncapsulateSet
.isSelected()) return true;
602 public void setValueAt(final Object aValue
, final int rowIndex
, final int columnIndex
) {
603 if (columnIndex
== CHECKED_COLUMN
) {
604 myCheckedMarks
[rowIndex
] = ((Boolean
) aValue
).booleanValue();
605 fireTableRowsUpdated(rowIndex
, rowIndex
);
607 String name
= (String
) aValue
;
608 PsiField field
= myFields
[rowIndex
];
609 switch (columnIndex
) {
611 myGetterNames
[rowIndex
] = name
;
612 myGetterPrototypes
[rowIndex
] = generateMethodPrototype(field
, name
, true);
616 mySetterNames
[rowIndex
] = name
;
617 mySetterPrototypes
[rowIndex
] = generateMethodPrototype(field
, name
, false);
621 throw new RuntimeException("Incorrect column index");
627 private static final Icon OVERRIDING_METHOD_ICON
= IconLoader
.getIcon("/general/overridingMethod.png");
628 private static final Icon IMPLEMENTING_METHOD_ICON
= IconLoader
.getIcon("/general/implementingMethod.png");
629 private static final Icon EMPTY_OVERRIDE_ICON
= new EmptyIcon(16, 16);
631 private class MyTableRenderer
extends DefaultTableCellRenderer
{
632 public Component
getTableCellRendererComponent(JTable table
, final Object value
,
633 boolean isSelected
, boolean hasFocus
, final int row
,
635 super.getTableCellRendererComponent(table
, value
, isSelected
, hasFocus
, row
, column
);
636 final int modelColumn
= myTable
.convertColumnIndexToModel(column
);
638 this.setIconTextGap(0);
639 PsiField field
= myFields
[row
];
640 switch (modelColumn
) {
643 Icon icon
= field
.getIcon(Iconable
.ICON_FLAG_VISIBILITY
);
644 MyTableRenderer
.this.setIcon(icon
);
645 MyTableRenderer
.this.setDisabledIcon(icon
);
646 configureColors(isSelected
, table
, hasFocus
, row
, column
);
653 Icon methodIcon
= IconUtil
.getEmptyIcon(true);
654 Icon overrideIcon
= EMPTY_OVERRIDE_ICON
;
656 PsiMethod prototype
= modelColumn
== GETTER_COLUMN ? myGetterPrototypes
[row
] : mySetterPrototypes
[row
];
657 if (prototype
!= null) {
658 // MyTableRenderer.this.setForeground(Color.black);
659 configureColors(isSelected
, table
, hasFocus
, row
, column
);
661 PsiMethod existing
= myClass
.findMethodBySignature(prototype
, false);
662 if (existing
!= null) {
663 methodIcon
= existing
.getIcon(Iconable
.ICON_FLAG_VISIBILITY
);
666 PsiMethod
[] superMethods
= prototype
.findSuperMethods(myClass
);
667 if (superMethods
.length
> 0) {
668 if (!superMethods
[0].hasModifierProperty(PsiModifier
.ABSTRACT
)) {
669 overrideIcon
= OVERRIDING_METHOD_ICON
;
671 overrideIcon
= IMPLEMENTING_METHOD_ICON
;
675 MyTableRenderer
.this.setForeground(Color
.red
);
678 RowIcon icon
= new RowIcon(2);
679 icon
.setIcon(methodIcon
, 0);
680 icon
.setIcon(overrideIcon
, 1);
681 MyTableRenderer
.this.setIcon(icon
);
682 MyTableRenderer
.this.setDisabledIcon(icon
);
688 MyTableRenderer
.this.setIcon(null);
689 MyTableRenderer
.this.setDisabledIcon(null);
692 boolean enabled
= myCheckedMarks
[row
];
694 if (modelColumn
== GETTER_COLUMN
) {
695 enabled
= myCbEncapsulateGet
.isSelected();
696 } else if (modelColumn
== SETTER_COLUMN
) {
697 enabled
= !myFinalMarks
[row
] && myCbEncapsulateSet
.isSelected();
700 this.setEnabled(enabled
);
704 private void configureColors(boolean isSelected
, JTable table
, boolean hasFocus
, final int row
, final int column
) {
706 setForeground(table
.getSelectionForeground());
708 setForeground(UIUtil
.getTableForeground());
712 if (table
.isCellEditable(row
, column
)) {
713 super.setForeground(UIUtil
.getTableFocusCellForeground());
714 super.setBackground(UIUtil
.getTableFocusCellBackground());