1 11 12 package org.eclipse.ui.internal.commands; 13 14 import java.io.IOException ; 15 import java.io.Reader ; 16 import java.io.StringReader ; 17 import java.io.StringWriter ; 18 import java.io.Writer ; 19 import java.util.Collections ; 20 import java.util.List ; 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 35 public final class PreferenceCommandRegistry extends 36 AbstractMutableCommandRegistry { 37 38 41 private final static String KEY = Persistence.PACKAGE_FULL; 42 43 47 private IPreferenceStore preferenceStore; 48 49 56 public PreferenceCommandRegistry(IPreferenceStore preferenceStore) { 57 if (preferenceStore == null) throw new NullPointerException (); 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 e) { 69 e.printStackTrace(); 70 } 71 } 72 }); 73 74 try { 75 load(); 76 } catch (IOException eIO) { 77 } 79 } 80 81 88 public void load() throws IOException { 89 String preferenceString = preferenceStore.getString(KEY); 90 91 if (preferenceString != null && preferenceString.length() != 0) { 92 Reader reader = new StringReader (preferenceString); 93 94 try { 95 IMemento memento = XMLMemento.createReadRoot(reader); 96 List activeKeyConfigurationDefinitions = Collections 97 .unmodifiableList(Persistence 98 .readActiveKeyConfigurationDefinitions( 99 memento, 100 Persistence.TAG_ACTIVE_KEY_CONFIGURATION, 101 null)); 102 List categoryDefinitions = Collections 103 .unmodifiableList(Persistence.readCategoryDefinitions( 104 memento, Persistence.TAG_CATEGORY, null)); 105 List commandDefinitions = Collections 106 .unmodifiableList(Persistence.readCommandDefinitions( 107 memento, Persistence.TAG_COMMAND, null)); 108 List keyConfigurationDefinitions = Collections 109 .unmodifiableList(Persistence 110 .readKeyConfigurationDefinitions(memento, 111 Persistence.TAG_KEY_CONFIGURATION, null)); 112 List 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 (); 150 } finally { 151 reader.close(); 152 } 153 } 154 } 155 156 163 public void save() throws IOException { 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 writer = new StringWriter (); 178 179 try { 180 xmlMemento.save(writer); 181 preferenceStore.setValue(KEY, writer.toString()); 182 } finally { 183 writer.close(); 184 } 185 } 186 } 187 | Popular Tags |