Search in sources :

Example 1 with CompatibilityEditor

use of org.eclipse.ui.internal.e4.compatibility.CompatibilityEditor in project eclipse.platform.ui by eclipse-platform.

the class WorkbenchPage method getActiveEditor.

/**
 * @see IWorkbenchPage
 */
@Override
public IEditorPart getActiveEditor() {
    IWorkbenchPart activePart = getActivePart();
    if (activePart instanceof IEditorPart) {
        // if the currently active part is an editor, return it
        return (IEditorPart) activePart;
    }
    if (!activationList.isEmpty()) {
        IEditorPart editor = findActiveEditor();
        if (editor != null) {
            return editor;
        }
    }
    MUIElement area = findSharedArea();
    if (area instanceof MPlaceholder) {
        area = ((MPlaceholder) area).getRef();
    }
    if (area != null && area.isVisible() && area.isToBeRendered()) {
        // we have a shared area, try iterating over its editors first
        List<MPart> editors = modelService.findElements(area, CompatibilityEditor.MODEL_ELEMENT_ID, MPart.class);
        for (MPart model : editors) {
            Object object = model.getObject();
            if (object instanceof CompatibilityEditor) {
                CompatibilityEditor editor = (CompatibilityEditor) object;
                // see bug 308492
                if (!editor.isBeingDisposed() && isInArea(area, model)) {
                    return ((CompatibilityEditor) object).getEditor();
                }
            }
        }
    }
    MPerspective perspective = getPerspectiveStack().getSelectedElement();
    if (perspective == null) {
        return null;
    }
    List<MPart> parts = modelService.findElements(perspective, CompatibilityEditor.MODEL_ELEMENT_ID, MPart.class, null);
    for (MPart part : parts) {
        Object object = part.getObject();
        if (object instanceof CompatibilityEditor) {
            CompatibilityEditor editor = (CompatibilityEditor) object;
            // see bug 308492
            if (!editor.isBeingDisposed()) {
                if (isValid(perspective, part) || isValid(window, part)) {
                    return ((CompatibilityEditor) object).getEditor();
                }
            }
        }
    }
    return null;
}
Also used : MPart(org.eclipse.e4.ui.model.application.ui.basic.MPart) MPerspective(org.eclipse.e4.ui.model.application.ui.advanced.MPerspective) IWorkbenchPart(org.eclipse.ui.IWorkbenchPart) MPlaceholder(org.eclipse.e4.ui.model.application.ui.advanced.MPlaceholder) CompatibilityEditor(org.eclipse.ui.internal.e4.compatibility.CompatibilityEditor) MUIElement(org.eclipse.e4.ui.model.application.ui.MUIElement) EObject(org.eclipse.emf.ecore.EObject) IEditorPart(org.eclipse.ui.IEditorPart)

Example 2 with CompatibilityEditor

use of org.eclipse.ui.internal.e4.compatibility.CompatibilityEditor in project eclipse.platform.ui by eclipse-platform.

the class WorkbenchPage method deactivateLastEditor.

/**
 * Deactivate the last editor's action bars if another type of editor has // *
 * been activated.
 *
 * @param part the part that is being activated
 */
private void deactivateLastEditor(MPart part) {
    Object client = part.getObject();
    // we only care if the currently activated part is an editor
    if (client instanceof CompatibilityEditor) {
        IWorkbenchPart activePart = getWrappedPart((CompatibilityEditor) client);
        if (activePart == null) {
            return;
        }
        String activeId = activePart.getSite().getId();
        // find another editor that was last activated
        for (MPart previouslyActive : activationList) {
            if (previouslyActive != part) {
                Object object = previouslyActive.getObject();
                if (object instanceof CompatibilityEditor) {
                    IWorkbenchPart workbenchPart = getWrappedPart((CompatibilityEditor) object);
                    if (workbenchPart == null) {
                        continue;
                    }
                    EditorSite site = (EditorSite) workbenchPart.getSite();
                    String lastId = site.getId();
                    // if not the same, hide the other editor's action bars
                    if (lastId != null && !lastId.equals(activeId)) {
                        site.deactivateActionBars(true);
                    }
                    break;
                }
            }
        }
    }
}
Also used : MPart(org.eclipse.e4.ui.model.application.ui.basic.MPart) IWorkbenchPart(org.eclipse.ui.IWorkbenchPart) CompatibilityEditor(org.eclipse.ui.internal.e4.compatibility.CompatibilityEditor) EObject(org.eclipse.emf.ecore.EObject)

