Beispiel #1
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);
   }
 }
  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();
  }
 /** 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);
   }
 }
Beispiel #4
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());
   }
 }
 /**
  * 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;
 }
 /**
  * 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;
 }
  /*
   * (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();
  }
  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;
  }
Beispiel #9
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;
  }
  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();
    }
  }
  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());
    }
  }
  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));
    }
  }
  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;
  }