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-2003 Sun
 * Microsystems, Inc. All Rights Reserved.
 */
package org.netbeans.modules.javacore.jmiimpl.javamodel;

import java.util.*;
import org.netbeans.jmi.javamodel.Element;
import org.netbeans.jmi.javamodel.LabeledStatement;
import org.netbeans.jmi.javamodel.Statement;
import org.netbeans.lib.java.parser.ASTree;
import org.netbeans.mdr.storagemodel.StorableObject;
import org.netbeans.modules.javacore.parser.ASTProvider;
import org.netbeans.modules.javacore.parser.ASTUtil;

/**
 *
 * @author  mm109185
 */
public abstract class LabeledStatementImpl extends StatementImpl implements LabeledStatement {
    private String label = null;
    private Statement body = null;
    
    /** Creates a new instance of LabeledStatementImpl */
    public LabeledStatementImpl(StorableObject o) {
        super(o);
    }
    
    public void setLabel(String label) {
        objectChanged(CHANGED_LABEL);
        this.label = label;
    }
    
    public String getLabel() {
        if (isChanged(CHANGED_LABEL)) {
            return label;
        } else {
            return ASTUtil.getIdentifier(getASTree().getSubTrees()[0]);
        }
    }
    
    public void setBody(Statement body) {
        objectChanged(CHANGED_BODY);
        changeChild(getBody(), body);
        this.body = body;
    }

    public List getChildren() {
        List list = new ArrayList(1);
        addIfNotNull(list, getBody()); 
        return list;
    }
    
    protected void initChildren() {
        childrenInited = false;
        ASTree tree = getASTree();
        if (tree != null) {
            body = (Statement) initOrCreate(body, getASTree().getSubTrees()[1]);
        }
        childrenInited = true;
    }
    
    public Statement getBody() {
        if (!childrenInited) {
            initChildren();
        }
        return body;
    }
    
    public String getSourceText() {
        String origElem;
        if ((origElem = checkChange()) != null)
            return origElem;
        StringBuffer buf = new StringBuffer();
        StatementImpl body = (StatementImpl)getBody();
        String label = getLabel();
        buf.append('\n');
        buf.append(getIndentation());
        buf.append(label);
        buf.append(": "); // NOI18N
        buf.append(body.getSourceText());
        return buf.toString();
    }
        
    public void getDiff(List diff) {
        ASTProvider parser = getParser();
        ASTree tree = getASTree();
        ASTree[] children = tree.getSubTrees();
        
        if (isChanged(CHANGED_LABEL)) {
            replaceNode(diff, parser, children[0], getLabel(), 0, null);
        }
        getChildDiff(diff, parser, children[1], (MetadataElement) getBody(), CHANGED_BODY);
    }
    
    void setData(String label, Statement body) {
        this.label = label;
        changeChild(null, body);
        this.body = body;
    }

    protected void _delete() {
        // --- delete components -------------------------------------------
        if (childrenInited) {
            deleteChild(body);
        }
        // --- delete links -----------------------------------------------
        // no links to delete
        // --- call super ---------------------------------------
        super._delete();
    }
    
    public void replaceChild(Element oldElement,Element newElement) {
        if (childrenInited) {
            if (oldElement.equals(body)) {
                setBody((Statement)newElement);
            }
        }
    }
}
... 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.