001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.refactoring.java.ui;
042:
043: import java.awt.Color;
044: import java.awt.Component;
045: import java.awt.event.ActionEvent;
046: import java.awt.event.KeyEvent;
047: import java.io.IOException;
048: import java.text.MessageFormat;
049: import java.util.*;
050: import java.util.Set;
051: import java.util.Set;
052: import javax.lang.model.element.*;
053: import javax.lang.model.type.ArrayType;
054: import javax.lang.model.type.TypeMirror;
055: import javax.swing.*;
056: import javax.swing.event.*;
057: import javax.swing.table.*;
058: import org.netbeans.api.java.source.CompilationController;
059: import org.netbeans.api.java.source.SourceUtils;
060: import org.netbeans.api.java.source.TreePathHandle;
061: import org.netbeans.modules.refactoring.java.RetoucheUtils;
062: import org.netbeans.modules.refactoring.java.plugins.LocalVarScanner;
063: import org.netbeans.modules.refactoring.spi.ui.CustomRefactoringPanel;
064: import org.openide.util.Exceptions;
065: import org.openide.util.NbBundle;
066:
067: /**
068: * Panel contains components for signature change. There is table with
069: * parameters, you can add parameters, reorder parameteres or remove not
070: * used paramaters (not available yet). You can also change access modifier.
071: *
072: * @author Pavel Flaska, Jan Becicka
073: */
074: public class ChangeParametersPanel extends JPanel implements
075: CustomRefactoringPanel {
076:
077: TreePathHandle refactoredObj;
078: ParamTableModel model;
079: private ChangeListener parent;
080:
081: private static Action editAction = null;
082: private String returnType;
083:
084: private static final String[] modifierNames = { "public", // NOI18N
085: "protected", // NOI18N
086: "<default>", // NOI18N
087: "private" // NOI18N
088: };
089:
090: public Component getComponent() {
091: return this ;
092: }
093:
094: private static final String[] columnNames = {
095: getString("LBL_ChangeParsColName"), // NOI18N
096: getString("LBL_ChangeParsColType"), // NOI18N
097: getString("LBL_ChangeParsColDefVal"), // NOI18N
098: getString("LBL_ChangeParsColOrigIdx"), // NOI18N
099: getString("LBL_ChangeParsParUsed") // NOI18N
100: };
101:
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;
107:
108: private static final String ACTION_INLINE_EDITOR = "invokeInlineEditor"; //NOI18N
109:
110: /** Creates new form ChangeMethodSignature */
111: public ChangeParametersPanel(TreePathHandle refactoredObj,
112: ChangeListener parent) {
113: this .refactoredObj = refactoredObj;
114: this .parent = parent;
115: model = new ParamTableModel(columnNames, 0);
116: initComponents();
117: }
118:
119: private boolean initialized = false;
120:
121: public void initialize() {
122: try {
123: if (initialized) {
124: return;
125: }
126: org.netbeans.api.java.source.JavaSource source = org.netbeans.api.java.source.JavaSource
127: .forFileObject(org.netbeans.modules.refactoring.java.RetoucheUtils
128: .getFileObject(refactoredObj));
129: source
130: .runUserActionTask(
131: new org.netbeans.api.java.source.CancellableTask<org.netbeans.api.java.source.CompilationController>() {
132: public void run(
133: org.netbeans.api.java.source.CompilationController info) {
134: try {
135: info
136: .toPhase(org.netbeans.api.java.source.JavaSource.Phase.RESOLVED);
137: ExecutableElement e = (ExecutableElement) refactoredObj
138: .resolveElement(info);
139: returnType = e.getReturnType()
140: .toString();
141: Element def = SourceUtils
142: .getEnclosingTypeElement(e);
143: if (def.getKind().isInterface()) {
144: modifiersCombo
145: .setEnabled(false);
146: }
147: initTableData(info);
148: setModifier(e.getModifiers());
149: previewChange
150: .setText(genDeclarationString());
151: } catch (IOException ex) {
152: Exceptions.printStackTrace(ex);
153: }
154: }
155:
156: public void cancel() {
157: }
158: }, true);
159: initialized = true;
160: } catch (IOException ex) {
161: Exceptions.printStackTrace(ex);
162: }
163: }
164:
165: protected DefaultTableModel getTableModel() {
166: return model;
167: }
168:
169: protected Set<Modifier> getModifier() {
170: modifiers.remove(Modifier.PRIVATE);
171: modifiers.remove(Modifier.PUBLIC);
172: modifiers.remove(Modifier.PROTECTED);
173:
174: switch (modifiersCombo.getSelectedIndex()) {
175: case MOD_PRIVATE_INDEX:
176: modifiers.add(Modifier.PRIVATE);
177: break;
178: case MOD_DEFAULT_INDEX:
179: break; /* no modifier */
180: case MOD_PROTECTED_INDEX:
181: modifiers.add(Modifier.PROTECTED);
182: break;
183: case MOD_PUBLIC_INDEX:
184: modifiers.add(Modifier.PUBLIC);
185: break;
186: }
187: return modifiers;
188: }
189:
190: /** This method is called from within the constructor to
191: * initialize the form.
192: * WARNING: Do NOT modify this code. The content of this method is
193: * always regenerated by the Form Editor.
194: */
195: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
196: private void initComponents() {
197: java.awt.GridBagConstraints gridBagConstraints;
198:
199: modifiersPanel = new javax.swing.JPanel();
200: modifiersLabel = new javax.swing.JLabel();
201: modifiersCombo = new javax.swing.JComboBox();
202: eastPanel = new javax.swing.JPanel();
203: buttonsPanel = new javax.swing.JPanel();
204: addButton = new javax.swing.JButton();
205: removeButton = new javax.swing.JButton();
206: moveUpButton = new javax.swing.JButton();
207: moveDownButton = new javax.swing.JButton();
208: fillPanel = new javax.swing.JPanel();
209: westPanel = new javax.swing.JScrollPane();
210: paramTable = new javax.swing.JTable();
211: paramTitle = new javax.swing.JLabel();
212: previewChange = new javax.swing.JLabel();
213:
214: setBorder(javax.swing.BorderFactory.createEmptyBorder(12, 12,
215: 11, 11));
216: setAutoscrolls(true);
217: setName(getString("LBL_TitleChangeParameters"));
218: setLayout(new java.awt.GridBagLayout());
219:
220: modifiersPanel.setLayout(new java.awt.GridBagLayout());
221:
222: modifiersLabel.setLabelFor(modifiersCombo);
223: java.util.ResourceBundle bundle = java.util.ResourceBundle
224: .getBundle("org/netbeans/modules/refactoring/java/ui/Bundle"); // NOI18N
225: org.openide.awt.Mnemonics.setLocalizedText(modifiersLabel,
226: bundle.getString("LBL_ChangeParsMods")); // NOI18N
227: gridBagConstraints = new java.awt.GridBagConstraints();
228: gridBagConstraints.gridx = 0;
229: gridBagConstraints.gridy = 0;
230: modifiersPanel.add(modifiersLabel, gridBagConstraints);
231:
232: modifiersCombo
233: .setModel(new DefaultComboBoxModel(modifierNames));
234: modifiersCombo
235: .addActionListener(new java.awt.event.ActionListener() {
236: public void actionPerformed(
237: java.awt.event.ActionEvent evt) {
238: modifiersComboActionPerformed(evt);
239: }
240: });
241: gridBagConstraints = new java.awt.GridBagConstraints();
242: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
243: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
244: gridBagConstraints.weightx = 1.0;
245: gridBagConstraints.insets = new java.awt.Insets(0, 12, 0, 0);
246: modifiersPanel.add(modifiersCombo, gridBagConstraints);
247: modifiersCombo.getAccessibleContext().setAccessibleDescription(
248: bundle.getString("ACSD_modifiersCombo")); // NOI18N
249:
250: gridBagConstraints = new java.awt.GridBagConstraints();
251: gridBagConstraints.gridx = 0;
252: gridBagConstraints.gridy = 2;
253: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
254: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
255: gridBagConstraints.insets = new java.awt.Insets(11, 0, 0, 0);
256: add(modifiersPanel, gridBagConstraints);
257:
258: eastPanel.setLayout(new java.awt.GridBagLayout());
259:
260: buttonsPanel.setBorder(javax.swing.BorderFactory
261: .createEmptyBorder(1, 11, 1, 1));
262: buttonsPanel.setLayout(new java.awt.GridBagLayout());
263:
264: org.openide.awt.Mnemonics.setLocalizedText(addButton, bundle
265: .getString("LBL_ChangeParsAdd")); // NOI18N
266: addButton
267: .addActionListener(new java.awt.event.ActionListener() {
268: public void actionPerformed(
269: java.awt.event.ActionEvent evt) {
270: addButtonActionPerformed(evt);
271: }
272: });
273: gridBagConstraints = new java.awt.GridBagConstraints();
274: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
275: gridBagConstraints.insets = new java.awt.Insets(0, 0, 5, 0);
276: buttonsPanel.add(addButton, gridBagConstraints);
277: addButton.getAccessibleContext().setAccessibleDescription(
278: bundle.getString("ACSD_ChangeParsAdd")); // NOI18N
279:
280: org.openide.awt.Mnemonics.setLocalizedText(removeButton, bundle
281: .getString("LBL_ChangeParsRemove")); // NOI18N
282: removeButton.setEnabled(false);
283: removeButton
284: .addActionListener(new java.awt.event.ActionListener() {
285: public void actionPerformed(
286: java.awt.event.ActionEvent evt) {
287: removeButtonActionPerformed(evt);
288: }
289: });
290: gridBagConstraints = new java.awt.GridBagConstraints();
291: gridBagConstraints.gridx = 0;
292: gridBagConstraints.gridy = 1;
293: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
294: gridBagConstraints.insets = new java.awt.Insets(0, 0, 5, 0);
295: buttonsPanel.add(removeButton, gridBagConstraints);
296: removeButton.getAccessibleContext().setAccessibleDescription(
297: bundle.getString("ACSD_ChangeParsRemove")); // NOI18N
298:
299: org.openide.awt.Mnemonics.setLocalizedText(moveUpButton, bundle
300: .getString("LBL_ChangeParsMoveUp")); // NOI18N
301: moveUpButton.setEnabled(false);
302: moveUpButton
303: .addActionListener(new java.awt.event.ActionListener() {
304: public void actionPerformed(
305: java.awt.event.ActionEvent evt) {
306: moveUpButtonActionPerformed(evt);
307: }
308: });
309: gridBagConstraints = new java.awt.GridBagConstraints();
310: gridBagConstraints.gridx = 0;
311: gridBagConstraints.gridy = 2;
312: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
313: gridBagConstraints.insets = new java.awt.Insets(0, 0, 5, 0);
314: buttonsPanel.add(moveUpButton, gridBagConstraints);
315: moveUpButton.getAccessibleContext().setAccessibleDescription(
316: bundle.getString("ACSD_ChangeParsMoveUp")); // NOI18N
317:
318: org.openide.awt.Mnemonics.setLocalizedText(moveDownButton,
319: bundle.getString("LBL_ChangeParsMoveDown")); // NOI18N
320: moveDownButton.setEnabled(false);
321: moveDownButton
322: .addActionListener(new java.awt.event.ActionListener() {
323: public void actionPerformed(
324: java.awt.event.ActionEvent evt) {
325: moveDownButtonActionPerformed(evt);
326: }
327: });
328: gridBagConstraints = new java.awt.GridBagConstraints();
329: gridBagConstraints.gridx = 0;
330: gridBagConstraints.gridy = 3;
331: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
332: buttonsPanel.add(moveDownButton, gridBagConstraints);
333: moveDownButton.getAccessibleContext().setAccessibleDescription(
334: bundle.getString("ACSD_ChangeParsMoveDown")); // NOI18N
335:
336: gridBagConstraints = new java.awt.GridBagConstraints();
337: gridBagConstraints.gridx = 0;
338: gridBagConstraints.gridy = 0;
339: eastPanel.add(buttonsPanel, gridBagConstraints);
340: gridBagConstraints = new java.awt.GridBagConstraints();
341: gridBagConstraints.gridx = 0;
342: gridBagConstraints.gridy = 1;
343: gridBagConstraints.weightx = 1.0;
344: gridBagConstraints.weighty = 1.0;
345: eastPanel.add(fillPanel, gridBagConstraints);
346:
347: gridBagConstraints = new java.awt.GridBagConstraints();
348: gridBagConstraints.gridx = 1;
349: gridBagConstraints.gridy = 1;
350: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
351: add(eastPanel, gridBagConstraints);
352:
353: westPanel.setPreferredSize(new java.awt.Dimension(453, 100));
354:
355: paramTable.setModel(model);
356: initRenderer();
357: paramTable.getSelectionModel().addListSelectionListener(
358: getListener1());
359: paramTable.getSelectionModel().setSelectionMode(
360: ListSelectionModel.SINGLE_INTERVAL_SELECTION);
361: model.addTableModelListener(getListener2());
362: paramTable.getInputMap().put(
363: KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0),
364: ACTION_INLINE_EDITOR); //NOI18N
365: paramTable.getInputMap().put(
366: KeyStroke.getKeyStroke(KeyEvent.VK_F2, 0),
367: ACTION_INLINE_EDITOR); //NOI18N
368: paramTable.getActionMap().put(ACTION_INLINE_EDITOR,
369: getEditAction()); //NOI18N
370: paramTable.setSurrendersFocusOnKeystroke(true);
371: paramTable.setCellSelectionEnabled(false);
372: paramTable.setRowSelectionAllowed(true);
373: paramTable.putClientProperty("JTable.autoStartsEdit",
374: Boolean.FALSE); //NOI18N
375: paramTable.putClientProperty("terminateEditOnFocusLost",
376: Boolean.TRUE); //NOI18N
377: westPanel.setViewportView(paramTable);
378: paramTable.getAccessibleContext().setAccessibleDescription(
379: bundle.getString("ACSD_paramTable")); // NOI18N
380:
381: gridBagConstraints = new java.awt.GridBagConstraints();
382: gridBagConstraints.gridx = 0;
383: gridBagConstraints.gridy = 1;
384: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
385: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
386: gridBagConstraints.weightx = 1.0;
387: gridBagConstraints.weighty = 1.0;
388: add(westPanel, gridBagConstraints);
389:
390: paramTitle
391: .setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
392: paramTitle.setLabelFor(paramTable);
393: org.openide.awt.Mnemonics.setLocalizedText(paramTitle, bundle
394: .getString("LBL_ChangeParsParameters")); // NOI18N
395: gridBagConstraints = new java.awt.GridBagConstraints();
396: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
397: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
398: gridBagConstraints.insets = new java.awt.Insets(0, 0, 2, 0);
399: add(paramTitle, gridBagConstraints);
400:
401: previewChange.setBorder(javax.swing.BorderFactory
402: .createTitledBorder(org.openide.util.NbBundle
403: .getBundle(ChangeParametersPanel.class)
404: .getString("LBL_ChangeParsPreview"))); // NOI18N
405: gridBagConstraints = new java.awt.GridBagConstraints();
406: gridBagConstraints.gridx = 0;
407: gridBagConstraints.gridy = 3;
408: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
409: gridBagConstraints.insets = new java.awt.Insets(11, 0, 0, 0);
410: add(previewChange, gridBagConstraints);
411: }// </editor-fold>//GEN-END:initComponents
412:
413: private void modifiersComboActionPerformed(
414: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_modifiersComboActionPerformed
415: previewChange.setText(genDeclarationString());
416: }//GEN-LAST:event_modifiersComboActionPerformed
417:
418: private void removeButtonActionPerformed(
419: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeButtonActionPerformed
420: acceptEditedValue();
421: int[] selectedRows = paramTable.getSelectedRows();
422: ListSelectionModel selectionModel = paramTable
423: .getSelectionModel();
424: for (int i = 0; i < selectedRows.length; ++i) {
425: boolean b = ((Boolean) ((Vector) model.getDataVector().get(
426: selectedRows[i] - i)).get(4)).booleanValue();
427: if (!b) {
428: String title = getString("LBL_ChangeParsCannotDeleteTitle");
429: String mes = MessageFormat.format(
430: getString("LBL_ChangeParsCannotDelete"),
431: ((Vector) model.getDataVector().get(
432: selectedRows[i] - i)).get(0));
433: int a = new JOptionPane().showConfirmDialog(this , mes,
434: title, JOptionPane.YES_NO_OPTION);
435: if (a == JOptionPane.YES_OPTION) {
436: model.removeRow(selectedRows[i] - i);
437: selectionModel.clearSelection();
438: }
439: } else {
440: model.removeRow(selectedRows[i] - i);
441: selectionModel.clearSelection();
442: }
443: }
444: }//GEN-LAST:event_removeButtonActionPerformed
445:
446: private void moveDownButtonActionPerformed(
447: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_moveDownButtonActionPerformed
448: doMove(1);
449: }//GEN-LAST:event_moveDownButtonActionPerformed
450:
451: private void moveUpButtonActionPerformed(
452: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_moveUpButtonActionPerformed
453: doMove(-1);
454: }//GEN-LAST:event_moveUpButtonActionPerformed
455:
456: private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addButtonActionPerformed
457: acceptEditedValue();
458: int rowCount = model.getRowCount();
459: model.addRow(new Object[] { "par" + rowCount, "Object", "null",
460: new Integer(-1), Boolean.TRUE }); // NOI18N
461: }//GEN-LAST:event_addButtonActionPerformed
462:
463: // Variables declaration - do not modify//GEN-BEGIN:variables
464: private javax.swing.JButton addButton;
465: private javax.swing.JPanel buttonsPanel;
466: private javax.swing.JPanel eastPanel;
467: private javax.swing.JPanel fillPanel;
468: private javax.swing.JComboBox modifiersCombo;
469: private javax.swing.JLabel modifiersLabel;
470: private javax.swing.JPanel modifiersPanel;
471: private javax.swing.JButton moveDownButton;
472: private javax.swing.JButton moveUpButton;
473: private javax.swing.JTable paramTable;
474: private javax.swing.JLabel paramTitle;
475: private javax.swing.JLabel previewChange;
476: private javax.swing.JButton removeButton;
477: private javax.swing.JScrollPane westPanel;
478:
479: // End of variables declaration//GEN-END:variables
480:
481: private ListSelectionListener getListener1() {
482: return new ListSelectionListener() {
483: public void valueChanged(ListSelectionEvent e) {
484: if (e.getValueIsAdjusting())
485: return;
486:
487: ListSelectionModel lsm = (ListSelectionModel) e
488: .getSource();
489:
490: if (!lsm.isSelectionEmpty()) {
491: // Find out which indexes are selected.
492: int minIndex = lsm.getMinSelectionIndex();
493: int maxIndex = lsm.getMaxSelectionIndex();
494: setButtons(minIndex, maxIndex);
495:
496: boolean enableRemoveBtn = true;
497: for (int i = minIndex; i <= maxIndex; i++) {
498: enableRemoveBtn = model.isRemovable(i);
499: if (!enableRemoveBtn)
500: break;
501: }
502: removeButton.setEnabled(enableRemoveBtn);
503: } else {
504: moveDownButton.setEnabled(false);
505: moveUpButton.setEnabled(false);
506: removeButton.setEnabled(false);
507: }
508: }
509: };
510: }
511:
512: private TableModelListener getListener2() {
513: return new TableModelListener() {
514: public void tableChanged(TableModelEvent e) {
515: // update buttons availability
516: int[] selectedRows = paramTable.getSelectedRows();
517: if (selectedRows.length == 0) {
518: removeButton.setEnabled(false);
519: } else {
520: boolean enableRemoveBtn = true;
521: for (int i = 0; i < selectedRows.length; i++) {
522: if (selectedRows[i] < model.getRowCount()) {
523: enableRemoveBtn = model.isCellEditable(
524: selectedRows[i], 0);
525: if (!enableRemoveBtn)
526: break;
527: }
528: }
529: removeButton.setEnabled(enableRemoveBtn);
530: int min = selectedRows[0];
531: int max = selectedRows[selectedRows.length - 1];
532: setButtons(min, max);
533: }
534:
535: // update preview
536: previewChange.setText(genDeclarationString());
537:
538: parent.stateChanged(null);
539: }
540: };
541: }
542:
543: private void initTableData(CompilationController info) {
544: ExecutableElement method = (ExecutableElement) refactoredObj
545: .resolveElement(info);
546:
547: List<? extends VariableElement> pars = method.getParameters();
548:
549: // List typeList = new ArrayList();
550: // for (Iterator parIt = pars.iterator(); parIt.hasNext(); ) {
551: // Parameter par = (Parameter) parIt.next();
552: // typeList.add(par.getType());
553: // }
554:
555: Collection<ExecutableElement> allMethods = new ArrayList();
556: allMethods.addAll(RetoucheUtils.getOverridenMethods(method,
557: info));
558: allMethods.addAll(RetoucheUtils.getOverridingMethods(method,
559: info));
560: allMethods.add(method);
561:
562: for (ExecutableElement currentMethod : allMethods) {
563: int originalIndex = 0;
564: for (VariableElement par : currentMethod.getParameters()) {
565: TypeMirror desc = par.asType();
566: String typeRepresentation;
567: if (method.isVarArgs()
568: && originalIndex == pars.size() - 1) {
569: typeRepresentation = getTypeStringRepresentation(((ArrayType) desc)
570: .getComponentType())
571: + " ..."; // NOI18N
572: } else {
573: typeRepresentation = getTypeStringRepresentation(desc);
574: }
575: LocalVarScanner scan = new LocalVarScanner(info, null);
576: scan.scan(info.getTrees().getPath(method), par);
577: Boolean removable = !scan.hasRefernces();
578: if (model.getRowCount() <= originalIndex) {
579: Object[] parRep = new Object[] { par.toString(),
580: typeRepresentation, "",
581: new Integer(originalIndex), removable };
582: model.addRow(parRep);
583: } else {
584: removable = Boolean.valueOf(model
585: .isRemovable(originalIndex)
586: && removable.booleanValue());
587: ((Vector) model.getDataVector().get(originalIndex))
588: .set(4, removable);
589: }
590: originalIndex++;
591: }
592: }
593: }
594:
595: private static String getTypeStringRepresentation(TypeMirror desc) {
596: return desc.toString();
597: }
598:
599: private boolean acceptEditedValue() {
600: TableCellEditor tce = paramTable.getCellEditor();
601: if (tce != null)
602: return paramTable.getCellEditor().stopCellEditing();
603: return false;
604: }
605:
606: private void doMove(int step) {
607: acceptEditedValue();
608:
609: ListSelectionModel selectionModel = paramTable
610: .getSelectionModel();
611: int min = selectionModel.getMinSelectionIndex();
612: int max = selectionModel.getMaxSelectionIndex();
613:
614: selectionModel.clearSelection();
615: model.moveRow(min, max, min + step);
616: selectionModel.addSelectionInterval(min + step, max + step);
617: }
618:
619: private void setButtons(int min, int max) {
620: int r = model.getRowCount() - 1;
621: moveUpButton.setEnabled(min > 0 ? true : false);
622: moveDownButton.setEnabled(max < r ? true : false);
623: }
624:
625: private void initRenderer() {
626: TableColumnModel tcm = paramTable.getColumnModel();
627: paramTable.removeColumn(tcm.getColumn(3));
628: paramTable.removeColumn(tcm.getColumn(3));
629: Enumeration columns = paramTable.getColumnModel().getColumns();
630: TableColumn tc = null;
631: while (columns.hasMoreElements()) {
632: tc = (TableColumn) columns.nextElement();
633: tc.setCellRenderer(new ParamRenderer());
634: }
635: }
636:
637: private Set<Modifier> modifiers = new HashSet();
638:
639: private void setModifier(Set<Modifier> mods) {
640: this .modifiers.clear();
641: this .modifiers.addAll(mods);
642: if (mods.contains(Modifier.PRIVATE))
643: modifiersCombo.setSelectedIndex(MOD_PRIVATE_INDEX);
644: else if (mods.contains(Modifier.PROTECTED))
645: modifiersCombo.setSelectedIndex(MOD_PROTECTED_INDEX);
646: else if (mods.contains(Modifier.PUBLIC))
647: modifiersCombo.setSelectedIndex(MOD_PUBLIC_INDEX);
648: else
649: modifiersCombo.setSelectedIndex(MOD_DEFAULT_INDEX);
650: }
651:
652: public String genDeclarationString() {
653: // generate preview for modifiers
654: // access modifiers
655: String mod = modifiersCombo.getSelectedIndex() != MOD_DEFAULT_INDEX /*default modifier?*/? (String) modifiersCombo
656: .getSelectedItem() + ' '
657: : ""; // NOI18N
658:
659: StringBuffer buf = new StringBuffer(mod);
660: // other than access modifiers - using data provided by the element
661: // first of all, reset access modifier, because it is generated from combo value
662: // String otherMod = Modifier.toString(((CallableFeature) refactoredObj).getModifiers() & 0xFFFFFFF8);
663: // if (otherMod.length() != 0) {
664: // buf.append(otherMod);
665: // buf.append(' ');
666: // }
667: // generate the return type for the method and name
668: // for the both - method and constructor
669: String name;
670: if (RetoucheUtils.getElementKind(refactoredObj) == ElementKind.METHOD) {
671: buf.append(returnType);
672: buf.append(' ');
673: name = RetoucheUtils.getSimpleName(refactoredObj);
674: } else {
675: // for constructor, get name from the declaring class
676: name = RetoucheUtils.getSimpleName(refactoredObj);
677: }
678: buf.append(name);
679: buf.append('(');
680: // generate parameters to the preview string
681: List[] parameters = (List[]) model.getDataVector().toArray(
682: new List[0]);
683: if (parameters.length > 0) {
684: int i;
685: for (i = 0; i < parameters.length - 1; i++) {
686: buf.append((String) parameters[i].get(1));
687: buf.append(' ');
688: buf.append((String) parameters[i].get(0));
689: buf.append(',').append(' ');
690: }
691: buf.append((String) parameters[i].get(1));
692: buf.append(' ');
693: buf.append((String) parameters[i].get(0));
694: }
695: buf.append(')'); //NOI18N
696:
697: return buf.toString();
698: }
699:
700: private static String getString(String key) {
701: return NbBundle.getMessage(ChangeParametersPanel.class, key);
702: }
703:
704: private static Action getEditAction() {
705: if (editAction == null) {
706: editAction = new EditAction();
707: }
708: return editAction;
709: }
710:
711: private static void autoEdit(JTable tab) {
712: if (tab.editCellAt(tab.getSelectedRow(), tab
713: .getSelectedColumn(), null)
714: && tab.getEditorComponent() != null) {
715: JTextField field = (JTextField) tab.getEditorComponent();
716: int len = field.getText().length();
717: field.setCaretPosition(field.getText().length());
718: field.requestFocusInWindow();
719: field.selectAll();
720: }
721: }
722:
723: ////////////////////////////////////////////////////////////////////////////
724: // INNER CLASSES
725: ////////////////////////////////////////////////////////////////////////////
726: // this class is used for marking rows as read-only. If the user uses
727: // standard DefaultTableModel, rows added through its methods is added
728: // as a read-write. -- Use methods with Boolean paramater to add
729: // rows marked as read-only.
730: static class ParamTableModel extends DefaultTableModel {
731:
732: public ParamTableModel(Object[] data, int rowCount) {
733: super (data, rowCount);
734: }
735:
736: public boolean isCellEditable(int row, int column) {
737: if (column > 2) {
738: // check box indicating usage of parameter is not editable
739: return false;
740: }
741: // otherwise, check that user can change only the values provided
742: // for the new parameter. (name change of old parameters aren't
743: // allowed.
744: Integer origIdx = (Integer) ((Vector) getDataVector().get(
745: row)).get(3);
746: return origIdx.intValue() == -1 ? true : false;
747: }
748:
749: public boolean isRemovable(int row) {
750: return true;//((Boolean) ((Vector) getDataVector().get(row)).get(4)).booleanValue();
751: }
752:
753: public Class getColumnClass(int c) {
754: return getValueAt(0, c).getClass();
755: }
756: } // end ParamTableModel
757:
758: private static class EditAction extends AbstractAction {
759: public void actionPerformed(ActionEvent ae) {
760: autoEdit((JTable) ae.getSource());
761: }
762: }
763:
764: class ParamRenderer extends DefaultTableCellRenderer implements
765: TableCellRenderer {
766: Color origBackground;
767:
768: public ParamRenderer() {
769: setOpaque(true);
770: origBackground = getBackground();
771: }
772:
773: public Component getTableCellRendererComponent(JTable table,
774: Object value, boolean isSelected, boolean hasFocus,
775: int row, int column) {
776: super .getTableCellRendererComponent(table, value,
777: isSelected, hasFocus, row, column);
778: boolean isRemovable = model.isRemovable(row);
779: if (!isSelected) {
780: if (!isRemovable) {
781: setBackground(UIManager
782: .getColor("Panel.background")); // NOI18N
783: } else {
784: setBackground(origBackground);
785: }
786: }
787: return this ;
788: }
789:
790: }
791:
792: // end INNERCLASSES
793: }
|