/*
   * (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();
  }
  /*
   * Creates the action bars for an editor. Editors of the same type should
   * share a single editor action bar, so this implementation may return an
   * existing action bar vector.
   */
  private static EditorActionBars createEditorActionBars(
      WorkbenchPage page, EditorDescriptor desc, final IEditorSite site) {
    // Get the editor type.
    String type = desc.getId();

    // If an action bar already exists for this editor type return it.
    Set<EditorActionBars> candidates = actionCache.get(type);
    if (candidates != null) {
      for (EditorActionBars candidate : candidates) {
        if (candidate.getPage() == page) {
          candidate.addRef();
          return candidate;
        }
      }
    }

    // Create a new action bar set.
    EditorActionBars actionBars = new EditorActionBars(page, page.getWorkbenchWindow(), type);
    actionBars.addRef();
    if (candidates == null) {
      candidates = new HashSet<EditorActionBars>(3);
      candidates.add(actionBars);
      actionCache.put(type, candidates);
    } else candidates.add(actionBars);

    // Read base contributor.
    IEditorActionBarContributor contr = desc.createActionBarContributor();
    if (contr != null) {
      actionBars.setEditorContributor(contr);
      contr.init(actionBars, page);
    }

    // Read action extensions.
    EditorActionBuilder builder = new EditorActionBuilder();
    contr = builder.readActionExtensions(desc);
    if (contr != null) {
      actionBars.setExtensionContributor(contr);
      contr.init(actionBars, page);
    }

    // Return action bars.
    return actionBars;
  }
 public String getTitle() {
   String label = Util.safeString(getModel().getLocalizedLabel());
   if (label.length() == 0) {
     if (input == null) {
       if (descriptor != null) {
         return descriptor.getLabel();
       }
     } else {
       return Util.safeString(input.getName());
     }
   }
   return label;
 }
 /*
  * (non-Javadoc)
  *
  * @see
  * org.eclipse.ui.internal.e4.compatibility.WorkbenchPartReference#createPart
  * ()
  */
 @Override
 public IWorkbenchPart createPart() throws PartInitException {
   try {
     if (descriptor == null) {
       return createErrorPart();
     } else if (descriptor.getId().equals(IEditorRegistry.SYSTEM_INPLACE_EDITOR_ID)) {
       IEditorPart part = ComponentSupport.getSystemInPlaceEditor();
       if (part == null) {
         throw new PartInitException(WorkbenchMessages.EditorManager_no_in_place_support);
       }
       return part;
     }
     return descriptor.createEditor();
   } catch (CoreException e) {
     IStatus status = e.getStatus();
     throw new PartInitException(
         new Status(
             IStatus.ERROR,
             WorkbenchPlugin.PI_WORKBENCH,
             status.getCode(),
             status.getMessage(),
             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 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;
  }