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;
}
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;
}
}
}
}
}
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;
}
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();
}
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;
}
Aggregations