Skip to content

Commit

Permalink
changes
Browse files Browse the repository at this point in the history
  • Loading branch information
alliuqemal committed Jan 22, 2025
1 parent 2bb5dc5 commit 89ac0d4
Showing 1 changed file with 66 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,50 +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 type = getTypeFromSelection(sel);

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

return null;
}

private static void openTypeEditor(final IFile file) {
private static void openTypeEditor(final EObject type) {
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(type);
} catch (final Exception e) {
FordiacLogHelper.logError(e.getMessage(), e);
}
Expand All @@ -67,37 +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(getTypeFromSelection(sel) != null);
}

private static IFile getSelectedTypeFile(final ISelection sel) {
private static EObject getTypeFromSelection(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 type = switch (obj) {
case final FBNetwork fbn -> getTypeFromFbn(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 ((type != null)) {
return type;
}
}

return null;
}

private static LibraryElement getTypeFromFbn(final FBNetwork fbn) {
private static EObject getTypeFromFbn(final FBNetwork fbn) {

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

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

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

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

return getEObjectFromString(subApps, tsa.getType());
}

element = element.eContainer();
}
}

if (fbn.eContainer() instanceof final FBNetworkElement fbne) {
return fbne.getType();
}

return null;
}

private static SubApp getEObjectFromString(final List<String> subApps, final SubAppType type) {
FBNetwork fb = type.getFBNetwork();
SubApp last = null;

for (final String s : subApps.reversed()) {
final SubApp sa = fb.getSubAppNamed(s);

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

return last;
}

}

0 comments on commit 89ac0d4

Please sign in to comment.