KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > mdrxml > looks > actions > DeleteAction


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is Forte for Java, Community Edition. The Initial
16  * Developer of the Original Software is Sun Microsystems, Inc. Portions
17  * Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.mdrxml.looks.actions;
21
22 import java.util.Collection JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import javax.jmi.reflect.*;
25 import org.openide.nodes.*;
26 import org.openide.util.actions.NodeAction;
27 import org.openide.util.NbBundle;
28 import org.openide.util.HelpCtx;
29 import org.netbeans.api.looks.*;
30 import org.netbeans.api.mdr.MDRObject;
31 import org.netbeans.api.mdr.MDRepository;
32 /**
33  *
34  * @author Tomas Zezula
35  */

36 public class DeleteAction extends NodeAction {
37
38     /** Creates a new instance of DeleteAction */
39     public DeleteAction() {
40     }
41     
42     public void performAction(Node[] nodes) {
43         for (int i=0; i< nodes.length; i++) {
44             LookNode ln = (LookNode) nodes[i];
45             xmlmodel.Node node = (xmlmodel.Node) ln.getRepresentedObject();
46             MDRepository repo = ((MDRObject)node).repository();
47             repo.beginTrans(true);
48             try {
49                 if (node instanceof xmlmodel.ElementNode)
50                     this.deleteHierarchy ((xmlmodel.ElementNode)node);
51                 ((RefObject)node).refDelete();
52             }catch (Exception JavaDoc e) {
53                 e.printStackTrace();
54             }
55             finally {
56                 repo.endTrans();
57             }
58         }
59     }
60     
61     public boolean enable(Node[] nodes) {
62         if (nodes == null)
63             return false;
64         for (int i=0; i< nodes.length; i++) {
65             if (!(nodes[i] instanceof LookNode))
66                 return false;
67             if (!(((LookNode)nodes[i]).getRepresentedObject() instanceof xmlmodel.Node))
68                 return false;
69         }
70         return true;
71     }
72     
73     public String JavaDoc getName() {
74         return NbBundle.getMessage(DeleteAction.class, "TXT_Delete");
75     }
76     
77     public HelpCtx getHelpCtx() {
78         return HelpCtx.DEFAULT_HELP;
79     }
80     
81     
82     protected void deleteHierarchy(xmlmodel.ElementNode node) {
83         Collection JavaDoc subNodes = node.getNodes();
84         for (Iterator JavaDoc it = subNodes.iterator(); it.hasNext();) {
85             xmlmodel.Node subNode = (xmlmodel.Node)it.next();
86         it.remove ();
87             if (subNode instanceof xmlmodel.ElementNode)
88                 deleteHierarchy ((xmlmodel.ElementNode)subNode);
89         if (subNode != null)
90         try {
91              ((RefObject)subNode).refDelete();
92         } catch (javax.jmi.reflect.InvalidObjectException invalidObjectExc) {
93             // Already deleted object
94
}
95         }
96     }
97 }
98
Popular Tags