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 Forte for Java, Community Edition. The Initial
 * Developer of the Original Code is Sun Microsystems, Inc. Portions
 * Copyright 1997-2002 Sun Microsystems, Inc. All Rights Reserved.
 */

package org.netbeans.modules.mdrxml.looks.actions;

import java.util.Collection;
import java.util.Iterator;
import javax.jmi.reflect.*;
import org.openide.nodes.*;
import org.openide.util.actions.NodeAction;
import org.openide.util.NbBundle;
import org.openide.util.HelpCtx;
import org.netbeans.api.looks.*;
import org.netbeans.api.mdr.MDRObject;
import org.netbeans.api.mdr.MDRepository;
/**
 *
 * @author  Tomas Zezula
 */
public class DeleteAction extends NodeAction {
    
    /** Creates a new instance of DeleteAction */
    public DeleteAction() {
    }
    
    public void performAction(Node[] nodes) {
        for (int i=0; i< nodes.length; i++) {
            LookNode ln = (LookNode) nodes[i];
            xmlmodel.Node node = (xmlmodel.Node) ln.getRepresentedObject();
            MDRepository repo = ((MDRObject)node).repository();
            repo.beginTrans(true);
            try {
                if (node instanceof xmlmodel.ElementNode)
                    this.deleteHierarchy ((xmlmodel.ElementNode)node);
                ((RefObject)node).refDelete();
            }catch (Exception e) {
                e.printStackTrace();
            }
            finally {
                repo.endTrans();
            }
        }
    }
    
    public boolean enable(Node[] nodes) {
        if (nodes == null)
            return false;
        for (int i=0; i< nodes.length; i++) {
            if (!(nodes[i] instanceof LookNode))
                return false;
            if (!(((LookNode)nodes[i]).getRepresentedObject() instanceof xmlmodel.Node))
                return false;
        }
        return true;
    }
    
    public String getName() {
        return NbBundle.getMessage(DeleteAction.class, "TXT_Delete");
    }
    
    public HelpCtx getHelpCtx() {
        return HelpCtx.DEFAULT_HELP;
    }
    
    
    protected void deleteHierarchy(xmlmodel.ElementNode node) {
        Collection subNodes = node.getNodes();
        for (Iterator it = subNodes.iterator(); it.hasNext();) {
            xmlmodel.Node subNode = (xmlmodel.Node)it.next();
	    it.remove ();
            if (subNode instanceof xmlmodel.ElementNode)
                deleteHierarchy ((xmlmodel.ElementNode)subNode);
	    if (subNode != null)
		try {
        	 ((RefObject)subNode).refDelete();
		} catch (javax.jmi.reflect.InvalidObjectException invalidObjectExc) {
		    // Already deleted object
		}
        }
    }
}
... 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.