-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathProductNodeStyle.java
193 lines (169 loc) · 6.97 KB
/
ProductNodeStyle.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/****************************************************************************
**
** This demo file is part of yFiles for Java (Swing) 3.6.
**
** Copyright (c) 2000-2023 by yWorks GmbH, Vor dem Kreuzberg 28,
** 72070 Tuebingen, Germany. All rights reserved.
**
** yFiles demo files exhibit yFiles for Java (Swing) functionalities. Any redistribution
** of demo files in source code or binary form, with or without
** modification, is not permitted.
**
** Owners of a valid software license for a yFiles for Java (Swing) version that this
** demo is shipped with are allowed to use the demo source code as basis
** for their own yFiles for Java (Swing) powered applications. Use of such programs is
** governed by the rights and conditions as set out in the yFiles for Java (Swing)
** license agreement.
**
** THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY EXPRESS OR IMPLIED
** WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
** MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
** NO EVENT SHALL yWorks BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
** TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
** PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
** LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
** NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**
***************************************************************************/
package style.jcomponentstyle;
import com.yworks.yfiles.graph.styles.AbstractJComponentStyle;
import com.yworks.yfiles.view.Colors;
import com.yworks.yfiles.view.IRenderContext;
import com.yworks.yfiles.graph.INode;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.Paint;
import java.util.Objects;
/**
* Component node style that displays the business data of a Product.
*/
public class ProductNodeStyle extends ComponentNodeStyle {
private static final BackgroundPaints BACKGROUND_PAINTS;
/**
* Initialize the backgrounds used for selected and unselected node states.
*/
static {
Color color1 = new Color(255, 221, 51);
Color color2 = new Color(255, 119, 51);
Paint selectedBackground = createLinearGradient(color1, color2);
Paint unselectedBackground = createLinearGradient(color1, color2.brighter());
BACKGROUND_PAINTS = new BackgroundPaints(unselectedBackground, selectedBackground);
}
public ProductNodeStyle() {
this.setStyleTag(BACKGROUND_PAINTS);
}
/**
* Creates the component representing a node.
* <p>
* This implementation returns a customized JPanel that contains several JLabels showing the node's business data.
* </p>
* @param ctx The context for which the component should be created.
* @param node The node that will be rendered.
*/
@Override
public JComponent createComponent(final IRenderContext ctx, final INode node) {
// create the customized JPanel that uses a gradient paint as background.
final ProductJPanel panel = new ProductJPanel();
// react on edits of the header text field
panel.addPropertyChangeListener(NodeJPanel.HEADER, evt -> {
Product product = getProduct(node);
final String newName = (String) evt.getNewValue();
if (!Objects.equals(product.getName(), newName)) {
// write the edited name back to the business data
product.setName(newName);
// update the node bounds
updateNodeSize(ctx, node);
}
});
// react on edits of the id field
panel.addPropertyChangeListener(NodeJPanel.ID, evt -> {
Object newValue = evt.getNewValue();
if (newValue instanceof Number) {
Product product = getProduct(node);
int newId = ((Number) newValue).intValue();
if (product.getId() != newId) {
// write the edited id back to the business data
product.setId(newId);
// update the node bounds
updateNodeSize(ctx, node);
}
}
});
// react on edits of the inStock check box
panel.addPropertyChangeListener("node.inStock", evt -> {
Product product = getProduct(node);
boolean newValue = (Boolean) evt.getNewValue();
if (product.getInStock() != newValue) {
// write the edited inStock value back to the business data
product.setInStock(newValue);
}
});
return panel;
}
/**
* Returns the tag of the specified node cast to a Product
*/
private static Product getProduct(INode node) {
return (Product) node.getTag();
}
/**
* A ComponentNodeStyle.NodeJPanel customized to visualize the business data of a Product.
* <p>
* The panel supports automatic update of the displayed business data by
* listening for client property changes that are associated to property key
* {@link com.yworks.yfiles.graph.styles.AbstractJComponentStyle#USER_TAG_KEY}.
* </p>
*/
static class ProductJPanel extends ComponentNodeStyle.NodeJPanel {
private JCheckBox inStockCheckBox;
@Override
protected void initializeChildren() {
super.initializeChildren();
// additional info whether the product is in stock
JPanel inStockPanel = createGridBagPanel();
JLabel inStockDescription = new JLabel();
inStockDescription.setFont(smallPlainFont);
inStockDescription.setText("In Stock:");
inStockCheckBox = new JCheckBox();
inStockCheckBox.setBackground(Colors.TRANSPARENT);
inStockCheckBox.addActionListener(e -> {
boolean newValue = inStockCheckBox.isSelected();
firePropertyChange("node.inStock", !newValue, newValue);
});
// add the components using a GridBagLayout
GridBagConstraints constraints = new GridBagConstraints();
constraints.gridx = 0;
constraints.gridy = 0;
constraints.ipadx = 5;
constraints.anchor = GridBagConstraints.WEST;
constraints.weightx = 0;
inStockPanel.add(inStockDescription, constraints);
constraints.gridx = 1;
constraints.weightx = 1;
inStockPanel.add(inStockCheckBox, constraints);
constraints.gridx = 0;
constraints.gridy = 2;
constraints.insets = new Insets(3, 8, 8, 3);
this.add(inStockPanel, constraints);
}
@Override
protected void addChildPropertyListener() {
super.addChildPropertyListener();
// set business data values for headerTextField, idTextField and inStockCheckBox
// whenever the user tag of the node is set
addPropertyChangeListener(AbstractJComponentStyle.USER_TAG_KEY, evt -> {
Product product = (Product) evt.getNewValue();
headerTextField.setText(product.getName());
idTextField.setText(String.valueOf(product.getId()));
inStockCheckBox.setSelected(product.getInStock());
});
}
}
}