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