KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > text > formatter > FormattingContext


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.jface.text.formatter;
13
14 import java.util.HashMap JavaDoc;
15 import java.util.Map JavaDoc;
16
17 import org.eclipse.jface.preference.IPreferenceStore;
18
19 /**
20  * Default implementation of <code>IFormattingContext</code>.
21  *
22  * @since 3.0
23  */

24 public class FormattingContext implements IFormattingContext {
25
26     /** Map to store the properties */
27     private final Map JavaDoc fMap= new HashMap JavaDoc();
28
29     /*
30      * @see org.eclipse.jface.text.formatter.IFormattingContext#dispose()
31      */

32     public void dispose() {
33         fMap.clear();
34     }
35
36     /*
37      * @see org.eclipse.jface.text.formatter.IFormattingContext#getPreferenceKeys()
38      */

39     public String JavaDoc[] getPreferenceKeys() {
40         return new String JavaDoc[] {
41         };
42     }
43
44     /*
45      * @see org.eclipse.jface.text.formatter.IFormattingContext#getProperty(java.lang.Object)
46      */

47     public Object JavaDoc getProperty(Object JavaDoc key) {
48         return fMap.get(key);
49     }
50
51     /*
52      * @see org.eclipse.jface.text.formatter.IFormattingContext#isBooleanPreference(java.lang.String)
53      */

54     public boolean isBooleanPreference(String JavaDoc key) {
55         return false;
56     }
57
58     /*
59      * @see org.eclipse.jface.text.formatter.IFormattingContext#isDoublePreference(java.lang.String)
60      */

61     public boolean isDoublePreference(String JavaDoc key) {
62         return false;
63     }
64
65     /*
66      * @see org.eclipse.jface.text.formatter.IFormattingContext#isFloatPreference(java.lang.String)
67      */

68     public boolean isFloatPreference(String JavaDoc key) {
69         return false;
70     }
71
72     /*
73      * @see org.eclipse.jface.text.formatter.IFormattingContext#isIntegerPreference(java.lang.String)
74      */

75     public boolean isIntegerPreference(String JavaDoc key) {
76         return false;
77     }
78
79     /*
80      * @see org.eclipse.jface.text.formatter.IFormattingContext#isLongPreference(java.lang.String)
81      */

82     public boolean isLongPreference(String JavaDoc key) {
83         return false;
84     }
85
86     /*
87      * @see org.eclipse.jface.text.formatter.IFormattingContext#isStringPreference(java.lang.String)
88      */

89     public boolean isStringPreference(String JavaDoc key) {
90         return false;
91     }
92
93     /*
94      * @see org.eclipse.jface.text.formatter.IFormattingContext#mapToStore(java.util.Map, org.eclipse.jface.preference.IPreferenceStore)
95      */

96     public void mapToStore(Map JavaDoc map, IPreferenceStore store) {
97
98         final String JavaDoc[] preferences= getPreferenceKeys();
99
100         String JavaDoc result= null;
101         String JavaDoc preference= null;
102
103         for (int index= 0; index < preferences.length; index++) {
104
105             preference= preferences[index];
106             result= (String JavaDoc)map.get(preference);
107
108             if (result != null) {
109
110                 try {
111                     if (isBooleanPreference(preference)) {
112                         store.setValue(preference, result.equals(IPreferenceStore.TRUE));
113                     } else if (isIntegerPreference(preference)) {
114                         store.setValue(preference, Integer.parseInt(result));
115                     } else if (isStringPreference(preference)) {
116                         store.setValue(preference, result);
117                     } else if (isDoublePreference(preference)) {
118                         store.setValue(preference, Double.parseDouble(result));
119                     } else if (isFloatPreference(preference)) {
120                         store.setValue(preference, Float.parseFloat(result));
121                     } else if (isLongPreference(preference)) {
122                         store.setValue(preference, Long.parseLong(result));
123                     }
124                 } catch (NumberFormatException JavaDoc exception) {
125                     // Do nothing
126
}
127             }
128         }
129     }
130
131     /*
132      * @see org.eclipse.jface.text.formatter.IFormattingContext#setProperty(java.lang.Object, java.lang.Object)
133      */

134     public void setProperty(Object JavaDoc key, Object JavaDoc property) {
135         fMap.put(key, property);
136     }
137
138     /*
139      * @see org.eclipse.jface.text.formatter.IFormattingContext#storeToMap(org.eclipse.jface.preference.IPreferenceStore, java.util.Map, boolean)
140      */

141     public void storeToMap(IPreferenceStore store, Map JavaDoc map, boolean useDefault) {
142
143         final String JavaDoc[] preferences= getPreferenceKeys();
144
145         String JavaDoc preference= null;
146         for (int index= 0; index < preferences.length; index++) {
147
148             preference= preferences[index];
149
150             if (isBooleanPreference(preference)) {
151                 map.put(preference, (useDefault ? store.getDefaultBoolean(preference) : store.getBoolean(preference)) ? IPreferenceStore.TRUE : IPreferenceStore.FALSE);
152             } else if (isIntegerPreference(preference)) {
153                 map.put(preference, String.valueOf(useDefault ? store.getDefaultInt(preference) : store.getInt(preference)));
154             } else if (isStringPreference(preference)) {
155                 map.put(preference, useDefault ? store.getDefaultString(preference) : store.getString(preference));
156             } else if (isDoublePreference(preference)) {
157                 map.put(preference, String.valueOf(useDefault ? store.getDefaultDouble(preference) : store.getDouble(preference)));
158             } else if (isFloatPreference(preference)) {
159                 map.put(preference, String.valueOf(useDefault ? store.getDefaultFloat(preference) : store.getFloat(preference)));
160             } else if (isLongPreference(preference)) {
161                 map.put(preference, String.valueOf(useDefault ? store.getDefaultLong(preference) : store.getLong(preference)));
162             }
163         }
164     }
165 }
166
Popular Tags