alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 *                 Sun Public License Notice
 *
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 *
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2004 Sun
 * Microsystems, Inc. All Rights Reserved.
 */
package org.netbeans.modules.refactoring.api;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.netbeans.jmi.javamodel.Element;
import org.netbeans.jmi.javamodel.EnumConstant;
import org.netbeans.jmi.javamodel.Field;
import org.netbeans.jmi.javamodel.JavaClass;
import org.netbeans.modules.refactoring.NbAbstractRefactoring;
import org.openide.util.NbBundle;
import org.netbeans.modules.javacore.internalapi.ProgressListener;
import org.netbeans.modules.javacore.internalapi.ProgressEvent;

/** Encapsulate fields refactoring. This is a composed refactoring (uses instances of {@link org.netbeans.modules.refactoring.api.EncapsulateFieldRefactoring}
 * to encapsulate several fields at once.
 *
 * @author  Pavel Flaska
 */
public class EncapsulateFields extends NbAbstractRefactoring {
    
    // represents selected fields in explorer
    private Element[] selectedObjects;
    private EncapsulateFieldRefactoring[] refactorings;
    private ProgressListener listener = new ProgressListener() {
        public void start(ProgressEvent event) {
            fireProgressListenerStart(event.getOperationType(),event.getCount());
        }

        public void step(ProgressEvent event) {
            fireProgressListenerStep();
        }

       public void stop(ProgressEvent event) {
            fireProgressListenerStop();
        }
    };

    /** Creates a new instance of EcapsulateFields.
     * @param selectedObjects Array of objects (fields) that should be encapsulated.
     */
    public EncapsulateFields(Element[] selectedObjects) {
        this.selectedObjects = selectedObjects;
    }
    
    public Element[] getFields() {
        return selectedObjects;
    }
    
    public Problem preCheck() {
        fireProgressListenerStart(PRE_CHECK, 2);
        try {
            Problem result = null;

            fireProgressListenerStep();
            for (int i = 0; i < selectedObjects.length; i++) {
                Element jmiObject = selectedObjects[i];
                if (!(jmiObject instanceof Field) || (jmiObject instanceof EnumConstant)) {
                    if (selectedObjects.length == 1 && selectedObjects[0] instanceof JavaClass) {
                        JavaClass encClass = (JavaClass) selectedObjects[0];
                        List fields = new ArrayList();
                        List features = encClass.getFeatures();
                        for (Iterator it = features.iterator(); it.hasNext(); ) {
                            Object o = it.next();
                            if ((o instanceof Field) && !(o instanceof EnumConstant)) {
                                fields.add(o);
                            }
                        }
                        if (fields.size() == 0) { // there are no fields in class
                            String pattern = NbBundle.getMessage(EncapsulateFields.class, "ERR_EncapsulateNoFields");
                            String text = new MessageFormat(pattern).format(new Object[] { encClass.getSimpleName() });
                            result = createProblem(result, true, text);
                        } else {
                            selectedObjects = (Field[]) fields.toArray(new Field[fields.size()]);
                        }
                    } else {
                        result = createProblem(result, true, NbBundle.getMessage(EncapsulateFields.class, "ERR_EncapsulateWrongType"));
                    }
                }
            }
            return result;
        } finally {
            fireProgressListenerStop();
        }
    }
    
    public Problem prepare(java.util.Collection elements) {
        Problem problem = null, lastProblem = null;
        for (int i = 0; i < refactorings.length; i++) {
            if (cancelRequest) {
                return null;
            }
            Problem p = refactorings[i].prepare(elements);
            if (p != null) {
                if (problem == null)
                    problem = lastProblem = p;
                else {
                    lastProblem.setNext(p);
                    lastProblem = p;
                }
            }
        }
        referencesIterator = null;
        return problem;
    }
    
    public void cancelPrepare() {
        super.cancelPrepare();
        if (refactorings != null) {
            // if there were initialized refactorings for some fields,
            // cancel their prepare phase
            for (int i = 0; i < refactorings.length; i++) {
                refactorings[i].cancelPrepare();
            }
        }
    }
    
    public Problem checkParameters(EncapsulateFieldInfo[] refactorFields, int methodModifier, int fieldModifier) {
        return setParameters(true, refactorFields, methodModifier, fieldModifier);
    }
    
    public Problem setParameters(EncapsulateFieldInfo[] refactorFields, int methodModifier, int fieldModifier) {
        return setParameters(false, refactorFields, methodModifier, fieldModifier);
    }
    
    public void setClassPath() {
        setClassPath((Element) selectedObjects[0]);
    }
    
    private Problem setParameters(boolean checkOnly, EncapsulateFieldInfo[] refactorFields, int methodModifier, int fieldModifier) {
        if (!checkOnly) {
            refactorings = new EncapsulateFieldRefactoring[refactorFields.length];
        }
        Problem problem = null, lastProblem = null;
        for (int i = 0; i < refactorFields.length; i++) {
            EncapsulateFieldInfo info = refactorFields[i];
            EncapsulateFieldRefactoring ref;
            
              ref = new EncapsulateFieldRefactoring(info.getField());
            if (!checkOnly) {
                refactorings[i] = ref;
            }
            ref.addProgressListener(listener);
            Problem p;
            if (checkOnly) {
                p = ref.checkParameters(info.getGetterName(), info.getSetterName(), methodModifier, fieldModifier);
            } else {
                p = ref.setParameters(info.getGetterName(), info.getSetterName(), methodModifier, fieldModifier);
            }
            if (p != null) {
                if (problem == null)
                    problem = lastProblem = p;
                else {
                    lastProblem.setNext(p);
                    lastProblem = p;
                }
            }
        }
        return problem;
    }
    
    ////////////////////////////////////////////////////////////////////////////
    // INNER CLASSES
    ////////////////////////////////////////////////////////////////////////////
    /**
     * Represents data from the panel.
     */
    public static class EncapsulateFieldInfo {
        Field field;
        String getterName;
        String setterName;
        
        public EncapsulateFieldInfo(Field field, String getterName, String setterName) {
            this.field = field;
            this.getterName = getterName;
            this.setterName = setterName;
        }
        
        /**
         * Getter for property getterName.
         *
         * @return Value of property getterName.
         */
        public String getGetterName() { return getterName; }
        
        /**
         * Getter for property setterName.
         * 
         * @return Value of property setterName.
         */
        public String getSetterName() { return setterName; }
        
        /**
         * Getter for property field.
         *
         * @return Value of property field.
         */
        public Field getField() { return field; }
        
    }
}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.