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;

import java.lang.reflect.Modifier;
import java.text.MessageFormat;
import javax.jmi.reflect.RefObject;
import org.netbeans.modules.refactoring.api.RefactoringElement;
import org.netbeans.jmi.javamodel.*;
import org.netbeans.modules.javacore.internalapi.JavaMetamodel;
import org.openide.text.PositionBounds;
import org.openide.util.NbBundle;

/**
 * Changes the modifier of element.
 * For example, it is used by EncapsulateField refactoring - 
 * field is changed to private as default, but user can
 * use another value. You can use it also for other features.
 *
 * @author  Pavel Flaska
 */
public class ChangeModElement implements RefactoringElement {
    
    private int modifier;
    private Feature element;
    
    private PositionBounds bounds;
    boolean enabled;
    private final String text;
    private final String displayText;
    private int status;
    
    /** 
     * Creates a new instance of Change field access modifier element.
     *
     * @param element   access modifier will be changed on this parameter
     * @param modifier  new modifier for the element
     */
    public ChangeModElement(Feature element, int modifier) {
        this.element = element;
        this.modifier = modifier;
        bounds = null;
        enabled = true;
        String elementType;
        if (element instanceof Field) {
            elementType = "field"; // NOI18N
        } else if (element instanceof Constructor) {
            elementType = "constructor"; // NOI18N
        } else if (element instanceof Method) {
            elementType = "method"; // NOI18N
        } else {
            elementType = "class"; //NOI18N
        }
        Object[] o = new Object[] { 
                elementType, 
                element.getName(), 
                Modifier.toString(modifier) 
        };
        text = new MessageFormat(
            NbBundle.getMessage(ChangeModElement.class, "DSC_ChangeModifier")).format(o);
        displayText = new MessageFormat(
            NbBundle.getMessage(ChangeModElement.class, "DSC_ChangeModifier_html")).format(o);
        if (JavaMetamodel.getManager().isElementGuarded(element)) {
            status = RefactoringElement.GUARDED;
        } else {
            status = RefactoringElement.NORMAL;
        }
    }
    
    /**
     * Returns refactored element.
     *
     * @return  refactored element
     */        
    public Element getJavaElement() { return element; }
    
    /**
     * Returns bounds of the field declaration,
     * e.g. int a = 5
     *
     * @return bounds of the field declaration
     */        
    public PositionBounds getPosition() {
        if (bounds == null) {
            bounds = NbAbstractRefactoring.getPositionBounds(element);
        }
        return bounds;
    }
    
    /**
     * Returns the text describing the change provided by element.
     *
     * @return  the text describing element functionality
     */        
    public String getText() { return text; }

    /**
     * Returns text containing the description of the element
     * (i.e. 'Change declaration' and the current declaration of the method.
     *
     * @return description text with method declaration change text
     */
    public String getDisplayText() { return displayText; }
    
    /**
     * If the refactoring of this element is enabled, it returns true.
     *
     * @return true, if the refactoring of this element is enabled
     */        
    public boolean isEnabled() { return enabled; }

    /**
     * Sets the enabled flag. It allows to stop refactoring on the
     * element, when you call setEnabled(false);
     *
     * @param enabled tell if the element can be refactored
     */
    public void setEnabled(boolean enabled) { this.enabled = enabled; }
    
    /** 
     * Performs the change represented by this refactoring element.
     */
    public void performChange() {
        if (isEnabled()) {
            element.setModifiers(modifier);
        }
    }
    
    public int getStatus() {
        return status;
    }
    
    /** 
     * Undoes the change represented by this refactoring element.
     */
    public void undoChange() { throw new UnsupportedOperationException(); }
        
}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 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.