1 11 12 package org.eclipse.team.internal.core; 13 14 import java.util.*; 15 16 import org.eclipse.core.runtime.Assert; 17 import org.eclipse.core.runtime.Preferences; 18 import org.eclipse.core.runtime.Preferences.PropertyChangeEvent; 19 import org.eclipse.team.core.Team; 20 21 22 public class UserStringMappings implements Preferences.IPropertyChangeListener { 23 24 public static final Integer BINARY= new Integer (Team.BINARY); 25 public static final Integer TEXT= new Integer (Team.TEXT); 26 public static final Integer UNKNOWN= new Integer (Team.UNKNOWN); 27 28 29 private static final String PREF_TEAM_SEPARATOR = "\n"; 31 private final Preferences fPreferences; 32 private final String fKey; 33 34 private Map fMap; 35 36 public UserStringMappings(String key) { 37 fKey= key; 38 fPreferences= TeamPlugin.getPlugin().getPluginPreferences(); 39 fPreferences.addPropertyChangeListener(this); 40 } 41 42 public Map referenceMap() { 43 if (fMap == null) { 44 fMap= loadMappingsFromPreferences(); 45 } 46 return fMap; 47 } 48 49 public void addStringMappings(String [] names, int[] types) { 50 Assert.isTrue(names.length == types.length); 51 final Map map= referenceMap(); 52 53 for (int i = 0; i < names.length; i++) { 54 switch (types[i]) { 55 case Team.BINARY: map.put(names[i], BINARY); break; 56 case Team.TEXT: map.put(names[i], TEXT); break; 57 case Team.UNKNOWN: map.put(names[i], UNKNOWN); break; 58 } 59 } 60 save(); 61 } 62 63 public void setStringMappings(String [] names, int [] types) { 64 Assert.isTrue(names.length == types.length); 65 referenceMap().clear(); 66 addStringMappings(names, types); 67 } 68 69 public int getType(String string) { 70 if (string == null) 71 return Team.UNKNOWN; 72 final Integer type= (Integer )referenceMap().get(string); 73 return type != null ? type.intValue() : Team.UNKNOWN; 74 } 75 76 public void propertyChange(PropertyChangeEvent event) { 77 if(event.getProperty().equals(fKey)) 78 fMap= null; 79 } 80 81 public void save() { 82 final StringBuffer buffer = new StringBuffer (); 84 final Iterator e = fMap.keySet().iterator(); 85 86 while (e.hasNext()) { 87 final String filename = (String )e.next(); 88 buffer.append(filename); 89 buffer.append(PREF_TEAM_SEPARATOR); 90 final Integer type = (Integer )fMap.get(filename); 91 buffer.append(type); 92 buffer.append(PREF_TEAM_SEPARATOR); 93 } 94 TeamPlugin.getPlugin().getPluginPreferences().setValue(fKey, buffer.toString()); 95 } 96 97 protected Map loadMappingsFromPreferences() { 98 final Map result= new HashMap(); 99 100 if (!fPreferences.contains(fKey)) 101 return result; 102 103 final String prefTypes = fPreferences.getString(fKey); 104 final StringTokenizer tok = new StringTokenizer(prefTypes, PREF_TEAM_SEPARATOR); 105 try { 106 while (tok.hasMoreElements()) { 107 final String name = tok.nextToken(); 108 final String mode= tok.nextToken(); 109 result.put(name, Integer.valueOf(mode)); 110 } 111 } catch (NoSuchElementException e) { 112 } 113 return result; 114 } 115 } 116 | Popular Tags |