Example 3 with CompatibilityEditor

use of org.eclipse.ui.internal.e4.compatibility.CompatibilityEditor in project eclipse.platform.ui by eclipse-platform.

the class WorkbenchPage method openEditors.

@Override
public IEditorReference[] openEditors(IEditorInput[] inputs, String[] editorIDs, IMemento[] mementos, int matchFlags, int activationIndex) throws MultiPartInitException {
    // nulls
    if (inputs == null) {
        Assert.isTrue(mementos != null);
        inputs = new IEditorInput[mementos.length];
    }
    // nulls
    if (editorIDs == null) {
        Assert.isTrue(mementos != null);
        editorIDs = new String[mementos.length];
    }
    Assert.isTrue(inputs.length == editorIDs.length);
    Assert.isTrue(inputs.length > 0);
    Assert.isTrue(mementos == null || mementos.length == inputs.length);
    PartInitException[] exceptions = new PartInitException[inputs.length];
    IEditorReference[] references = new IEditorReference[inputs.length];
    boolean hasFailures = false;
    IEditorRegistry reg = getWorkbenchWindow().getWorkbench().getEditorRegistry();
    MPart editorToActivate = null;
    for (int i = 0; i < inputs.length; i++) {
        String curEditorID = editorIDs[i];
        IEditorInput curInput = inputs[i];
        IMemento curMemento = mementos == null ? null : mementos[i];
        // If we don't have an editorID get it from the memento
        if (curEditorID == null && curMemento != null) {
            curEditorID = curMemento.getString(IWorkbenchConstants.TAG_ID);
        }
        // If we don't have an input create on from the memento
        if (curInput == null && curMemento != null) {
            try {
                curInput = EditorReference.createInput(curMemento);
            } catch (PartInitException e) {
                curInput = null;
                exceptions[i] = e;
                hasFailures = true;
                continue;
            }
        }
        // both input and editor state)
        if (curMemento != null && !curMemento.getID().equals(IWorkbenchConstants.TAG_EDITOR)) {
            XMLMemento outerMem = XMLMemento.createWriteRoot(IWorkbenchConstants.TAG_EDITOR);
            outerMem.putString(IWorkbenchConstants.TAG_ID, curEditorID);
            outerMem.copyChild(curMemento);
            XMLMemento inputMem = (XMLMemento) outerMem.createChild(IWorkbenchConstants.TAG_INPUT);
            inputMem.putString(IWorkbenchConstants.TAG_FACTORY_ID, curInput.getPersistable().getFactoryId());
            inputMem.putString(IWorkbenchConstants.TAG_PATH, curInput.getName());
        }
        // and the memento (if any)
        if (reg.findEditor(curEditorID) == null) {
            references[i] = null;
            exceptions[i] = new PartInitException(NLS.bind(WorkbenchMessages.EditorManager_unknownEditorIDMessage, curEditorID));
            hasFailures = true;
        } else if (curInput == null) {
            references[i] = null;
            exceptions[i] = new PartInitException(NLS.bind(WorkbenchMessages.EditorManager_no_persisted_state, curEditorID));
            hasFailures = true;
        } else {
            // Is there an existing editor ?
            IEditorReference[] existingEditors = findEditors(curInput, curEditorID, matchFlags);
            if (existingEditors.length == 0) {
                MPart editor = partService.createPart(CompatibilityEditor.MODEL_ELEMENT_ID);
                references[i] = createEditorReferenceForPart(editor, curInput, curEditorID, null);
                if (i == activationIndex)
                    editorToActivate = editor;
                // editor's model
                if (curMemento instanceof XMLMemento) {
                    XMLMemento memento = (XMLMemento) curMemento;
                    StringWriter writer = new StringWriter();
                    try {
                        memento.save(writer);
                        editor.getPersistedState().put(WorkbenchPartReference.MEMENTO_KEY, writer.toString());
                    } catch (IOException e) {
                        WorkbenchPlugin.log(e);
                    }
                }
                editor.setLabel(references[i].getTitle());
                editor.setTooltip(references[i].getTitleToolTip());
                editor.setIconURI(getEditorImageURI((EditorReference) references[i]));
                ((PartServiceImpl) partService).addPart(editor);
            } else {
                // Use the existing editor, update the state if it has *not*
                // been rendered
                EditorReference ee = (EditorReference) existingEditors[0];
                if (i == activationIndex)
                    editorToActivate = ee.getModel();
                if (ee.getModel().getWidget() == null) {
                    // editor's model
                    if (curMemento instanceof XMLMemento) {
                        XMLMemento momento = (XMLMemento) curMemento;
                        StringWriter writer = new StringWriter();
                        try {
                            momento.save(writer);
                            ee.getModel().getPersistedState().put(WorkbenchPartReference.MEMENTO_KEY, writer.toString());
                        } catch (IOException e) {
                            WorkbenchPlugin.log(e);
                        }
                    }
                } else // editor already rendered, try to update its state
                if (curMemento != null && ee.getModel().getObject() instanceof CompatibilityEditor) {
                    CompatibilityEditor ce = (CompatibilityEditor) ee.getModel().getObject();
                    if (ce.getEditor() instanceof IPersistableEditor) {
                        IPersistableEditor pe = (IPersistableEditor) ce.getEditor();
                        // Extract the 'editorState' from the memento
                        IMemento editorMem = curMemento.getChild(IWorkbenchConstants.TAG_EDITOR_STATE);
                        if (editorMem == null) {
                            // Must be an externally defined memento,
                            // take the second child
                            IMemento[] kids = curMemento.getChildren();
                            if (kids.length == 2)
                                editorMem = kids[1];
                        }
                        if (editorMem != null)
                            pe.restoreState(editorMem);
                    }
                }
            }
        }
    }
    if (editorToActivate != null) {
        partService.activate(editorToActivate);
    }
    boolean hasSuccesses = false;
    for (IEditorReference reference : references) {
        if (reference != null) {
            hasSuccesses = true;
            legacyWindow.firePerspectiveChanged(this, getPerspective(), reference, CHANGE_EDITOR_OPEN);
        }
    }
    // only fire this event if an editor was opened
    if (hasSuccesses) {
        legacyWindow.firePerspectiveChanged(this, getPerspective(), CHANGE_EDITOR_OPEN);
    }
    if (hasFailures) {
        throw new MultiPartInitException(references, exceptions);
    }
    return references;
}
Also used : MPart(org.eclipse.e4.ui.model.application.ui.basic.MPart) IOException(java.io.IOException) IMemento(org.eclipse.ui.IMemento) IEditorReference(org.eclipse.ui.IEditorReference) IEditorReference(org.eclipse.ui.IEditorReference) XMLMemento(org.eclipse.ui.XMLMemento) StringWriter(java.io.StringWriter) CompatibilityEditor(org.eclipse.ui.internal.e4.compatibility.CompatibilityEditor) MultiPartInitException(org.eclipse.ui.MultiPartInitException) PartInitException(org.eclipse.ui.PartInitException) MultiPartInitException(org.eclipse.ui.MultiPartInitException) IPersistableEditor(org.eclipse.ui.IPersistableEditor) IEditorRegistry(org.eclipse.ui.IEditorRegistry) IEditorInput(org.eclipse.ui.IEditorInput)

