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.ISimpleCS; 17 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSConstants; 18 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSDescription; 19 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSIntro; 20 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSItem; 21 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSModelFactory; 22 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSObject; 23 import org.eclipse.pde.internal.ui.PDEUIMessages; 24 import org.eclipse.pde.internal.ui.editor.cheatsheet.CSAbstractAddAction; 25 import org.eclipse.pde.internal.ui.wizards.cheatsheet.BaseCSCreationOperation; 26 27 31 public class SimpleCSAddStepAction extends CSAbstractAddAction { 32 33 private ISimpleCS fCheatsheet; 34 35 private ISimpleCSItem fItem; 36 37 private ISimpleCSIntro fIntro; 38 39 42 public SimpleCSAddStepAction() { 43 setText(PDEUIMessages.SimpleCSAddStepAction_0); 44 } 45 46 49 public void setDataObject(ISimpleCSObject csObject) { 50 if (csObject.getType() == ISimpleCSConstants.TYPE_CHEAT_SHEET) { 52 fIntro = null; 53 fItem = null; 54 fCheatsheet = (ISimpleCS)csObject; 55 } else if (csObject.getType() == ISimpleCSConstants.TYPE_ITEM) { 56 fIntro = null; 57 fItem = (ISimpleCSItem)csObject; 58 fCheatsheet = fItem.getSimpleCS(); 59 } else if (csObject.getType() == ISimpleCSConstants.TYPE_INTRO) { 60 fIntro = (ISimpleCSIntro)csObject; 61 fItem = null; 62 fCheatsheet = fIntro.getSimpleCS(); 63 } else { 64 fIntro = null; 66 fItem = null; 67 fCheatsheet = null; 68 } 69 } 70 71 74 public void run() { 75 if (fCheatsheet == null) { 77 return; 78 } 79 ISimpleCSItem newItem = createNewItem(); 81 insertNewItem(newItem); 83 } 84 85 88 private void insertNewItem(ISimpleCSItem newItem) { 89 if (fIntro != null) { 91 fCheatsheet.addItem(0, newItem); 95 } else if (fItem != null) { 96 int index = fCheatsheet.indexOfItem(fItem) + 1; 99 fCheatsheet.addItem(index, newItem); 100 } else { 101 fCheatsheet.addItem(newItem); 104 } 105 } 106 107 110 private ISimpleCSItem createNewItem() { 111 ISimpleCSModelFactory factory = fCheatsheet.getModel().getFactory(); 112 ISimpleCSItem item = factory.createSimpleCSItem(fCheatsheet); 115 item.setTitle(generateItemTitle(PDEUIMessages.SimpleCheatSheetCreationOperation_1)); 116 ISimpleCSDescription description = factory.createSimpleCSDescription(item); 118 description.setContent( 119 BaseCSCreationOperation.formatTextBold( 120 PDEUIMessages.SimpleCheatSheetCreationOperation_2)); 121 item.setDescription(description); 122 return item; 123 } 124 125 128 private String generateItemTitle(String base) { 129 StringBuffer result = new StringBuffer (base); 130 ISimpleCSItem[] items = fCheatsheet.getItems(); 131 HashSet set = new HashSet (); 133 134 for (int i = 0; i < items.length; i++) { 138 ISimpleCSItem item = items[i]; 139 compareTitleWithBase(base, set, item.getTitle()); 140 } 141 addNumberToBase(result, set); 143 144 return result.toString(); 145 } 146 147 } 148 | Popular Tags |