1 19 20 package org.netbeans.modules.mdrxml.looks; 21 22 import java.io.ByteArrayOutputStream ; 23 import java.io.IOException ; 24 import java.awt.datatransfer.Transferable ; 25 import java.awt.datatransfer.StringSelection ; 26 import java.util.ArrayList ; 27 import java.util.Collection ; 28 import java.util.Iterator ; 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 47 public class ElementNodeLook extends NodeLook { 48 49 private static final String ICON = "org/netbeans/modules/mdrxml/resources/elementNode"; 50 51 52 public ElementNodeLook() { 53 this(NbBundle.getMessage(ElementNodeLook.class, "TXT_ElementNodeLook")); 54 } 55 56 public Object 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 name) { 69 super(name); 70 } 71 72 public String iconBase(Look.NodeSubstitute substitute) { 73 return ICON; 74 } 75 76 public javax.swing.Action [] getActions(Look.NodeSubstitute substitute) { 77 javax.swing.Action [] parentActions = super.getActions(substitute); 78 javax.swing.Action [] result = new javax.swing.Action [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 [] 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 nodes = en.getNodes(); 92 Object [] result = new Object [nodes.size()]; 93 Iterator 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 clipboardCopy (Look.NodeSubstitute substitute) throws IOException { 107 ByteArrayOutputStream bos = new ByteArrayOutputStream (); 108 new XMLGenerator ().generateXML (bos, (ElementNode)substitute.getRepresentedObject()); 109 return new StringSelection (bos.toString()); 110 } 111 112 } 113 | Popular Tags |