Example 4 with CompatibilityEditor

use of org.eclipse.ui.internal.e4.compatibility.CompatibilityEditor in project eclipse.platform.ui by eclipse-platform.

the class WorkbenchPage method busyOpenEditor.

/**
 * @see #openEditor(IEditorInput, String, boolean, int)
 */
private IEditorPart busyOpenEditor(IEditorInput input, String editorId, boolean activate, int matchFlags, IMemento editorState, boolean notify) throws PartInitException {
    if (input == null || editorId == null) {
        throw new IllegalArgumentException();
    }
    // Special handling for external editors (they have no tabs...)
    if (IEditorRegistry.SYSTEM_EXTERNAL_EDITOR_ID.equals(editorId)) {
        IPathEditorInput fileInput = getPathEditorInput(input);
        if (fileInput == null) {
            throw new PartInitException(WorkbenchMessages.EditorManager_systemEditorError);
        }
        String fullPath = fileInput.getPath().toOSString();
        Program.launch(fullPath);
        return null;
    }
    IEditorRegistry editorRegistry = getWorkbenchWindow().getWorkbench().getEditorRegistry();
    IEditorDescriptor desc = editorRegistry.findEditor(editorId);
    boolean ignoreFileSize = (matchFlags & MATCH_IGNORE_SIZE) != 0;
    if (ignoreFileSize) {
        // clear the flag so code below do not need to have extra cases for that
        matchFlags ^= MATCH_IGNORE_SIZE;
    } else if (desc != null && !desc.isOpenExternal()) {
        java.util.Optional<String> largeFileEditorId = largeFileLimitsPreferenceHandler.getEditorForInput(input);
        if (largeFileEditorId == null) {
            // the user pressed cancel in the editor selection dialog
            return null;
        }
        if (largeFileEditorId.isPresent()) {
            // preference for large documents is set and applies
            editorId = largeFileEditorId.get();
            desc = editorRegistry.findEditor(editorId);
            if (desc instanceof EditorDescriptor && desc.isOpenExternal()) {
                openExternalEditor((EditorDescriptor) desc, input);
                return null;
            }
        }
    }
    if (desc == null) {
        throw new PartInitException(NLS.bind(WorkbenchMessages.EditorManager_unknownEditorIDMessage, editorId));
    }
    setEditorAreaVisible(true);
    IEditorReference[] editorReferences = findEditors(input, editorId, matchFlags);
    if (editorReferences.length != 0) {
        IEditorPart editor = editorReferences[0].getEditor(true);
        if (editor instanceof IShowEditorInput) {
            ((IShowEditorInput) editor).showEditorInput(input);
        }
        partService.showPart(((EditorReference) editorReferences[0]).getModel(), PartState.VISIBLE);
        if (activate) {
            activate(editor);
        }
        recordEditor(input, desc);
        return editor;
    } else if (desc.isInternal()) {
        // look for an editor to reuse
        EditorReference reusableEditorRef = (EditorReference) ((TabBehaviour) Tweaklets.get(TabBehaviour.KEY)).findReusableEditor(this);
        if (reusableEditorRef != null) {
            IEditorPart reusableEditor = reusableEditorRef.getEditor(false);
            if (editorId.equals(reusableEditorRef.getId()) && reusableEditor instanceof IReusableEditor) {
                // reusable editors that share the same id are okay
                recordEditor(input, desc);
                reuseEditor((IReusableEditor) reusableEditor, input);
                MPart editor = reusableEditorRef.getModel();
                partService.showPart(editor, PartState.VISIBLE);
                if (activate) {
                    partService.activate(editor);
                } else {
                    updateActiveEditorSources(editor);
                }
                return reusableEditor;
            }
            // should have saved already if necessary, close this editor, a
            // new one will be opened
            closeEditor(reusableEditorRef, false);
        }
    } else if (desc.isOpenExternal()) {
        openExternalEditor((EditorDescriptor) desc, input);
        // no editor parts for external editors, return null
        return null;
    }
    MPart editor = partService.createPart(CompatibilityEditor.MODEL_ELEMENT_ID);
    editor.getTags().add(editorId);
    EditorReference ref = createEditorReferenceForPart(editor, input, editorId, editorState);
    partService.showPart(editor, PartState.VISIBLE);
    CompatibilityEditor compatibilityEditor = (CompatibilityEditor) editor.getObject();
    if (compatibilityEditor == null) {
        return null;
    }
    if (activate) {
        partService.activate(editor);
    } else {
        updateActiveEditorSources(editor);
    }
    if (notify) {
        legacyWindow.firePerspectiveChanged(this, getPerspective(), ref, CHANGE_EDITOR_OPEN);
        legacyWindow.firePerspectiveChanged(this, getPerspective(), CHANGE_EDITOR_OPEN);
    }
    recordEditor(input, desc);
    return compatibilityEditor.getEditor();
}
Also used : TabBehaviour(org.eclipse.ui.internal.tweaklets.TabBehaviour) MPart(org.eclipse.e4.ui.model.application.ui.basic.MPart) Optional(org.eclipse.e4.core.di.annotations.Optional) IEditorDescriptor(org.eclipse.ui.IEditorDescriptor) IReusableEditor(org.eclipse.ui.IReusableEditor) IEditorPart(org.eclipse.ui.IEditorPart) IShowEditorInput(org.eclipse.ui.IShowEditorInput) EditorDescriptor(org.eclipse.ui.internal.registry.EditorDescriptor) IEditorDescriptor(org.eclipse.ui.IEditorDescriptor) IPathEditorInput(org.eclipse.ui.IPathEditorInput) IEditorReference(org.eclipse.ui.IEditorReference) IEditorReference(org.eclipse.ui.IEditorReference) CompatibilityEditor(org.eclipse.ui.internal.e4.compatibility.CompatibilityEditor) PartInitException(org.eclipse.ui.PartInitException) MultiPartInitException(org.eclipse.ui.MultiPartInitException) IEditorRegistry(org.eclipse.ui.IEditorRegistry)

