1 11 package org.eclipse.pde.internal.ui.model.plugin; 12 13 import java.util.*; 14 15 import org.eclipse.pde.internal.ui.model.*; 16 17 public abstract class PluginDocumentNode implements IDocumentNode { 18 19 private IDocumentNode fParent; 20 private ArrayList fChildren = new ArrayList(); 21 private boolean fIsErrorNode; 22 private int fLength = -1; 23 private int fOffset = -1; 24 protected Map fAttributes = new TreeMap(); 25 private String fTag; 26 private int fIndent = 0; 27 private IDocumentNode fPreviousSibling; 28 protected IDocumentTextNode fTextNode; 29 30 33 public IDocumentNode[] getChildNodes() { 34 return (IDocumentNode[]) fChildren.toArray(new IDocumentNode[fChildren.size()]); 35 } 36 37 40 public int indexOf(IDocumentNode child) { 41 return fChildren.indexOf(child); 42 } 43 44 47 public IDocumentNode getChildAt(int index) { 48 if (index < fChildren.size()) 49 return (IDocumentNode)fChildren.get(index); 50 return null; 51 } 52 55 public IDocumentNode getParentNode() { 56 return fParent; 57 } 58 61 public void setParentNode(IDocumentNode node) { 62 fParent = node; 63 } 64 67 public void addChildNode(IDocumentNode child) { 68 addChildNode(child, fChildren.size()); 69 } 70 71 74 public void addChildNode(IDocumentNode child, int position) { 75 fChildren.add(position, child); 76 if (position > 0 && fChildren.size() > 1) 77 child.setPreviousSibling((IDocumentNode)fChildren.get(position - 1)); 78 if (fChildren.size() > 1 && position < fChildren.size() - 1) 79 ((IDocumentNode)fChildren.get(position + 1)).setPreviousSibling(child); 80 child.setParentNode(this); 81 } 82 83 86 public IDocumentNode removeChildNode(IDocumentNode child) { 87 int index = fChildren.indexOf(child); 88 if (index != -1) { 89 fChildren.remove(child); 90 if (index < fChildren.size()) { 91 IDocumentNode prevSibling = index == 0 ? null : (IDocumentNode)fChildren.get(index - 1); 92 ((IDocumentNode)fChildren.get(index)).setPreviousSibling(prevSibling); 93 return child; 94 } 95 } 96 return null; 97 } 98 101 public boolean isErrorNode() { 102 return fIsErrorNode; 103 } 104 107 public void setIsErrorNode(boolean isErrorNode) { 108 fIsErrorNode = isErrorNode; 109 } 110 113 public void setOffset(int offset) { 114 fOffset = offset; 115 } 116 119 public void setLength(int length) { 120 fLength = length; 121 } 122 125 public int getOffset() { 126 return fOffset; 127 } 128 131 public int getLength() { 132 return fLength; 133 } 134 137 public void setXMLAttribute(IDocumentAttribute attribute) { 138 fAttributes.put(attribute.getAttributeName(), attribute); 139 } 140 143 public String getXMLAttributeValue(String name) { 144 PluginAttribute attr = (PluginAttribute)fAttributes.get(name); 145 return attr == null ? null : attr.getValue(); 146 } 147 148 151 public void setXMLTagName(String tag) { 152 fTag = tag; 153 } 154 155 158 public String getXMLTagName() { 159 return fTag; 160 } 161 162 165 public IDocumentAttribute getDocumentAttribute(String name) { 166 return (IDocumentAttribute)fAttributes.get(name); 167 } 168 169 172 public int getLineIndent() { 173 return fIndent; 174 } 175 176 179 public void setLineIndent(int indent) { 180 fIndent = indent; 181 } 182 183 186 public IDocumentAttribute[] getNodeAttributes() { 187 ArrayList list = new ArrayList(); 188 Iterator iter = fAttributes.values().iterator(); 189 while (iter.hasNext()) 190 list.add(iter.next()); 191 return (IDocumentAttribute[])list.toArray(new IDocumentAttribute[list.size()]); 192 } 193 194 197 public IDocumentNode getPreviousSibling() { 198 return fPreviousSibling; 199 } 200 201 204 public void setPreviousSibling(IDocumentNode sibling) { 205 fPreviousSibling = sibling; 206 } 207 208 protected String getIndent() { 209 StringBuffer buffer = new StringBuffer (); 210 for (int i = 0; i < fIndent; i++) { 211 buffer.append(" "); } 213 return buffer.toString(); 214 } 215 216 219 public void swap(IDocumentNode child1, IDocumentNode child2) { 220 int index1 = fChildren.indexOf(child1); 221 int index2 = fChildren.indexOf(child2); 222 223 fChildren.set(index1, child2); 224 fChildren.set(index2, child1); 225 226 child1.setPreviousSibling(index2 == 0 ? null : (IDocumentNode)fChildren.get(index2 - 1)); 227 child2.setPreviousSibling(index1 == 0 ? null : (IDocumentNode)fChildren.get(index1 - 1)); 228 229 if (index1 < fChildren.size() - 1) 230 ((IDocumentNode)fChildren.get(index1 + 1)).setPreviousSibling(child2); 231 232 if (index2 < fChildren.size() - 1) 233 ((IDocumentNode)fChildren.get(index2 + 1)).setPreviousSibling(child1); 234 } 235 236 239 public void addTextNode(IDocumentTextNode textNode) { 240 fTextNode = textNode; 241 } 242 243 246 public IDocumentTextNode getTextNode() { 247 return fTextNode; 248 } 249 252 public void removeTextNode() { 253 fTextNode = null; 254 } 255 256 259 public void removeDocumentAttribute(IDocumentAttribute attr) { 260 fAttributes.remove(attr.getAttributeName()); 261 } 262 263 } 264 | Popular Tags |