/**
   * Deletes a local palette definition
   *
   * @param id the id of the palette to delete
   */
  public static void deleteLocalPalette(String id) {
    // retrieves memento
    XMLMemento rootMemento = getExistingLocalPalettes();

    // search existing customization
    IMemento paletteMemento = searchPaletteMemento(rootMemento, id);

    if (paletteMemento == null) {
      PapyrusTrace.log(IStatus.WARNING, "impossible to find the palette with id: " + id);
      return;
    }

    // no remove method...
    // so, creation of a new root memento, then, duplicate all entries
    // except the one to
    // delete...

    XMLMemento newRootMemento = XMLMemento.createWriteRoot(PALETTE_LOCAL_DEFINITIONS);
    for (IMemento memento : rootMemento.getChildren(PALETTE)) {
      if (!memento.getString(ID).equals(paletteMemento.getString(ID))) {
        IMemento newChild = newRootMemento.createChild(PALETTE);
        newChild.putMemento(memento);
      }
    }
    // save new Memento
    saveLocalPalettes(newRootMemento);
  }
 /**
  * search the palette memento for the given palette ID.
  *
  * @param rootMemento the root memento to look in
  * @param paletteID the palette ID to search
  * @return the palette memento or <code>null</code> if none was found.
  */
 protected static IMemento searchPaletteMemento(XMLMemento rootMemento, String paletteID) {
   for (IMemento memento : rootMemento.getChildren(PALETTE)) {
     String id = memento.getString(ID);
     if (paletteID.equals(id)) {
       return memento;
     }
   }
   // palette not found
   return null;
 }
 /**
  * Returns the memento associated to the palette, or <code>null</code> if none exists
  *
  * @param paletteID the identifier of the palette to find
  * @return the memento found or <code>null</code> if no customization exists for this palette
  */
 private static IMemento getPaletteRedefinitionNode(String paletteID) {
   XMLMemento rootMemento = getLocalRedefinitions();
   IMemento[] redefinitions = rootMemento.getChildren(PALETTE_REDEFINITION);
   for (IMemento redefinitionMemento : redefinitions) {
     String paletteNodeID = redefinitionMemento.getString(ID);
     // check equals. Palette ID is not null, as checked at the begining
     // of the method.
     if (paletteID.equals(paletteNodeID)) {
       return redefinitionMemento;
     }
   }
   return null;
 }
  /**
   * Retrieves the memento for the current editor from the root memento
   *
   * @param rootMemento the root memento from which the editor memento is retrieved
   * @param currentEditorClass the current editor class name
   * @return the memento for the current editor from the root memento or <code>null</code>;
   */
  protected static IMemento getEditorMemento(XMLMemento rootMemento, String currentEditorClass) {
    IMemento[] editorsMementos = rootMemento.getChildren(EDITOR);
    for (IMemento editorMemento : editorsMementos) {
      String editorClass = editorMemento.getString(CLASS);
      if (currentEditorClass.equals(editorClass)) {
        return editorMemento;
      }
    }

    // create one if none was found
    IMemento memento = rootMemento.createChild(EDITOR);
    memento.putString(CLASS, currentEditorClass);
    return memento;
  }
 /**
  * Unregister a specific local redefinition
  *
  * @param paletteID the identifier of the palette to unregister
  */
 public static void unregisterLocalRedefinition(String paletteID) {
   XMLMemento rootMemento = getLocalRedefinitions();
   // no remove method...
   // so, creation of a new root memento, then, duplicate all entries
   // except the one to
   // delete...
   XMLMemento newRootMemento = XMLMemento.createWriteRoot(PALETTE_REDEFINITIONS);
   for (IMemento memento : rootMemento.getChildren(PALETTE_REDEFINITION)) {
     if (!memento.getString(ID).equals(paletteID)) {
       newRootMemento.putMemento(memento);
     }
   }
   // save new Memento
   saveLocalRedefinitions(newRootMemento);
 }
  /**
   * Returns local palettes description contained in the preference field.
   *
   * @return the list of local palette description
   */
  public static List<IPaletteDescription> getLocalPalettes() {
    ArrayList<IPaletteDescription> paletteDescriptions = new ArrayList<IPaletteDescription>();

    // retrieve the value of the preference field
    XMLMemento rootMemento = getExistingLocalPalettes();

    if (rootMemento == null) {
      return paletteDescriptions;
    }

    // retrieve all palette descriptions
    for (IMemento memento : rootMemento.getChildren(PALETTE)) {
      // there should be a factory here ?!
      IPaletteDescription description = PapyrusPaletteDescription.create(memento);

      paletteDescriptions.add(description);
    }

    return paletteDescriptions;
  }
  private void restoreState() {
    String mementoString =
        Activator.getInstance().getPreferenceStore().getString("expressionMemento");
    if (mementoString == null || "".equals(mementoString)) return;
    StringReader mementoReader = new StringReader(mementoString);
    try {
      XMLMemento memento = XMLMemento.createReadRoot(mementoReader);
      IMemento[] workingSets = memento.getChildren("workingSet");
      for (int i = 0; i < workingSets.length; i++) {
        IMemento workingSetMemento = workingSets[i];
        String workingSetId = workingSetMemento.getID();
        if (workingSetId == null || "".equals(workingSetId)) {
          StatusManager.getManager()
              .handle(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "missing working set id"));
          continue;
        }
        String patternString = workingSetMemento.getString("pattern");
        if (patternString == null || "".equals(patternString)) {
          StatusManager.getManager()
              .handle(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "missing pattern"));
          continue;
        }

        Pattern pattern = null;
        try {
          pattern = Pattern.compile(patternString);
        } catch (PatternSyntaxException e) {
          StatusManager.getManager()
              .handle(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "bad pattern", e));
          continue;
        }

        IMemento rootMemento = memento.getChild("root");
        if (rootMemento == null) {
          StatusManager.getManager()
              .handle(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "missing root memento"));
          continue;
        }

        String factoryId = rootMemento.getString("factoryID");
        if (factoryId == null || "".equals(factoryId)) {
          StatusManager.getManager()
              .handle(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "missing root memento"));
          continue;
        }

        IElementFactory factory = PlatformUI.getWorkbench().getElementFactory(factoryId);
        if (factory == null) {
          StatusManager.getManager()
              .handle(
                  new Status(
                      IStatus.ERROR,
                      Activator.PLUGIN_ID,
                      "cannot create element factory: " + factoryId));
          continue;
        }

        IAdaptable item = factory.createElement(rootMemento);
        if (item == null) {
          StatusManager.getManager()
              .handle(
                  new Status(
                      IStatus.ERROR,
                      Activator.PLUGIN_ID,
                      "cannot create element from factory: " + factoryId));
          continue;
        }

        Record record = new Record();
        record.pattern = pattern;
        record.root = item;
        map.put(workingSetId, record);
      }
    } catch (WorkbenchException e) {
      StatusManager.getManager()
          .handle(
              new Status(
                  IStatus.ERROR, Activator.PLUGIN_ID, "problem restoring working set states", e));
    }
  }