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();
  }
Пример #2
0
  /**
   * 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);
  }
Пример #3
0
 /**
  * Saves the code folding state to a XML file in the state location.
  *
  * @param uriString the key to determine the file to save to
  */
 public void saveCodeFoldingStateFile(String uriString) {
   org.eclipse.jface.text.IDocument document = sourceViewer.getDocument();
   if (document == null) {
     return;
   }
   org.eclipse.ui.XMLMemento codeFoldingMemento = org.eclipse.ui.XMLMemento.createWriteRoot(MODEL);
   codeFoldingMemento.putString(VERIFY_KEY, makeMD5(document.get()));
   saveCodeFolding(codeFoldingMemento);
   java.io.File stateFile = getCodeFoldingStateFile(uriString);
   if (stateFile == null) {
     return;
   }
   try {
     java.io.FileOutputStream stream = new java.io.FileOutputStream(stateFile);
     java.io.OutputStreamWriter writer = new java.io.OutputStreamWriter(stream, "utf-8");
     codeFoldingMemento.save(writer);
     writer.close();
   } catch (java.io.IOException e) {
     stateFile.delete();
     org.eclipse.jface.dialogs.MessageDialog.openError(
         (org.eclipse.swt.widgets.Shell) null,
         "Saving Problems",
         "Unable to save code folding state.");
   }
 }
  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();
    }
  }
Пример #5
0
 /**
  * Reads an object from a {@link IMemento}. The memento's type (root) must equal the bean's {@link
  * Root} annotation name attribute.
  */
 static <T> T fromMemento(Class<T> clazz, IMemento memento) throws IOException {
   try {
     final StringWriter sw = new StringWriter();
     final XMLMemento m = XMLMemento.createWriteRoot(memento.getType());
     m.putMemento(memento);
     m.save(sw);
     return new Persister().read(clazz, new StringReader(sw.toString()));
   } catch (Exception e) {
     throw ExceptionUtils.wrapAs(IOException.class, e);
   }
 }
 protected String createViewMemento(IIssue issue) {
   StringWriter out = new StringWriter();
   XMLMemento memento = XMLMemento.createWriteRoot("root"); // $NON-NLS-1$
   issue.getViewMemento(memento);
   try {
     memento.save(out);
   } catch (IOException e) {
     IssuesActivator.log("", e); // $NON-NLS-1$
   }
   return out.toString();
 }
Пример #7
0
 /**
  * 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;
 }
Пример #8
0
  /**
   * Save monitors to persistent storage. Just saves the monitor metadata. The actual monitor
   * information will be saved when the monitor is created.
   */
  private void saveMonitors() {
    final XMLMemento memento = XMLMemento.createWriteRoot(MONITORS_ATTR);
    for (IMonitorControl monitor : fMonitorControls.values()) {
      memento.createChild(MONITOR_ID_ATTR, monitor.getId());
    }

    try {
      FileWriter writer = new FileWriter(getSaveLocation());
      memento.save(writer);
    } catch (final IOException e) {
      LMLMonitorCorePlugin.log(e.getLocalizedMessage());
    }
  }
Пример #9
0
 /**
  * Retrieves the root memento from the plugin preferences if there were existing palette
  * customizations.
  *
  * @return the root memento if there were existing customizations; null otherwise
  */
 protected static XMLMemento getExistingCustomizations() {
   String sValue = getPreferenceStore().getString(PALETTE_CUSTOMIZATIONS_ID);
   try {
     if (sValue != null && !sValue.equals("")) { // $NON-NLS-1$
       XMLMemento rootMemento = XMLMemento.createReadRoot(new StringReader(sValue));
       return rootMemento;
     } else {
       return XMLMemento.createWriteRoot(PALETTE_CUSTOMIZATIONS_ID);
     }
   } catch (WorkbenchException e) {
     Activator.getDefault().logError("Impossible to read preferences", e);
   }
   return null;
 }
