Skip to content

Commit

Permalink
[1146] Add support for EditingContext actions
Browse files Browse the repository at this point in the history
Bug: eclipse-sirius#1146
Signed-off-by: Raphaël Pagé <raphael.page@obeo.fr>
  • Loading branch information
RaphaelPageObeo authored and sbegaudeau committed Apr 13, 2022
1 parent 9a96908 commit 45a4c76
Show file tree
Hide file tree
Showing 13 changed files with 666 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*******************************************************************************
* Copyright (c) 2022 Obeo.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/
package org.eclipse.sirius.components.collaborative.api;

import org.eclipse.sirius.components.collaborative.dto.InvokeEditingContextActionInput;
import org.eclipse.sirius.components.core.api.IEditingContext;
import org.eclipse.sirius.components.representations.IStatus;

/**
* Process the given {@link InvokeEditingContextActionInput}.
*
* @author rpage
*/
public interface IEditingContextActionHandler {
boolean canHandle(IEditingContext editingContext, String actionId);

IStatus handle(IEditingContext editingContext, String actionId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*******************************************************************************
* Copyright (c) 2022 Obeo.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/
package org.eclipse.sirius.components.collaborative.api;

import java.util.List;

import org.eclipse.sirius.components.collaborative.dto.EditingContextAction;
import org.eclipse.sirius.components.core.api.IEditingContext;

/**
* Used to provide the EditingContext Actions of an {@link IEditingContext}.
*
* @author rpage
*/
public interface IEditingContextActionProvider {
List<EditingContextAction> getEditingContextAction(IEditingContext editingContext);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*******************************************************************************
* Copyright (c) 2022 Obeo.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/
package org.eclipse.sirius.components.collaborative.dto;

import java.text.MessageFormat;
import java.util.Objects;

/**
* Details of an EditingContext Action.
*
* @author rpage
*/
public class EditingContextAction {
private final String id;

private final String label;

public EditingContextAction(String id, String label) {
this.id = Objects.requireNonNull(id);
this.label = Objects.requireNonNull(label);
}

public String getId() {
return this.id;
}

public String getLabel() {
return this.label;
}

@Override
public String toString() {
String pattern = "{0} '{'id: {1}, label: {2}'}'"; //$NON-NLS-1$
return MessageFormat.format(pattern, this.getClass().getSimpleName(), this.id, this.label);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*******************************************************************************
* Copyright (c) 2022 Obeo.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/
package org.eclipse.sirius.components.collaborative.dto;

import java.text.MessageFormat;
import java.util.Objects;
import java.util.UUID;

import org.eclipse.sirius.components.core.api.IInput;

/**
* The input object for this query.
*
* @author rpage
*/
public final class GetEditingContextActionsInput implements IInput {
private final UUID id;

private final String editingContextId;

public GetEditingContextActionsInput(UUID id, String editingContextId) {
this.id = Objects.requireNonNull(id);
this.editingContextId = Objects.requireNonNull(editingContextId);
}

@Override
public UUID getId() {
return this.id;
}

public String getEditingContextId() {
return this.editingContextId;
}

@Override
public String toString() {
String pattern = "{0} '{'id: {1}, editingContextId: {2}'}'"; //$NON-NLS-1$
return MessageFormat.format(pattern, this.getClass().getSimpleName(), this.id, this.editingContextId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*******************************************************************************
* Copyright (c) 2022 Obeo.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/
package org.eclipse.sirius.components.collaborative.dto;

import java.text.MessageFormat;
import java.util.List;
import java.util.Objects;
import java.util.UUID;

import org.eclipse.sirius.components.core.api.IPayload;

/**
* The payload object for this query.
*
* @author rpage
*/
public final class GetEditingContextActionsSuccessPayload implements IPayload {
private final UUID id;

private final List<EditingContextAction> editingContextActions;

public GetEditingContextActionsSuccessPayload(UUID id, List<EditingContextAction> editingContextActions) {
this.id = Objects.requireNonNull(id);
this.editingContextActions = Objects.requireNonNull(editingContextActions);
}

@Override
public UUID getId() {
return this.id;
}

public List<EditingContextAction> getEditingContextActions() {
return this.editingContextActions;
}

@Override
public String toString() {
String pattern = "{0} '{'id: {1}, editingContextActions: {2}'}'"; //$NON-NLS-1$
return MessageFormat.format(pattern, this.getClass().getSimpleName(), this.id, this.editingContextActions);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*******************************************************************************
* Copyright (c) 2022 Obeo.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/
package org.eclipse.sirius.components.collaborative.dto;

import java.text.MessageFormat;
import java.util.UUID;

import org.eclipse.sirius.components.collaborative.handlers.InvokeEditingContextActionEventHandler;
import org.eclipse.sirius.components.core.api.IInput;

/**
* The input object of the {@link InvokeEditingContextActionEventHandler}.
*
* @author rpage
*/
public final class InvokeEditingContextActionInput implements IInput {
private UUID id;

private String editingContextId;

private String actionId;

@Override
public UUID getId() {
return this.id;
}

public String getEditingContextId() {
return this.editingContextId;
}

public String getActionId() {
return this.actionId;
}

@Override
public String toString() {
String pattern = "{0} '{'id: {1}, editingContextId: {2}, actionId: {3}'}'"; //$NON-NLS-1$
return MessageFormat.format(pattern, this.getClass().getSimpleName(), this.id, this.editingContextId, this.actionId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*******************************************************************************
* Copyright (c) 2022 Obeo.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/
package org.eclipse.sirius.components.collaborative.dto;

import java.text.MessageFormat;
import java.util.Objects;
import java.util.UUID;

import org.eclipse.sirius.components.core.api.IPayload;

/**
* The payload of the Invoke Editing Context Action mutation.
*
* @author rpage
*/
public final class InvokeEditingContextActionSuccessPayload implements IPayload {
private final UUID id;

public InvokeEditingContextActionSuccessPayload(UUID id) {
this.id = Objects.requireNonNull(id);
}

@Override
public UUID getId() {
return this.id;
}

@Override
public String toString() {
String pattern = "{0} '{'id: {1}'}'"; //$NON-NLS-1$
return MessageFormat.format(pattern, this.getClass().getSimpleName(), this.id);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*******************************************************************************
* Copyright (c) 2022 Obeo.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/
package org.eclipse.sirius.components.collaborative.handlers;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import org.eclipse.sirius.components.collaborative.api.ChangeDescription;
import org.eclipse.sirius.components.collaborative.api.ChangeKind;
import org.eclipse.sirius.components.collaborative.api.IEditingContextActionProvider;
import org.eclipse.sirius.components.collaborative.api.IEditingContextEventHandler;
import org.eclipse.sirius.components.collaborative.api.Monitoring;
import org.eclipse.sirius.components.collaborative.dto.EditingContextAction;
import org.eclipse.sirius.components.collaborative.dto.GetEditingContextActionsInput;
import org.eclipse.sirius.components.collaborative.dto.GetEditingContextActionsSuccessPayload;
import org.eclipse.sirius.components.collaborative.messages.ICollaborativeMessageService;
import org.eclipse.sirius.components.core.api.ErrorPayload;
import org.eclipse.sirius.components.core.api.IEditingContext;
import org.eclipse.sirius.components.core.api.IInput;
import org.eclipse.sirius.components.core.api.IPayload;
import org.springframework.stereotype.Service;

import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.MeterRegistry;
import reactor.core.publisher.Sinks.Many;
import reactor.core.publisher.Sinks.One;

/**
* Handler used to access to the EditingContext Actions.
*
* @author rpage
*/
@Service
public class GetEditingContextActionsEventHandler implements IEditingContextEventHandler {
private final ICollaborativeMessageService messageService;

private final Counter counter;

private final List<IEditingContextActionProvider> editingContextActionProviders;

public GetEditingContextActionsEventHandler(ICollaborativeMessageService messageService, List<IEditingContextActionProvider> editingContextActionProviders, MeterRegistry meterRegistry) {
this.messageService = Objects.requireNonNull(messageService);
this.editingContextActionProviders = Objects.requireNonNull(editingContextActionProviders);

// @formatter:off
this.counter = Counter.builder(Monitoring.EVENT_HANDLER)
.tag(Monitoring.NAME, this.getClass().getSimpleName())
.register(meterRegistry);
// @formatter:on
}

@Override
public boolean canHandle(IEditingContext editingContext, IInput input) {
return input instanceof GetEditingContextActionsInput;
}

@Override
public void handle(One<IPayload> payloadSink, Many<ChangeDescription> changeDescriptionSink, IEditingContext editingContext, IInput input) {
this.counter.increment();

String message = this.messageService.invalidInput(input.getClass().getSimpleName(), GetEditingContextActionsInput.class.getSimpleName());
IPayload payload = new ErrorPayload(input.getId(), message);
ChangeDescription changeDescription = new ChangeDescription(ChangeKind.NOTHING, editingContext.getId(), input);

if (input instanceof GetEditingContextActionsInput) {
GetEditingContextActionsInput editingContextActionsInput = (GetEditingContextActionsInput) input;

// @formatter:off
List<EditingContextAction> editingContextActions = this.editingContextActionProviders.stream()
.flatMap(provider -> provider.getEditingContextAction(editingContext).stream())
.collect(Collectors.toList());
// @formatter:on

editingContextActions.sort((a1, a2) -> a1.getLabel().compareTo(a2.getLabel()));

payload = new GetEditingContextActionsSuccessPayload(editingContextActionsInput.getId(), editingContextActions);
changeDescription = new ChangeDescription(ChangeKind.NOTHING, editingContext.getId(), input);
}

payloadSink.tryEmitValue(payload);
changeDescriptionSink.tryEmitNext(changeDescription);
}
}
Loading

0 comments on commit 45a4c76

Please sign in to comment.