KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > mdrxml > looks > ElementNodeLook


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;
21
22 import java.io.ByteArrayOutputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.awt.datatransfer.Transferable JavaDoc;
25 import java.awt.datatransfer.StringSelection JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.Collection JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import javax.jmi.reflect.RefBaseObject;
30 import org.openide.util.NbBundle;
31 import org.openide.util.actions.SystemAction;
32 import org.netbeans.api.mdr.*;
33 import org.netbeans.api.looks.*;
34 import org.netbeans.spi.looks.*;
35 import xmlmodel.ElementNode;
36 import xmlmodel.Node;
37 import xmlmodel.AttributeNode;
38 import org.netbeans.modules.mdrxml.looks.actions.GenerateXMLAction;
39 import org.netbeans.modules.mdrxml.looks.actions.NewAction;
40 import org.netbeans.modules.mdrxml.util.XMLModelEventTranslator;
41 import org.netbeans.modules.mdrxml.util.NodeEventTranslator;
42 import org.netbeans.modules.mdrxml.util.XMLGenerator;
43 /**
44  *
45  * @author Tomas Zezula
46  */

47 public class ElementNodeLook extends NodeLook {
48     
49     private static final String JavaDoc ICON = "org/netbeans/modules/mdrxml/resources/elementNode";
50     
51     /** Creates a new instance of ElementNode */
52     public ElementNodeLook() {
53         this(NbBundle.getMessage(ElementNodeLook.class, "TXT_ElementNodeLook"));
54     }
55     
56     public Object JavaDoc attachTo(Look.NodeSubstitute substitute) {
57         super.attachTo(substitute);
58         NodeEventTranslator nt = null;
59         try {
60             RefBaseObject mdrInstance = (javax.jmi.reflect.RefObject)substitute.getRepresentedObject();
61             XMLModelEventTranslator translator = XMLModelEventTranslator.getTranslator((xmlmodel.XmlmodelPackage)mdrInstance.refImmediatePackage());
62             nt = new NodeEventTranslator(translator,mdrInstance,substitute);
63         }catch (javax.jmi.reflect.InvalidObjectException ioe) {
64         }
65         return nt;
66     }
67     
68     public ElementNodeLook(String JavaDoc name) {
69         super(name);
70     }
71     
72     public String JavaDoc iconBase(Look.NodeSubstitute substitute) {
73         return ICON;
74     }
75     
76     public javax.swing.Action JavaDoc[] getActions(Look.NodeSubstitute substitute) {
77         javax.swing.Action JavaDoc[] parentActions = super.getActions(substitute);
78         javax.swing.Action JavaDoc[] result = new javax.swing.Action JavaDoc [parentActions.length + 2];
79         result[0] = SystemAction.get(GenerateXMLAction.class);
80         result[1] = SystemAction.get(NewAction.class);
81         System.arraycopy(parentActions,0,result,2,parentActions.length);
82         return result;
83     }
84     
85     public Object JavaDoc[] getChildObjects(Look.NodeSubstitute substitute) {
86         try {
87             ElementNode en = (ElementNode) substitute.getRepresentedObject();
88             MDRepository repo = ((MDRObject)en).repository();
89             repo.beginTrans(false);
90             try {
91                 Collection JavaDoc nodes = en.getNodes();
92                 Object JavaDoc[] result = new Object JavaDoc [nodes.size()];
93                 Iterator JavaDoc it = nodes.iterator();
94                 for (int i=0; it.hasNext(); i++) {
95                     result[i] = it.next();
96                 }
97                 return result;
98             }finally {
99                 repo.endTrans();
100             }
101         }catch (javax.jmi.reflect.InvalidObjectException ioe) {
102             return null;
103         }
104     }
105     
106     public Transferable JavaDoc clipboardCopy (Look.NodeSubstitute substitute) throws IOException JavaDoc {
107         ByteArrayOutputStream JavaDoc bos = new ByteArrayOutputStream JavaDoc ();
108         new XMLGenerator ().generateXML (bos, (ElementNode)substitute.getRepresentedObject());
109         return new StringSelection JavaDoc (bos.toString());
110     }
111     
112 }
113
Popular Tags