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.javacore.jmiimpl.javamodel;

import org.netbeans.jmi.javamodel.Element;
import org.netbeans.jmi.javamodel.Expression;
import org.netbeans.jmi.javamodel.SwitchStatement;
import org.netbeans.lib.java.parser.ASTree;
import org.netbeans.lib.java.parser.ASTreeTypes;
import org.netbeans.mdr.storagemodel.StorableObject;
import org.netbeans.modules.javacore.parser.ASTProvider;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

/**
 *
 * @author  Martin Matula
 */
public abstract class SwitchStatementImpl extends ConditionImpl implements SwitchStatement {
    private LightAttrList cases = null;
    
    /** Creates a new instance of SwitchStatementImpl */
    public SwitchStatementImpl(StorableObject o) {
        super(o);
    }
    
    public List getCases() {
        if (!childrenInited) {
            initChildren();
        }
        return cases;
    }
    
    public List getChildren() {
        List list = super.getChildren();
        list.addAll(getCases()); 
        return list;
    }
    
    protected void initChildren() {
        childrenInited = false;
        ASTree tree = getASTree();
        if (tree != null) {
            ASTree[] parts = tree.getSubTrees();
            expression = (Expression) initOrCreate(expression, parts[0]);
            ASTree[] block = parts[1].getSubTrees();
            ArrayList children = new ArrayList();
            if (block[0] != null) {
                ASTree[] groups = block[0].getSubTrees();
                for (int i = 0; i < groups.length; i++) {
                    ASTree[] cases = groups[i].getSubTrees();
                    if (cases[0].getType() == ASTreeTypes.SWITCH_LABELS) {
                        ASTree[] labels = cases[0].getSubTrees();
                        for (int j = 0; j < labels.length - 1; j++) {
                            children.add(labels[j]);
                        }
                    }
                    children.add(groups[i]);
                }
            }
            if (block[1] != null) {
                if (block[1].getType() == ASTreeTypes.SWITCH_LABELS) {
                    ASTree[] labels = block[1].getSubTrees();
                    for (int j = 0; j < labels.length - 1; j++) {
                        children.add(labels[j]);
                    }
                }
                children.add(block[1]);
            }
            cases = createChildrenList(cases, "cases", (ASTree[]) children.toArray(new ASTree[children.size()]), CHANGED_CASES, false); // NOI18N
        }
        childrenInited = true;
    }
    
    public String getSourceText() {
        String origElem;
        if ((origElem = checkChange()) != null)
            return origElem;
        StringBuffer buf = new StringBuffer();
        List cases = getCases();
        TransientElement expr = (TransientElement) getExpression();
        
        buf.append('\n');
        formatElementPart(SWITCH_KEYWORD, buf);
        formatElementPart(STMT_OPEN_BRACKET, buf);
        buf.append(expr.getSourceText());
        formatElementPart(STMT_CLOSE_BRACKET, buf);
        formatElementPart(BLOCK_OPEN_CURLY, buf);
        for (Iterator iter = cases.iterator(); iter.hasNext();) {
            CaseImpl casik = (CaseImpl) iter.next();
            buf.append(casik.getSourceText());
        }
        formatElementPart(BLOCK_CLOSE_CURLY, buf);
        return buf.toString();
    }

    public void getDiff(List diff) {
        if (isChanged()) {
            ASTProvider parser = getParser();
            ASTree tree = getASTree();
            
            if (isChanged(CHANGED_CASES)) {
                diff.add(new DiffElement(parser.getToken(tree.getFirstToken()).getStartOffset(), parser.getToken(tree.getLastToken()).getEndOffset(), getSourceText()));
            } else {
                getChildDiff(diff, parser, tree.getSubTrees()[0], (MetadataElement) getExpression(), CHANGED_EXPRESSION);
                if (isChanged(CHANGED_CHILDREN)) {
                    for (Iterator it = getCases().iterator(); it.hasNext();) {
                        CaseImpl c = (CaseImpl) it.next();
                        if (c.isChanged()) {
                            c.getDiff(diff);
                        }
                    }
                }
            }
        }
    }
    
    void setData(Expression expression, List cases) {
        // expression
        changeChild(null, expression);
        this.expression = expression;
        // cases
        this.cases = createChildrenList("cases", cases, CHANGED_CASES); // NOI18N
    }

    protected void _delete() {
        // --- delete components -------------------------------------------
        if (childrenInited) {
            deleteChildren(cases);
        }
        // --- delete links -----------------------------------------------
        // no links to delete
        // --- call super ---------------------------------------
        super._delete();
    }
    
    public void replaceChild(Element oldElement,Element newElement) {
        if (childrenInited) {
            if (replaceObject(getCases(),oldElement,newElement))
                return;
            super.replaceChild(oldElement,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.