Skip to content

Commit

Permalink
[1439] Add support for toolbar actions (buttons) on groups
Browse files Browse the repository at this point in the history
Bug: #1439
Signed-off-by: Pierre-Charles David <pierre-charles.david@obeo.fr>
Signed-off-by: Axel RICHARD <axel.richard@obeo.fr>
  • Loading branch information
pcdavid authored and AxelRICHARD committed Nov 8, 2022
1 parent 8547f95 commit a55f3d2
Show file tree
Hide file tree
Showing 13 changed files with 252 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ type Group {
id: ID!
label: String!
displayMode: GroupDisplayMode!
toolbarActions: [Button!]!
widgets: [Widget!]!
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public final class Group {

private GroupDisplayMode displayMode;

private List<Button> toolbarActions;

private List<AbstractWidget> widgets;

private Group() {
Expand All @@ -45,6 +47,10 @@ public String getLabel() {
return this.label;
}

public List<Button> getToolbarActions() {
return this.toolbarActions;
}

public List<AbstractWidget> getWidgets() {
return this.widgets;
}
Expand All @@ -59,8 +65,8 @@ public static Builder newGroup(String id) {

@Override
public String toString() {
String pattern = "{0} '{'id: {1}, label: {2}, displayMode: {3}, widgetCount: {4}'}'"; //$NON-NLS-1$
return MessageFormat.format(pattern, this.getClass().getSimpleName(), this.id, this.label, this.displayMode, this.widgets.size());
String pattern = "{0} '{'id: {1}, label: {2}, displayMode: {3}, toolbarActionsCount: {4}, widgetCount: {5}'}'"; //$NON-NLS-1$
return MessageFormat.format(pattern, this.getClass().getSimpleName(), this.id, this.label, this.displayMode, this.toolbarActions.size(), this.widgets.size());
}

/**
Expand All @@ -76,6 +82,8 @@ public static final class Builder {

private GroupDisplayMode displayMode = GroupDisplayMode.LIST;

private List<Button> toolbarActions = List.of();

private List<AbstractWidget> widgets;

private Builder(String id) {
Expand All @@ -92,6 +100,11 @@ public Builder displayMode(GroupDisplayMode displayMode) {
return this;
}

public Builder toolbarActions(List<Button> toolbarActions) {
this.toolbarActions = Objects.requireNonNull(toolbarActions);
return this;
}

public Builder widgets(List<AbstractWidget> widgets) {
this.widgets = Objects.requireNonNull(widgets);
return this;
Expand All @@ -103,6 +116,7 @@ public Group build() {
group.label = Objects.requireNonNull(this.label);
group.displayMode = Objects.requireNonNull(this.displayMode);
group.widgets = Objects.requireNonNull(this.widgets);
group.toolbarActions = Objects.requireNonNull(this.toolbarActions);
return group;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*******************************************************************************
* 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.forms;

import java.util.List;
import java.util.Objects;

import org.eclipse.sirius.components.annotations.Immutable;

/**
* "Virtual" container used only during the rendering process to distinguish toolbar actions from the actual contents of
* a Group.
*
* @author pcdavid
*/
@Immutable
public final class GroupContents {
public static final String TYPE = "GroupContents"; //$NON-NLS-1$

private List<AbstractWidget> widgets;

public GroupContents(List<AbstractWidget> widgets) {
this.widgets = Objects.requireNonNull(widgets);
}

public List<AbstractWidget> getWidgets() {
return this.widgets;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*******************************************************************************
* 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.forms;

import java.util.List;
import java.util.Objects;

import org.eclipse.sirius.components.annotations.Immutable;

/**
* "Virtual" container used only during the rendering process to distinguish toolbar actions from the actual contents of
* a Group.
*
* @author pcdavid
*/
@Immutable
public final class GroupToolbar {
public static final String TYPE = "GroupToolbar"; //$NON-NLS-1$

private List<Button> toolbarActions;

public GroupToolbar(List<Button> toolbarActions) {
this.toolbarActions = Objects.requireNonNull(toolbarActions);
}

public List<Button> getToolbarActions() {
return this.toolbarActions;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.eclipse.sirius.components.forms.GroupDisplayMode;
import org.eclipse.sirius.components.forms.description.AbstractControlDescription;
import org.eclipse.sirius.components.forms.description.AbstractWidgetDescription;
import org.eclipse.sirius.components.forms.description.ButtonDescription;
import org.eclipse.sirius.components.forms.description.ForDescription;
import org.eclipse.sirius.components.forms.description.GroupDescription;
import org.eclipse.sirius.components.forms.elements.GroupElementProps;
Expand Down Expand Up @@ -54,6 +55,7 @@ public Element render() {
WidgetIdCounter widgetIdCounter = new WidgetIdCounter();

List<?> semanticElements = groupDescription.getSemanticElementsProvider().apply(variableManager);

List<Element> children = new ArrayList<>(semanticElements.size());

for (Object semanticElement : semanticElements) {
Expand All @@ -65,21 +67,30 @@ public Element render() {
String label = groupDescription.getLabelProvider().apply(groupVariableManager);
GroupDisplayMode displayMode = groupDescription.getDisplayModeProvider().apply(groupVariableManager);

List<Element> toolbarActions = new ArrayList<>();
for (ButtonDescription toolbarActionDescription : groupDescription.getToolbarActionDescriptions()) {
toolbarActions.add(new Element(ButtonComponent.class, new ButtonComponentProps(groupVariableManager, toolbarActionDescription)));
}

// @formatter:off
List<Element> groupChildren = new ArrayList<>();
List<Element> groupContents = new ArrayList<>();
List<AbstractControlDescription> controlDescriptions = groupDescription.getControlDescriptions();
for (AbstractControlDescription controlDescription : controlDescriptions) {
if (controlDescription instanceof AbstractWidgetDescription) {
AbstractWidgetDescription widgetDescription = (AbstractWidgetDescription) controlDescription;
WidgetComponentProps widgetComponentProps = new WidgetComponentProps(groupVariableManager, widgetDescription);
groupChildren.add(new Element(WidgetComponent.class, widgetComponentProps));
groupContents.add(new Element(WidgetComponent.class, widgetComponentProps));
} else if (controlDescription instanceof ForDescription) {
ForDescription forDescription = (ForDescription) controlDescription;
ForComponentProps forComponentProps = new ForComponentProps(groupVariableManager, forDescription);
groupChildren.add(new Element(ForComponent.class, forComponentProps));
groupContents.add(new Element(ForComponent.class, forComponentProps));
}
}

List<Element> groupChildren = List.of(
new Element(GroupToolbarComponent.class, new FragmentProps(toolbarActions)),
new Element(GroupContentsComponent.class, new FragmentProps(groupContents))
);
GroupElementProps groupElementProps = GroupElementProps.newGroupElementProps(id)
.label(label)
.displayMode(displayMode)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*******************************************************************************
* 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.forms.components;

import java.util.Objects;

import org.eclipse.sirius.components.forms.GroupContents;
import org.eclipse.sirius.components.representations.Element;
import org.eclipse.sirius.components.representations.FragmentProps;
import org.eclipse.sirius.components.representations.IComponent;

/**
* Intermediate fragment-like component to distinguish the children of a group which correspond to its actual content.
*
* @author pcdavid
*/
public class GroupContentsComponent implements IComponent {
private final FragmentProps props;

public GroupContentsComponent(FragmentProps props) {
this.props = Objects.requireNonNull(props);
}

@Override
public Element render() {
return new Element(GroupContents.TYPE, this.props);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*******************************************************************************
* 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.forms.components;

import java.util.Objects;

import org.eclipse.sirius.components.forms.GroupToolbar;
import org.eclipse.sirius.components.representations.Element;
import org.eclipse.sirius.components.representations.FragmentProps;
import org.eclipse.sirius.components.representations.IComponent;

/**
* Intermediate fragment-like component to distinguish the children of a group which correspond to its toolbar.
*
* @author pcdavid
*/
public class GroupToolbarComponent implements IComponent {
private final FragmentProps props;

public GroupToolbarComponent(FragmentProps props) {
this.props = Objects.requireNonNull(props);
}

@Override
public Element render() {
return new Element(GroupToolbar.TYPE, this.props);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public final class GroupDescription {

private Function<VariableManager, List<?>> semanticElementsProvider;

private List<ButtonDescription> toolbarActionDescriptions;

private List<AbstractControlDescription> controlDescriptions;

private GroupDescription() {
Expand All @@ -64,6 +66,10 @@ public Function<VariableManager, List<?>> getSemanticElementsProvider() {
return this.semanticElementsProvider;
}

public List<ButtonDescription> getToolbarActionDescriptions() {
return this.toolbarActionDescriptions;
}

public List<AbstractControlDescription> getControlDescriptions() {
return this.controlDescriptions;
}
Expand Down Expand Up @@ -95,6 +101,8 @@ public static final class Builder {

private Function<VariableManager, List<?>> semanticElementsProvider;

private List<ButtonDescription> toolbarActionDescriptions = List.of();

private List<AbstractControlDescription> controlDescriptions;

private Builder(String id) {
Expand All @@ -121,6 +129,11 @@ public Builder semanticElementsProvider(Function<VariableManager, List<?>> seman
return this;
}

public Builder toolbarActionDescriptions(List<ButtonDescription> toolbarActionDescriptions) {
this.toolbarActionDescriptions = Objects.requireNonNull(toolbarActionDescriptions);
return this;
}

public Builder controlDescriptions(List<AbstractControlDescription> controlDescriptions) {
this.controlDescriptions = Objects.requireNonNull(controlDescriptions);
return this;
Expand All @@ -134,6 +147,7 @@ public GroupDescription build() {
groupDescription.displayModeProvider = Objects.requireNonNull(this.displayModeProvider);
groupDescription.semanticElementsProvider = Objects.requireNonNull(this.semanticElementsProvider);
groupDescription.controlDescriptions = Objects.requireNonNull(this.controlDescriptions);
groupDescription.toolbarActionDescriptions = Objects.requireNonNull(this.toolbarActionDescriptions);
return groupDescription;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import org.eclipse.sirius.components.forms.components.FormComponentProps;
import org.eclipse.sirius.components.forms.components.GroupComponent;
import org.eclipse.sirius.components.forms.components.GroupComponentProps;
import org.eclipse.sirius.components.forms.components.GroupContentsComponent;
import org.eclipse.sirius.components.forms.components.GroupToolbarComponent;
import org.eclipse.sirius.components.forms.components.IfComponent;
import org.eclipse.sirius.components.forms.components.IfComponentProps;
import org.eclipse.sirius.components.forms.components.LabelWidgetComponent;
Expand All @@ -56,6 +58,7 @@
import org.eclipse.sirius.components.forms.components.WidgetComponentProps;
import org.eclipse.sirius.components.forms.validation.DiagnosticComponent;
import org.eclipse.sirius.components.forms.validation.DiagnosticComponentProps;
import org.eclipse.sirius.components.representations.FragmentProps;
import org.eclipse.sirius.components.representations.IComponentPropsValidator;
import org.eclipse.sirius.components.representations.IProps;

Expand Down Expand Up @@ -114,6 +117,10 @@ public boolean validateComponentProps(Class<?> componentType, IProps props) {
checkValidProps = props instanceof FlexboxContainerComponentProps;
} else if (TreeComponent.class.equals(componentType)) {
checkValidProps = props instanceof TreeComponentProps;
} else if (GroupToolbarComponent.class.equals(componentType)) {
checkValidProps = props instanceof FragmentProps;
} else if (GroupContentsComponent.class.equals(componentType)) {
checkValidProps = props instanceof FragmentProps;
}

return checkValidProps;
Expand Down
Loading

0 comments on commit a55f3d2

Please sign in to comment.