KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > commands > PreferenceCommandRegistry


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.ui.internal.commands;
13
14 import java.io.IOException JavaDoc;
15 import java.io.Reader JavaDoc;
16 import java.io.StringReader JavaDoc;
17 import java.io.StringWriter JavaDoc;
18 import java.io.Writer JavaDoc;
19 import java.util.Collections JavaDoc;
20 import java.util.List JavaDoc;
21
22 import org.eclipse.jface.preference.IPreferenceStore;
23 import org.eclipse.jface.util.IPropertyChangeListener;
24 import org.eclipse.jface.util.PropertyChangeEvent;
25 import org.eclipse.ui.IMemento;
26 import org.eclipse.ui.WorkbenchException;
27 import org.eclipse.ui.XMLMemento;
28
29 /**
30  * The persistent store for the preferences related to the commands
31  * infrastructure. This store handles loading and storing of preferences.
32  *
33  * @since 3.0
34  */

35 public final class PreferenceCommandRegistry extends
36         AbstractMutableCommandRegistry {
37
38     /**
39      * The full package name for the store.
40      */

41     private final static String JavaDoc KEY = Persistence.PACKAGE_FULL;
42
43     /**
44      * The underlying preference store. This value will never be
45      * <code>null</code>.
46      */

47     private IPreferenceStore preferenceStore;
48
49     /**
50      * Constructs a new instance of <code>PreferenceCommandRegistry</code>
51      * with the preference store it is supposed to use.
52      *
53      * @param preferenceStore
54      * The preference store to use; must not be <code>null</code>.
55      */

56     public PreferenceCommandRegistry(IPreferenceStore preferenceStore) {
57         if (preferenceStore == null) throw new NullPointerException JavaDoc();
58
59         this.preferenceStore = preferenceStore;
60
61         this.preferenceStore
62                 .addPropertyChangeListener(new IPropertyChangeListener() {
63
64                     public void propertyChange(
65                             PropertyChangeEvent propertyChangeEvent) {
66                         if (KEY.equals(propertyChangeEvent.getProperty())) try {
67                             load();
68                         } catch (final IOException JavaDoc e) {
69                             e.printStackTrace();
70                         }
71                     }
72                 });
73
74         try {
75             load();
76         } catch (IOException JavaDoc eIO) {
77             // At least we tried....
78
}
79     }
80
81     /**
82      * Loads all of the preferences from the store, and sets member variables
83      * containing all of the values.
84      *
85      * @throws IOException
86      * If something happens while trying to read the store.
87      */

88     public void load() throws IOException JavaDoc {
89         String JavaDoc preferenceString = preferenceStore.getString(KEY);
90
91         if (preferenceString != null && preferenceString.length() != 0) {
92             Reader JavaDoc reader = new StringReader JavaDoc(preferenceString);
93
94             try {
95                 IMemento memento = XMLMemento.createReadRoot(reader);
96                 List JavaDoc activeKeyConfigurationDefinitions = Collections
97                         .unmodifiableList(Persistence
98                                 .readActiveKeyConfigurationDefinitions(
99                                         memento,
100                                         Persistence.TAG_ACTIVE_KEY_CONFIGURATION,
101                                         null));
102                 List JavaDoc categoryDefinitions = Collections
103                         .unmodifiableList(Persistence.readCategoryDefinitions(
104                                 memento, Persistence.TAG_CATEGORY, null));
105                 List JavaDoc commandDefinitions = Collections
106                         .unmodifiableList(Persistence.readCommandDefinitions(
107                                 memento, Persistence.TAG_COMMAND, null));
108                 List JavaDoc keyConfigurationDefinitions = Collections
109                         .unmodifiableList(Persistence
110                                 .readKeyConfigurationDefinitions(memento,
111                                         Persistence.TAG_KEY_CONFIGURATION, null));
112                 List JavaDoc keySequenceBindingDefinitions = Collections
113                         .unmodifiableList(Persistence
114                                 .readKeySequenceBindingDefinitions(memento,
115                                         Persistence.TAG_KEY_SEQUENCE_BINDING,
116                                         null));
117                 boolean commandRegistryChanged = false;
118
119                 if (!activeKeyConfigurationDefinitions
120                         .equals(this.activeKeyConfigurationDefinitions)) {
121                     this.activeKeyConfigurationDefinitions = activeKeyConfigurationDefinitions;
122                     commandRegistryChanged = true;
123                 }
124
125                 if (!categoryDefinitions.equals(this.categoryDefinitions)) {
126                     this.categoryDefinitions = categoryDefinitions;
127                     commandRegistryChanged = true;
128                 }
129
130                 if (!commandDefinitions.equals(this.commandDefinitions)) {
131                     this.commandDefinitions = commandDefinitions;
132                     commandRegistryChanged = true;
133                 }
134
135                 if (!keyConfigurationDefinitions
136                         .equals(this.keyConfigurationDefinitions)) {
137                     this.keyConfigurationDefinitions = keyConfigurationDefinitions;
138                     commandRegistryChanged = true;
139                 }
140
141                 if (!keySequenceBindingDefinitions
142                         .equals(this.keySequenceBindingDefinitions)) {
143                     this.keySequenceBindingDefinitions = keySequenceBindingDefinitions;
144                     commandRegistryChanged = true;
145                 }
146
147                 if (commandRegistryChanged) fireCommandRegistryChanged();
148             } catch (WorkbenchException eWorkbench) {
149                 throw new IOException JavaDoc();
150             } finally {
151                 reader.close();
152             }
153         }
154     }
155
156     /**
157      * Saves all of the preferences to the preference store.
158      *
159      * @throws IOException
160      * If something happens while trying to write to the preference
161      * store.
162      */

163     public void save() throws IOException JavaDoc {
164         XMLMemento xmlMemento = XMLMemento.createWriteRoot(KEY);
165         Persistence.writeActiveKeyConfigurationDefinitions(xmlMemento,
166                 Persistence.TAG_ACTIVE_KEY_CONFIGURATION,
167                 activeKeyConfigurationDefinitions);
168         Persistence.writeCategoryDefinitions(xmlMemento,
169                 Persistence.TAG_CATEGORY, categoryDefinitions);
170         Persistence.writeCommandDefinitions(xmlMemento,
171                 Persistence.TAG_COMMAND, commandDefinitions);
172         Persistence.writeKeyConfigurationDefinitions(xmlMemento,
173                 Persistence.TAG_KEY_CONFIGURATION, keyConfigurationDefinitions);
174         Persistence.writeKeySequenceBindingDefinitions(xmlMemento,
175                 Persistence.TAG_KEY_SEQUENCE_BINDING,
176                 keySequenceBindingDefinitions);
177         Writer JavaDoc writer = new StringWriter JavaDoc();
178
179         try {
180             xmlMemento.save(writer);
181             preferenceStore.setValue(KEY, writer.toString());
182         } finally {
183             writer.close();
184         }
185     }
186 }
187
Popular Tags