1 /*******************************************************************************
2  * Copyright (c) 2006, 2008 IBM Corporation and others.
3  *
4  * This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * which accompanies this distribution, and is available at
7  * https://www.eclipse.org/legal/epl-2.0/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     IBM Corporation - initial API and implementation
13  *******************************************************************************/
14 package org.eclipse.ui.texteditor.rulers;
15 
16 import java.util.Set;
17 
18 import org.eclipse.core.runtime.Assert;
19 
20 import org.eclipse.jface.preference.IPreferenceStore;
21 
22 import org.eclipse.ui.internal.texteditor.rulers.StringSetSerializer;
23 
24 /**
25  * Manages the preferences for ruler contributions stored in a preference store.
26  *
27  * @since 3.3
28  */
29 public final class RulerColumnPreferenceAdapter {
30 	private final IPreferenceStore fStore;
31 	private final String fKey;
32 
33 	/**
34 	 * Creates a new preference adapter that will read and write under the specified key in the
35 	 * given preference store.
36 	 *
37 	 * @param store the preference store
38 	 * @param key the key
39 	 */
RulerColumnPreferenceAdapter(IPreferenceStore store, String key)40 	public  RulerColumnPreferenceAdapter(IPreferenceStore store, String key) {
41 		Assert.isLegal(store != null);
42 		Assert.isLegal(key != null);
43 		fStore= store;
44 		fKey= key;
45 	}
46 
47 	/**
48 	 * Returns the enablement state of the given ruler contribution.
49 	 *
50 	 * @param descriptor a ruler contribution descriptor
51 	 * @return <code>true</code> if the ruler is enabled, <code>false</code> otherwise
52 	 */
isEnabled(RulerColumnDescriptor descriptor)53 	public boolean isEnabled(RulerColumnDescriptor descriptor) {
54 		Assert.isLegal(descriptor != null);
55 		String preference= fStore.getString(fKey);
56 		return StringSetSerializer.deserialize(preference).contains(descriptor.getId()) ^ descriptor.getDefaultEnablement();
57 	}
58 
59 	/**
60 	 * Sets the enablement state of the given ruler contribution.
61 	 *
62 	 * @param descriptor a ruler contribution descriptor
63 	 * @param enabled <code>true</code> to enable the contribution, <code>false</code> to
64 	 *        disable it
65 	 */
setEnabled(RulerColumnDescriptor descriptor, boolean enabled)66 	public void setEnabled(RulerColumnDescriptor descriptor, boolean enabled) {
67 		Assert.isLegal(descriptor != null);
68 		String id= descriptor.getId();
69 		String preference= fStore.getString(fKey);
70 		Set<String> marked= StringSetSerializer.deserialize(preference);
71 		boolean shouldMark= enabled ^ descriptor.getDefaultEnablement();
72 		boolean isMarked= marked.contains(id);
73 		if (isMarked != shouldMark) {
74 			if (shouldMark)
75 				marked.add(id);
76 			else
77 				marked.remove(id);
78 			fStore.setValue(fKey, StringSetSerializer.serialize(marked));
79 		}
80 	}
81 
82 	/**
83 	 * Toggles the enablement state of given the ruler contribution.
84 	 *
85 	 * @param descriptor a ruler contribution descriptor
86 	 */
toggle(RulerColumnDescriptor descriptor)87 	public void toggle(RulerColumnDescriptor descriptor) {
88 		Assert.isLegal(descriptor != null);
89 		setEnabled(descriptor, !isEnabled(descriptor));
90 	}
91 }
92