KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > core > UserStringMappings


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

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 JavaDoc BINARY= new Integer JavaDoc(Team.BINARY);
25     public static final Integer JavaDoc TEXT= new Integer JavaDoc(Team.TEXT);
26     public static final Integer JavaDoc UNKNOWN= new Integer JavaDoc(Team.UNKNOWN);
27     
28      
29     private static final String JavaDoc PREF_TEAM_SEPARATOR = "\n"; //$NON-NLS-1$
30

31     private final Preferences fPreferences;
32     private final String JavaDoc fKey;
33
34     private Map fMap;
35     
36     public UserStringMappings(String JavaDoc 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 JavaDoc[] 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 JavaDoc [] names, int [] types) {
64         Assert.isTrue(names.length == types.length);
65         referenceMap().clear();
66         addStringMappings(names, types);
67     }
68     
69     public int getType(String JavaDoc string) {
70         if (string == null)
71             return Team.UNKNOWN;
72         final Integer JavaDoc type= (Integer JavaDoc)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         // Now set into preferences
83
final StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
84         final Iterator e = fMap.keySet().iterator();
85         
86         while (e.hasNext()) {
87             final String JavaDoc filename = (String JavaDoc)e.next();
88             buffer.append(filename);
89             buffer.append(PREF_TEAM_SEPARATOR);
90             final Integer JavaDoc type = (Integer JavaDoc)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 JavaDoc prefTypes = fPreferences.getString(fKey);
104         final StringTokenizer tok = new StringTokenizer(prefTypes, PREF_TEAM_SEPARATOR);
105         try {
106             while (tok.hasMoreElements()) {
107                 final String JavaDoc name = tok.nextToken();
108                 final String JavaDoc mode= tok.nextToken();
109                 result.put(name, Integer.valueOf(mode));
110             }
111         } catch (NoSuchElementException e) {
112         }
113         return result;
114     }
115 }
116
Popular Tags