Example 5 with CompatibilityEditor

use of org.eclipse.ui.internal.e4.compatibility.CompatibilityEditor in project eclipse.platform.ui by eclipse-platform.

the class WorkbenchPage method findActiveEditor.

/**
 * Searches and returns an editor from the activation list that is being
 * displayed in the current presentation. If an editor is in the presentation
 * but is behind another part it will not be returned.
 *
 * @return an editor that is being shown in the current presentation and was
 *         previously activated, editors that are behind another part in a stack
 *         will not be returned
 */
private IEditorPart findActiveEditor() {
    List<MPart> candidates = new ArrayList<>(activationList);
    MUIElement area = findSharedArea();
    if (area instanceof MPlaceholder) {
        area = ((MPlaceholder) area).getRef();
    }
    if (area != null && area.isVisible() && area.isToBeRendered()) {
        // we have a shared area, try iterating over its editors first
        List<MPart> editors = modelService.findElements(area, CompatibilityEditor.MODEL_ELEMENT_ID, MPart.class);
        for (Iterator<MPart> it = candidates.iterator(); it.hasNext(); ) {
            MPart model = it.next();
            if (!editors.contains(model)) {
                continue;
            }
            Object object = model.getObject();
            if (object instanceof CompatibilityEditor) {
                CompatibilityEditor editor = (CompatibilityEditor) object;
                // see bug 308492
                if (!editor.isBeingDisposed() && isInArea(area, model)) {
                    return ((CompatibilityEditor) object).getEditor();
                }
            }
            it.remove();
        }
    }
    MPerspective perspective = getPerspectiveStack().getSelectedElement();
    for (MPart model : activationList) {
        Object object = model.getObject();
        if (object instanceof CompatibilityEditor) {
            CompatibilityEditor editor = (CompatibilityEditor) object;
            // see bug 308492
            if (!editor.isBeingDisposed()) {
                if (isValid(perspective, model) || isValid(window, model)) {
                    return ((CompatibilityEditor) object).getEditor();
                }
            }
        }
    }
    return null;
}
Also used : MPart(org.eclipse.e4.ui.model.application.ui.basic.MPart) MPerspective(org.eclipse.e4.ui.model.application.ui.advanced.MPerspective) MPlaceholder(org.eclipse.e4.ui.model.application.ui.advanced.MPlaceholder) ArrayList(java.util.ArrayList) CompatibilityEditor(org.eclipse.ui.internal.e4.compatibility.CompatibilityEditor) MUIElement(org.eclipse.e4.ui.model.application.ui.MUIElement) EObject(org.eclipse.emf.ecore.EObject)

