Skip to content

Commit

Permalink
Code cleanup and small fixes for MarkerResolutions
Browse files Browse the repository at this point in the history
  • Loading branch information
sebHollersbacher authored and oberlehner committed Sep 17, 2024
1 parent 23d3e4f commit 50259bb
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,12 @@
import org.eclipse.fordiac.ide.model.commands.change.ChangeStructCommand;
import org.eclipse.fordiac.ide.model.commands.change.ConfigureFBCommand;
import org.eclipse.fordiac.ide.model.data.DataType;
import org.eclipse.fordiac.ide.model.data.StructuredType;
import org.eclipse.fordiac.ide.model.errormarker.FordiacErrorMarker;
import org.eclipse.fordiac.ide.model.libraryElement.ConfigurableFB;
import org.eclipse.fordiac.ide.model.libraryElement.IInterfaceElement;
import org.eclipse.fordiac.ide.model.libraryElement.LibraryElement;
import org.eclipse.fordiac.ide.model.libraryElement.StructManipulator;
import org.eclipse.fordiac.ide.model.search.AbstractLiveSearchContext;
import org.eclipse.fordiac.ide.model.typelibrary.ErrorDataTypeEntry;
import org.eclipse.fordiac.ide.model.ui.editors.DataTypeTreeSelectionDialog;
import org.eclipse.fordiac.ide.model.ui.nat.DataTypeSelectionTreeContentProvider;
import org.eclipse.fordiac.ide.model.ui.nat.TypeNode;
Expand All @@ -36,7 +35,7 @@

