Skip to content

Commit 154f5c3

Browse files
authored
Provide new presentation model for the tree node (eclipse-che#6718)
* Provide new presentation model for the tree node Signed-off-by: Vladyslav Zhukovskyi <vzhukovs@redhat.com> * Add missing HasNewPresentation interface Signed-off-by: Vladyslav Zhukovskyi <vzhukovs@redhat.com>
1 parent 2ce3bca commit 154f5c3

6 files changed

Lines changed: 300 additions & 5 deletions

File tree

ide/che-core-ide-ui/src/main/java/org/eclipse/che/ide/ui/smartTree/presentation/DefaultPresentationRenderer.java

Lines changed: 62 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
*/
1111
package org.eclipse.che.ide.ui.smartTree.presentation;
1212

13+
import static com.google.common.base.Strings.nullToEmpty;
14+
1315
import com.google.common.base.Strings;
1416
import com.google.gwt.dom.client.DivElement;
1517
import com.google.gwt.dom.client.Document;
@@ -28,14 +30,55 @@ public DefaultPresentationRenderer(TreeStyles treeStyles) {
2830

2931
@Override
3032
public Element render(N node, String domID, Tree.Joint joint, int depth) {
31-
NodePresentation presentation;
32-
if (node instanceof HasPresentation) {
33-
presentation = ((HasPresentation) node).getPresentation(false);
33+
34+
if (node instanceof HasNewPresentation) {
35+
NewNodePresentation presentation = ((HasNewPresentation) node).getPresentation();
36+
37+
return renderNewPresentation(presentation, domID, joint, depth);
38+
} else if (node instanceof HasPresentation) {
39+
NodePresentation presentation = ((HasPresentation) node).getPresentation(false);
40+
41+
return renderGenericPresentation(presentation, domID, joint, depth);
3442
} else {
35-
presentation = new NodePresentation();
43+
NodePresentation presentation = new NodePresentation();
3644
presentation.setPresentableText(node.getName());
45+
46+
return renderGenericPresentation(presentation, domID, joint, depth);
3747
}
48+
}
3849

50+
Element renderNewPresentation(
51+
NewNodePresentation presentation, String domID, Tree.Joint joint, int depth) {
52+
Element rootContainer = getRootContainer(domID);
53+
Element nodeContainer = getNodeContainer();
54+
setIndentLevel(nodeContainer, depth);
55+
Element jointContainer = getJointContainer(joint);
56+
Element iconContainer = getIconContainer(presentation.getIcon());
57+
Element userElement = getUserElement(presentation.getUserElement());
58+
Element presentableTextContainer =
59+
getPresentableTextContainer(
60+
createStyledTextElement(presentation.getNodeText(), presentation.getNodeTextStyle()));
61+
Element infoTextContainer =
62+
getInfoTextContainer(
63+
createStyledTextElement(
64+
presentation.getNodeInfoText(), presentation.getNodeInfoTextStyle()));
65+
Element descendantsContainer = getDescendantsContainer();
66+
67+
nodeContainer.appendChild(jointContainer);
68+
nodeContainer.appendChild(iconContainer);
69+
nodeContainer.appendChild(
70+
userElement == null ? Document.get().createSpanElement() : userElement);
71+
nodeContainer.appendChild(presentableTextContainer);
72+
nodeContainer.appendChild(infoTextContainer);
73+
74+
rootContainer.appendChild(nodeContainer);
75+
rootContainer.appendChild(descendantsContainer);
76+
77+
return rootContainer;
78+
}
79+
80+
Element renderGenericPresentation(
81+
NodePresentation presentation, String domID, Tree.Joint joint, int depth) {
3982
Element rootContainer = getRootContainer(domID);
4083

4184
Element nodeContainer = getNodeContainer();
@@ -68,10 +111,24 @@ public Element render(N node, String domID, Tree.Joint joint, int depth) {
68111
return rootContainer;
69112
}
70113

114+
private Element createStyledTextElement(String content, StyleConfigurator styleConfigurator) {
115+
DivElement textElement = Document.get().createDivElement();
116+
117+
textElement.setInnerText(nullToEmpty(content));
118+
119+
if (styleConfigurator != null) {
120+
styleConfigurator
121+
.getCssConfiguration()
122+
.forEach((p, v) -> textElement.getStyle().setProperty(p, v));
123+
}
124+
125+
return textElement;
126+
}
127+
71128
private Element createPresentableTextElement(NodePresentation presentation) {
72129
DivElement textElement = Document.get().createDivElement();
73130

74-
textElement.setInnerText(Strings.nullToEmpty(presentation.getPresentableText()));
131+
textElement.setInnerText(nullToEmpty(presentation.getPresentableText()));
75132
textElement.setAttribute("style", presentation.getPresentableTextCss());
76133

77134
//TODO support text colorization
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright (c) 2012-2017 Red Hat, Inc.
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License v1.0
5+
* which accompanies this distribution, and is available at
6+
* http://www.eclipse.org/legal/epl-v10.html
7+
*
8+
* Contributors:
9+
* Red Hat, Inc. - initial API and implementation
10+
*/
11+
package org.eclipse.che.ide.ui.smartTree.presentation;
12+
13+
/**
14+
* Provider for the new type of presentation represented by {@link NewNodePresentation}.
15+
*
16+
* @author Vlad Zhukovskyi
17+
* @since 5.19.0
18+
*/
19+
public interface HasNewPresentation {
20+
21+
/**
22+
* Returns a new type of node presentation.
23+
*
24+
* @return presentation
25+
* @see NewNodePresentation
26+
*/
27+
NewNodePresentation getPresentation();
28+
}

ide/che-core-ide-ui/src/main/java/org/eclipse/che/ide/ui/smartTree/presentation/HasPresentation.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
* node icon, presentable text, info text, etc.
1818
*
1919
* @author Vlad Zhukovskiy
20+
* @deprecated use {@link HasNewPresentation} instead
2021
*/
22+
@Deprecated
2123
public interface HasPresentation {
2224
/**
2325
* Method called during node rendering.
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/*
2+
* Copyright (c) 2012-2017 Red Hat, Inc.
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License v1.0
5+
* which accompanies this distribution, and is available at
6+
* http://www.eclipse.org/legal/epl-v10.html
7+
*
8+
* Contributors:
9+
* Red Hat, Inc. - initial API and implementation
10+
*/
11+
package org.eclipse.che.ide.ui.smartTree.presentation;
12+
13+
import com.google.gwt.dom.client.Element;
14+
import org.vectomatic.dom.svg.ui.SVGResource;
15+
16+
/**
17+
* Node presentation which contains a useful builder to friendly build the presentation. Node which
18+
* should provide a new type of presentation should implement a {@link HasNewPresentation}
19+
* interface.
20+
*
21+
* @author Vlad Zhukovskyi
22+
* @since 5.19.0
23+
*/
24+
public class NewNodePresentation {
25+
26+
private String nodeText;
27+
private StyleConfigurator nodeTextStyle;
28+
private String nodeInfoText;
29+
private StyleConfigurator nodeInfoTextStyle;
30+
private SVGResource icon;
31+
private Element userElement;
32+
33+
private NewNodePresentation(Builder builder) {
34+
this.nodeText = builder.nodeText;
35+
this.nodeTextStyle = builder.nodeTextStyle;
36+
this.nodeInfoText = builder.nodeInfoText;
37+
this.nodeInfoTextStyle = builder.nodeInfoTextStyle;
38+
this.icon = builder.icon;
39+
this.userElement = builder.userElement;
40+
}
41+
42+
public String getNodeText() {
43+
return nodeText;
44+
}
45+
46+
public StyleConfigurator getNodeTextStyle() {
47+
return nodeTextStyle;
48+
}
49+
50+
public String getNodeInfoText() {
51+
return nodeInfoText;
52+
}
53+
54+
public StyleConfigurator getNodeInfoTextStyle() {
55+
return nodeInfoTextStyle;
56+
}
57+
58+
public SVGResource getIcon() {
59+
return icon;
60+
}
61+
62+
public Element getUserElement() {
63+
return userElement;
64+
}
65+
66+
public static class Builder {
67+
private String nodeText;
68+
private StyleConfigurator nodeTextStyle;
69+
private String nodeInfoText;
70+
private StyleConfigurator nodeInfoTextStyle;
71+
private SVGResource icon;
72+
private Element userElement;
73+
74+
public Builder() {}
75+
76+
public Builder withNodeText(String nodeText) {
77+
this.nodeText = nodeText;
78+
return this;
79+
}
80+
81+
public Builder withNodeTextStyle(StyleConfigurator nodeTextStyle) {
82+
this.nodeTextStyle = nodeTextStyle;
83+
return this;
84+
}
85+
86+
public Builder withNodeInfoText(String nodeInfoText) {
87+
this.nodeInfoText = nodeInfoText;
88+
return this;
89+
}
90+
91+
public Builder withNodeIntoTextStyle(StyleConfigurator nodeInfoTextStyle) {
92+
this.nodeInfoTextStyle = nodeInfoTextStyle;
93+
return this;
94+
}
95+
96+
public Builder withIcon(SVGResource icon) {
97+
this.icon = icon;
98+
return this;
99+
}
100+
101+
public Builder withUserElement(Element userElement) {
102+
this.userElement = userElement;
103+
return this;
104+
}
105+
106+
public NewNodePresentation build() {
107+
return new NewNodePresentation(this);
108+
}
109+
}
110+
}

ide/che-core-ide-ui/src/main/java/org/eclipse/che/ide/ui/smartTree/presentation/NodePresentation.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
* Node presentation.
1919
*
2020
* @author Vlad Zhukovskiy
21+
* @deprecated use {@link NewNodePresentation} instead
2122
*/
23+
@Deprecated
2224
public class NodePresentation {
2325
private String presentableText;
2426
private String presentableTextCss;
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* Copyright (c) 2012-2017 Red Hat, Inc.
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License v1.0
5+
* which accompanies this distribution, and is available at
6+
* http://www.eclipse.org/legal/epl-v10.html
7+
*
8+
* Contributors:
9+
* Red Hat, Inc. - initial API and implementation
10+
*/
11+
package org.eclipse.che.ide.ui.smartTree.presentation;
12+
13+
import static com.google.common.base.Strings.isNullOrEmpty;
14+
import static java.util.Collections.unmodifiableMap;
15+
16+
import com.google.gwt.dom.client.Style.FontStyle;
17+
import com.google.gwt.dom.client.Style.FontWeight;
18+
import java.util.HashMap;
19+
import java.util.Map;
20+
21+
/**
22+
* Css style configurator with builder. Consists of basic style configuration and a map with
23+
* additional css properties.
24+
*
25+
* @author Vlad Zhukovskyi
26+
* @since 5.19.0
27+
*/
28+
public class StyleConfigurator {
29+
30+
private static final String STYLE_COLOR = "color";
31+
private static final String STYLE_FONT_STYLE = "fontStyle";
32+
private static final String STYLE_FONT_WEIGHT = "fontWeight";
33+
34+
private final Map<String, String> cssConfiguration;
35+
36+
private StyleConfigurator(Builder builder) {
37+
cssConfiguration = new HashMap<>();
38+
39+
if (!isNullOrEmpty(builder.color)) {
40+
cssConfiguration.put(STYLE_COLOR, builder.color);
41+
}
42+
43+
if (builder.fontStyle != null) {
44+
cssConfiguration.put(STYLE_FONT_STYLE, builder.fontStyle.getCssName());
45+
}
46+
47+
if (builder.fontWeight != null) {
48+
cssConfiguration.put(STYLE_FONT_WEIGHT, builder.fontWeight.getCssName());
49+
}
50+
51+
if (builder.properties != null) {
52+
cssConfiguration.putAll(builder.properties);
53+
}
54+
}
55+
56+
public Map<String, String> getCssConfiguration() {
57+
return unmodifiableMap(cssConfiguration);
58+
}
59+
60+
public static class Builder {
61+
private String color;
62+
private FontStyle fontStyle;
63+
private FontWeight fontWeight;
64+
private Map<String, String> properties;
65+
66+
public Builder() {}
67+
68+
public Builder withColor(String color) {
69+
this.color = color;
70+
return this;
71+
}
72+
73+
public Builder withFontStyle(FontStyle fontStyle) {
74+
this.fontStyle = fontStyle;
75+
return this;
76+
}
77+
78+
public Builder withFontWeight(FontWeight fontWeight) {
79+
this.fontWeight = fontWeight;
80+
return this;
81+
}
82+
83+
public Builder withProperty(String property, String value) {
84+
if (properties == null) {
85+
properties = new HashMap<>();
86+
}
87+
88+
properties.put(property, value);
89+
return this;
90+
}
91+
92+
public StyleConfigurator build() {
93+
return new StyleConfigurator(this);
94+
}
95+
}
96+
}

0 commit comments

Comments
 (0)