1 package org.catacomb.druid.xtext.base; 2 3 import org.catacomb.druid.xtext.parse.TextSplitter; 4 import org.catacomb.interlish.structure.*; 5 import org.catacomb.report.E; 6 7 8 import java.util.ArrayList; 9 10 11 public class DocStore implements Producer, TreeProvider, Tree, TreeNode, 12 SelectionActor { 13 14 15 ArrayList<XTDoc> docs; 16 17 XTDoc dummyDoc; 18 19 static int iti = 100; 20 21 TreeExplorer treeExp; 22 23 24 DocDisplay docDisplay; 25 26 TreeChangeReporter tcReporter; 27 28 29 public DocStore() { 30 docs = new ArrayList<XTDoc>(); 31 } 32 33 34 public String toString() { 35 return "pages"; 36 } 37 38 39 public void setDocDisplay(DocDisplay dd) { 40 docDisplay = dd; 41 } 42 43 44 public XTDoc getDummyDoc(String txt) { 45 if (dummyDoc == null) { 46 47 TextSplitter ts = new TextSplitter(txt); 48 49 ContainerBlock rootBlock = ts.makeBlock(); 50 51 dummyDoc = new XTDoc(this, "example", rootBlock); 52 docs.add(dummyDoc); 53 updateTree(); 54 55 } 56 return dummyDoc; 57 } 58 59 60 public XTDoc getImportDoc(String txt, String name) { 61 TextSplitter ts = new TextSplitter(txt); 62 63 ContainerBlock rootBlock = ts.makeBlock(); 64 65 XTDoc xtd = new XTDoc(this, name, rootBlock); 66 docs.add(xtd); 67 updateTree(); 68 69 return xtd; 70 } 71 72 73 74 public XTDoc nextTextDoc() { 75 iti += 1; 76 77 ContainerBlock rb = ContainerBlock.newEmptyText(); 78 String title = "source_" + iti; 79 80 XTDoc xtd = new XTDoc(this, title, rb); 81 82 docs.add(xtd); 83 updateTree(); 84 85 return xtd; 86 } 87 88 89 90 public void updateTree() { 91 if (tcReporter != null) { 92 E.info("NB should use tcReporter"); 93 } 94 95 if (treeExp != null) { 96 treeExp.treeModified(); 97 98 Object[] oa = {this}; 99 treeExp.showNewItem(oa); 100 101 } 102 } 103 104 105 public void setTreeExplorer(TreeExplorer treeex) { 106 treeExp = treeex; 107 treeExp.setTree(this); 108 treeExp.setSelectionActor(this); 109 updateTree(); 110 } 111 112 113 public void setTreeChangeReporter(TreeChangeReporter tcr) { 114 tcReporter = tcr; 115 } 116 117 118 119 public Object getParent() { 120 return null; 121 } 122 123 124 public TreeNode getRoot() { 125 return this; 126 } 127 128 129 public int getRootPolicy() { 130 return Tree.SHOW_ROOT; 131 } 132 133 134 public int getChildCount() { 135 return docs.size(); 136 } 137 138 139 public Object getChild(int index) { 140 return docs.get(index); 141 } 142 143 144 public int getIndexOfChild(Object child) { 145 return docs.indexOf(child); 146 } 147 148 149 public boolean isLeaf() { 150 return false; 151 } 152 153 154 public XTDoc getDocByID(String s) { 155 XTDoc ret = null; 156 for (XTDoc xtd : docs) { 157 if (s.equals(xtd.toString())) { 158 ret = xtd; 159 break; 160 } 161 } 162 return ret; 163 } 164 165 166 167 public void selectionAction(Object obj, String id) { 168 if (obj instanceof XTDoc) { 169 if (docDisplay != null) { 170 docDisplay.showDoc((XTDoc)obj); 171 } 172 173 } else { 174 E.info("unexpected type " + obj); 175 176 XTDoc xtd = getDocByID(id); 177 if (docDisplay != null) { 178 docDisplay.showDoc(xtd); 179 } 180 } 181 182 } 183 184 185 public void pageDataChanged() { 186 if (docDisplay != null) { 187 docDisplay.repaintPageData(); 188 } 189 190 } 191 192 193 public Object[] getObjectPath(String s, boolean b) { 194 E.missing(); 195 return null; 196 } 197 198 199 200 201 202 }