-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCustomerNodeStyle.java
194 lines (170 loc) · 7.2 KB
/
CustomerNodeStyle.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
194
/****************************************************************************
**
** 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.IRenderContext;
import com.yworks.yfiles.graph.INode;
import javax.swing.JComponent;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
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 Customer.
*/
public class CustomerNodeStyle extends ComponentNodeStyle {
private static final BackgroundPaints BACKGROUND_PAINTS;
/**
* Initialize the backgrounds used for selected and unselected node states.
*/
static {
Color color1 = new Color(204, 255, 255);
Color color2 = new Color(36, 154, 231);
Paint selectedBackground = createLinearGradient(color1, color2);
Paint unselectedBackground = createLinearGradient(color1, color2.brighter());
BACKGROUND_PAINTS = new BackgroundPaints(unselectedBackground, selectedBackground);
}
public CustomerNodeStyle() {
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.
CustomerJPanel panel = new CustomerJPanel();
// react on edits of the header text field
panel.addPropertyChangeListener(NodeJPanel.HEADER, evt -> {
Customer customer = getCustomer(node);
String newName = (String) evt.getNewValue();
if (!Objects.equals(customer.getName(), newName)) {
// write the edited name back to the business data
customer.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) {
Customer customer = getCustomer(node);
int newId = ((Number) newValue).intValue();
if (customer.getId() != newId) {
// write the edited id back to the business data
customer.setId(newId);
// update the node bounds
updateNodeSize(ctx, node);
}
}
});
// react on edits of the location field
panel.addPropertyChangeListener("node.location", evt -> {
Customer customer = getCustomer(node);
final String newLocation = (String) evt.getNewValue();
if (!Objects.equals(customer.getLocation(), newLocation)) {
// write the edited location back to the business data
customer.setLocation(newLocation);
// update the node bounds
updateNodeSize(ctx, node);
}
});
return panel;
}
/**
* Returns the tag of the specified node cast to a Customer
*/
private static Customer getCustomer(INode node) {
return (Customer) node.getTag();
}
/**
* A ComponentNodeStyle.NodeJPanel customized to visualize the business data of a Customer.
* <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 CustomerJPanel extends ComponentNodeStyle.NodeJPanel {
private JFormattedTextField locationTextField;
@Override
protected void initializeChildren() {
super.initializeChildren();
// location:
JPanel locationPanel = createGridBagPanel();
JLabel locationDescription = new JLabel();
locationDescription.setFont(smallPlainFont);
locationDescription.setText("Location:");
// JFormattedTextField is used because of its property change event based value notification
locationTextField = configureFormattedField(new JFormattedTextField(), smallPlainFont, SwingConstants.LEFT);
locationTextField.addPropertyChangeListener("value",
evt -> firePropertyChange("node.location", evt.getOldValue(), locationTextField.getText()));
// 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;
locationPanel.add(locationDescription, constraints);
constraints.gridx = 1;
constraints.weightx = 1;
locationPanel.add(locationTextField, constraints);
constraints.gridx = 0;
constraints.gridy = 2;
constraints.weightx = 1;
constraints.insets = new Insets(3, 8, 8, 3);
this.add(locationPanel, constraints);
}
@Override
protected void addChildPropertyListener() {
super.addChildPropertyListener();
// set business data values for headerTextField, idTextField and locationTextField
// whenever the user tag of the node is set
addPropertyChangeListener(AbstractJComponentStyle.USER_TAG_KEY, evt -> {
Customer customer = (Customer) evt.getNewValue();
headerTextField.setText(customer.getName());
idTextField.setText(String.valueOf(customer.getId()));
locationTextField.setText(customer.getLocation());
});
}
}
}