Skip to content

Commit 5bc5ae8

Browse files
authored
更新 JFXTreeView (#5574)
1 parent c8183ea commit 5bc5ae8

3 files changed

Lines changed: 274 additions & 0 deletions

File tree

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package com.jfoenix.controls;
21+
22+
import com.jfoenix.utils.JFXNodeUtils;
23+
import javafx.beans.InvalidationListener;
24+
import javafx.beans.WeakInvalidationListener;
25+
import javafx.geometry.Insets;
26+
import javafx.scene.Node;
27+
import javafx.scene.control.TreeCell;
28+
import javafx.scene.control.TreeItem;
29+
import javafx.scene.layout.*;
30+
import javafx.scene.paint.Color;
31+
32+
import java.lang.ref.WeakReference;
33+
34+
/// JFXTreeCell is simple material design implementation of a tree cell.
35+
///
36+
/// @author Shadi Shaheen
37+
/// @version 1.0
38+
/// @since 2017-02-15
39+
public class JFXTreeCell<T> extends TreeCell<T> {
40+
41+
protected JFXRippler cellRippler = new JFXRippler(this) {
42+
@Override
43+
protected Node getMask() {
44+
Region clip = new Region();
45+
JFXNodeUtils.updateBackground(JFXTreeCell.this.getBackground(), clip);
46+
double width = control.getLayoutBounds().getWidth();
47+
double height = control.getLayoutBounds().getHeight();
48+
clip.resize(width, height);
49+
return clip;
50+
}
51+
52+
@Override
53+
protected void positionControl(Node control) {
54+
// do nothing
55+
}
56+
};
57+
private HBox hbox;
58+
private final StackPane selectedPane = new StackPane();
59+
60+
private final InvalidationListener treeItemGraphicInvalidationListener = observable -> updateDisplay(getItem(),
61+
isEmpty());
62+
private final WeakInvalidationListener weakTreeItemGraphicListener = new WeakInvalidationListener(
63+
treeItemGraphicInvalidationListener);
64+
65+
private WeakReference<TreeItem<T>> treeItemRef;
66+
67+
public JFXTreeCell() {
68+
selectedPane.getStyleClass().add("selection-bar");
69+
selectedPane.setBackground(new Background(new BackgroundFill(Color.RED, CornerRadii.EMPTY, Insets.EMPTY)));
70+
selectedPane.setPrefWidth(3);
71+
selectedPane.setMouseTransparent(true);
72+
selectedProperty().addListener((o, oldVal, newVal) -> selectedPane.setVisible(newVal ? true : false));
73+
74+
final InvalidationListener treeItemInvalidationListener = observable -> {
75+
TreeItem<T> oldTreeItem = treeItemRef == null ? null : treeItemRef.get();
76+
if (oldTreeItem != null) {
77+
oldTreeItem.graphicProperty().removeListener(weakTreeItemGraphicListener);
78+
}
79+
80+
TreeItem<T> newTreeItem = getTreeItem();
81+
if (newTreeItem != null) {
82+
newTreeItem.graphicProperty().addListener(weakTreeItemGraphicListener);
83+
treeItemRef = new WeakReference<>(newTreeItem);
84+
}
85+
};
86+
final WeakInvalidationListener weakTreeItemListener = new WeakInvalidationListener(treeItemInvalidationListener);
87+
treeItemProperty().addListener(weakTreeItemListener);
88+
if (getTreeItem() != null) {
89+
getTreeItem().graphicProperty().addListener(weakTreeItemGraphicListener);
90+
}
91+
}
92+
93+
@Override
94+
protected void layoutChildren() {
95+
super.layoutChildren();
96+
if (!getChildren().contains(selectedPane)) {
97+
getChildren().add(0, cellRippler);
98+
cellRippler.rippler.clear();
99+
getChildren().add(0, selectedPane);
100+
}
101+
cellRippler.resizeRelocate(0, 0, getWidth(), getHeight());
102+
cellRippler.releaseRipple();
103+
selectedPane.resizeRelocate(0, 0, selectedPane.prefWidth(-1), getHeight());
104+
selectedPane.setVisible(isSelected());
105+
}
106+
107+
private void updateDisplay(T item, boolean empty) {
108+
if (item == null || empty) {
109+
hbox = null;
110+
setText(null);
111+
setGraphic(null);
112+
} else {
113+
TreeItem<T> treeItem = getTreeItem();
114+
if (treeItem != null && treeItem.getGraphic() != null) {
115+
if (item instanceof Node) {
116+
setText(null);
117+
if (hbox == null) {
118+
hbox = new HBox(3);
119+
}
120+
hbox.getChildren().setAll(treeItem.getGraphic(), (Node) item);
121+
setGraphic(hbox);
122+
} else {
123+
hbox = null;
124+
setText(item.toString());
125+
setGraphic(treeItem.getGraphic());
126+
}
127+
} else {
128+
hbox = null;
129+
if (item instanceof Node) {
130+
setText(null);
131+
setGraphic((Node) item);
132+
} else {
133+
setText(item.toString());
134+
setGraphic(null);
135+
}
136+
}
137+
}
138+
}
139+
140+
@Override
141+
protected void updateItem(T item, boolean empty) {
142+
super.updateItem(item, empty);
143+
updateDisplay(item, empty);
144+
setMouseTransparent(item == null || empty);
145+
}
146+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package com.jfoenix.controls;
21+
22+
import javafx.scene.control.TreeItem;
23+
import javafx.scene.control.TreeView;
24+
25+
/// JFXTreeView is the material design implementation of a TreeView
26+
/// with expand/collapse animation and selection indicator.
27+
///
28+
/// @author Shadi Shaheen
29+
/// @version 1.0
30+
/// @since 2017-02-15
31+
public class JFXTreeView<T> extends TreeView<T> {
32+
33+
private static final String DEFAULT_STYLE_CLASS = "jfx-tree-view";
34+
35+
public JFXTreeView() {
36+
init();
37+
}
38+
39+
public JFXTreeView(TreeItem<T> root) {
40+
super(root);
41+
init();
42+
}
43+
44+
private void init() {
45+
this.setCellFactory((view) -> new JFXTreeCell<>());
46+
this.getStyleClass().add(DEFAULT_STYLE_CLASS);
47+
}
48+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package com.jfoenix.controls.datamodels.treetable;
21+
22+
import javafx.beans.property.ObjectProperty;
23+
import javafx.beans.property.SimpleObjectProperty;
24+
import javafx.collections.FXCollections;
25+
import javafx.collections.ObservableList;
26+
import javafx.scene.control.TreeTableColumn;
27+
28+
/// data model that is used in JFXTreeTableView, it's used to implement
29+
/// the grouping feature.
30+
///
31+
/// **Note:** the data object used in JFXTreeTableView **must** extends this class
32+
///
33+
/// @param <T> is the concrete object of the Tree table
34+
/// @author Shadi Shaheen
35+
/// @version 1.0
36+
/// @since 2016-03-09
37+
public class RecursiveTreeObject<T> {
38+
39+
/// grouped children objects
40+
private ObservableList<T> children = FXCollections.observableArrayList();
41+
42+
public ObservableList<T> getChildren() {
43+
return children;
44+
}
45+
46+
public void setChildren(ObservableList<T> children) {
47+
this.children = children;
48+
}
49+
50+
/// Whether or not the object is grouped by a specified tree table column
51+
ObjectProperty<TreeTableColumn<T, ?>> groupedColumn = new SimpleObjectProperty<>();
52+
53+
public final ObjectProperty<TreeTableColumn<T, ?>> groupedColumnProperty() {
54+
return this.groupedColumn;
55+
}
56+
57+
public final TreeTableColumn<T, ?> getGroupedColumn() {
58+
return this.groupedColumnProperty().get();
59+
}
60+
61+
public final void setGroupedColumn(final TreeTableColumn<T, ?> groupedColumn) {
62+
this.groupedColumnProperty().set(groupedColumn);
63+
}
64+
65+
/// the value that must be shown when grouped
66+
ObjectProperty<Object> groupedValue = new SimpleObjectProperty<>();
67+
68+
public final ObjectProperty<Object> groupedValueProperty() {
69+
return this.groupedValue;
70+
}
71+
72+
public final Object getGroupedValue() {
73+
return this.groupedValueProperty().get();
74+
}
75+
76+
public final void setGroupedValue(final Object groupedValue) {
77+
this.groupedValueProperty().set(groupedValue);
78+
}
79+
80+
}

0 commit comments

Comments
 (0)