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

Draft: Refactoring change preview - Editor integration #928

Draft
wants to merge 25 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
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
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,5 @@ Bundle-ManifestVersion: 2
Bundle-SymbolicName: org.eclipse.fordiac.ide.application;singleton:=true
Bundle-RequiredExecutionEnvironment: JavaSE-21
Automatic-Module-Name: org.eclipse.fordiac.ide.application
Import-Package: org.eclipse.ltk.core.refactoring,
org.eclipse.ltk.ui.refactoring
14 changes: 14 additions & 0 deletions plugins/org.eclipse.fordiac.ide.application/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1857,4 +1857,18 @@
type="java.lang.Object">
</propertyTester>
</extension>
<extension
point="org.eclipse.ltk.ui.refactoring.changePreviewViewers">
<changePreviewViewer
class="org.eclipse.fordiac.ide.applications.previews.ProjectChangePreviewViewer"
id="org.eclipse.fordiac.ide.applications.previews.ProjectChangePreviewViewer">
<enablement>
<or>
<instanceof
value="org.eclipse.fordiac.ide.typemanagement.refactoring.UpdateInstancesChange">
</instanceof>
</or>
</enablement>
</changePreviewViewer>
</extension>
</plugin>
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
* Contributors:
* Alois Zoitl, Filip Andren
* - initial API and implementation and/or initial documentation
* Martin Schwarz
* - Build fixes
*******************************************************************************/
package org.eclipse.fordiac.ide.application.actions;