Пример #10
0
  /**
   * 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;
  }
Пример #11
0
 /**
  * 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);
 }
Пример #12
0
 /**
  * Retrieves the root memento from the plugin preferences if there were existing local palette
  * redefinitions.
  *
  * @return the root memento if there were existing customizations, a newly created one otherwise
  *     (empty one)
  */
 protected static XMLMemento getLocalRedefinitions() {
   String sValue = getPreferenceStore().getString(PALETTE_REDEFINITIONS);
   try {
     if (sValue != null && !sValue.equals("")) { // $NON-NLS-1$
       XMLMemento rootMemento = XMLMemento.createReadRoot(new StringReader(sValue));
       return rootMemento;
     } else {
       return XMLMemento.createWriteRoot(PALETTE_REDEFINITIONS);
     }
   } catch (WorkbenchException e) {
     Activator.getDefault()
         .logError("Impossible to read preferences for palette local redefinitions", e);
   }
   return null;
 }
  boolean persist() {
    XMLMemento persistedState = (XMLMemento) getEditorState();
    if (persistedState == null) return false;

    StringWriter writer = new StringWriter();
    try {
      persistedState.save(writer);
      getModel().getPersistedState().put(MEMENTO_KEY, writer.toString());
    } catch (IOException e) {
      WorkbenchPlugin.log(e);
      return false;
    }

    return true;
  }
