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.text.MessageFormat;
044: import java.util.Iterator;
045: import java.util.List;
046: import java.util.Set;
047: import javax.lang.model.element.ElementKind;
048: import javax.lang.model.element.Modifier;
049: import javax.swing.event.ChangeListener;
050: import org.netbeans.api.java.source.CompilationInfo;
051: import org.netbeans.api.java.source.TreePathHandle;
052: import org.netbeans.api.java.source.TypeMirrorHandle;
053: import org.netbeans.modules.refactoring.api.AbstractRefactoring;
054: import org.netbeans.modules.refactoring.api.Problem;
055: import org.netbeans.modules.refactoring.java.RetoucheUtils;
056: import org.netbeans.modules.refactoring.java.api.ChangeParametersRefactoring;
057: import org.netbeans.modules.refactoring.spi.ui.CustomRefactoringPanel;
058: import org.netbeans.modules.refactoring.spi.ui.RefactoringUI;
059: import org.openide.util.HelpCtx;
060: import org.openide.util.NbBundle;
061:
062: /**
063: *
064: * @author Pavel Flaska, Jan Becicka
065: */
066: public class ChangeParametersUI implements RefactoringUI {
067:
068: TreePathHandle refactoredObj;
069: ChangeParametersPanel panel;
070: ChangeParametersRefactoring refactoring;
071:
072: /** Creates a new instance of ChangeMethodSignatureRefactoring */
073: public ChangeParametersUI(TreePathHandle refactoredObj,
074: CompilationInfo info) {
075: this .refactoring = new ChangeParametersRefactoring(
076: refactoredObj);
077: this .refactoredObj = refactoredObj;
078: }
079:
080: public String getDescription() {
081: String msg = NbBundle.getMessage(ChangeParametersUI.class,
082: "DSC_ChangeParsRootNode"); // NOI18N
083: String name = RetoucheUtils.getSimpleName(refactoredObj);
084: boolean isMethod = RetoucheUtils.getElementKind(refactoredObj)
085: .equals(ElementKind.METHOD);
086: return new MessageFormat(msg).format(new Object[] {
087: name,
088: NbBundle.getMessage(ChangeParametersUI.class,
089: "DSC_ChangeParsRootNode"
090: + (isMethod ? "Method" : "Constr")),
091: panel.genDeclarationString() });
092: }
093:
094: public CustomRefactoringPanel getPanel(ChangeListener parent) {
095: if (panel == null) {
096: //TODO:
097: //parent.setPreviewEnabled(true);
098: panel = new ChangeParametersPanel(refactoredObj, parent);
099: }
100: return panel;
101: }
102:
103: public AbstractRefactoring getRefactoring() {
104: return refactoring;
105: }
106:
107: public boolean isQuery() {
108: return false;
109: }
110:
111: private Problem setParameters(boolean checkOnly) {
112: List data = (List) panel.getTableModel().getDataVector();
113: ChangeParametersRefactoring.ParameterInfo[] paramList = new ChangeParametersRefactoring.ParameterInfo[data
114: .size()];
115: int counter = 0;
116: Problem problem = null;
117: for (Iterator rowIt = data.iterator(); rowIt.hasNext(); ++counter) {
118: List row = (List) rowIt.next();
119: int origIndex = ((Integer) row.get(3)).intValue();
120: String name = (String) row.get(0);
121: String type = (String) row.get(1);
122: String defaultVal = (String) row.get(2);
123: paramList[counter] = new ChangeParametersRefactoring.ParameterInfo(
124: origIndex, name, type, defaultVal);
125: }
126: Set<Modifier> modifier = panel.getModifier();
127: refactoring.setParameterInfo(paramList);
128: refactoring.setModifiers(modifier);
129: if (checkOnly) {
130: problem = refactoring.fastCheckParameters();
131: } else {
132: problem = refactoring.checkParameters();
133: }
134: return problem;
135: }
136:
137: public String getName() {
138: return NbBundle.getMessage(ChangeParametersUI.class,
139: "LBL_ChangeMethodSignature");
140: }
141:
142: public Problem checkParameters() {
143: return setParameters(true);
144: }
145:
146: public Problem setParameters() {
147: return setParameters(false);
148: }
149:
150: public boolean hasParameters() {
151: return true;
152: }
153:
154: public HelpCtx getHelpCtx() {
155: return new HelpCtx(ChangeParametersUI.class);
156: }
157: }
|