Aggregations

MPart (org.eclipse.e4.ui.model.application.ui.basic.MPart)5 CompatibilityEditor (org.eclipse.ui.internal.e4.compatibility.CompatibilityEditor)5 EObject (org.eclipse.emf.ecore.EObject)3 MUIElement (org.eclipse.e4.ui.model.application.ui.MUIElement)2 MPerspective (org.eclipse.e4.ui.model.application.ui.advanced.MPerspective)2 MPlaceholder (org.eclipse.e4.ui.model.application.ui.advanced.MPlaceholder)2 IEditorPart (org.eclipse.ui.IEditorPart)2 IEditorReference (org.eclipse.ui.IEditorReference)2 IEditorRegistry (org.eclipse.ui.IEditorRegistry)2 IWorkbenchPart (org.eclipse.ui.IWorkbenchPart)2 MultiPartInitException (org.eclipse.ui.MultiPartInitException)2 PartInitException (org.eclipse.ui.PartInitException)2 IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1 ArrayList (java.util.ArrayList)1 Optional (org.eclipse.e4.core.di.annotations.Optional)1 IEditorDescriptor (org.eclipse.ui.IEditorDescriptor)1 IEditorInput (org.eclipse.ui.IEditorInput)1 IMemento (org.eclipse.ui.IMemento)1 IPathEditorInput (org.eclipse.ui.IPathEditorInput)1