2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6
* The contents of this file are subject to the terms of either the GNU
7
* General Public License Version 2 only ("GPL") or the Common
8
* Development and Distribution License("CDDL") (collectively, the
9
* "License"). You may not use this file except in compliance with the
10
* License. You can obtain a copy of the License at
11
* http://www.netbeans.org/cddl-gplv2.html
12
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
* specific language governing permissions and limitations under the
14
* License. When distributing the software, include this License Header
15
* Notice in each file and include the License file at
16
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17
* particular file as subject to the "Classpath" exception as provided
18
* by Sun in the GPL Version 2 section of the License file that
19
* accompanied this code. If applicable, add the following below the
20
* License Header, with the fields enclosed by brackets [] replaced by
21
* your own identifying information:
22
* "Portions Copyrighted [year] [name of copyright owner]"
26
* The Original Software is NetBeans. The Initial Developer of the Original
27
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
28
* Microsystems, Inc. All Rights Reserved.
30
* If you wish your version of this file to be governed by only the CDDL
31
* or only the GPL Version 2, indicate your decision by adding
32
* "[Contributor] elects to include this software in this distribution
33
* under the [CDDL or GPL Version 2] license." If you do not indicate a
34
* single choice of license, a recipient has the option to distribute
35
* your version of this file under either the CDDL, the GPL Version 2 or
36
* to extend the choice of license to its licensees as provided above.
37
* However, if you add GPL Version 2 code and therefore, elected the GPL
38
* Version 2 license, then the option applies only if the new code is
39
* made subject to such option by the copyright holder.
41
package org.netbeans.modules.refactoring.java.ui;
43
import java.awt.Color;
44
import java.awt.Component;
45
import java.awt.event.ActionEvent;
46
import java.awt.event.KeyEvent;
47
import java.io.IOException;
48
import java.text.MessageFormat;
52
import javax.lang.model.element.*;
53
import javax.lang.model.type.ArrayType;
54
import javax.lang.model.type.TypeMirror;
56
import javax.swing.event.*;
57
import javax.swing.table.*;
58
import org.netbeans.api.java.source.CompilationController;
59
import org.netbeans.api.java.source.SourceUtils;
60
import org.netbeans.api.java.source.TreePathHandle;
61
import org.netbeans.modules.refactoring.java.RetoucheUtils;
62
import org.netbeans.modules.refactoring.java.plugins.LocalVarScanner;
63
import org.netbeans.modules.refactoring.spi.ui.CustomRefactoringPanel;
64
import org.openide.util.Exceptions;
65
import org.openide.util.NbBundle;
68
* Panel contains components for signature change. There is table with
69
* parameters, you can add parameters, reorder parameteres or remove not
70
* used paramaters (not available yet). You can also change access modifier.
72
* @author Pavel Flaska, Jan Becicka
74
public class ChangeParametersPanel extends JPanel implements CustomRefactoringPanel {
76
TreePathHandle refactoredObj;
77
ParamTableModel model;
78
private ChangeListener parent;
80
private static Action editAction = null;
81
private String returnType;
83
private static final String[] modifierNames = {
85
"protected", // NOI18N
86
"<default>", // NOI18N
90
public Component getComponent() {
94
private static final String[] columnNames = {
95
getString("LBL_ChangeParsColName"), // NOI18N
96
getString("LBL_ChangeParsColType"), // NOI18N
97
getString("LBL_ChangeParsColDefVal"), // NOI18N
98
getString("LBL_ChangeParsColOrigIdx"), // NOI18N
99
getString("LBL_ChangeParsParUsed") // NOI18N
102
// modifier items in combo - indexes
103
private static final int MOD_PUBLIC_INDEX = 0;
104
private static final int MOD_PROTECTED_INDEX = 1;
105
private static final int MOD_DEFAULT_INDEX = 2;
106
private static final int MOD_PRIVATE_INDEX = 3;
108
private static final String ACTION_INLINE_EDITOR = "invokeInlineEditor"; //NOI18N
110
/** Creates new form ChangeMethodSignature */
111
public ChangeParametersPanel(TreePathHandle refactoredObj, ChangeListener parent) {
112
this.refactoredObj = refactoredObj;
113
this.parent = parent;
114
model = new ParamTableModel(columnNames, 0);
118
private boolean initialized = false;
119
public void initialize() {
124
org.netbeans.api.java.source.JavaSource source = org.netbeans.api.java.source.JavaSource.forFileObject(org.netbeans.modules.refactoring.java.RetoucheUtils.getFileObject(refactoredObj));
125
source.runUserActionTask(new org.netbeans.api.java.source.CancellableTask<org.netbeans.api.java.source.CompilationController>() {
126
public void run(org.netbeans.api.java.source.CompilationController info) {
128
info.toPhase(org.netbeans.api.java.source.JavaSource.Phase.RESOLVED);
129
ExecutableElement e = (ExecutableElement) refactoredObj.resolveElement(info);
130
returnType = e.getReturnType().toString();
131
Element def = SourceUtils.getEnclosingTypeElement(e);
132
if (def.getKind().isInterface()) {
133
modifiersCombo.setEnabled(false);
136
setModifier(e.getModifiers());
137
previewChange.setText(genDeclarationString());
139
catch (IOException ex) {
140
Exceptions.printStackTrace(ex);
144
public void cancel() {
149
catch (IOException ex) {
150
Exceptions.printStackTrace(ex);
154
protected DefaultTableModel getTableModel() {
158
protected Set<Modifier> getModifier() {
159
modifiers.remove(Modifier.PRIVATE);
160
modifiers.remove(Modifier.PUBLIC);
161
modifiers.remove(Modifier.PROTECTED);
163
switch (modifiersCombo.getSelectedIndex()) {
164
case MOD_PRIVATE_INDEX: modifiers.add(Modifier.PRIVATE);break;
165
case MOD_DEFAULT_INDEX: break; /* no modifier */
166
case MOD_PROTECTED_INDEX: modifiers.add(Modifier.PROTECTED); break;
167
case MOD_PUBLIC_INDEX: modifiers.add(Modifier.PUBLIC); break;
171
/** This method is called from within the constructor to
172
* initialize the form.
173
* WARNING: Do NOT modify this code. The content of this method is
174
* always regenerated by the Form Editor.
176
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
177
private void initComponents() {
178
java.awt.GridBagConstraints gridBagConstraints;
180
modifiersPanel = new javax.swing.JPanel();
181
modifiersLabel = new javax.swing.JLabel();
182
modifiersCombo = new javax.swing.JComboBox();
183
eastPanel = new javax.swing.JPanel();
184
buttonsPanel = new javax.swing.JPanel();
185
addButton = new javax.swing.JButton();
186
removeButton = new javax.swing.JButton();
187
moveUpButton = new javax.swing.JButton();
188
moveDownButton = new javax.swing.JButton();
189
fillPanel = new javax.swing.JPanel();
190
westPanel = new javax.swing.JScrollPane();
191
paramTable = new javax.swing.JTable();
192
paramTitle = new javax.swing.JLabel();
193
previewChange = new javax.swing.JLabel();
195
setBorder(javax.swing.BorderFactory.createEmptyBorder(12, 12, 11, 11));
196
setAutoscrolls(true);
197
setName(getString("LBL_TitleChangeParameters"));
198
setLayout(new java.awt.GridBagLayout());
200
modifiersPanel.setLayout(new java.awt.GridBagLayout());
202
modifiersLabel.setLabelFor(modifiersCombo);
203
java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("org/netbeans/modules/refactoring/java/ui/Bundle"); // NOI18N
204
org.openide.awt.Mnemonics.setLocalizedText(modifiersLabel, bundle.getString("LBL_ChangeParsMods")); // NOI18N
205
gridBagConstraints = new java.awt.GridBagConstraints();
206
gridBagConstraints.gridx = 0;
207
gridBagConstraints.gridy = 0;
208
modifiersPanel.add(modifiersLabel, gridBagConstraints);
210
modifiersCombo.setModel(new DefaultComboBoxModel(modifierNames));
211
modifiersCombo.addActionListener(new java.awt.event.ActionListener() {
212
public void actionPerformed(java.awt.event.ActionEvent evt) {
213
modifiersComboActionPerformed(evt);
216
gridBagConstraints = new java.awt.GridBagConstraints();
217
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
218
gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
219
gridBagConstraints.weightx = 1.0;
220
gridBagConstraints.insets = new java.awt.Insets(0, 12, 0, 0);
221
modifiersPanel.add(modifiersCombo, gridBagConstraints);
222
modifiersCombo.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_modifiersCombo")); // NOI18N
224
gridBagConstraints = new java.awt.GridBagConstraints();
225
gridBagConstraints.gridx = 0;
226
gridBagConstraints.gridy = 2;
227
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
228
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
229
gridBagConstraints.insets = new java.awt.Insets(11, 0, 0, 0);
230
add(modifiersPanel, gridBagConstraints);
232
eastPanel.setLayout(new java.awt.GridBagLayout());
234
buttonsPanel.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 11, 1, 1));
235
buttonsPanel.setLayout(new java.awt.GridBagLayout());
237
org.openide.awt.Mnemonics.setLocalizedText(addButton, bundle.getString("LBL_ChangeParsAdd")); // NOI18N
238
addButton.addActionListener(new java.awt.event.ActionListener() {
239
public void actionPerformed(java.awt.event.ActionEvent evt) {
240
addButtonActionPerformed(evt);
243
gridBagConstraints = new java.awt.GridBagConstraints();
244
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
245
gridBagConstraints.insets = new java.awt.Insets(0, 0, 5, 0);
246
buttonsPanel.add(addButton, gridBagConstraints);
247
addButton.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_ChangeParsAdd")); // NOI18N
249
org.openide.awt.Mnemonics.setLocalizedText(removeButton, bundle.getString("LBL_ChangeParsRemove")); // NOI18N
250
removeButton.setEnabled(false);
251
removeButton.addActionListener(new java.awt.event.ActionListener() {
252
public void actionPerformed(java.awt.event.ActionEvent evt) {
253
removeButtonActionPerformed(evt);
256
gridBagConstraints = new java.awt.GridBagConstraints();
257
gridBagConstraints.gridx = 0;
258
gridBagConstraints.gridy = 1;
259
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
260
gridBagConstraints.insets = new java.awt.Insets(0, 0, 5, 0);
261
buttonsPanel.add(removeButton, gridBagConstraints);
262
removeButton.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_ChangeParsRemove")); // NOI18N
264
org.openide.awt.Mnemonics.setLocalizedText(moveUpButton, bundle.getString("LBL_ChangeParsMoveUp")); // NOI18N
265
moveUpButton.setEnabled(false);
266
moveUpButton.addActionListener(new java.awt.event.ActionListener() {
267
public void actionPerformed(java.awt.event.ActionEvent evt) {
268
moveUpButtonActionPerformed(evt);
271
gridBagConstraints = new java.awt.GridBagConstraints();
272
gridBagConstraints.gridx = 0;
273
gridBagConstraints.gridy = 2;
274
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
275
gridBagConstraints.insets = new java.awt.Insets(0, 0, 5, 0);
276
buttonsPanel.add(moveUpButton, gridBagConstraints);
277
moveUpButton.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_ChangeParsMoveUp")); // NOI18N
279
org.openide.awt.Mnemonics.setLocalizedText(moveDownButton, bundle.getString("LBL_ChangeParsMoveDown")); // NOI18N
280
moveDownButton.setEnabled(false);
281
moveDownButton.addActionListener(new java.awt.event.ActionListener() {
282
public void actionPerformed(java.awt.event.ActionEvent evt) {
283
moveDownButtonActionPerformed(evt);
286
gridBagConstraints = new java.awt.GridBagConstraints();
287
gridBagConstraints.gridx = 0;
288
gridBagConstraints.gridy = 3;
289
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
290
buttonsPanel.add(moveDownButton, gridBagConstraints);
291
moveDownButton.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_ChangeParsMoveDown")); // NOI18N
293
gridBagConstraints = new java.awt.GridBagConstraints();
294
gridBagConstraints.gridx = 0;
295
gridBagConstraints.gridy = 0;
296
eastPanel.add(buttonsPanel, gridBagConstraints);
297
gridBagConstraints = new java.awt.GridBagConstraints();
298
gridBagConstraints.gridx = 0;
299
gridBagConstraints.gridy = 1;
300
gridBagConstraints.weightx = 1.0;
301
gridBagConstraints.weighty = 1.0;
302
eastPanel.add(fillPanel, gridBagConstraints);
304
gridBagConstraints = new java.awt.GridBagConstraints();
305
gridBagConstraints.gridx = 1;
306
gridBagConstraints.gridy = 1;
307
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
308
add(eastPanel, gridBagConstraints);
310
westPanel.setPreferredSize(new java.awt.Dimension(453, 100));
312
paramTable.setModel(model);
314
paramTable.getSelectionModel().addListSelectionListener(getListener1());
315
paramTable.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
316
model.addTableModelListener(getListener2());
317
paramTable.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0), ACTION_INLINE_EDITOR); //NOI18N
318
paramTable.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_F2, 0), ACTION_INLINE_EDITOR); //NOI18N
319
paramTable.getActionMap().put(ACTION_INLINE_EDITOR, getEditAction()); //NOI18N
320
paramTable.setSurrendersFocusOnKeystroke(true);
321
paramTable.setCellSelectionEnabled(false);
322
paramTable.setRowSelectionAllowed(true);
323
paramTable.putClientProperty("JTable.autoStartsEdit", Boolean.FALSE); //NOI18N
324
paramTable.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE); //NOI18N
325
westPanel.setViewportView(paramTable);
326
paramTable.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_paramTable")); // NOI18N
328
gridBagConstraints = new java.awt.GridBagConstraints();
329
gridBagConstraints.gridx = 0;
330
gridBagConstraints.gridy = 1;
331
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
332
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
333
gridBagConstraints.weightx = 1.0;
334
gridBagConstraints.weighty = 1.0;
335
add(westPanel, gridBagConstraints);
337
paramTitle.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
338
paramTitle.setLabelFor(paramTable);
339
org.openide.awt.Mnemonics.setLocalizedText(paramTitle, bundle.getString("LBL_ChangeParsParameters")); // NOI18N
340
gridBagConstraints = new java.awt.GridBagConstraints();
341
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
342
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
343
gridBagConstraints.insets = new java.awt.Insets(0, 0, 2, 0);
344
add(paramTitle, gridBagConstraints);
346
previewChange.setBorder(javax.swing.BorderFactory.createTitledBorder(org.openide.util.NbBundle.getBundle(ChangeParametersPanel.class).getString("LBL_ChangeParsPreview"))); // NOI18N
347
gridBagConstraints = new java.awt.GridBagConstraints();
348
gridBagConstraints.gridx = 0;
349
gridBagConstraints.gridy = 3;
350
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
351
gridBagConstraints.insets = new java.awt.Insets(11, 0, 0, 0);
352
add(previewChange, gridBagConstraints);
353
}// </editor-fold>//GEN-END:initComponents
355
private void modifiersComboActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_modifiersComboActionPerformed
356
previewChange.setText(genDeclarationString());
357
}//GEN-LAST:event_modifiersComboActionPerformed
359
private void removeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeButtonActionPerformed
361
int[] selectedRows = paramTable.getSelectedRows();
362
ListSelectionModel selectionModel = paramTable.getSelectionModel();
363
for (int i = 0; i < selectedRows.length; ++i) {
364
boolean b = ((Boolean) ((Vector) model.getDataVector().get(selectedRows[i] - i)).get(4)).booleanValue();
366
String title = getString("LBL_ChangeParsCannotDeleteTitle");
367
String mes = MessageFormat.format(getString("LBL_ChangeParsCannotDelete"),((Vector) model.getDataVector().get(selectedRows[i] - i)).get(0));
368
int a = new JOptionPane().showConfirmDialog(this, mes, title, JOptionPane.YES_NO_OPTION);
369
if (a==JOptionPane.YES_OPTION) {
370
model.removeRow(selectedRows[i] - i);
371
selectionModel.clearSelection();
374
model.removeRow(selectedRows[i] - i);
375
selectionModel.clearSelection();
378
}//GEN-LAST:event_removeButtonActionPerformed
380
private void moveDownButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_moveDownButtonActionPerformed
382
}//GEN-LAST:event_moveDownButtonActionPerformed
384
private void moveUpButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_moveUpButtonActionPerformed
386
}//GEN-LAST:event_moveUpButtonActionPerformed
388
private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addButtonActionPerformed
390
int rowCount = model.getRowCount();
391
model.addRow(new Object[] { "par" + rowCount, "Object", "null", new Integer(-1), Boolean.TRUE }); // NOI18N
392
}//GEN-LAST:event_addButtonActionPerformed
394
// Variables declaration - do not modify//GEN-BEGIN:variables
395
private javax.swing.JButton addButton;
396
private javax.swing.JPanel buttonsPanel;
397
private javax.swing.JPanel eastPanel;
398
private javax.swing.JPanel fillPanel;
399
private javax.swing.JComboBox modifiersCombo;
400
private javax.swing.JLabel modifiersLabel;
401
private javax.swing.JPanel modifiersPanel;
402
private javax.swing.JButton moveDownButton;
403
private javax.swing.JButton moveUpButton;
404
private javax.swing.JTable paramTable;
405
private javax.swing.JLabel paramTitle;
406
private javax.swing.JLabel previewChange;
407
private javax.swing.JButton removeButton;
408
private javax.swing.JScrollPane westPanel;
409
// End of variables declaration//GEN-END:variables
411
private ListSelectionListener getListener1() {
412
return new ListSelectionListener() {
413
public void valueChanged(ListSelectionEvent e) {
414
if (e.getValueIsAdjusting())
417
ListSelectionModel lsm = (ListSelectionModel) e.getSource();
419
if (!lsm.isSelectionEmpty()) {
420
// Find out which indexes are selected.
421
int minIndex = lsm.getMinSelectionIndex();
422
int maxIndex = lsm.getMaxSelectionIndex();
423
setButtons(minIndex, maxIndex);
425
boolean enableRemoveBtn = true;
426
for (int i = minIndex; i <= maxIndex; i++) {
427
enableRemoveBtn = model.isRemovable(i);
428
if (!enableRemoveBtn)
431
removeButton.setEnabled(enableRemoveBtn);
434
moveDownButton.setEnabled(false);
435
moveUpButton.setEnabled(false);
436
removeButton.setEnabled(false);
442
private TableModelListener getListener2() {
443
return new TableModelListener() {
444
public void tableChanged(TableModelEvent e) {
445
// update buttons availability
446
int[] selectedRows = paramTable.getSelectedRows();
447
if (selectedRows.length == 0) {
448
removeButton.setEnabled(false);
451
boolean enableRemoveBtn = true;
452
for (int i = 0; i < selectedRows.length; i++) {
453
if (selectedRows[i] < model.getRowCount()) {
454
enableRemoveBtn = model.isCellEditable(selectedRows[i], 0);
455
if (!enableRemoveBtn)
459
removeButton.setEnabled(enableRemoveBtn);
460
int min = selectedRows[0];
461
int max = selectedRows[selectedRows.length - 1];
462
setButtons(min, max);
466
previewChange.setText(genDeclarationString());
468
parent.stateChanged(null);
473
private void initTableData(CompilationController info) {
474
ExecutableElement method = (ExecutableElement) refactoredObj.resolveElement(info);
476
List<? extends VariableElement> pars = method.getParameters();
478
// List typeList = new ArrayList();
479
// for (Iterator parIt = pars.iterator(); parIt.hasNext(); ) {
480
// Parameter par = (Parameter) parIt.next();
481
// typeList.add(par.getType());
484
Collection<ExecutableElement> allMethods = new ArrayList();
485
allMethods.addAll(RetoucheUtils.getOverridenMethods(method, info));
486
allMethods.addAll(RetoucheUtils.getOverridingMethods(method, info));
487
allMethods.add(method);
489
for (ExecutableElement currentMethod: allMethods) {
490
int originalIndex = 0;
491
for (VariableElement par:currentMethod.getParameters()) {
492
TypeMirror desc = par.asType();
493
String typeRepresentation;
494
if (method.isVarArgs() && originalIndex == pars.size()-1) {
495
typeRepresentation = getTypeStringRepresentation(((ArrayType)desc).getComponentType()) + " ..."; // NOI18N
497
typeRepresentation = getTypeStringRepresentation(desc);
499
LocalVarScanner scan = new LocalVarScanner(info, null);
500
scan.scan(info.getTrees().getPath(method), par);
501
Boolean removable = !scan.hasRefernces();
502
if (model.getRowCount()<=originalIndex) {
503
Object[] parRep = new Object[] { par.toString(), typeRepresentation, "", new Integer(originalIndex), removable };
504
model.addRow(parRep);
506
removable = Boolean.valueOf(model.isRemovable(originalIndex) && removable.booleanValue());
507
((Vector) model.getDataVector().get(originalIndex)).set(4, removable);
514
private static String getTypeStringRepresentation(TypeMirror desc) {
515
return desc.toString();
518
private boolean acceptEditedValue() {
519
TableCellEditor tce = paramTable.getCellEditor();
521
return paramTable.getCellEditor().stopCellEditing();
525
private void doMove(int step) {
528
ListSelectionModel selectionModel = paramTable.getSelectionModel();
529
int min = selectionModel.getMinSelectionIndex();
530
int max = selectionModel.getMaxSelectionIndex();
532
selectionModel.clearSelection();
533
model.moveRow(min, max, min + step);
534
selectionModel.addSelectionInterval(min + step, max + step);
537
private void setButtons(int min, int max) {
538
int r = model.getRowCount() - 1;
539
moveUpButton.setEnabled(min > 0 ? true : false);
540
moveDownButton.setEnabled(max < r ? true : false);
543
private void initRenderer() {
544
TableColumnModel tcm = paramTable.getColumnModel();
545
paramTable.removeColumn(tcm.getColumn(3));
546
paramTable.removeColumn(tcm.getColumn(3));
547
Enumeration columns = paramTable.getColumnModel().getColumns();
548
TableColumn tc = null;
549
while (columns.hasMoreElements()) {
550
tc = (TableColumn) columns.nextElement();
551
tc.setCellRenderer(new ParamRenderer());
555
private Set<Modifier> modifiers = new HashSet();
556
private void setModifier(Set<Modifier> mods) {
557
this.modifiers.clear();
558
this.modifiers.addAll(mods);
559
if (mods.contains(Modifier.PRIVATE))
560
modifiersCombo.setSelectedIndex(MOD_PRIVATE_INDEX);
561
else if (mods.contains(Modifier.PROTECTED))
562
modifiersCombo.setSelectedIndex(MOD_PROTECTED_INDEX);
563
else if (mods.contains(Modifier.PUBLIC))
564
modifiersCombo.setSelectedIndex(MOD_PUBLIC_INDEX);
566
modifiersCombo.setSelectedIndex(MOD_DEFAULT_INDEX);
569
public String genDeclarationString() {
570
// generate preview for modifiers
572
String mod = modifiersCombo.getSelectedIndex() != MOD_DEFAULT_INDEX /*default modifier?*/ ?
573
(String) modifiersCombo.getSelectedItem() + ' ' : ""; // NOI18N
575
StringBuffer buf = new StringBuffer(mod);
576
// other than access modifiers - using data provided by the element
577
// first of all, reset access modifier, because it is generated from combo value
578
// String otherMod = Modifier.toString(((CallableFeature) refactoredObj).getModifiers() & 0xFFFFFFF8);
579
// if (otherMod.length() != 0) {
580
// buf.append(otherMod);
583
// generate the return type for the method and name
584
// for the both - method and constructor
586
if (RetoucheUtils.getElementKind(refactoredObj) == ElementKind.METHOD) {
587
buf.append(returnType);
589
name = RetoucheUtils.getSimpleName(refactoredObj);
591
// for constructor, get name from the declaring class
592
name = RetoucheUtils.getSimpleName(refactoredObj);
596
// generate parameters to the preview string
597
List[] parameters = (List[]) model.getDataVector().toArray(new List[0]);
598
if (parameters.length > 0) {
600
for (i = 0; i < parameters.length - 1; i++) {
601
buf.append((String) parameters[i].get(1));
603
buf.append((String) parameters[i].get(0));
604
buf.append(',').append(' ');
606
buf.append((String) parameters[i].get(1));
608
buf.append((String) parameters[i].get(0));
610
buf.append(')'); //NOI18N
612
return buf.toString();
615
private static String getString(String key) {
616
return NbBundle.getMessage(ChangeParametersPanel.class, key);
619
private static Action getEditAction() {
620
if (editAction == null) {
621
editAction = new EditAction();
626
private static void autoEdit(JTable tab) {
627
if (tab.editCellAt(tab.getSelectedRow(), tab.getSelectedColumn(), null) &&
628
tab.getEditorComponent() != null)
630
JTextField field = (JTextField) tab.getEditorComponent();
631
int len = field.getText().length();
632
field.setCaretPosition(field.getText().length());
633
field.requestFocusInWindow();
638
////////////////////////////////////////////////////////////////////////////
640
////////////////////////////////////////////////////////////////////////////
641
// this class is used for marking rows as read-only. If the user uses
642
// standard DefaultTableModel, rows added through its methods is added
643
// as a read-write. -- Use methods with Boolean paramater to add
644
// rows marked as read-only.
645
static class ParamTableModel extends DefaultTableModel {
647
public ParamTableModel(Object[] data, int rowCount) {
648
super(data, rowCount);
651
public boolean isCellEditable(int row, int column) {
653
// check box indicating usage of parameter is not editable
656
// otherwise, check that user can change only the values provided
657
// for the new parameter. (name change of old parameters aren't
659
Integer origIdx = (Integer) ((Vector) getDataVector().get(row)).get(3);
660
return origIdx.intValue() == -1 ? true : false;
663
public boolean isRemovable(int row) {
664
return true;//((Boolean) ((Vector) getDataVector().get(row)).get(4)).booleanValue();
667
public Class getColumnClass(int c) {
668
return getValueAt(0, c).getClass();
670
} // end ParamTableModel
672
private static class EditAction extends AbstractAction {
673
public void actionPerformed(ActionEvent ae) {
674
autoEdit((JTable) ae.getSource());
678
class ParamRenderer extends DefaultTableCellRenderer implements TableCellRenderer {
679
Color origBackground;
681
public ParamRenderer() {
683
origBackground = getBackground();
686
public Component getTableCellRendererComponent(JTable table, Object value,
687
boolean isSelected, boolean hasFocus,
690
super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
691
boolean isRemovable = model.isRemovable(row);
694
setBackground(UIManager.getColor("Panel.background")); // NOI18N
696
setBackground(origBackground);