1 11 12 package org.eclipse.pde.internal.ui.editor.cheatsheet.simple.actions; 13 14 import java.util.HashSet ; 15 16 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSConstants; 17 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSItem; 18 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSModelFactory; 19 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSObject; 20 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSSubItem; 21 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSSubItemObject; 22 import org.eclipse.pde.internal.ui.PDEUIMessages; 23 import org.eclipse.pde.internal.ui.editor.cheatsheet.CSAbstractAddAction; 24 25 29 public class SimpleCSAddSubStepAction extends CSAbstractAddAction { 30 31 private ISimpleCSItem fItem; 32 33 private ISimpleCSSubItem fSubitem; 34 35 38 public SimpleCSAddSubStepAction() { 39 setText(PDEUIMessages.SimpleCSAddSubStepAction_0); 40 } 41 42 45 public void setDataObject(ISimpleCSObject csObject) { 46 if (csObject.getType() == ISimpleCSConstants.TYPE_ITEM) { 48 fSubitem = null; 49 fItem = (ISimpleCSItem)csObject; 50 } else if (csObject.getType() == ISimpleCSConstants.TYPE_SUBITEM) { 51 fSubitem = (ISimpleCSSubItem)csObject; 52 ISimpleCSObject parentObject = fSubitem.getParent(); 53 if (parentObject.getType() == ISimpleCSConstants.TYPE_ITEM) { 55 fItem = (ISimpleCSItem)parentObject; 56 } else if (parentObject.getType() == ISimpleCSConstants.TYPE_CONDITIONAL_SUBITEM) { 57 fItem = null; 59 } else if (parentObject.getType() == ISimpleCSConstants.TYPE_REPEATED_SUBITEM) { 60 fItem = null; 62 } 63 } else { 64 fSubitem = null; 66 fItem = null; 67 } 68 } 69 70 73 public void run() { 74 if (fItem == null) { 76 return; 77 } 78 ISimpleCSSubItem newSubItem = createNewSubItem(); 80 insertNewSubItem(newSubItem); 82 } 83 84 87 private ISimpleCSSubItem createNewSubItem() { 88 ISimpleCSModelFactory factory = fItem.getModel().getFactory(); 89 ISimpleCSSubItem subitem = factory.createSimpleCSSubItem(fItem); 91 subitem.setLabel(generateSubItemLabel(fItem, PDEUIMessages.SimpleCSAddSubStepAction_1)); 93 return subitem; 94 } 95 96 99 private void insertNewSubItem(ISimpleCSSubItem newSubItem) { 100 if (fSubitem != null) { 102 int index = fItem.indexOfSubItem(fSubitem) + 1; 105 fItem.addSubItem(index, newSubItem); 106 } else { 107 fItem.addSubItem(newSubItem); 110 } 111 } 112 113 116 private String generateSubItemLabel(ISimpleCSItem item, String base) { 117 StringBuffer result = new StringBuffer (base); 118 ISimpleCSSubItemObject[] subitems = item.getSubItems(); 119 HashSet set = new HashSet (); 121 122 for (int i = 0; i < subitems.length; i++) { 126 ISimpleCSSubItemObject object = subitems[i]; 127 if (object.getType() == ISimpleCSConstants.TYPE_SUBITEM) { 128 ISimpleCSSubItem subitem = (ISimpleCSSubItem)object; 129 compareTitleWithBase(base, set, subitem.getLabel()); 130 } 131 } 132 addNumberToBase(result, set); 134 135 return result.toString(); 136 } 137 } 138 | Popular Tags |