1 11 package org.eclipse.ui.internal.cheatsheets.views; 12 13 import java.util.ArrayList ; 14 import java.util.HashSet ; 15 import java.util.Hashtable ; 16 import java.util.Iterator ; 17 import java.util.List ; 18 import java.util.Map ; 19 import java.util.Set ; 20 21 import org.eclipse.ui.cheatsheets.*; 22 import org.eclipse.ui.internal.cheatsheets.registry.CheatSheetElement; 23 24 27 public class CheatSheetManager implements ICheatSheetManager { 28 29 private static final String PARENT_PREFIX = "parent."; private static final String VARIABLE_END = "}"; private static final String VARIABLE_BEGIN = "${"; private String cheatsheetID; 33 private List listeners; 34 private Map dataTable = null; 35 private ICheatSheetManager parent; 36 37 public CheatSheetManager(CheatSheetElement element) { 38 cheatsheetID = element.getID(); 39 listeners = new ArrayList (); 40 CheatSheetListener listener = element.createListenerInstance(); 41 if (listener != null) { 42 addListener(listener); 43 } 44 } 45 46 49 public String getCheatSheetID() { 50 return cheatsheetID; 51 } 52 53 public void fireEvent(int eventType) { 54 for (Iterator iterator = listeners.iterator();iterator.hasNext();) { 56 ICheatSheetEvent event = new CheatSheetEvent(eventType, cheatsheetID, this); 57 CheatSheetListener listener = (CheatSheetListener)iterator.next(); 58 listener.cheatSheetEvent(event); 59 } 60 } 61 62 65 public Map getData() { 66 return dataTable; 67 } 68 69 73 public void setData(Map data) { 74 dataTable = data; 75 } 76 77 public String getData(String key) { 78 if (dataTable == null) 79 return null; 80 return (String ) dataTable.get(key); 81 } 82 83 89 public String getDataQualified(String qualifiedKey) { 90 if (qualifiedKey.startsWith(PARENT_PREFIX) && parent != null) { 91 return parent.getData(qualifiedKey.substring(PARENT_PREFIX.length())); 92 } else { 93 return getData(qualifiedKey); 94 } 95 } 96 97 public String getVariableData(String variable) { 98 String result = variable; 99 if(variable != null && variable.startsWith(VARIABLE_BEGIN) && variable.endsWith(VARIABLE_END)) { 100 result = variable.substring(2,variable.length()-1); 101 result = getDataQualified(result); 102 } 103 return result; 104 } 105 106 113 public String performVariableSubstitution(String input) 114 { 115 String remaining = input; 116 String output = ""; while (remaining.length() > 0) { 118 int varIndex = remaining.indexOf(VARIABLE_BEGIN); 119 int endIndex = remaining.indexOf(VARIABLE_END, varIndex + 1); 120 if (varIndex < 0 || endIndex < 0) { 121 output += remaining; 122 remaining = ""; } else { 124 String varName = remaining.substring(varIndex + VARIABLE_BEGIN.length(), 125 endIndex); 126 String value = getDataQualified(varName); 127 output += remaining.substring(0, varIndex); 128 if (value != null) { 129 output += value; 130 } 131 remaining = remaining.substring(endIndex + VARIABLE_END.length()); 132 } 133 } 134 return output; 135 } 136 137 void setData(Hashtable data) { 138 dataTable = data; 139 } 140 141 144 public void setData(String key, String data) { 145 if (key == null) { 146 throw new IllegalArgumentException (); 147 } 148 149 if(data == null && dataTable != null) { 150 dataTable.remove(key); 151 return; 152 } 153 154 if (dataTable == null) { 155 dataTable = new Hashtable (30); 156 } 157 158 dataTable.put(key, data); 159 } 160 161 167 public void setDataQualified(String qualifiedKey, String data) { 168 if (qualifiedKey == null) { 169 throw new IllegalArgumentException (); 170 } 171 if (qualifiedKey.startsWith(PARENT_PREFIX) && parent != null) { 172 parent.setData(qualifiedKey.substring(PARENT_PREFIX.length()), data); 173 } else { 174 setData(qualifiedKey, data); 175 } 176 } 177 178 182 public void addListener(CheatSheetListener listener) { 183 if (listener != null) { 184 listeners.add(listener); 185 } 186 } 187 188 public ICheatSheetManager getParent() { 189 return parent; 190 } 191 192 public void setParent(ICheatSheetManager parent) { 193 this.parent = parent; 194 } 195 196 public Set getKeySet() { 197 if (dataTable == null) { 198 return new HashSet (); 199 } else { 200 return dataTable.keySet(); 201 } 202 } 203 } 204 | Popular Tags |