private void saveState() {
    XMLMemento memento = XMLMemento.createWriteRoot("map");
    for (Iterator i = map.entrySet().iterator(); i.hasNext(); ) {
      Map.Entry entry = (Map.Entry) i.next();
      String workingSetId = (String) entry.getKey();
      Record record = (Record) entry.getValue();
      IPersistableElement element =
          (IPersistableElement) record.root.getAdapter(IPersistableElement.class);
      if (element == null) {
        StatusManager.getManager()
            .handle(
                new Status(
                    IStatus.ERROR,
                    Activator.PLUGIN_ID,
                    "could not persist root element for dynamic resource working set.  No persistable element adapter found."));
      }

      IMemento child = memento.createChild("workingSet", workingSetId);
      child.putString("pattern", record.pattern.pattern());

      IMemento rootItem = memento.createChild("root");
      rootItem.putString("factoryID", element.getFactoryId());
      element.saveState(rootItem);
    }
    StringWriter result = new StringWriter();
    try {
      memento.save(result);
      Activator.getInstance().getPreferenceStore().setValue("expressionMemento", result.toString());
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
Exemple #2
0
  /**
   * Saves the object state in the given memento.
   *
   * @param memento the memento to save the object state in
   */
  public IStatus saveState(IMemento memento) {
    if (!isRestored()) {
      memento.putMemento(this.memento);
    } else if (input != null) {

      IPersistableElement persistable = input.getPersistable();
      if (persistable != null) {
        /*
         * Store IPersistable of the IEditorInput in a separate section
         * since it could potentially use a tag already used in the parent
         * memento and thus overwrite data.
         */
        IMemento persistableMemento = memento.createChild(IWorkbenchConstants.TAG_PERSISTABLE);
        persistable.saveState(persistableMemento);
        memento.putString(IWorkbenchConstants.TAG_FACTORY_ID, persistable.getFactoryId());
        if (descriptor != null && descriptor.getId() != null) {
          memento.putString(IWorkbenchConstants.TAG_ID, descriptor.getId());
        }
        // save the name and tooltip separately so they can be restored
        // without having to instantiate the input, which can activate plugins
        memento.putString(IWorkbenchConstants.TAG_NAME, input.getName());
        memento.putString(IWorkbenchConstants.TAG_TOOLTIP, input.getToolTipText());
      }
    }
    return Status.OK_STATUS;
  }
Exemple #3
0
  /**
   * Implements IPersistableElement. Persist the working set name and working set contents. The
   * contents has to be either IPersistableElements or provide adapters for it to be persistent.
   *
   * @see org.eclipse.ui.IPersistableElement#saveState(IMemento)
   */
  public void saveState(IMemento memento) {
    if (workingSetMemento != null) {
      // just re-save the previous memento if the working set has
      // not been restored
      memento.putMemento(workingSetMemento);
    } else {
      memento.putString(IWorkbenchConstants.TAG_NAME, getName());
      memento.putString(IWorkbenchConstants.TAG_LABEL, getLabel());
      memento.putString(IWorkbenchConstants.TAG_ID, getUniqueId());
      memento.putString(IWorkbenchConstants.TAG_EDIT_PAGE_ID, editPageId);
      Iterator iterator = elements.iterator();
      while (iterator.hasNext()) {
        IAdaptable adaptable = (IAdaptable) iterator.next();
        final IPersistableElement persistable =
            (IPersistableElement) Util.getAdapter(adaptable, IPersistableElement.class);
        if (persistable != null) {
          final IMemento itemMemento = memento.createChild(IWorkbenchConstants.TAG_ITEM);

          itemMemento.putString(IWorkbenchConstants.TAG_FACTORY_ID, persistable.getFactoryId());
          SafeRunner.run(
              new SafeRunnable(
                  "Problems occurred while saving persistable item state") { //$NON-NLS-1$

                public void run() throws Exception {
                  persistable.saveState(itemMemento);
                }
              });
        }
      }
    }
  }
  public String getFactoryId() {
    IEditorPart editor = getEditor(false);
    if (editor == null) {
      if (input == null) {
        String memento = getModel().getPersistedState().get(MEMENTO_KEY);
        if (memento != null) {
          try {
            XMLMemento createReadRoot = XMLMemento.createReadRoot(new StringReader(memento));
            IMemento inputMem = createReadRoot.getChild(IWorkbenchConstants.TAG_INPUT);
            if (inputMem != null) {
              return inputMem.getString(IWorkbenchConstants.TAG_FACTORY_ID);
            }
          } catch (WorkbenchException e) {
            return null;
          }
        }
        return null;
      }

      IPersistableElement persistable = input.getPersistable();
      return persistable == null ? null : persistable.getFactoryId();
    }

    IPersistableElement persistable = editor.getEditorInput().getPersistable();
    return persistable == null ? null : persistable.getFactoryId();
  }
Exemple #5
0
 /**
  * Returns the factory id of this item, either from the input if restored, otherwise from the
  * memento. Returns <code>null</code> if there is no factory id.
  */
 public String getFactoryId() {
   if (isRestored()) {
     if (input != null) {
       IPersistableElement persistable = input.getPersistable();
       if (persistable != null) {
         return persistable.getFactoryId();
       }
     }
   } else if (memento != null) {
     return memento.getString(IWorkbenchConstants.TAG_FACTORY_ID);
   }
   return null;
 }
Exemple #6
0
 /** Returns whether this item matches the given editor input. */
 public boolean matches(IEditorInput input) {
   if (isRestored()) {
     return input.equals(getInput());
   }
   // if not restored, compare name, tool tip text and factory id,
   // avoiding as much work as possible
   if (!getName().equals(input.getName())) {
     return false;
   }
   if (!getToolTipText().equals(input.getToolTipText())) {
     return false;
   }
   IPersistableElement persistable = input.getPersistable();
   String inputId = persistable == null ? null : persistable.getFactoryId();
   String myId = getFactoryId();
   return myId == null ? inputId == null : myId.equals(inputId);
 }
  IMemento getEditorState() {
    IEditorPart editor = getEditor(false);

    // If the editor hasn't been rendered yet then see if we can grab the
    // info from the model
    if (editor == null && getModel() != null) {
      String savedState = getModel().getPersistedState().get(MEMENTO_KEY);
      if (savedState != null) {
        StringReader sr = new StringReader(savedState);
        try {
          XMLMemento memento = XMLMemento.createReadRoot(sr);
          return memento;
        } catch (WorkbenchException e) {
          WorkbenchPlugin.log(e);
          return null;
        }
      }
      return null;
    }

    IEditorInput input = editor.getEditorInput();
    if (input == null) {
      return null;
    }

    IPersistableElement persistable = input.getPersistable();
    if (persistable == null) {
      return null;
    }

    XMLMemento root = XMLMemento.createWriteRoot(IWorkbenchConstants.TAG_EDITOR);
    root.putString(IWorkbenchConstants.TAG_ID, descriptor.getId());

    IMemento inputMem = root.createChild(IWorkbenchConstants.TAG_INPUT);
    inputMem.putString(IWorkbenchConstants.TAG_FACTORY_ID, persistable.getFactoryId());
    persistable.saveState(inputMem);

    if (editor instanceof IPersistableEditor) {
      IMemento editorStateMem = root.createChild(IWorkbenchConstants.TAG_EDITOR_STATE);
      ((IPersistableEditor) editor).saveState(editorStateMem);
    }

    return root;
  }
  IMemento getEditorState() {
    IEditorPart editor = getEditor(false);

    // If the editor hasn't been rendered yet then see if we can grab the
    // info from the model
    if (editor == null && getModel() != null) {
      String savedState = getModel().getPersistedState().get(MEMENTO_KEY);
      if (savedState != null) {
        StringReader sr = new StringReader(savedState);
        try {
          XMLMemento memento = XMLMemento.createReadRoot(sr);
          return memento;
        } catch (WorkbenchException e) {
          WorkbenchPlugin.log(e);
          return null;
        }
      }
      return null;
    }

    IEditorInput input = editor.getEditorInput();
    if (input == null) {
      return null;
    }

    IPersistableElement persistable = input.getPersistable();
    if (persistable == null) {
      return null;
    }

    XMLMemento editorMem = XMLMemento.createWriteRoot(IWorkbenchConstants.TAG_EDITOR);
    editorMem.putString(IWorkbenchConstants.TAG_ID, descriptor.getId());
    editorMem.putString(IWorkbenchConstants.TAG_TITLE, getTitle());
    editorMem.putString(IWorkbenchConstants.TAG_NAME, getName());
    editorMem.putString(IWorkbenchConstants.TAG_ID, getId());
    editorMem.putString(IWorkbenchConstants.TAG_TOOLTIP, getTitleToolTip());
    editorMem.putString(IWorkbenchConstants.TAG_PART_NAME, getPartName());

    if (editor instanceof IWorkbenchPart3) {
      Map properties = ((IWorkbenchPart3) editor).getPartProperties();
      if (!properties.isEmpty()) {
        IMemento propBag = editorMem.createChild(IWorkbenchConstants.TAG_PROPERTIES);
        Iterator i = properties.entrySet().iterator();
        while (i.hasNext()) {
          Map.Entry entry = (Map.Entry) i.next();
          IMemento p =
              propBag.createChild(IWorkbenchConstants.TAG_PROPERTY, (String) entry.getKey());
          p.putTextData((String) entry.getValue());
        }
      }
    }

    if (isPinned()) {
      editorMem.putString(IWorkbenchConstants.TAG_PINNED, "true"); // $NON-NLS-1$
    }

    IMemento inputMem = editorMem.createChild(IWorkbenchConstants.TAG_INPUT);
    inputMem.putString(IWorkbenchConstants.TAG_FACTORY_ID, persistable.getFactoryId());
    persistable.saveState(inputMem);

    if (editor instanceof IPersistableEditor) {
      IMemento editorStateMem = editorMem.createChild(IWorkbenchConstants.TAG_EDITOR_STATE);
      ((IPersistableEditor) editor).saveState(editorStateMem);
    }

    return editorMem;
  }