1 4 package org.oddjob.designer.model; 5 6 import java.io.ByteArrayOutputStream ; 7 import java.util.Observable ; 8 9 import org.apache.log4j.Logger; 10 import org.oddjob.arooa.ArooaContext; 11 import org.oddjob.arooa.handlers.MainHandler; 12 import org.oddjob.arooa.handlers.XmlHandler; 13 import org.oddjob.designer.DesignerXMLParser; 14 import org.oddjob.designer.arooa.DesignParser; 15 import org.xml.sax.SAXParseException ; 16 17 20 public class DesignerModel extends Observable { 21 private static final Logger logger = Logger.getLogger(DesignerModel.class); 22 23 24 private final DesignTreeModel treeModel; 25 26 27 private final DesignComponent rootComponent; 28 29 30 private DesignTreeNode currentSelection; 31 32 37 public DesignerModel(DesignComponent start) { 38 this.rootComponent = start; 39 treeModel = new DesignTreeModel(start); 40 treeModel.build(); 41 42 } 43 44 48 public DesignComponent getRootComponent() { 49 return rootComponent; 50 } 51 52 57 public DesignTreeModel getTreeModel() { 58 return treeModel; 59 } 60 61 66 public void setCurrentSelection(DesignTreeNode node) { 67 this.currentSelection = node; 68 logger.debug("Current selection is [" + currentSelection + "]"); 69 setChanged(); 70 notifyObservers(); 71 } 72 73 78 public DesignTreeNode getCurrentSelection() { 79 return currentSelection; 80 } 81 82 public DesignComponent getCurrentComponent() { 83 if (currentSelection == null) { 84 return null; 85 } 86 return currentSelection.getDesignComponent(); 87 } 88 89 public String getCurrentXML() { 90 if (currentSelection == null) { 91 return null; 92 } 93 DesignComponent component = currentSelection.getDesignComponent(); 94 95 ByteArrayOutputStream out = new ByteArrayOutputStream (); 96 XmlHandler handler = new XmlHandler(out); 97 DesignParser dp = new DesignParser(new ArooaContext()); 98 try { 99 dp.parse(component.tag(), component, new MainHandler(handler)); 100 } catch (SAXParseException ex) { 101 ex.printStackTrace(); 102 } 103 return out.toString(); 104 105 } 106 107 public void replaceSelected(String xml) { 108 StructuralDesignComponent parentComponent = (StructuralDesignComponent) 109 ((DesignTreeNode) 110 currentSelection.getParent()).getDesignComponent(); 111 112 DesignComponent dc = DesignerXMLParser.buildComponent(xml); 113 parentComponent.replaceChild(currentSelection.getDesignComponent(), dc); 114 this.treeModel.fireTreeNodesChanged(currentSelection); 115 } 116 117 public void replaceSelected(DesignComponent dc) { 118 StructuralDesignComponent parentComponent = (StructuralDesignComponent) 119 getParentComponent(currentSelection); 120 121 parentComponent.replaceChild(currentSelection.getDesignComponent(), dc); 122 this.treeModel.fireTreeNodesChanged(currentSelection); 123 124 } 125 126 public void delete(DesignTreeNode child) { 127 if (child == null) { 128 throw new NullPointerException ("Child can not be null!"); 129 } 130 StructuralDesignComponent parentComponent = (StructuralDesignComponent) 131 getParentComponent(child); 132 parentComponent.deleteChild(child.getDesignComponent()); 133 } 134 135 public static DesignComponent getParentComponent(DesignTreeNode child) { 136 if (child == null) { 137 return null; 138 } 139 DesignTreeNode parent = (DesignTreeNode) child.getParent(); 140 if (parent == null) { 141 return null; 142 } 143 return parent.getDesignComponent(); 144 145 } 146 147 public static int getIndex(DesignTreeNode child) { 148 if (child == null) { 149 throw new NullPointerException ("Child can't be null!"); 150 } 151 return child.getParent().getIndex(child); 152 } 153 } 154 | Popular Tags |