Skip to main content
summaryrefslogtreecommitdiffstats
blob: adce03929477d3de547799b55f76d02b0a70092a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*******************************************************************************
 * Copyright (c) 2000, 2015 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

package org.eclipse.jface.text.formatter;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.jface.preference.IPreferenceStore;

/**
 * Default implementation of <code>IFormattingContext</code>.
 *
 * @since 3.0
 */
public class FormattingContext implements IFormattingContext {

	/** Map to store the properties */
	private final Map<Object, Object> fMap= new HashMap<>();

	@Override
	public void dispose() {
		fMap.clear();
	}

	@Override
	public String[] getPreferenceKeys() {
		return new String[] {
		};
	}

	@Override
	public Object getProperty(Object key) {
		return fMap.get(key);
	}

	@Override
	public boolean isBooleanPreference(String key) {
		return false;
	}

	@Override
	public boolean isDoublePreference(String key) {
		return false;
	}

	@Override
	public boolean isFloatPreference(String key) {
		return false;
	}

	@Override
	public boolean isIntegerPreference(String key) {
		return false;
	}

	@Override
	public boolean isLongPreference(String key) {
		return false;
	}

	@Override
	public boolean isStringPreference(String key) {
		return false;
	}

	@Override
	public void mapToStore(Map<Object, Object> map, IPreferenceStore store) {

		final String[] preferences= getPreferenceKeys();

		String result= null;
		String preference= null;

		for (int index= 0; index < preferences.length; index++) {

			preference= preferences[index];
			result= (String)map.get(preference);

			if (result != null) {

				try {
					if (isBooleanPreference(preference)) {
						store.setValue(preference, result.equals(IPreferenceStore.TRUE));
					} else if (isIntegerPreference(preference)) {
						store.setValue(preference, Integer.parseInt(result));
					} else if (isStringPreference(preference)) {
						store.setValue(preference, result);
					} else if (isDoublePreference(preference)) {
						store.setValue(preference, Double.parseDouble(result));
					} else if (isFloatPreference(preference)) {
						store.setValue(preference, Float.parseFloat(result));
					} else if (isLongPreference(preference)) {
						store.setValue(preference, Long.parseLong(result));
					}
				} catch (NumberFormatException exception) {
					// Do nothing
				}
			}
		}
	}

	@Override
	public void setProperty(Object key, Object property) {
		fMap.put(key, property);
	}

	@Override
	public void storeToMap(IPreferenceStore store, Map<Object, Object> map, boolean useDefault) {

		final String[] preferences= getPreferenceKeys();

		String preference= null;
		for (int index= 0; index < preferences.length; index++) {

			preference= preferences[index];

			if (isBooleanPreference(preference)) {
				map.put(preference, (useDefault ? store.getDefaultBoolean(preference) : store.getBoolean(preference)) ? IPreferenceStore.TRUE : IPreferenceStore.FALSE);
			} else if (isIntegerPreference(preference)) {
				map.put(preference, String.valueOf(useDefault ? store.getDefaultInt(preference) : store.getInt(preference)));
			} else if (isStringPreference(preference)) {
				map.put(preference, useDefault ? store.getDefaultString(preference) : store.getString(preference));
			} else if (isDoublePreference(preference)) {
				map.put(preference, String.valueOf(useDefault ? store.getDefaultDouble(preference) : store.getDouble(preference)));
			} else if (isFloatPreference(preference)) {
				map.put(preference, String.valueOf(useDefault ? store.getDefaultFloat(preference) : store.getFloat(preference)));
			} else if (isLongPreference(preference)) {
				map.put(preference, String.valueOf(useDefault ? store.getDefaultLong(preference) : store.getLong(preference)));
			}
		}
	}
}

Back to the top