Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open Type Editor from Instance Viewer #988

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 19 additions & 13 deletions plugins/org.eclipse.fordiac.ide.application/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1499,19 +1499,25 @@
</command>
<command commandId="org.eclipse.fordiac.ide.application.commands.openType">
<visibleWhen checkEnabled="true">
<with variable="selection" >
<count value="1" />
<iterate ifEmpty="false">
<or>
<instanceof value="org.eclipse.fordiac.ide.model.libraryElement.FBNetworkElement" />
<reference definitionId="org.eclipse.fordiac.ide.application.FBNelementEditPartSelection" />
<instanceof value="org.eclipse.fordiac.ide.model.libraryElement.IInterfaceElement" />
<instanceof value="org.eclipse.fordiac.ide.gef.editparts.InterfaceEditPart"/>
</or>
</iterate>
</with>
<or>
<with variable="selection" >
<count value="1" />
<iterate ifEmpty="false">
<or>
<instanceof value="org.eclipse.fordiac.ide.model.libraryElement.FBNetworkElement" />
<reference definitionId="org.eclipse.fordiac.ide.application.FBNelementEditPartSelection" />
<instanceof value="org.eclipse.fordiac.ide.model.libraryElement.IInterfaceElement" />
<instanceof value="org.eclipse.fordiac.ide.gef.editparts.InterfaceEditPart"/>
</or>
</iterate>
</with>
<with variable="activeEditor">
<adapt type="org.eclipse.fordiac.ide.model.libraryElement.FBNetworkElement" />
</with>
</or>
</visibleWhen>
</command>
</command>

<command commandId="org.eclipse.fordiac.ide.application.commands.saveAsSubappType">
<visibleWhen checkEnabled="true">
<and>
Expand Down Expand Up @@ -1783,4 +1789,4 @@
type="java.lang.Object">
</propertyTester>
</extension>
</plugin>
</plugin>
Original file line number Diff line number Diff line change
Expand Up @@ -12,49 +12,54 @@
*******************************************************************************/
package org.eclipse.fordiac.ide.application.handlers;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.Status;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.fordiac.ide.model.libraryElement.FBNetwork;
import org.eclipse.fordiac.ide.model.libraryElement.FBNetworkElement;
import org.eclipse.fordiac.ide.model.libraryElement.IInterfaceElement;
import org.eclipse.fordiac.ide.model.libraryElement.LibraryElement;
import org.eclipse.fordiac.ide.model.libraryElement.INamedElement;
import org.eclipse.fordiac.ide.model.libraryElement.SubApp;
import org.eclipse.fordiac.ide.model.libraryElement.SubAppType;
import org.eclipse.fordiac.ide.model.libraryElement.TypedSubApp;
import org.eclipse.fordiac.ide.model.libraryElement.UntypedSubApp;
import org.eclipse.fordiac.ide.model.ui.actions.OpenListenerManager;
import org.eclipse.fordiac.ide.ui.FordiacLogHelper;
import org.eclipse.gef.EditPart;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IEditorDescriptor;
import org.eclipse.ui.ISources;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.handlers.HandlerUtil;
import org.eclipse.ui.part.FileEditorInput;

public class OpenTypeHandler extends AbstractHandler {

@Override
public Object execute(final ExecutionEvent event) throws ExecutionException {

final IStructuredSelection sel = HandlerUtil.getCurrentStructuredSelection(event);
final IFile typeFile = getSelectedTypeFile(sel);
if (typeFile != null) {
openTypeEditor(typeFile);
final EObject object = getSelectedObject(sel);

if (object != null) {
openTypeEditor(object);
}
return Status.OK_STATUS;

return null;
}

private static void openTypeEditor(final IFile file) {
private static void openTypeEditor(final EObject object) {
final IWorkbench workbench = PlatformUI.getWorkbench();
if (null != workbench) {
if (workbench != null) {
final IWorkbenchWindow activeWorkbenchWindow = workbench.getActiveWorkbenchWindow();
if (null != activeWorkbenchWindow) {
final IWorkbenchPage activePage = activeWorkbenchWindow.getActivePage();
final IEditorDescriptor desc = PlatformUI.getWorkbench().getEditorRegistry()
.getDefaultEditor(file.getName());
if (activeWorkbenchWindow != null) {
try {
activePage.openEditor(new FileEditorInput(file), desc.getId());
OpenListenerManager.openEditor(object);
} catch (final Exception e) {
FordiacLogHelper.logError(e.getMessage(), e);
}
Expand All @@ -66,27 +71,76 @@ private static void openTypeEditor(final IFile file) {
public void setEnabled(final Object evaluationContext) {
final ISelection sel = (ISelection) HandlerUtil.getVariable(evaluationContext,
ISources.ACTIVE_CURRENT_SELECTION_NAME);
setBaseEnabled(getSelectedTypeFile(sel) != null);

setBaseEnabled(getSelectedObject(sel) != null);
}

private static IFile getSelectedTypeFile(final ISelection sel) {
private static EObject getSelectedObject(final ISelection sel) {
if ((sel instanceof final IStructuredSelection structSel) && !sel.isEmpty() && (structSel.size() == 1)) {
Object obj = structSel.getFirstElement();

if (obj instanceof final EditPart ep) {
obj = ep.getModel();
}

final LibraryElement type = switch (obj) {
final EObject eObject = switch (obj) {
case final FBNetwork fbn -> getEObjectForOpening(fbn);
case final FBNetworkElement fbnEl -> fbnEl.getType();
case final IInterfaceElement ie -> ie.getType();
default -> null;
};

if ((type != null) && (type.getTypeEntry() != null)) {
return type.getTypeEntry().getFile();
if ((eObject != null)) {
return eObject;
}
}

return null;
}

}
private static EObject getEObjectForOpening(final FBNetwork fbn) {

if (fbn.eContainer() instanceof final UntypedSubApp usa) {
EObject element = usa;

final List<String> subAppNames = new ArrayList<>();

while (element != null) {
if (element instanceof final INamedElement ine) {
subAppNames.add(ine.getName());
}

if (element.eContainer() instanceof final TypedSubApp tsa) {
subAppNames.add(tsa.getName());

return getMatchingSubAppFromType(subAppNames.reversed(), tsa.getType());
}

element = element.eContainer();
}
}

if (fbn.eContainer() instanceof final TypedSubApp tsa) {
return tsa.getType();
}

return null;
}

private static SubApp getMatchingSubAppFromType(final List<String> subAppNames, final SubAppType type) {
FBNetwork fb = type.getFBNetwork();
SubApp lastSubapp = null;

for (final String subAppName : subAppNames) {
final SubApp sa = fb.getSubAppNamed(subAppName);

if (sa != null) {
lastSubapp = sa;
fb = sa.getSubAppNetwork();
}
}

return lastSubapp;
}

}
Loading