1 11 12 package org.eclipse.pde.internal.core.cheatsheet.simple; 13 14 import java.io.IOException ; 15 import java.io.PrintWriter ; 16 import java.util.ArrayList ; 17 import java.util.Iterator ; 18 import java.util.List ; 19 20 import org.eclipse.pde.core.IModelChangedEvent; 21 import org.eclipse.pde.internal.core.XMLPrintHandler; 22 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCS; 23 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSIntro; 24 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSItem; 25 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSModel; 26 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSModelFactory; 27 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSObject; 28 import org.eclipse.pde.internal.core.util.PDETextHelper; 29 import org.w3c.dom.Element ; 30 import org.w3c.dom.Node ; 31 import org.w3c.dom.NodeList ; 32 33 37 public class SimpleCS extends SimpleCSObject implements ISimpleCS { 38 39 42 private ISimpleCSIntro fIntro; 43 44 47 private String fTitle; 48 49 52 private ArrayList fItems; 53 54 57 private static final long serialVersionUID = 1L; 58 59 62 public SimpleCS(ISimpleCSModel model) { 63 super(model, null); 64 reset(); 65 } 66 67 70 public ISimpleCSIntro getIntro() { 71 return fIntro; 72 } 73 74 77 public ISimpleCSItem[] getItems() { 78 return (ISimpleCSItem[])fItems.toArray(new ISimpleCSItem[fItems.size()]); 79 } 80 81 84 public String getTitle() { 85 return fTitle; 86 } 87 88 91 public void reset() { 92 fIntro = null; 93 fTitle = null; 94 fItems = new ArrayList (); 95 } 96 97 100 public void setIntro(ISimpleCSIntro intro) { 101 ISimpleCSObject old = fIntro; 102 fIntro = intro; 103 104 if (isEditable()) { 105 fireStructureChanged(intro, old); 106 } 107 } 108 109 112 public void setTitle(String title) { 113 String old = fTitle; 114 fTitle = title; 115 if (isEditable()) { 116 firePropertyChanged(ATTRIBUTE_TITLE, old, fTitle); 117 } 118 } 119 120 123 public void parse(Element element) { 124 if (element.getNodeName().equals(ELEMENT_CHEATSHEET)) { 126 fTitle = element.getAttribute(ATTRIBUTE_TITLE).trim(); 129 NodeList children = element.getChildNodes(); 131 ISimpleCSModelFactory factory = getModel().getFactory(); 132 for (int i = 0; i < children.getLength(); i++) { 133 Node child = children.item(i); 134 if (child.getNodeType() == Node.ELEMENT_NODE) { 135 String name = child.getNodeName(); 136 Element childElement = (Element )child; 137 if (name.equals(ELEMENT_INTRO)) { 138 fIntro = factory.createSimpleCSIntro(this); 139 fIntro.parse(childElement); 140 } else if (name.equals(ELEMENT_ITEM)) { 141 ISimpleCSItem item = factory.createSimpleCSItem(this); 142 fItems.add(item); 143 item.parse(childElement); 144 } 145 } 146 } 147 } 148 149 } 150 151 154 public void write(String indent, PrintWriter writer) { 155 156 StringBuffer buffer = new StringBuffer (); 157 String newIndent = indent + XMLPrintHandler.XML_INDENT; 158 159 try { 160 XMLPrintHandler.printHead(writer, ATTRIBUTE_VALUE_ENCODING); 162 buffer.append(ELEMENT_CHEATSHEET); 164 if ((fTitle != null) && 166 (fTitle.length() > 0)) { 167 buffer.append(XMLPrintHandler.wrapAttribute( 170 ATTRIBUTE_TITLE, 171 PDETextHelper.translateWriteText( 172 fTitle.trim(), SUBSTITUTE_CHARS))); 173 } 174 XMLPrintHandler.printBeginElement(writer, buffer.toString(), 176 indent, false); 177 if (fIntro != null) { 179 fIntro.write(newIndent, writer); 180 } 181 Iterator iterator = fItems.iterator(); 183 while (iterator.hasNext()) { 184 ISimpleCSItem item = (ISimpleCSItem)iterator.next(); 185 item.write(newIndent, writer); 186 } 187 XMLPrintHandler.printEndElement(writer, ELEMENT_CHEATSHEET, indent); 189 190 } catch (IOException e) { 191 } 194 } 195 196 199 public void addItem(ISimpleCSItem item) { 200 fItems.add(item); 201 202 if (isEditable()) { 203 fireStructureChanged(item, IModelChangedEvent.INSERT); 204 } 205 } 206 207 210 public void removeItem(ISimpleCSItem item) { 211 fItems.remove(item); 212 213 if (isEditable()) { 214 fireStructureChanged(item, IModelChangedEvent.REMOVE); 215 } 216 } 217 218 221 public int getType() { 222 return TYPE_CHEAT_SHEET; 223 } 224 225 228 public String getName() { 229 return fTitle; 230 } 231 232 235 public List getChildren() { 236 ArrayList list = new ArrayList (); 237 if (fIntro != null) { 239 list.add(fIntro); 240 } 241 if (fItems.size() > 0) { 243 list.addAll(fItems); 244 } 245 return list; 246 } 247 248 251 public boolean isFirstItem(ISimpleCSItem item) { 252 int position = fItems.indexOf(item); 253 if (position == 0) { 254 return true; 255 } 256 return false; 257 } 258 259 262 public boolean isLastItem(ISimpleCSItem item) { 263 int position = fItems.indexOf(item); 264 int lastPosition = fItems.size() - 1; 265 if (position == lastPosition) { 266 return true; 267 } 268 return false; 269 } 270 271 274 public void addItem(int index, ISimpleCSItem item) { 275 276 if (index < 0) { 277 return; 278 } 279 if (index >= fItems.size()) { 280 fItems.add(item); 281 } else { 282 fItems.add(index, item); 283 } 284 285 if (isEditable()) { 286 fireStructureChanged(item, IModelChangedEvent.INSERT); 287 } 288 289 } 290 291 294 public int indexOfItem(ISimpleCSItem item) { 295 return fItems.indexOf(item); 296 } 297 298 301 public void removeItem(int index) { 302 303 if ((index < 0) || 304 (index > (fItems.size() - 1))) { 305 return; 306 } 307 308 ISimpleCSItem item = (ISimpleCSItem)fItems.remove(index); 309 310 if (isEditable()) { 311 fireStructureChanged(item, IModelChangedEvent.REMOVE); 312 } 313 } 314 315 318 public int getItemCount() { 319 return fItems.size(); 320 } 321 322 325 public boolean hasItems() { 326 if (fItems.isEmpty()) { 327 return false; 328 } 329 return true; 330 } 331 332 335 public ISimpleCSItem getNextSibling(ISimpleCSItem item) { 336 int position = fItems.indexOf(item); 337 int lastIndex = fItems.size() - 1; 338 if ((position == -1) || 339 (position == lastIndex)) { 340 return null; 343 } 344 return (ISimpleCSItem)fItems.get(position + 1); 345 } 346 347 350 public ISimpleCSItem getPreviousSibling(ISimpleCSItem item) { 351 int position = fItems.indexOf(item); 352 if ((position == -1) || 353 (position == 0)) { 354 return null; 357 } 358 return (ISimpleCSItem)fItems.get(position - 1); 359 } 360 361 364 public void moveItem(ISimpleCSItem item, int newRelativeIndex) { 365 int currentIndex = fItems.indexOf(item); 367 if (currentIndex == -1) { 369 return; 370 } 371 int newIndex = newRelativeIndex + currentIndex; 373 if ((newIndex < 0) || 375 (newIndex >= fItems.size())) { 376 return; 377 } 378 fItems.remove(item); 380 fItems.add(newIndex, item); 382 if (isEditable()) { 384 fireStructureChanged(item, IModelChangedEvent.INSERT); 385 } 386 } 387 388 } 389 | Popular Tags |