Skip to content

Commit

Permalink
[2123] Add support for non-selectable tree items
Browse files Browse the repository at this point in the history
Bug: #2123
Signed-off-by: Pierre-Charles David <pierre-charles.david@obeo.fr>
  • Loading branch information
pcdavid committed Jun 29, 2023
1 parent 7181f25 commit 50448e2
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 18 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.adoc
Expand Up @@ -67,6 +67,7 @@ The help text can include multiple lines (separated by `\n`), but no text format
- https://github.com/eclipse-sirius/sirius-components/issues/2083[#2083] [diagram] Add support for edge markers
- https://github.com/eclipse-sirius/sirius-components/issues/2086[#2086] [diagram] Add a panel with the default diagram actions
- https://github.com/eclipse-sirius/sirius-components/issues/2087[#2087] [diagram] Add support for snap to grid
- https://github.com/eclipse-sirius/sirius-components/issues/2123[#2123] [tree] Add support for non-selectable tree items

== v2023.6.0

Expand Down
Expand Up @@ -29,8 +29,8 @@
import org.eclipse.sirius.components.compatibility.services.ImageConstants;
import org.eclipse.sirius.components.core.RepresentationMetadata;
import org.eclipse.sirius.components.core.api.IEditingContext;
import org.eclipse.sirius.components.core.api.IURLParser;
import org.eclipse.sirius.components.core.api.IObjectService;
import org.eclipse.sirius.components.core.api.IURLParser;
import org.eclipse.sirius.components.core.api.SemanticKindConstants;
import org.eclipse.sirius.components.emf.services.EditingContext;
import org.eclipse.sirius.components.representations.Failure;
Expand Down Expand Up @@ -96,6 +96,7 @@ public TreeDescription getDescription() {
.imageURLProvider(this::getImageURL)
.editableProvider(this::isEditable)
.deletableProvider(this::isDeletable)
.selectableProvider(this::isSelectable)
.elementsProvider(this::getElements)
.hasChildrenProvider(this::hasChildren)
.childrenProvider(this::getChildren)
Expand Down Expand Up @@ -186,6 +187,17 @@ private boolean isDeletable(VariableManager variableManager) {
return true;
}

private boolean isSelectable(VariableManager variableManager) {
Object self = variableManager.getVariables().get(VariableManager.SELF);
boolean selectable = true;
if (self instanceof RepresentationMetadata) {
selectable = false;
} else if (self instanceof Resource) {
selectable = false;
}
return selectable;
}

private String getImageURL(VariableManager variableManager) {
Object self = variableManager.getVariables().get(VariableManager.SELF);

Expand Down
Expand Up @@ -38,6 +38,7 @@ type TreeItem {
imageURL: String!
editable: Boolean!
deletable: Boolean!
selectable: Boolean!
expanded: Boolean!
hasChildren: Boolean!
children: [TreeItem]!
Expand Down
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2019, 2022 Obeo.
* Copyright (c) 2019, 2023 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
Expand Down Expand Up @@ -40,6 +40,8 @@ public final class TreeItem {

private boolean deletable;

private boolean selectable;

private boolean hasChildren;

private boolean expanded;
Expand Down Expand Up @@ -70,6 +72,10 @@ public boolean isDeletable() {
return this.deletable;
}

public boolean isSelectable() {
return this.selectable;
}

public String getImageURL() {
return this.imageURL;
}
Expand Down Expand Up @@ -116,6 +122,8 @@ public static final class Builder {

private boolean deletable;

private boolean selectable;

private boolean hasChildren;

private boolean expanded;
Expand All @@ -142,12 +150,17 @@ public Builder imageURL(String imageURL) {
}

public Builder editable(boolean editable) {
this.editable = Objects.requireNonNull(editable);
this.editable = editable;
return this;
}

public Builder deletable(boolean deletable) {
this.deletable = Objects.requireNonNull(deletable);
this.deletable = deletable;
return this;
}

public Builder selectable(boolean selectable) {
this.selectable = selectable;
return this;
}

Expand All @@ -172,10 +185,11 @@ public TreeItem build() {
treeItem.kind = Objects.requireNonNull(this.kind);
treeItem.label = Objects.requireNonNull(this.label);
treeItem.imageURL = Objects.requireNonNull(this.imageURL);
treeItem.editable = Objects.requireNonNull(this.editable);
treeItem.deletable = Objects.requireNonNull(this.deletable);
treeItem.expanded = Objects.requireNonNull(this.expanded);
treeItem.hasChildren = Objects.requireNonNull(this.hasChildren);
treeItem.editable = this.editable;
treeItem.deletable = this.deletable;
treeItem.selectable = this.selectable;
treeItem.expanded = this.expanded;
treeItem.hasChildren = this.hasChildren;
treeItem.children = Objects.requireNonNull(this.children);
return treeItem;
}
Expand Down
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2019, 2022 Obeo.
* Copyright (c) 2019, 2023 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
Expand Down Expand Up @@ -49,6 +49,8 @@ public final class TreeDescription implements IRepresentationDescription {

private Function<VariableManager, Boolean> deletableProvider;

private Function<VariableManager, Boolean> selectableProvider;

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

private Function<VariableManager, List<?>> childrenProvider;
Expand Down Expand Up @@ -103,6 +105,11 @@ public Function<VariableManager, Boolean> getDeletableProvider() {
return this.deletableProvider;
}

public Function<VariableManager, Boolean> getSelectableProvider() {
return this.selectableProvider;
}


public Function<VariableManager, List<?>> getElementsProvider() {
return this.elementsProvider;
}
Expand Down Expand Up @@ -163,6 +170,8 @@ public static final class Builder {

private Function<VariableManager, Boolean> deletableProvider;

private Function<VariableManager, Boolean> selectableProvider = (variableManager) -> true;

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

private Function<VariableManager, List<?>> childrenProvider;
Expand Down Expand Up @@ -219,6 +228,11 @@ public Builder deletableProvider(Function<VariableManager, Boolean> deletablePro
return this;
}

public Builder selectableProvider(Function<VariableManager, Boolean> selectableProvider) {
this.selectableProvider = Objects.requireNonNull(selectableProvider);
return this;
}

public Builder elementsProvider(Function<VariableManager, List<?>> elementsProvider) {
this.elementsProvider = Objects.requireNonNull(elementsProvider);
return this;
Expand Down Expand Up @@ -260,6 +274,7 @@ public TreeDescription build() {
treeDescription.imageURLProvider = Objects.requireNonNull(this.imageURLProvider);
treeDescription.editableProvider = Objects.requireNonNull(this.editableProvider);
treeDescription.deletableProvider = Objects.requireNonNull(this.deletableProvider);
treeDescription.selectableProvider = Objects.requireNonNull(this.selectableProvider);
treeDescription.elementsProvider = Objects.requireNonNull(this.elementsProvider);
treeDescription.childrenProvider = Objects.requireNonNull(this.childrenProvider);
treeDescription.hasChildrenProvider = Objects.requireNonNull(this.hasChildrenProvider);
Expand Down
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2019, 2022 Obeo.
* Copyright (c) 2019, 2023 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
Expand Down Expand Up @@ -66,6 +66,7 @@ private TreeItem renderTreeItem(VariableManager treeItemVariableManager) {
String label = this.treeDescription.getLabelProvider().apply(treeItemVariableManager);
boolean editable = this.treeDescription.getEditableProvider().apply(treeItemVariableManager);
boolean deletable = this.treeDescription.getDeletableProvider().apply(treeItemVariableManager);
boolean selectable = this.treeDescription.getSelectableProvider().apply(treeItemVariableManager);
String imageURL = this.treeDescription.getImageURLProvider().apply(treeItemVariableManager);
Boolean hasChildren = this.treeDescription.getHasChildrenProvider().apply(treeItemVariableManager);

Expand All @@ -85,6 +86,7 @@ private TreeItem renderTreeItem(VariableManager treeItemVariableManager) {
.label(label)
.editable(editable)
.deletable(deletable)
.selectable(selectable)
.imageURL(imageURL)
.children(childrenTreeItems)
.hasChildren(hasChildren)
Expand Down
Expand Up @@ -60,6 +60,9 @@ const useTreeItemStyle = makeStyles((theme) => ({
backgroundColor: 'var(--blue-lagoon-lighten-90)',
},
},
nonSelectable: {
fontStyle: 'italic',
},
arrow: {
cursor: 'pointer',
},
Expand Down Expand Up @@ -227,7 +230,11 @@ export const TreeItem = ({
onExpand={onExpand}
onExpandAll={onExpandAll}
selection={selection}
setSelection={setSelection}
setSelection={(selection) => {
if (item.selectable) {
setSelection(selection);
}
}}
enterEditingMode={enterEditingMode}
onClose={closeContextMenu}
/>
Expand All @@ -249,7 +256,11 @@ export const TreeItem = ({
onExpand={onExpand}
onExpandAll={onExpandAll}
selection={selection}
setSelection={setSelection}
setSelection={(selection) => {
if (childItem.selectable) {
setSelection(selection);
}
}}
readOnly={readOnly}
textToHighlight={textToHighlight}
isFilterEnabled={isFilterEnabled}
Expand Down Expand Up @@ -366,7 +377,11 @@ export const TreeItem = ({
);
}
text = (
<Typography variant="body2" className={`${classes.label} ${selected ? classes.selectedLabel : ''}`}>
<Typography
variant="body2"
className={`${classes.label} ${selected ? classes.selectedLabel : ''} ${
item.selectable ? '' : classes.nonSelectable
}`}>
{itemLabel}
</Typography>
);
Expand All @@ -380,16 +395,22 @@ export const TreeItem = ({
const isItemInSelection = selection.entries.find((entry) => entry.id === item.id);
if (isItemInSelection) {
const newSelection: Selection = { entries: selection.entries.filter((entry) => entry.id !== item.id) };
setSelection(newSelection);
if (item.selectable) {
setSelection(newSelection);
}
} else {
const { id, label, kind } = item;
const newEntry = { id, label, kind };
const newSelection: Selection = { entries: [...selection.entries, newEntry] };
setSelection(newSelection);
if (item.selectable) {
setSelection(newSelection);
}
}
} else {
const { id, label, kind } = item;
setSelection({ entries: [{ id, label, kind }] });
if (item.selectable) {
setSelection({ entries: [{ id, label, kind }] });
}
}
};

Expand Down
Expand Up @@ -49,6 +49,7 @@ export interface GQLTreeItem {
expanded: boolean;
editable: boolean;
deletable: boolean;
selectable: boolean;
}

export interface GQLGetTreePathVariables {
Expand Down
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2019, 2022 Obeo.
* Copyright (c) 2019, 2023 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
Expand Down Expand Up @@ -43,6 +43,7 @@ const getDocumentSubscription = gql`
label
editable
deletable
selectable
kind
imageURL
}
Expand Down
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2019, 2022 Obeo.
* Copyright (c) 2019, 2023 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
Expand Down Expand Up @@ -39,6 +39,7 @@ fragment treeItemFields on TreeItem {
label
editable
deletable
selectable
kind
imageURL
}
Expand Down

0 comments on commit 50448e2

Please sign in to comment.