Пример #14
0
  /** Convert any {@link IMemento} to a string. */
  public static String toString(IMemento memento) {
    if (!(memento instanceof XMLMemento)) {
      XMLMemento m = XMLMemento.createWriteRoot(memento.getType());
      m.putMemento(memento);
      memento = m;
    }

    try {
      final StringWriter w = new StringWriter();
      ((XMLMemento) memento).save(w);
      return w.toString();
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }
Пример #15
0
 private void openUnclosedMapLastSession(File statusFile, final IWorkbenchPage page)
     throws FileNotFoundException, UnsupportedEncodingException, WorkbenchException, CoreException,
         PartInitException {
   FileInputStream input = new FileInputStream(statusFile);
   BufferedReader reader =
       new BufferedReader(new InputStreamReader(input, "utf-8")); // $NON-NLS-1$
   IMemento memento = XMLMemento.createReadRoot(reader);
   IMemento childMem = memento.getChild(IWorkbenchConstants.TAG_EDITORS);
   //        ((WorkbenchPage) page).getEditorManager().restoreState(childMem);
   IMemento[] childrenEditor = childMem.getChildren("editor"); // $NON-NLS-1$
   IEditorPart activeEditorPart = null;
   for (IMemento childEditor : childrenEditor) {
     IMemento childInput = childEditor.getChild("input"); // $NON-NLS-1$
     String path = childInput.getString("path"); // $NON-NLS-1$
     if (path != null) {
       IEditorInput editorInput = MME.createFileEditorInput(path);
       IEditorPart editorPart = page.openEditor(editorInput, MindMapUI.MINDMAP_EDITOR_ID);
       if ("true".equals(childEditor.getString("activePart"))) { // $NON-NLS-1$ //$NON-NLS-2$
         activeEditorPart = editorPart;
       }
     }
   }
   if (activeEditorPart != null) {
     page.activate(activeEditorPart);
   }
 }
Пример #16
0
 /**
  * Test that element format can be persisted in memento and viewer can restore to the persisted
  * settings.
  */
 public void testPersistElementFormat() {
   recreateViewModel();
   String preferenceFormat = IFormattedValues.HEX_FORMAT;
   setInput(IFormattedValues.NATURAL_FORMAT);
   setFormatAndValidate(preferenceFormat, false, false, false);
   // set each element to a format, and verify
   HashMap<String, ElementFormatSetting> map = new HashMap<String, ElementFormatSetting>();
   String[] format = {
     IFormattedValues.HEX_FORMAT,
     IFormattedValues.DECIMAL_FORMAT,
     IFormattedValues.OCTAL_FORMAT,
     IFormattedValues.BINARY_FORMAT,
     IFormattedValues.NATURAL_FORMAT
   };
   makeElementFormatSetting(fViewer, TreePath.EMPTY, format, -1, 0, map);
   ArrayList<ElementFormatSetting> elementFormats =
       new ArrayList<ElementFormatSetting>(map.values());
   setFormatAndValidate(preferenceFormat, elementFormats, elementFormats, false, false, false);
   // save settings
   XMLMemento memento = XMLMemento.createWriteRoot("TEST");
   if (fViewer instanceof TreeModelViewer == false) return;
   ((TreeModelViewer) fViewer).saveState(memento);
   // throw away any settings inside the viewer and create a new viewer
   // with memento settings, this is the same effect resulted from closing
   // and opening workspace again.
   recreateViewer();
   if (fViewer instanceof TreeModelViewer == false) return;
   ((TreeModelViewer) fViewer).initState(memento);
   setInput(IFormattedValues.NATURAL_FORMAT);
   preferenceFormat =
       (String)
           fViewer.getPresentationContext().getProperty(PROP_FORMATTED_VALUE_FORMAT_PREFERENCE);
   validateModel(elementFormats, preferenceFormat, "", preferenceFormat, "");
 }
Пример #17
0
  public void testPreferenceMigration() throws Exception {
    final TaskListSorter sorter = new TaskListSorter();
    TaskListView view = new TaskListView();
    XMLMemento memento = XMLMemento.createWriteRoot(MEMENTO_KEY_SORT_INDEX);
    IMemento child = memento.createChild(MEMENTO_KEY_SORTER);
    child.putInteger(MEMENTO_KEY_SORT_INDEX, 0);
    child.putInteger(MEMENTO_KEY_SORT_DIRECTION, 1);
    child = memento.createChild(MEMENTO_KEY_SORTER2);
    child.putInteger(MEMENTO_KEY_SORT_INDEX, 1);
    child.putInteger(MEMENTO_KEY_SORT_DIRECTION, -1);
    view.migrateSorterState(sorter, memento);
    assertEquals(
        SortCriterion.SortKey.PRIORITY, sorter.getComparator().getSortCriterion(0).getKey());
    assertEquals(1, sorter.getComparator().getSortCriterion(0).getDirection());
    assertEquals(
        SortCriterion.SortKey.SUMMARY, sorter.getComparator().getSortCriterion(1).getKey());
    assertEquals(-1, sorter.getComparator().getSortCriterion(1).getDirection());

    memento = XMLMemento.createWriteRoot(MEMENTO_KEY_SORT_INDEX);
    child = memento.createChild(MEMENTO_KEY_SORTER);
    child.putInteger(MEMENTO_KEY_SORT_INDEX, 3);
    child.putInteger(MEMENTO_KEY_SORT_DIRECTION, -1);
    child = memento.createChild(MEMENTO_KEY_SORTER2);
    child.putInteger(MEMENTO_KEY_SORT_INDEX, 2);
    child.putInteger(MEMENTO_KEY_SORT_DIRECTION, -1);
    view.migrateSorterState(sorter, memento);
    assertEquals(
        SortCriterion.SortKey.TASK_ID, sorter.getComparator().getSortCriterion(0).getKey());
    assertEquals(-1, sorter.getComparator().getSortCriterion(0).getDirection());
    assertEquals(
        SortCriterion.SortKey.DATE_CREATED, sorter.getComparator().getSortCriterion(1).getKey());
    assertEquals(-1, sorter.getComparator().getSortCriterion(1).getDirection());
  }
Пример #18
0
 /** Creates an {@link XMLMemento} from Simple XML-annotated bean. */
 static IMemento toMemento(Object benchmarkSettings) throws IOException {
   try {
     final StringWriter w = new StringWriter();
     new Persister().write(benchmarkSettings, w);
     XMLMemento memento = XMLMemento.createReadRoot(new StringReader(w.toString()));
     return memento;
   } catch (Exception e) {
     throw ExceptionUtils.wrapAs(IOException.class, e);
   }
 }
Пример #19
0
 /**
  * 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;
 }
Пример #20
0
  /**
   * Register a new local redefinition of a palette.
   *
   * @param paletteID the id of the palette to register
   * @param path the path to the configuration of the palette
   * @return the memento that has been registered
   */
  public static IMemento registerLocalRedefinition(String paletteID, String path) {
    XMLMemento rootMemento = getLocalRedefinitions();

    // try to find an existing local definition for this palette
    IMemento memento = getPaletteRedefinitionNode(paletteID);

    // if one exists, remove it from the preferences
    if (memento != null) {
      unregisterLocalRedefinition(paletteID);
    }

    // then register the new one
    IMemento newMemento = rootMemento.createChild(PALETTE_REDEFINITION);
    newMemento.putString(ID, paletteID);
    newMemento.putString(PATH, path);

    saveLocalRedefinitions(rootMemento);

    return newMemento;
  }
Пример #21
0
  /**
   * 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;
  }
  public EditorReference(
      IEclipseContext windowContext,
      IWorkbenchPage page,
      MPart part,
      IEditorInput input,
      EditorDescriptor descriptor,
      IMemento editorState) {
    super(windowContext, page, part);
    this.input = input;
    this.descriptor = descriptor;
    this.editorState = editorState;

    if (descriptor == null) {
      try {
        String memento = getModel().getPersistedState().get(MEMENTO_KEY);
        if (memento == null) {
          descriptorId = EditorRegistry.EMPTY_EDITOR_ID;
        } else {
          XMLMemento createReadRoot = XMLMemento.createReadRoot(new StringReader(memento));
          IEditorRegistry registry =
              getPage().getWorkbenchWindow().getWorkbench().getEditorRegistry();
          descriptorId = createReadRoot.getString(IWorkbenchConstants.TAG_ID);
          this.descriptor = (EditorDescriptor) registry.findEditor(descriptorId);

          boolean pinnedVal =
              "true"
                  .equals(createReadRoot.getString(IWorkbenchConstants.TAG_PINNED)); // $NON-NLS-1$
          setPinned(pinnedVal);

          String ttip = createReadRoot.getString(IWorkbenchConstants.TAG_TOOLTIP);
          part.getTransientData().put(IPresentationEngine.OVERRIDE_TITLE_TOOL_TIP_KEY, ttip);
        }
      } catch (WorkbenchException e) {
        WorkbenchPlugin.log(e);
      }
    } else {
      descriptorId = this.descriptor.getId();
    }
  }
Пример #23
0
  /**
   * saves the given root memento with the given key in the preference area
   *
   * @param xmlMemento the memento to save
   * @param key the key for the preference store
   */
  private static void saveMemento(XMLMemento xmlMemento, String key) {
    // save memento
    StringWriter writer = new StringWriter();
    try {
      xmlMemento.save(writer);

      if (getPreferenceStore() != null) {
        getPreferenceStore().setValue(key, writer.toString());
      }
    } catch (IOException e) {
      Activator.getDefault().logError("input/ouput exception", e);
    }
  }
  public EditorReference(
      IEclipseContext windowContext,
      IWorkbenchPage page,
      MPart part,
      IEditorInput input,
      EditorDescriptor descriptor,
      IMemento editorState) {
    super(windowContext, page, part);
    this.input = input;
    this.descriptor = descriptor;
    this.editorState = editorState;

    if (descriptor == null) {
      try {
        String memento = getModel().getPersistedState().get(MEMENTO_KEY);
        if (memento == null) {
          descriptorId = EditorRegistry.EMPTY_EDITOR_ID;
        } else {
          XMLMemento createReadRoot = XMLMemento.createReadRoot(new StringReader(memento));
          IEditorRegistry registry =
              getPage().getWorkbenchWindow().getWorkbench().getEditorRegistry();
          descriptorId = createReadRoot.getString(IWorkbenchConstants.TAG_ID);
          this.descriptor = (EditorDescriptor) registry.findEditor(descriptorId);
        }

        if (this.descriptor == null) {
          setImageDescriptor(ImageDescriptor.getMissingImageDescriptor());
        } else {
          setImageDescriptor(this.descriptor.getImageDescriptor());
        }
      } catch (WorkbenchException e) {
        WorkbenchPlugin.log(e);
      }
    } else {
      descriptorId = this.descriptor.getId();
      setImageDescriptor(this.descriptor.getImageDescriptor());
    }
  }
Пример #25
0
  private static void writeMementoToDisk(XMLMemento memento) {
    IPath path = URLWaypointPlugin.getDefault().getStateLocation().append(URL_FILE);
    File mementoFile = path.toFile();
    mementoFile.getParentFile().mkdirs();

    try {
      FileOutputStream stream = new FileOutputStream(mementoFile);
      OutputStreamWriter writer = new OutputStreamWriter(stream, "utf-8"); // $NON-NLS-1$
      memento.save(writer);
      writer.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
Пример #26
0
 /** Load all monitors from persistent storage. */
 private void loadMonitors() {
   try {
     FileReader reader = new FileReader(getSaveLocation());
     IMemento memento = XMLMemento.createReadRoot(reader);
     IMemento[] monitorsMemento = memento.getChildren(MONITOR_ID_ATTR);
     for (IMemento monitorMemento : monitorsMemento) {
       loadMonitor(monitorMemento);
     }
   } catch (FileNotFoundException e) {
     LMLMonitorCorePlugin.log(e.getLocalizedMessage());
   } catch (WorkbenchException e) {
     LMLMonitorCorePlugin.log(e.getLocalizedMessage());
   }
 }
  /*
   * (non-Javadoc)
   *
   * @see
   * org.eclipse.ui.internal.e4.compatibility.WorkbenchPartReference#initialize
   * (org.eclipse.ui.IWorkbenchPart)
   */
  @Override
  public void initialize(IWorkbenchPart part) throws PartInitException {
    IConfigurationElement element = descriptor.getConfigurationElement();
    editorSite = new EditorSite(getModel(), part, this, element);
    if (element == null) {
      editorSite.setExtensionId(descriptor.getId());
    }
    editorSite.setActionBars(
        createEditorActionBars((WorkbenchPage) getPage(), descriptor, editorSite));
    IEditorPart editor = (IEditorPart) part;
    try {
      editor.init(editorSite, getEditorInput());
    } catch (PartInitException e) {
      if (editor instanceof ErrorEditorPart) {
        editor.init(editorSite, new NullEditorInput(this));
      } else {
        throw e;
      }
    }

    if (editor.getSite() != editorSite || editor.getEditorSite() != editorSite) {
      String id = descriptor == null ? getModel().getElementId() : descriptor.getId();
      throw new PartInitException(NLS.bind(WorkbenchMessages.EditorManager_siteIncorrect, id));
    }

    if (part instanceof IPersistableEditor) {
      if (editorState != null) {
        ((IPersistableEditor) part).restoreState(editorState);
      } else if (useIPersistableEditor()) {
        String mementoString = getModel().getPersistedState().get(MEMENTO_KEY);
        if (mementoString != null) {
          try {
            IMemento createReadRoot = XMLMemento.createReadRoot(new StringReader(mementoString));
            IMemento editorStateMemento =
                createReadRoot.getChild(IWorkbenchConstants.TAG_EDITOR_STATE);
            if (editorStateMemento != null) {
              ((IPersistableEditor) part).restoreState(editorStateMemento);
            }
          } catch (WorkbenchException e) {
            throw new PartInitException(e.getStatus());
          }
        }
      }
    }

    legacyPart = part;
    addPropertyListeners();
  }
Пример #28
0
  public static void deSerialize() {
    IPath path = URLWaypointPlugin.getDefault().getStateLocation().append(URL_FILE);
    File mementoFile = path.toFile();

    if (mementoFile.exists()) {
      try {
        FileInputStream input = new FileInputStream(mementoFile);
        BufferedReader reader =
            new BufferedReader(new InputStreamReader(input, "utf-8")); // $NON-NLS-1$
        IMemento memento = XMLMemento.createReadRoot(reader);
        restoreStateFromMemento(memento);
      } catch (FileNotFoundException e) {
        e.printStackTrace();
      } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
      } catch (WorkbenchException e) {
        e.printStackTrace();
      }
    }
  }
  /*
   * (non-Javadoc)
   *
   * @see org.eclipse.ui.IEditorReference#getEditorInput()
   */
  public IEditorInput getEditorInput() throws PartInitException {
    IEditorPart editor = getEditor(false);
    if (editor != null) {
      return editor.getEditorInput();
    }

    if (input == null) {
      String memento = getModel().getPersistedState().get(MEMENTO_KEY);
      if (memento == null) {
        input = new NullEditorInput();
      } else {
        try {
          XMLMemento createReadRoot = XMLMemento.createReadRoot(new StringReader(memento));
          input = restoreInput(createReadRoot);
        } catch (WorkbenchException e) {
          throw new PartInitException(e.getStatus());
        }
      }
    }
    return input;
  }
  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;
  }