KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > texteditor > rulers > RulerColumnPreferenceAdapter


1 /*******************************************************************************
2  * Copyright (c) 2006, 2007 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 package org.eclipse.ui.texteditor.rulers;
12
13 import java.util.Set JavaDoc;
14
15 import org.eclipse.core.runtime.Assert;
16
17 import org.eclipse.jface.preference.IPreferenceStore;
18 import org.eclipse.ui.internal.texteditor.rulers.StringSetSerializer;
19
20 /**
21  * Manages the preferences for ruler contributions stored in a preference store.
22  *
23  * @since 3.3
24  */

25 public final class RulerColumnPreferenceAdapter {
26     private final IPreferenceStore fStore;
27     private final String JavaDoc fKey;
28
29     /**
30      * Creates a new preference adapter that will read and write under the specified key in the
31      * given preference store.
32      *
33      * @param store the preference store
34      * @param key the key
35      */

36     public RulerColumnPreferenceAdapter(IPreferenceStore store, String JavaDoc 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 JavaDoc preference= fStore.getString(fKey);
52         return StringSetSerializer.deserialize(preference).contains(descriptor.getId()) ^ descriptor.getDefaultEnablement();
53     }
54     
55     /**
56      * Sets the enablement state of the given ruler contribution.
57      *
58      * @param descriptor a ruler contribution descriptor
59      * @param enabled <code>true</code> to enable the contribution, <code>false</code> to
60      * disable it
61      */

62     public void setEnabled(RulerColumnDescriptor descriptor, boolean enabled) {
63         Assert.isLegal(descriptor != null);
64         String JavaDoc id= descriptor.getId();
65         String JavaDoc preference= fStore.getString(fKey);
66         Set JavaDoc marked= StringSetSerializer.deserialize(preference);
67         boolean shouldMark= enabled ^ descriptor.getDefaultEnablement();
68         boolean isMarked= marked.contains(id);
69         if (isMarked != shouldMark) {
70             if (shouldMark)
71                 marked.add(id);
72             else
73                 marked.remove(id);
74             fStore.setValue(fKey, StringSetSerializer.serialize(marked));
75         }
76     }
77     
78     /**
79      * Toggles the enablement state of given the ruler contribution.
80      *
81      * @param descriptor a ruler contribution descriptor
82      */

83     public void toggle(RulerColumnDescriptor descriptor) {
84         Assert.isLegal(descriptor != null);
85         setEnabled(descriptor, !isEnabled(descriptor));
86     }
87 }
88
Popular Tags