1 11 12 package org.eclipse.ui.internal.services; 13 14 import java.util.ArrayList ; 15 import java.util.Collection ; 16 import java.util.List ; 17 18 import org.eclipse.core.commands.Command; 19 import org.eclipse.core.commands.IParameter; 20 import org.eclipse.core.commands.Parameterization; 21 import org.eclipse.core.commands.ParameterizedCommand; 22 import org.eclipse.core.commands.common.NotDefinedException; 23 import org.eclipse.core.runtime.IStatus; 24 import org.eclipse.core.runtime.Status; 25 import org.eclipse.jface.preference.IPreferenceStore; 26 import org.eclipse.jface.util.IPropertyChangeListener; 27 import org.eclipse.jface.util.PropertyChangeEvent; 28 import org.eclipse.ui.IMemento; 29 import org.eclipse.ui.commands.ICommandService; 30 import org.eclipse.ui.internal.WorkbenchPlugin; 31 import org.eclipse.ui.internal.util.Util; 32 33 47 public abstract class PreferencePersistence extends RegistryPersistence { 48 49 65 protected static final void addElementToIndexedArray( 66 final IMemento elementToAdd, final IMemento[][] indexedArray, 67 final int index, final int currentCount) { 68 final IMemento[] elements; 69 if (currentCount == 0) { 70 elements = new IMemento[1]; 71 indexedArray[index] = elements; 72 } else { 73 if (currentCount >= indexedArray[index].length) { 74 final IMemento[] copy = new IMemento[indexedArray[index].length * 2]; 75 System.arraycopy(indexedArray[index], 0, copy, 0, currentCount); 76 elements = copy; 77 indexedArray[index] = elements; 78 } else { 79 elements = indexedArray[index]; 80 } 81 } 82 elements[currentCount] = elementToAdd; 83 } 84 85 94 protected static final void addWarning(final List warningsToLog, 95 final String message) { 96 addWarning(warningsToLog, message, null, null, null); 97 } 98 99 112 protected static final void addWarning(final List warningsToLog, 113 final String message, final String id) { 114 addWarning(warningsToLog, message, id, null, null); 115 } 116 117 136 protected static final void addWarning(final List warningsToLog, 137 final String message, final String id, 138 final String extraAttributeName, final String extraAttributeValue) { 139 String statusMessage = message; 140 if (id != null) { 141 statusMessage = statusMessage + ": id='" + id + '\''; } 143 if (extraAttributeName != null) { 144 if (id != null) { 145 statusMessage = statusMessage + ','; 146 } else { 147 statusMessage = statusMessage + ':'; 148 } 149 statusMessage = statusMessage + ' ' + extraAttributeName + "='" + extraAttributeValue + '\''; 151 } 152 153 final IStatus status = new Status(IStatus.WARNING, 154 WorkbenchPlugin.PI_WORKBENCH, 0, statusMessage, null); 155 warningsToLog.add(status); 156 } 157 158 170 protected static final boolean readBoolean(final IMemento memento, 171 final String attribute, final boolean defaultValue) { 172 final String value = memento.getString(attribute); 173 if (value == null) { 174 return defaultValue; 175 } 176 177 if (defaultValue) { 178 return !value.equalsIgnoreCase("false"); } 180 181 return !value.equalsIgnoreCase("true"); } 183 184 195 protected static final String readOptional(final IMemento memento, 196 final String attribute) { 197 String value = memento.getString(attribute); 198 if ((value != null) && (value.length() == 0)) { 199 value = null; 200 } 201 202 return value; 203 } 204 205 229 protected static final ParameterizedCommand readParameterizedCommand( 230 final IMemento memento, final ICommandService commandService, 231 final List warningsToLog, final String message, final String id) { 232 final String commandId = readRequired(memento, ATT_COMMAND_ID, 233 warningsToLog, message, id); 234 if (commandId == null) { 235 return null; 236 } 237 238 final Command command = commandService.getCommand(commandId); 239 final ParameterizedCommand parameterizedCommand = readParameters( 240 memento, warningsToLog, command); 241 242 return parameterizedCommand; 243 } 244 245 264 protected static final ParameterizedCommand readParameters( 265 final IMemento memento, final List warningsToLog, 266 final Command command) { 267 final IMemento[] parameterMementos = memento 268 .getChildren(TAG_PARAMETER); 269 if ((parameterMementos == null) || (parameterMementos.length == 0)) { 270 return new ParameterizedCommand(command, null); 271 } 272 273 final Collection parameters = new ArrayList (); 274 for (int i = 0; i < parameterMementos.length; i++) { 275 final IMemento parameterMemento = parameterMementos[i]; 276 277 final String id = parameterMemento.getString(ATT_ID); 279 if ((id == null) || (id.length() == 0)) { 280 addWarning(warningsToLog, "Parameters need a name"); continue; 283 } 284 285 IParameter parameter = null; 287 try { 288 final IParameter[] commandParameters = command.getParameters(); 289 if (parameters != null) { 290 for (int j = 0; j < commandParameters.length; j++) { 291 final IParameter currentParameter = commandParameters[j]; 292 if (Util.equals(currentParameter.getId(), id)) { 293 parameter = currentParameter; 294 break; 295 } 296 } 297 298 } 299 } catch (final NotDefinedException e) { 300 } 302 if (parameter == null) { 303 addWarning(warningsToLog, 305 "Could not find a matching parameter", id); continue; 307 } 308 309 final String value = parameterMemento.getString(ATT_VALUE); 311 if ((value == null) || (value.length() == 0)) { 312 addWarning(warningsToLog, "Parameters need a value", id); continue; 315 } 316 317 parameters.add(new Parameterization(parameter, value)); 318 } 319 320 if (parameters.isEmpty()) { 321 return new ParameterizedCommand(command, null); 322 } 323 324 return new ParameterizedCommand(command, 325 (Parameterization[]) parameters 326 .toArray(new Parameterization[parameters.size()])); 327 } 328 329 343 protected static final String readRequired(final IMemento memento, 344 final String attribute, final List warningsToLog, 345 final String message) { 346 return readRequired(memento, attribute, warningsToLog, message, null); 347 } 348 349 367 protected static final String readRequired(final IMemento memento, 368 final String attribute, final List warningsToLog, 369 final String message, final String id) { 370 final String value = memento.getString(attribute); 371 if ((value == null) || (value.length() == 0)) { 372 addWarning(warningsToLog, message, id); 373 return null; 374 } 375 376 return value; 377 } 378 379 383 protected boolean preferenceListenerAttached = false; 384 385 388 private final IPropertyChangeListener preferenceChangeListener; 389 390 393 public final void dispose() { 394 super.dispose(); 395 396 final IPreferenceStore store = WorkbenchPlugin.getDefault() 397 .getPreferenceStore(); 398 store.removePropertyChangeListener(preferenceChangeListener); 399 } 400 401 410 protected abstract boolean isChangeImportant(final PropertyChangeEvent event); 411 412 416 protected void read() { 417 super.read(); 418 419 if (!preferenceListenerAttached) { 420 final IPreferenceStore store = WorkbenchPlugin.getDefault() 421 .getPreferenceStore(); 422 store.addPropertyChangeListener(preferenceChangeListener); 423 } 424 } 425 426 430 protected PreferencePersistence() { 431 super(); 432 433 preferenceChangeListener = new IPropertyChangeListener() { 434 public final void propertyChange(final PropertyChangeEvent event) { 435 if (isChangeImportant(event)) { 436 read(); 437 } 438 } 439 }; 440 } 441 } 442 | Popular Tags |