public class ChangeDataTypeMarkerResolution extends AbstractErrorMarkerResolution {

private LibraryElement newEntry;
private DataType selectedType;
private boolean canceled = false;

public ChangeDataTypeMarkerResolution(final IMarker marker) {
Expand All @@ -49,33 +48,36 @@ public void run(final IMarker marker) {
return;
}

if (newEntry == null) {
createNewEntry();
if (selectedType == null) {
selectDataType();
}

if (newEntry != null) {
if (selectedType != null) {
final EObject errorType = FordiacErrorMarker.getTarget(marker);
if (errorType instanceof final IInterfaceElement interfaceElement
&& interfaceElement.getType().getTypeEntry() instanceof ErrorDataTypeEntry
&& newEntry instanceof final DataType d) {
AbstractLiveSearchContext.executeAndSave(ChangeDataTypeCommand.forDataType(interfaceElement, d),
interfaceElement, new NullProgressMonitor());
} else if (errorType instanceof final StructManipulator fb && newEntry instanceof final DataType d) {
AbstractLiveSearchContext.executeAndSave(new ChangeStructCommand(fb, d), fb, new NullProgressMonitor());
} else if (errorType instanceof final ConfigurableFB fb && newEntry instanceof final DataType d) {
AbstractLiveSearchContext.executeAndSave(new ConfigureFBCommand(fb, d), fb, new NullProgressMonitor());
if (errorType instanceof final IInterfaceElement interfaceElement) {
AbstractLiveSearchContext.executeAndSave(
ChangeDataTypeCommand.forDataType(interfaceElement, selectedType), interfaceElement,
new NullProgressMonitor());
} else if (errorType instanceof final StructManipulator fb && selectedType instanceof StructuredType) {
AbstractLiveSearchContext.executeAndSave(new ChangeStructCommand(fb, selectedType), fb,
new NullProgressMonitor());
} else if (errorType instanceof final ConfigurableFB fb) {
AbstractLiveSearchContext.executeAndSave(new ConfigureFBCommand(fb, selectedType), fb,
new NullProgressMonitor());
}
}
}

private void createNewEntry() {
private void selectDataType() {
final DataTypeTreeSelectionDialog dialog = new DataTypeTreeSelectionDialog(
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),
DataTypeSelectionTreeContentProvider.INSTANCE);
dialog.setInput(getTypeLibrary());
if ((dialog.open() == Window.OK)
&& (dialog.getFirstResult() instanceof final TypeNode node && !node.isDirectory())) {
newEntry = node.getType();
if (dialog.open() == Window.OK && dialog.getFirstResult() instanceof final TypeNode node
&& !node.isDirectory()) {
if (node.getType() instanceof final DataType dataType) {
selectedType = dataType;
}
} else {
canceled = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
import org.eclipse.fordiac.ide.model.commands.change.UpdateInternalFBCommand;
import org.eclipse.fordiac.ide.model.errormarker.FordiacErrorMarker;
import org.eclipse.fordiac.ide.model.libraryElement.BaseFBType;
import org.eclipse.fordiac.ide.model.libraryElement.ErrorMarkerFBNElement;
import org.eclipse.fordiac.ide.model.libraryElement.FB;
import org.eclipse.fordiac.ide.model.libraryElement.FBNetworkElement;
import org.eclipse.fordiac.ide.model.search.AbstractLiveSearchContext;
import org.eclipse.fordiac.ide.model.typelibrary.TypeEntry;
import org.eclipse.fordiac.ide.model.ui.editors.DataTypeTreeSelectionDialog;
Expand All @@ -33,7 +33,7 @@

public class ChangeFBMarkerResolution extends AbstractErrorMarkerResolution {

private TypeEntry newEntry;
private TypeEntry selectedEntry;
private boolean canceled = false;

public ChangeFBMarkerResolution(final IMarker marker) {
Expand All @@ -46,32 +46,31 @@ public void run(final IMarker marker) {
return;
}

if (newEntry == null) {
createNewEntry();
if (selectedEntry == null) {
selectTypeEntry();
}

if (newEntry != null) {
if (selectedEntry != null) {
final EObject target = FordiacErrorMarker.getTarget(marker);
if (target instanceof final ErrorMarkerFBNElement err) {
AbstractLiveSearchContext.executeAndSave(new UpdateFBTypeCommand(err, newEntry), err,
new NullProgressMonitor());
}
if (target instanceof final FB fb && fb.eContainer() instanceof final BaseFBType base
&& base.getInternalFbs().contains(fb)) {
AbstractLiveSearchContext.executeAndSave(new UpdateInternalFBCommand(fb, newEntry), fb,
AbstractLiveSearchContext.executeAndSave(new UpdateInternalFBCommand(fb, selectedEntry), fb,
new NullProgressMonitor());
} else if (target instanceof final FBNetworkElement fbne) {
AbstractLiveSearchContext.executeAndSave(new UpdateFBTypeCommand(fbne, selectedEntry), fbne,
new NullProgressMonitor());
}
}
}

private void createNewEntry() {
private void selectTypeEntry() {
final DataTypeTreeSelectionDialog dialog = new DataTypeTreeSelectionDialog(
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),
FBTypeSelectionTreeContentProvider.INSTANCE);
dialog.setInput(getTypeLibrary());
if (dialog.open() == Window.OK && dialog.getFirstResult() instanceof final TypeNode node
&& !node.isDirectory()) {
newEntry = node.getType().getTypeEntry();
selectedEntry = node.getTypeEntry();
} else {
canceled = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Path;
Expand All @@ -26,14 +26,18 @@
import org.eclipse.fordiac.ide.model.commands.change.ChangeDataTypeCommand;
import org.eclipse.fordiac.ide.model.commands.change.ChangeStructCommand;
import org.eclipse.fordiac.ide.model.commands.change.ConfigureFBCommand;
import org.eclipse.fordiac.ide.model.data.DataType;
import org.eclipse.fordiac.ide.model.data.StructuredType;
import org.eclipse.fordiac.ide.model.errormarker.FordiacErrorMarker;
import org.eclipse.fordiac.ide.model.helpers.PackageNameHelper;
import org.eclipse.fordiac.ide.model.libraryElement.ConfigurableFB;
import org.eclipse.fordiac.ide.model.libraryElement.IInterfaceElement;
import org.eclipse.fordiac.ide.model.libraryElement.StructManipulator;
import org.eclipse.fordiac.ide.model.libraryElement.VarDeclaration;
import org.eclipse.fordiac.ide.model.search.AbstractLiveSearchContext;
import org.eclipse.fordiac.ide.model.typelibrary.DataTypeEntry;
import org.eclipse.fordiac.ide.model.typelibrary.TypeLibraryTags;
import org.eclipse.fordiac.ide.systemmanagement.SystemManager;
import org.eclipse.fordiac.ide.typemanagement.util.TypeFromTemplateCreator;
import org.eclipse.fordiac.ide.typemanagement.wizards.NewTypeWizard;
import org.eclipse.fordiac.ide.ui.FordiacMessages;

public class CreateDataTypeMarkerResolution extends AbstractErrorMarkerResolution {
Expand All @@ -54,29 +58,35 @@ public void run(final IMarker marker) {

if (newEntry != null) {
final EObject errorType = FordiacErrorMarker.getTarget(marker);
if (errorType instanceof final VarDeclaration target) {
if (errorType instanceof final IInterfaceElement target) {
AbstractLiveSearchContext.executeAndSave(ChangeDataTypeCommand.forDataType(target, newEntry.getType()),
target, new NullProgressMonitor());
} else if (errorType instanceof final StructManipulator fb && newEntry instanceof final DataType d) {
AbstractLiveSearchContext.executeAndSave(new ChangeStructCommand(fb, d), fb, new NullProgressMonitor());
} else if (errorType instanceof final ConfigurableFB fb && newEntry instanceof final DataType d) {
AbstractLiveSearchContext.executeAndSave(new ConfigureFBCommand(fb, d), fb, new NullProgressMonitor());
} else if (errorType instanceof final StructManipulator fb
&& newEntry.getType() instanceof final StructuredType struct) {
AbstractLiveSearchContext.executeAndSave(new ChangeStructCommand(fb, struct), fb,
new NullProgressMonitor());
} else if (errorType instanceof final ConfigurableFB fb) {
AbstractLiveSearchContext.executeAndSave(new ConfigureFBCommand(fb, newEntry.getType()), fb,
new NullProgressMonitor());
}
}
}

private void createNewEntry() {
final File template = new File(TEMPLATE_PATH);
final IFile targetFile = getTargetFile(FordiacErrorMarker.getData(this.marker)[0],
marker.getResource().getProject().getFullPath());
final TypeFromTemplateCreator creator = new TypeFromTemplateCreator(targetFile, template, ""); //$NON-NLS-1$
final String typeName = FordiacErrorMarker.getData(this.marker)[0];
final IFile targetFile = getTargetFile(typeName, marker.getResource().getProject());
final TypeFromTemplateCreator creator = new TypeFromTemplateCreator(targetFile, template,
PackageNameHelper.extractPackageName(typeName));
creator.createTypeFromTemplate(new NullProgressMonitor());
NewTypeWizard.openTypeEditor(targetFile);

newEntry = (DataTypeEntry) creator.getTypeEntry();
}

private static IFile getTargetFile(final String typeName, final IPath path) {
return ResourcesPlugin.getWorkspace().getRoot()
.getFile(new Path(path + File.separator + "Type Library" + File.separator + typeName + ".dtp")); //$NON-NLS-1$ //$NON-NLS-2$
private static IFile getTargetFile(final String typeName, final IProject project) {
return project.getFile(Path.fromOSString(SystemManager.TYPE_LIB_FOLDER_NAME)
.append(typeName.replace(PackageNameHelper.PACKAGE_NAME_DELIMITER, String.valueOf(IPath.SEPARATOR)))
.addFileExtension(TypeLibraryTags.DATA_TYPE_FILE_ENDING));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,10 @@ public CreateMissingFBMarkerResolution(final IMarker marker) {

@Override
public void run(final IMarker marker) {
final EObject target = FordiacErrorMarker.getTarget(marker);
updateTarget(target);
if (!marker.exists()) {
return;
}
run(new IMarker[] { marker }, new NullProgressMonitor());
}

private void updateTarget(final EObject target) {
Expand Down Expand Up @@ -75,12 +77,7 @@ public void run(final IMarker[] markers, final IProgressMonitor monitor) {
return;
}

if (markers[0].exists() && !errorName.equals(newEntry.getTypeName())) {
super.run(markers, monitor);
} else {
// markers got invalid
targets.forEach(this::updateTarget);
}
targets.forEach(this::updateTarget);
}

private void createNewEntry(final String defaultName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,17 @@ public LibraryElement getType() {
return null;
}

public TypeEntry getTypeEntry() {
if (type instanceof final TypeEntry typeEntry) {
return typeEntry;
}
if (type instanceof final LibraryElement libraryElement) {
return libraryElement.getTypeEntry();
}

return null;
}

public boolean isDirectory() {
return null == type;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,10 @@ public IContainer getSelectedContainer() {
return null;
}

public void setPackageName(final String packageName) {
packageNameText.setText(packageName);
}

public String getPackageName() {
return packageNameText.getText();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ public boolean performFinish() {
return false;
}

public static void openTypeEditor(final IFile file) {
final IEditorDescriptor desc = PlatformUI.getWorkbench().getEditorRegistry().getDefaultEditor(file.getName());
EditorUtils.openEditor(new FileEditorInput(file), desc.getId());
}

private IFile getTargetFile() {
final String typeName = page1.getFileName();
return ResourcesPlugin.getWorkspace().getRoot()
Expand All @@ -117,11 +122,6 @@ private static void templateNotAvailable(final String templatePath) {
mbx.open();
}

private static void openTypeEditor(final IFile file) {
final IEditorDescriptor desc = PlatformUI.getWorkbench().getEditorRegistry().getDefaultEditor(file.getName());
EditorUtils.openEditor(new FileEditorInput(file), desc.getId());
}

public TypeEntry getTypeEntry() {
return entry;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.util.Arrays;
import java.util.List;

import org.eclipse.fordiac.ide.model.helpers.PackageNameHelper;
import org.eclipse.fordiac.ide.model.typelibrary.TypeEntry;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TableViewer;
Expand All @@ -38,15 +39,16 @@ protected NewFBTypeWizardPage createNewFBTypeWizardPage() {
return new NewFBTypeWizardPage(new StructuredSelection());
}

public static TypeEntry showRestrictedNewTypeWizard(final Shell shell, final String name, final String fileEnding,
final String projectName) {
public static TypeEntry showRestrictedNewTypeWizard(final Shell shell, final String fullName,
final String fileEnding, final String projectName) {
final RestrictedNewTypeWizard wizard = new RestrictedNewTypeWizard();
final WizardDialog dialog = new WizardDialog(shell, wizard);
dialog.create();

final NewFBTypeWizardPage page = (NewFBTypeWizardPage) dialog.getCurrentPage();
page.setTemplateFileFilter(pathname -> pathname.getName().toUpperCase().endsWith(fileEnding.toUpperCase()));
page.setFileName(name);
page.setFileName(PackageNameHelper.extractPlainTypeName(fullName));
page.setPackageName(PackageNameHelper.extractPackageName(fullName));

final TableViewer tv = page.getTemplateViewer();
final Object selection = tv.getElementAt(0);
Expand Down

0 comments on commit 50259bb

Please sign in to comment.