Expand All @@ -22,6 +24,7 @@
import org.eclipse.gef.EditPart;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.gef.GraphicalViewer;
import org.eclipse.gef.editparts.AbstractGraphicalEditPart;
import org.eclipse.gef.ui.actions.SelectAllAction;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.ui.IWorkbenchPart;
Expand All @@ -48,7 +51,7 @@ private static List<EditPart> getSelectableEditParts(final GraphicalViewer viewe

for (final Object child : children) {
if ((child instanceof AbstractFBNElementEditPart) || (child instanceof GroupEditPart)) {
final GraphicalEditPart childPart = (GraphicalEditPart) child;
final EditPart childPart = (EditPart) child;
if (childPart.isSelectable()) {
selectableChildren.add(childPart);
addConnectionsTo(selectableChildren, childPart);
Expand All @@ -58,14 +61,19 @@ private static List<EditPart> getSelectableEditParts(final GraphicalViewer viewe
return Collections.unmodifiableList(selectableChildren);
}

private static void addConnectionsTo(final List<EditPart> selectableChildren, final GraphicalEditPart child) {
private static void addConnectionsTo(final List<EditPart> selectableChildren, final EditPart child) {
// the editparts are in charge of managing the connections if we take all source
// connections
// from one edit part we should get all connections in the end.

for (final GraphicalEditPart elementChild : child.getChildren()) {
elementChild.getSourceConnections().stream().filter(EditPart::isSelectable)
.forEach(selectableChildren::add);
final List<?> elementChildren = child.getChildren();
for (final Object elementChild : elementChildren) {
if (elementChild instanceof AbstractGraphicalEditPart) {
final List<? extends GraphicalEditPart> connections = ((AbstractGraphicalEditPart) elementChild)
.getSourceConnections();
connections.stream().filter(EditPart::isSelectable).forEach(selectableChildren::add);
}

}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*
* Contributors:
* Fabio Gandolfi - initial implementation and/or documentation
* Martin Schwarz - Build fixes
*******************************************************************************/
package org.eclipse.fordiac.ide.application.commands;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public void notifyChanged(final Notification notification) {

@Override
public void setInput(final IEditorInput input) {
System.out.println("ApplicationEditorInput called");
if (input instanceof final ApplicationEditorInput appInput) {
final Application app = appInput.getContent();
setModel(app.getFBNetwork());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ public FBNetwork getModel() {
* Instantiates a new fB network editor.
*/
public FBNetworkEditor() {
System.out.println("FBNetworkEditor called");

// empty constructor
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* Alois Zoitl - extracted most code into common base class for group
* infrastructure
* - extracted this policy from the AbstractContainerContentEditPart
* Martin Schwarz - Build fixes
*******************************************************************************/
package org.eclipse.fordiac.ide.application.policies;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
/*******************************************************************************
* Copyright (c) 2024 Johannes Kepler University
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Martin Schwarz - initial implementation
*******************************************************************************/
package org.eclipse.fordiac.ide.applications.previews;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.FigureCanvas;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Polyline;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.fordiac.ide.application.editparts.ElementEditPartFactory;
import org.eclipse.fordiac.ide.model.libraryElement.AutomationSystem;
import org.eclipse.fordiac.ide.model.libraryElement.FBNetwork;
import org.eclipse.fordiac.ide.model.libraryElement.FBNetworkElement;
import org.eclipse.fordiac.ide.model.ui.editors.AdvancedScrollingGraphicalViewer;
import org.eclipse.fordiac.ide.systemmanagement.SystemManager;
import org.eclipse.fordiac.ide.typemanagement.refactoring.UpdateInstancesChange;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.editparts.AbstractGraphicalEditPart;
import org.eclipse.gef.editparts.ScalableFreeformRootEditPart;
import org.eclipse.gef.ui.parts.GraphicalEditor;
import org.eclipse.ltk.ui.refactoring.ChangePreviewViewerInput;
import org.eclipse.ltk.ui.refactoring.IChangePreviewViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;

@SuppressWarnings("restriction")
public class ProjectChangePreviewViewer implements IChangePreviewViewer {

private SashForm control;
private Composite parent;

private AdvancedScrollingGraphicalViewer graphicalViewer;

@Override
public void createControl(final Composite parent) {
parent.setLayout(new GridLayout(1, false));

this.parent = parent;

resetControl();
}

private void resetControl() {
if (control != null) {
control.dispose();
}

control = new SashForm(parent, SWT.VERTICAL);
control.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

final Composite compl = new Composite(control, SWT.NONE);
compl.setLayout(new GridLayout());
final Label labell = new Label(compl, SWT.NONE);
graphicalViewer = new AdvancedScrollingGraphicalViewer();
graphicalViewer.createControl(compl);
graphicalViewer.getControl().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
parent.layout(true, true);
}

@Override
public Control getControl() {
return control;
}

@Override
public void setInput(final ChangePreviewViewerInput input) {
if (!(input.getChange() instanceof UpdateInstancesChange)) {
return;
}

final UpdateInstancesChange change = (UpdateInstancesChange) input.getChange();

final AutomationSystem system = SystemManager.INSTANCE.getProjectSystems(change.getProject()).getFirst();

final FBNetwork network = system.getApplication().getFirst().getFBNetwork();

final GraphicalEditor mockEditor = new GraphicalEditor() {

@Override
protected void initializeGraphicalViewer() {
// No initialization needed for mock
}

@Override
public void doSave(final IProgressMonitor monitor) {
// No save final operation needed for mock

}

};

resetControl();

graphicalViewer.setRootEditPart(new ScalableFreeformRootEditPart());
graphicalViewer.setEditPartFactory(new ElementEditPartFactory(mockEditor));
graphicalViewer.setContents(network); // exception here

drawRedRectangleForElement(graphicalViewer, change.getFBNetworkElement());
}

private void resetGraphicalViewer(final AdvancedScrollingGraphicalViewer viewer) {
if (viewer != null) {
viewer.setContents(null);
if (viewer.getControl() != null) {
viewer.getControl().setRedraw(false);
viewer.getControl().dispose();
}
}
}

private void drawRedRectangleForElement(final AdvancedScrollingGraphicalViewer viewer, final FBNetworkElement fb) {
if (viewer != null && fb != null) {
final EditPart editPart = viewer.getEditPartRegistry().get(fb);

if (editPart != null) {
final IFigure figure = ((AbstractGraphicalEditPart) editPart).getFigure();
if (figure != null) {
// Ensure layout is complete before accessing bounds
viewer.getControl().getDisplay().asyncExec(() -> {
final Rectangle bounds = figure.getBounds();

figure.translateToAbsolute(bounds);

createRedBorder(viewer, bounds);

final FigureCanvas canvas = (FigureCanvas) viewer.getControl();
if (canvas != null) {
canvas.scrollTo(bounds.x - 10, bounds.y - 10);
}

});
}
}
}
}

private void createRedBorder(final AdvancedScrollingGraphicalViewer viewer, final Rectangle bounds) {
final int padding = 5;

final Polyline topLine = new Polyline();
topLine.addPoint(new Point(bounds.x - padding, bounds.y - padding));
topLine.addPoint(new Point(bounds.x + bounds.width + padding, bounds.y - padding));
topLine.setForegroundColor(ColorConstants.red);
topLine.setLineWidth(2);

final Polyline bottomLine = new Polyline();
bottomLine.addPoint(new Point(bounds.x - padding, bounds.y + bounds.height + padding));
bottomLine.addPoint(new Point(bounds.x + bounds.width + padding, bounds.y + bounds.height + padding));
bottomLine.setForegroundColor(ColorConstants.red);
bottomLine.setLineWidth(2);

final Polyline leftLine = new Polyline();
leftLine.addPoint(new Point(bounds.x - padding, bounds.y - padding));
leftLine.addPoint(new Point(bounds.x - padding, bounds.y + bounds.height + padding));
leftLine.setForegroundColor(ColorConstants.red);
leftLine.setLineWidth(2);

final Polyline rightLine = new Polyline();
rightLine.addPoint(new Point(bounds.x + bounds.width + padding, bounds.y - padding));
rightLine.addPoint(new Point(bounds.x + bounds.width + padding, bounds.y + bounds.height + padding));
rightLine.setForegroundColor(ColorConstants.red);
rightLine.setLineWidth(2);

final FigureCanvas canvas = (FigureCanvas) viewer.getControl();
if (canvas != null) {
canvas.getContents().add(topLine);
canvas.getContents().add(bottomLine);
canvas.getContents().add(leftLine);
canvas.getContents().add(rightLine);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* Contributors:
* Alois Zoitl - initial API and implementation and/or initial documentation
* Martin Jobst - refactor evaluator API
* Martin Schwarz - Build fixes
*******************************************************************************/
package org.eclipse.fordiac.ide.debug.ui.view.editparts;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ Require-Bundle: org.eclipse.ui,
org.eclipse.xtext.ui,
org.eclipse.nebula.widgets.nattable.core,
org.eclipse.fordiac.ide.model.search,
org.eclipse.fordiac.ide.typeeditor
org.eclipse.fordiac.ide.typeeditor,
org.eclipse.ltk.ui.refactoring
Bundle-Vendor: Eclipse 4diac
Bundle-ActivationPolicy: lazy
Bundle-Version: 3.0.0.qualifier
Expand Down
14 changes: 14 additions & 0 deletions plugins/org.eclipse.fordiac.ide.fbtypeeditor/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -175,5 +175,19 @@
</propertySection>
</propertySections>
</extension>
<extension
point="org.eclipse.ltk.ui.refactoring.changePreviewViewers">
<changePreviewViewer
class="org.eclipse.fordiac.ide.fbtypeeditor.previews.InterfaceDataTypeChangePreviewViewer"
id="org.eclipse.fordiac.ide.fbtypeeditor.previews.InterfaceDataTypeChangePreviewViewer">
<enablement>
<or>
<instanceof
value="org.eclipse.fordiac.ide.typemanagement.refactoring.InterfaceDataTypeChange">
</instanceof>
</or>
</enablement>
</changePreviewViewer>
</extension>

</plugin>
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public FBInterfaceEditPartFactory(final GraphicalEditor editor, final TypeLibrar

@Override
protected EditPart getPartForElement(final EditPart context, final Object modelElement) {
System.out.println("Loaded editor for:" + modelElement.getClass());
if (modelElement instanceof FBType && context == null) {
return new FBTypeRootEditPart();
}
Expand Down
Loading