Skip to content

Commit

Permalink
remove code duplications obsolete after Sprotty update
Browse files Browse the repository at this point in the history
  • Loading branch information
NiklasRentzCAU committed Mar 14, 2024
1 parent ccf948c commit 941eacd
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 225 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
*/
package de.cau.cs.kieler.klighd.lsp

import com.google.common.base.Strings
import com.google.common.base.Throwables
import com.google.common.io.ByteStreams
import com.google.inject.Inject
Expand All @@ -28,7 +27,6 @@ import de.cau.cs.kieler.klighd.ViewContext
import de.cau.cs.kieler.klighd.kgraph.KNode
import de.cau.cs.kieler.klighd.lsp.interactive.layered.LayeredInteractiveActionHandler
import de.cau.cs.kieler.klighd.lsp.interactive.rectpacking.RectpackingInteractiveActionHandler
import de.cau.cs.kieler.klighd.lsp.launch.AbstractLanguageServer
import de.cau.cs.kieler.klighd.lsp.model.CheckImagesAction
import de.cau.cs.kieler.klighd.lsp.model.CheckedImagesAction
import de.cau.cs.kieler.klighd.lsp.model.DisplayedActionUIData
Expand All @@ -51,8 +49,6 @@ import java.util.Base64
import java.util.Collection
import java.util.List
import java.util.Map
import java.util.concurrent.CompletableFuture
import org.apache.log4j.Logger
import org.eclipse.core.runtime.Platform
import org.eclipse.elk.core.data.LayoutMetaDataService
import org.eclipse.elk.core.data.LayoutOptionData
Expand All @@ -68,10 +64,7 @@ import org.eclipse.sprotty.IDiagramSelectionListener
import org.eclipse.sprotty.ILayoutEngine
import org.eclipse.sprotty.IModelUpdateListener
import org.eclipse.sprotty.IPopupModelFactory
import org.eclipse.sprotty.LayoutAction
import org.eclipse.sprotty.RejectAction
import org.eclipse.sprotty.RequestBoundsAction
import org.eclipse.sprotty.RequestModelAction
import org.eclipse.sprotty.SModelCloner
import org.eclipse.sprotty.SModelElement
import org.eclipse.sprotty.SModelRoot
Expand All @@ -88,7 +81,6 @@ import org.eclipse.xtend.lib.annotations.Accessors
* @author nre
*/
class KGraphDiagramServer extends LanguageAwareDiagramServer {
static val LOG = Logger.getLogger(KGraphDiagramServer)

@Inject
protected LayeredInteractiveActionHandler constraintActionHandler
Expand Down Expand Up @@ -121,31 +113,6 @@ class KGraphDiagramServer extends LanguageAwareDiagramServer {
@Accessors(PUBLIC_GETTER)
protected Object modelLock = new Object

/**
* Needed for KeithUpdateModelAction
*
* FIXME Remove this if UpdateModelAction has a cause.
*/
protected String lastSubmittedModelType

/**
* Needed for KeithUpdateModelAction
*
* FIXME Remove this if UpdateModelAction has a cause.
*/
protected int revision = 0

/**
* Needed for KeithUpdateModelAction
*
* FIXME Remove this if UpdateModelAction has a cause.
*/
new() {
currentRoot = new SModelRoot();
currentRoot.setType("NONE");
currentRoot.setId("ROOT");
}

/**
* Prepares the client side update of the model by processing the potentially needed images on the client. Checks
* for client-side cached images with the {@link CheckImagesAction}. If the corresponding response to the
Expand Down Expand Up @@ -269,149 +236,6 @@ class KGraphDiagramServer extends LanguageAwareDiagramServer {
notificationHandler.sendErrorAndThrow(e)
}
}

/**
* Submit a new or updated model to the client. If client layout is required, a {@link RequestBoundsAction}
* is sent, otherwise either a {@link SetModelAction} or an {@link UpdateModelAction} is sent depending on
* the {@code update} parameter.
* Needed for KeithUpdateModelAction
*
* FIXME Remove this if UpdateModelAction has a cause.
*/
override CompletableFuture<Void> submitModel(SModelRoot newRoot, boolean update, Action cause) {
if (needsClientLayout(newRoot)) {
if (!needsServerLayout(newRoot, cause)) {
// In this case the client won't send us the computed bounds
dispatch(new RequestBoundsAction(newRoot));
val IModelUpdateListener listener = getModelUpdateListener();
if (listener !== null) {
listener.modelSubmitted(newRoot, this);
}
} else {
return request(new RequestBoundsAction(newRoot)).handle([response, exception | {
if (exception !== null) {
LOG.error("RequestBoundsAction failed with an exception.", exception);
} else {
try {
var SModelRoot model = handle(response);
if (model !== null)
doSubmitModel(model, true, cause);
} catch (Exception exc) {
LOG.error("Exception while processing ComputedBoundsAction.", exc);
}
}
return null;
}])
}
} else {
doSubmitModel(newRoot, update, cause);
}
return CompletableFuture.completedFuture(null);
}

/**
* Needed for KeithUpdateModelAction
*
* FIXME Remove this if UpdateModelAction has a cause.
*/
override CompletableFuture<Void> setModel(SModelRoot newRoot) {
if (newRoot === null)
throw new NullPointerException();
synchronized(modelLock) {
newRoot.setRevision(revision + 1);
revision++
currentRoot = newRoot;
}
return submitModel(newRoot, false, null);
}

/**
* Needed for KeithUpdateModelAction
*
* FIXME Remove this if UpdateModelAction has a cause.
*/
override CompletableFuture<Void> updateModel(SModelRoot newRoot) {
if (newRoot === null)
throw new IllegalArgumentException("updateModel() cannot be called with null");
synchronized(modelLock) {
currentRoot = newRoot;
newRoot.setRevision(revision + 1);
revision++
}
return submitModel(newRoot, true, null);
}


/**
* Needed for KeithUpdateModelAction
*
* FIXME Remove this if UpdateModelAction has a cause.
*/
def void doSubmitModel(SModelRoot newRoot, boolean update, Action cause) {
val ILayoutEngine layoutEngine = getLayoutEngine();
if (needsServerLayout(newRoot, cause)) {
AbstractLanguageServer.addToMainThreadQueue([
layoutEngine.layout(newRoot, cause)
])
}
synchronized (modelLock) {
if (newRoot.getRevision() == revision) {
var String modelType = newRoot.getType();
if (cause instanceof RequestModelAction
&& !Strings.isNullOrEmpty((cause as RequestModelAction).getRequestId())) {
var RequestModelAction request = cause as RequestModelAction;
var SetModelAction response = new SetModelAction(newRoot);
response.setResponseId(request.getRequestId());
dispatch(response);
} else if (update && modelType !== null && modelType.equals(lastSubmittedModelType)) {
dispatch(new UpdateModelAction(newRoot));
} else {
dispatch(new SetModelAction(newRoot));
}
lastSubmittedModelType = modelType;
var IModelUpdateListener listener = getModelUpdateListener();
if (listener !== null) {
listener.modelSubmitted(newRoot, this);
}
}
}
}

/**
* Taken from {@code DefaultDiagramServer.handle(RequestModelAction)} to use this getModel.
* Needed for KeithUpdateModelAction
*
* FIXME Remove this if UpdateModelAction has a cause.
*/
override protected handle(RequestModelAction request) {
if (diagramLanguageServer !== null) {
copyOptions(request)
diagramLanguageServer.diagramUpdater.updateDiagram(this, request)
} else {
super.handle(request)
}
}

/**
* Taken from {@code DefaultDiagramServer.handle(LayoutAction)} to use this getModel.
* Needed for KeithUpdateModelAction
*
* FIXME Remove this if UpdateModelAction has a cause.
*/
override void handle(LayoutAction action) {
if (needsServerLayout(model,action)) {
// Clone the current model, as it has already been sent to the client with the old revision
val SModelCloner cloner = getSModelCloner();
val SModelRoot newRoot = cloner.clone(model);
synchronized(modelLock) {
newRoot.setRevision(revision + 1);
revision++
currentRoot = newRoot;
}
// the actual layout is performed in doSubmitModel
doSubmitModel(newRoot, true, action);
}
}

/**
* Called when a {@link PerformActionAction} is received.
Expand Down Expand Up @@ -632,15 +456,6 @@ class KGraphDiagramServer extends LanguageAwareDiagramServer {
}
newModel = false
}

/**
* Needed for KeithUpdateModelAction
*
* FIXME Remove this if UpdateModelAction has a cause.
*/
override SModelRoot getModel() {
return currentRoot;
}

// Repeat injection of multiple methods of the DefaultDiagramServer as the javax.inject->jakarta.inject transition
// broke something here.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*
* http://rtsys.informatik.uni-kiel.de/kieler
*
* Copyright 2018-2023 by
* Copyright 2018-2024 by
* + Kiel University
* + Department of Computer Science
* + Real-Time and Embedded Systems Group
Expand All @@ -18,6 +18,7 @@ package de.cau.cs.kieler.klighd.lsp

import com.google.inject.Inject
import de.cau.cs.kieler.klighd.LightDiagramLayoutConfig
import de.cau.cs.kieler.klighd.lsp.launch.AbstractLanguageServer
import de.cau.cs.kieler.klighd.lsp.utils.KGraphMappingUtil
import de.cau.cs.kieler.klighd.lsp.utils.RenderingPreparer
import java.io.ByteArrayOutputStream
Expand Down Expand Up @@ -48,16 +49,18 @@ class KGraphLayoutEngine extends ElkLayoutEngine {
public static val LOG = Logger.getLogger(KGraphLayoutEngine)

override layout(SModelRoot root, Action cause) {
synchronized (diagramState) {
if (root instanceof SGraph) {
// The layout is executed on the KGraph, not the SGraph. So get the KGraph belonging to this SGraph from
// the KGraphContext.
onlyLayoutOnKGraph(root.id)

// map layouted KGraph to SGraph
KGraphMappingUtil.mapLayout(diagramState.getKGraphToSModelElementMap(root.id))
AbstractLanguageServer.addToMainThreadQueue([
synchronized (diagramState) {
if (root instanceof SGraph) {
// The layout is executed on the KGraph, not the SGraph. So get the KGraph belonging to this SGraph from
// the KGraphContext.
onlyLayoutOnKGraph(root.id)

// map layouted KGraph to SGraph
KGraphMappingUtil.mapLayout(diagramState.getKGraphToSModelElementMap(root.id))
}
}
}
])
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,36 +239,6 @@ class RefreshLayoutAction implements Action {
}
}

/**
* Updates the model and sends the cause to the client.
* Extends to UpdateModelAction.
* FIXME Remove this if the UpdateModelAction includes a cause.
*
* @author sdo
*/
@Accessors
@EqualsHashCode
@ToString(skipNulls = true)
public class KeithUpdateModelAction extends UpdateModelAction {
public static val KIND = 'updateModel'
String kind = KIND


SModelRoot newRoot
Boolean animate
Action cause

new() {}
new(Consumer<KeithUpdateModelAction> initializer) {
initializer.accept(this)
}

new(SModelRoot newRoot, Action cause) {
this.newRoot = newRoot
this.cause = cause
}
}

/**
* Sent from client to request a certain piece of the diagram.
*
Expand Down

0 comments on commit 941eacd

Please sign in to comment.