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.ISimpleCSConditionalSubItem; 23 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSConstants; 24 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSDescription; 25 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSItem; 26 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSModel; 27 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSModelFactory; 28 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSObject; 29 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSOnCompletion; 30 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSRepeatedSubItem; 31 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSRunContainerObject; 32 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSSubItem; 33 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSSubItemObject; 34 import org.eclipse.pde.internal.core.util.PDETextHelper; 35 import org.w3c.dom.Element ; 36 import org.w3c.dom.Node ; 37 import org.w3c.dom.NodeList ; 38 39 43 public class SimpleCSItem extends SimpleCSObject implements ISimpleCSItem { 44 45 46 49 private ISimpleCSOnCompletion fOnCompletion; 50 51 54 private ISimpleCSRunContainerObject fExecutable; 55 56 59 private boolean fSkip; 60 61 64 private boolean fDialog; 65 66 69 private ISimpleCSDescription fDescription; 70 71 74 private String fTitle; 75 76 79 private String fContextId; 80 81 84 private String fHref; 85 86 89 private ArrayList fSubItems; 90 91 94 private static final long serialVersionUID = 1L; 95 96 100 public SimpleCSItem(ISimpleCSModel model, ISimpleCSObject parent) { 101 super(model, parent); 102 reset(); 103 } 104 105 108 public String getContextId() { 109 return fContextId; 110 } 111 112 115 public ISimpleCSDescription getDescription() { 116 return fDescription; 117 } 118 119 122 public boolean getDialog() { 123 return fDialog; 124 } 125 126 129 public ISimpleCSRunContainerObject getExecutable() { 130 return fExecutable; 131 } 132 133 136 public String getHref() { 137 return fHref; 138 } 139 140 143 public boolean getSkip() { 144 return fSkip; 145 } 146 147 150 public ISimpleCSSubItemObject[] getSubItems() { 151 return (ISimpleCSSubItemObject[]) fSubItems 152 .toArray(new ISimpleCSSubItemObject[fSubItems.size()]); 153 } 154 155 158 public String getTitle() { 159 return fTitle; 160 } 161 162 165 public void setContextId(String contextId) { 166 String old = fContextId; 167 fContextId = contextId; 168 if (isEditable()) { 169 firePropertyChanged(ATTRIBUTE_CONTEXTID, old, fContextId); 170 } 171 } 172 173 176 public void setDescription(ISimpleCSDescription description) { 177 ISimpleCSObject old = fDescription; 178 fDescription = description; 179 180 if (isEditable()) { 181 fireStructureChanged(description, old); 182 } 183 } 184 185 188 public void setDialog(boolean dialog) { 189 Boolean old = Boolean.valueOf(fDialog); 190 fDialog = dialog; 191 if (isEditable()) { 192 firePropertyChanged(ATTRIBUTE_DIALOG, old, Boolean.valueOf(fDialog)); 193 } 194 } 195 196 199 public void setExecutable(ISimpleCSRunContainerObject executable) { 200 ISimpleCSObject old = fExecutable; 201 fExecutable = executable; 202 203 if (isEditable()) { 204 fireStructureChanged(executable, old); 205 } 206 } 207 208 211 public void setHref(String href) { 212 String old = fHref; 213 fHref = href; 214 if (isEditable()) { 215 firePropertyChanged(ATTRIBUTE_HREF, old, fHref); 216 } 217 } 218 219 222 public void setSkip(boolean skip) { 223 Boolean old = Boolean.valueOf(fSkip); 224 fSkip = skip; 225 if (isEditable()) { 226 firePropertyChanged(ATTRIBUTE_SKIP, old, Boolean.valueOf(fSkip)); 227 } 228 } 229 230 233 public void setTitle(String title) { 234 String old = fTitle; 235 fTitle = title; 236 if (isEditable()) { 237 firePropertyChanged(ATTRIBUTE_TITLE, old, fTitle); 238 } 239 } 240 241 244 public void parse(Element element) { 245 fTitle = element.getAttribute(ATTRIBUTE_TITLE).trim(); 247 if (element.getAttribute(ATTRIBUTE_DIALOG).compareTo( 249 ATTRIBUTE_VALUE_FALSE) == 0) { 250 fDialog = false; 251 } 252 if (element.getAttribute(ATTRIBUTE_SKIP).compareTo( 254 ATTRIBUTE_VALUE_TRUE) == 0) { 255 fSkip = true; 256 } 257 fContextId = element.getAttribute(ATTRIBUTE_CONTEXTID).trim(); 259 fHref = element.getAttribute(ATTRIBUTE_HREF).trim(); 261 262 264 NodeList children = element.getChildNodes(); 265 ISimpleCSModelFactory factory = getModel().getFactory(); 266 for (int i = 0; i < children.getLength(); i++) { 267 Node child = children.item(i); 268 if (child.getNodeType() == Node.ELEMENT_NODE) { 269 String name = child.getNodeName(); 270 Element childElement = (Element )child; 271 272 if (name.equals(ELEMENT_DESCRIPTION)) { 273 fDescription = factory.createSimpleCSDescription(this); 274 fDescription.parse(childElement); 275 } else if (name.equals(ELEMENT_ACTION)) { 276 fExecutable = factory.createSimpleCSAction(this); 277 fExecutable.parse(childElement); 278 } else if (name.equals(ELEMENT_COMMAND)) { 279 fExecutable = factory.createSimpleCSCommand(this); 280 fExecutable.parse(childElement); 281 } else if (name.equals(ELEMENT_PERFORM_WHEN)) { 282 fExecutable = factory.createSimpleCSPerformWhen(this); 283 fExecutable.parse(childElement); 284 } else if (name.equals(ELEMENT_SUBITEM)) { 285 ISimpleCSSubItem subitem = factory.createSimpleCSSubItem(this); 286 fSubItems.add(subitem); 287 subitem.parse(childElement); 288 } else if (name.equals(ELEMENT_REPEATED_SUBITEM)) { 289 ISimpleCSRepeatedSubItem subitem = factory.createSimpleCSRepeatedSubItem(this); 290 fSubItems.add(subitem); 291 subitem.parse(childElement); 292 } else if (name.equals(ELEMENT_CONDITIONAL_SUBITEM)) { 293 ISimpleCSConditionalSubItem subitem = factory.createSimpleCSConditionalSubItem(this); 294 fSubItems.add(subitem); 295 subitem.parse(childElement); 296 } else if (name.equals(ELEMENT_ONCOMPLETION)) { 297 fOnCompletion = factory.createSimpleCSOnCompletion(this); 298 fOnCompletion.parse(childElement); 299 } 300 } 301 } 302 } 303 304 307 public void write(String indent, PrintWriter writer) { 308 309 StringBuffer buffer = new StringBuffer (); 310 String newIndent = indent + XMLPrintHandler.XML_INDENT; 311 312 try { 313 buffer.append(ELEMENT_ITEM); if ((fTitle != null) && 317 (fTitle.length() > 0)) { 318 buffer.append(XMLPrintHandler.wrapAttribute( 319 ATTRIBUTE_TITLE, 320 PDETextHelper.translateWriteText( 321 fTitle.trim(), SUBSTITUTE_CHARS))); 322 } 323 buffer.append(XMLPrintHandler.wrapAttribute( 325 ATTRIBUTE_DIALOG, new Boolean (fDialog).toString())); 326 buffer.append(XMLPrintHandler.wrapAttribute( 328 ATTRIBUTE_SKIP, new Boolean (fSkip).toString())); 329 if ((fContextId != null) && 332 (fContextId.length() > 0)) { 333 buffer.append(XMLPrintHandler.wrapAttribute( 334 ATTRIBUTE_CONTEXTID, 335 PDETextHelper.translateWriteText( 336 fContextId.trim(), SUBSTITUTE_CHARS))); 337 } else if ((fHref != null) && 338 (fHref.length() > 0)) { 339 buffer.append(XMLPrintHandler.wrapAttribute( 340 ATTRIBUTE_HREF, 341 PDETextHelper.translateWriteText( 342 fHref.trim(), SUBSTITUTE_CHARS))); 343 } 344 XMLPrintHandler.printBeginElement(writer, buffer.toString(), 346 indent, false); 347 if (fDescription != null) { 349 fDescription.write(newIndent, writer); 350 } 351 if (fExecutable != null) { 353 fExecutable.write(newIndent, writer); 354 } 355 Iterator iterator = fSubItems.iterator(); 357 while (iterator.hasNext()) { 358 ISimpleCSSubItemObject subitem = (ISimpleCSSubItemObject)iterator.next(); 359 subitem.write(newIndent, writer); 360 } 361 if (fOnCompletion != null) { 363 fOnCompletion.write(newIndent, writer); 364 } 365 XMLPrintHandler.printEndElement(writer, ELEMENT_ITEM, indent); 367 368 } catch (IOException e) { 369 } 372 373 } 374 375 378 public void reset() { 379 fOnCompletion = null; 380 fExecutable = null; 381 fSkip = false; 382 fDialog = true; 383 fDescription = null; 384 fTitle = null; 385 fContextId = null; 386 fHref = null; 387 fSubItems = new ArrayList (); 388 } 389 390 393 public void addSubItem(ISimpleCSSubItemObject subitem) { 394 fSubItems.add(subitem); 395 396 if (isEditable()) { 397 fireStructureChanged(subitem, IModelChangedEvent.INSERT); 398 } 399 } 400 401 404 public ISimpleCSOnCompletion getOnCompletion() { 405 return fOnCompletion; 406 } 407 408 411 public void removeSubItem(ISimpleCSSubItemObject subitem) { 412 fSubItems.remove(subitem); 413 414 if (isEditable()) { 415 fireStructureChanged(subitem, IModelChangedEvent.REMOVE); 416 } 417 } 418 419 422 public void setOnCompletion(ISimpleCSOnCompletion onCompletion) { 423 ISimpleCSObject old = fOnCompletion; 424 fOnCompletion = onCompletion; 425 426 if (isEditable()) { 427 fireStructureChanged(onCompletion, old); 428 } 429 } 430 431 434 public int getType() { 435 return TYPE_ITEM; 436 } 437 438 441 public String getName() { 442 return fTitle; 443 } 444 445 448 public List getChildren() { 449 ArrayList list = new ArrayList (); 450 if (fSubItems.size() > 0) { 452 list.addAll(fSubItems); 453 } 454 if ((fExecutable != null) && 456 (fExecutable.getType() == ISimpleCSConstants.TYPE_PERFORM_WHEN)) { 457 list.add(fExecutable); 458 } 459 return list; 460 } 461 462 465 public void addSubItem(int index, ISimpleCSSubItemObject subitem) { 466 if (index < 0){ 467 return; 468 } 469 if (index >= fSubItems.size()) { 470 fSubItems.add(subitem); 471 } else { 472 fSubItems.add(index, subitem); 473 } 474 475 if (isEditable()) { 476 fireStructureChanged(subitem, IModelChangedEvent.INSERT); 477 } 478 479 } 480 481 484 public int indexOfSubItem(ISimpleCSSubItemObject subitem) { 485 return fSubItems.indexOf(subitem); 486 } 487 488 491 public boolean isFirstSubItem(ISimpleCSSubItemObject subitem) { 492 int position = fSubItems.indexOf(subitem); 493 if (position == 0) { 494 return true; 495 } 496 return false; 497 } 498 499 502 public boolean isLastSubItem(ISimpleCSSubItemObject subitem) { 503 int position = fSubItems.indexOf(subitem); 504 int lastPosition = fSubItems.size() - 1; 505 if (position == lastPosition) { 506 return true; 507 } 508 return false; 509 } 510 511 514 public void removeSubItem(int index) { 515 516 if ((index < 0) || 517 (index > (fSubItems.size() - 1))) { 518 return; 519 } 520 521 ISimpleCSSubItemObject subitem = (ISimpleCSSubItemObject)fSubItems.remove(index); 522 523 if (isEditable()) { 524 fireStructureChanged(subitem, IModelChangedEvent.REMOVE); 525 } 526 } 527 528 531 public int getSubItemCount() { 532 return fSubItems.size(); 533 } 534 535 538 public boolean hasSubItems() { 539 if (fSubItems.isEmpty()) { 540 return false; 541 } 542 return true; 543 } 544 545 548 public ISimpleCSSubItemObject getNextSibling(ISimpleCSSubItemObject subitem) { 549 int position = fSubItems.indexOf(subitem); 550 int lastIndex = fSubItems.size() - 1; 551 if ((position == -1) || 552 (position == lastIndex)) { 553 return null; 556 } 557 return (ISimpleCSSubItemObject)fSubItems.get(position + 1); 558 } 559 560 563 public ISimpleCSSubItemObject getPreviousSibling( 564 ISimpleCSSubItemObject subitem) { 565 int position = fSubItems.indexOf(subitem); 566 if ((position == -1) || 567 (position == 0)) { 568 return null; 571 } 572 return (ISimpleCSSubItemObject)fSubItems.get(position - 1); 573 } 574 575 578 public void moveSubItem(ISimpleCSSubItemObject subitem, int newRelativeIndex) { 579 int currentIndex = fSubItems.indexOf(subitem); 581 if (currentIndex == -1) { 583 return; 584 } 585 int newIndex = newRelativeIndex + currentIndex; 587 if ((newIndex < 0) || 589 (newIndex >= fSubItems.size())) { 590 return; 591 } 592 fSubItems.remove(subitem); 594 fSubItems.add(newIndex, subitem); 596 if (isEditable()) { 598 fireStructureChanged(subitem, IModelChangedEvent.INSERT); 599 } 600 } 601 602 } 603 | Popular Tags |