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-2007 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 com.sun.source.util.TreePath;
044: import java.text.MessageFormat;
045: import javax.lang.model.element.Element;
046: import javax.lang.model.element.ElementKind;
047: import javax.lang.model.element.NestingKind;
048: import javax.lang.model.element.TypeElement;
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.modules.refactoring.api.AbstractRefactoring;
053: import org.netbeans.modules.refactoring.api.Problem;
054: import org.netbeans.modules.refactoring.java.RetoucheUtils;
055: import org.netbeans.modules.refactoring.spi.ui.CustomRefactoringPanel;
056: import org.netbeans.modules.refactoring.spi.ui.RefactoringUI;
057: import org.openide.util.HelpCtx;
058: import org.openide.util.NbBundle;
059:
060: /**
061: *
062: *
063: * @author Tomas Hurka
064: * @author Pavel Flaska
065: * @author Jan Pokorsky
066: */
067: public final class EncapsulateFieldUI implements RefactoringUI {
068:
069: private EncapsulateFieldPanel panel;
070: private transient EncapsulateFieldsRefactoring refactoring;
071:
072: /** Creates new form RenamePanelName */
073: public EncapsulateFieldUI(TreePathHandle selectedObject,
074: CompilationInfo info) {
075:
076: refactoring = new EncapsulateFieldsRefactoring(
077: resolveSourceType(selectedObject, info));
078: }
079:
080: public boolean isQuery() {
081: return false;
082: }
083:
084: public CustomRefactoringPanel getPanel(ChangeListener parent) {
085: if (panel == null) {
086: panel = new EncapsulateFieldPanel(refactoring
087: .getSelectedObject(), parent);
088: }
089: return panel;
090: }
091:
092: private Problem setParameters(boolean checkOnly) {
093: refactoring.setRefactorFields(panel.getAllFields());
094: refactoring.setMethodModifiers(panel.getMethodModifiers());
095: refactoring.setFieldModifiers(panel.getFieldModifiers());
096: refactoring.setAlwaysUseAccessors(panel.isCheckAccess());
097: if (checkOnly) {
098: return refactoring.fastCheckParameters();
099: } else {
100: return refactoring.checkParameters();
101: }
102: }
103:
104: public AbstractRefactoring getRefactoring() {
105: return refactoring;
106: }
107:
108: public String getDescription() {
109: String name = panel.getClassname();
110: // name = "<anonymous>"; // NOI18N
111: return new MessageFormat(NbBundle.getMessage(
112: EncapsulateFieldUI.class, "DSC_EncapsulateFields"))
113: .format(new Object[] { name });
114: }
115:
116: public String getName() {
117: return NbBundle.getMessage(EncapsulateFieldUI.class,
118: "LBL_EncapsulateFields");
119: }
120:
121: public Problem checkParameters() {
122: return setParameters(true);
123: }
124:
125: public Problem setParameters() {
126: return setParameters(false);
127: }
128:
129: public boolean hasParameters() {
130: return true;
131: }
132:
133: public HelpCtx getHelpCtx() {
134: return new HelpCtx(EncapsulateFieldUI.class);
135: }
136:
137: /**
138: * returns field in case the selectedObject is field or enclosing class
139: * in other cases.
140: */
141: private static TreePathHandle resolveSourceType(
142: TreePathHandle selectedObject, CompilationInfo javac) {
143: TreePath selectedField = selectedObject.resolve(javac);
144: Element elm = javac.getTrees().getElement(selectedField);
145: TypeElement encloser = null;
146: if (elm != null && ElementKind.FIELD == elm.getKind()) {
147: encloser = (TypeElement) elm.getEnclosingElement();
148: if (ElementKind.INTERFACE != encloser.getKind()
149: && NestingKind.ANONYMOUS != encloser
150: .getNestingKind()) {
151: // interface constants, local variables and annonymous declarations are unsupported
152: TreePath tp = javac.getTrees().getPath(elm);
153: return TreePathHandle.create(tp, javac);
154: }
155: }
156:
157: // neither interface, annotation type nor annonymous declaration
158: TreePath tpencloser = RetoucheUtils.findEnclosingClass(javac,
159: selectedField, true, false, true, false, false);
160: return TreePathHandle.create(tpencloser, javac);
161: }
162: }
|