Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 643184ac86adf677ffd5d2a240d97a8cd459898a (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
/*******************************************************************************
 * Copyright (c) 2011 Florian Thienel 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:
 * 		Florian Thienel - initial API and implementation
 *******************************************************************************/
package org.eclipse.vex.ui.internal;

import java.util.Arrays;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.preferences.InstanceScope;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.vex.core.internal.css.IStyleSheetProvider;
import org.eclipse.vex.core.internal.css.StyleSheet;
import org.eclipse.vex.core.provisional.dom.DocumentContentModel;
import org.eclipse.vex.ui.internal.config.ConfigurationRegistry;
import org.eclipse.vex.ui.internal.config.DocumentType;
import org.eclipse.vex.ui.internal.config.Style;
import org.eclipse.vex.ui.internal.editor.VexDocumentContentModel;
import org.osgi.service.prefs.BackingStoreException;
import org.osgi.service.prefs.Preferences;

/**
 * @author Florian Thienel
 */
public class VexPreferences implements IStyleSheetProvider {

	public static final String INDENTATION_CHAR_CHOICE = "indetationCharChoice";

	public static final String INDENTATION_CHAR_TAB = "\t";

	public static final String INDENTATION_CHAR_SPACE = " ";

	public static final String INDENTATION_SIZE = "indetationSize";

	public static final String LINE_WIDTH = "lineWidth";

	private static final String PREFERRED_STYLE_SUFFIX = ".style";

	private final IPreferenceStore preferenceStore;

	private final ConfigurationRegistry configurationRegistry;

	public VexPreferences(final IPreferenceStore preferenceStore, final ConfigurationRegistry configurationRegistry) {
		this.preferenceStore = preferenceStore;
		this.configurationRegistry = configurationRegistry;
	}

	public void setPreferredStyleId(final DocumentType doctype, final String styleId) {
		final Preferences preferences = InstanceScope.INSTANCE.getNode(VexPlugin.ID);
		final String key = getStylePreferenceKey(doctype.getUniqueId());
		preferences.put(key, styleId);
		try {
			preferences.flush();
		} catch (final BackingStoreException e) {
			VexPlugin.getDefault().log(IStatus.ERROR, Messages.getString("VexEditor.errorSavingStylePreference"), e); //$NON-NLS-1$
		}
	}

	public String getPreferredStyleId(final DocumentType doctype) {
		final Preferences preferences = InstanceScope.INSTANCE.getNode(VexPlugin.ID);
		final String preferredStyleId = preferences.get(getStylePreferenceKey(doctype.getUniqueId()), null);
		return preferredStyleId;
	}

	private static String getStylePreferenceKey(final String publicId) {
		return publicId + PREFERRED_STYLE_SUFFIX;
	}

	public Style getPreferredStyle(final DocumentType doctype) {
		return configurationRegistry.getStyle(doctype, getPreferredStyleId(doctype));
	}

	public String getIndentationPattern() {
		final String indentationChar = preferenceStore.getString(INDENTATION_CHAR_CHOICE);
		final int indentationSize = preferenceStore.getInt(INDENTATION_SIZE);
		final char[] pattern = new char[indentationSize];
		Arrays.fill(pattern, indentationChar.charAt(0));
		return new String(pattern);
	}

	public int getLineWidth() {
		return preferenceStore.getInt(LINE_WIDTH);
	}

	@Override
	public StyleSheet getStyleSheet(final DocumentContentModel documentContentModel) {
		Style style = null;
		if (documentContentModel instanceof VexDocumentContentModel) {
			// We only support the VexDocumentContentModel
			// If there will ever be another DocumentContentModel implememntation, it will
			// have to define it's own method for resolving styles
			style = getPreferredStyle(((VexDocumentContentModel) documentContentModel).getDocumentType());
		}
		if (style == null) {
			return StyleSheet.NULL;
		}
		return style.getStyleSheet();
	}
}

Back to the top