1 | package com.framsticks.gui; |
---|
2 | |
---|
3 | import com.framsticks.gui.components.Control; |
---|
4 | import com.framsticks.gui.components.ValueControl; |
---|
5 | import com.framsticks.gui.components.ValueControlListener; |
---|
6 | import com.framsticks.params.AccessInterface; |
---|
7 | import com.framsticks.params.types.CompositeParam; |
---|
8 | import com.framsticks.params.Param; |
---|
9 | import com.framsticks.util.Strings; |
---|
10 | import org.apache.log4j.Logger; |
---|
11 | |
---|
12 | import javax.swing.*; |
---|
13 | import java.awt.*; |
---|
14 | import java.util.Collection; |
---|
15 | import java.util.HashMap; |
---|
16 | import java.util.Map; |
---|
17 | import java.util.List; |
---|
18 | |
---|
19 | @SuppressWarnings("serial") |
---|
20 | public class ObjectPanel extends com.framsticks.gui.Panel { |
---|
21 | |
---|
22 | private static final Logger LOGGER = Logger.getLogger(ObjectPanel.class.getName()); |
---|
23 | |
---|
24 | final protected Map<Param, Control> components = new HashMap<Param, Control>(); |
---|
25 | final protected Map<Param, ValueControl> valueComponents = new HashMap<Param, ValueControl>(); |
---|
26 | |
---|
27 | public static void fillWithComponents(JPanel panel, Collection<Param> params, Map<Param, Control> components) { |
---|
28 | for (Param param : params) { |
---|
29 | if (param.isUserHidden()) { |
---|
30 | continue; |
---|
31 | } |
---|
32 | assert !(param instanceof CompositeParam); |
---|
33 | Control control = Control.createSuitable(param); |
---|
34 | if (control != null) { |
---|
35 | LOGGER.debug("add component for " + param); |
---|
36 | JPanel line = new JPanel(); |
---|
37 | line.setLayout(new BoxLayout(line, BoxLayout.LINE_AXIS)); |
---|
38 | line.setAlignmentX(LEFT_ALIGNMENT); |
---|
39 | JLabel label = new JLabel(Strings.notEmpty(param.getName()) ? param.getName() : "? (" + param.getId() + ")"); |
---|
40 | label.setToolTipText(label.getText()); |
---|
41 | label.setHorizontalAlignment(JLabel.RIGHT); |
---|
42 | Dimension labelSize = new Dimension(150, 30); |
---|
43 | label.setMaximumSize(labelSize); |
---|
44 | label.setMinimumSize(labelSize); |
---|
45 | label.setPreferredSize(labelSize); |
---|
46 | line.add(label); |
---|
47 | line.add(Box.createRigidArea(new Dimension(8, 0))); |
---|
48 | line.add(control); |
---|
49 | line.revalidate(); |
---|
50 | panel.add(line); |
---|
51 | panel.add(Box.createRigidArea(new Dimension(0, 8))); |
---|
52 | //component.setAlignmentX(LEFT_ALIGNMENT); |
---|
53 | components.put(param, control); |
---|
54 | continue; |
---|
55 | } |
---|
56 | LOGGER.error("component for param " + param + " of type " + param.getClass().getSimpleName() + " was not added"); |
---|
57 | } |
---|
58 | |
---|
59 | } |
---|
60 | |
---|
61 | public ObjectPanel(EndpointAtFrame endpoint, String className, List<Param> params) { |
---|
62 | super(endpoint, className); |
---|
63 | |
---|
64 | fillWithComponents(contentPanel, params, components); |
---|
65 | |
---|
66 | for (Map.Entry<Param, Control> e : components.entrySet()) { |
---|
67 | Control control = e.getValue(); |
---|
68 | Param param = e.getKey(); |
---|
69 | if (control instanceof ValueControl) { |
---|
70 | final ValueControl valueComponent = (ValueControl) control; |
---|
71 | valueComponents.put(param, valueComponent); |
---|
72 | valueComponent.setListener(new ValueControlListener() { |
---|
73 | @Override |
---|
74 | public boolean onChange(Object newValue) { |
---|
75 | if (currentTreeNode == null) { |
---|
76 | return true; |
---|
77 | } |
---|
78 | boolean result = currentTreeNode.changeValue(valueComponent, newValue); |
---|
79 | refreshControlButtons(); |
---|
80 | return result; |
---|
81 | } |
---|
82 | }); |
---|
83 | } |
---|
84 | } |
---|
85 | contentPanel.add(Box.createVerticalGlue()); |
---|
86 | this.revalidate(); |
---|
87 | } |
---|
88 | |
---|
89 | @Override |
---|
90 | protected void applyChanges() { |
---|
91 | assert frame.isActive(); |
---|
92 | assert currentTreeNode != null; |
---|
93 | currentTreeNode.applyChanges(); |
---|
94 | } |
---|
95 | |
---|
96 | protected void refreshControlButtons() { |
---|
97 | assert frame.isActive(); |
---|
98 | applyButton.setEnabled(currentTreeNode.changedValues != null); |
---|
99 | } |
---|
100 | |
---|
101 | @Override |
---|
102 | public void refreshComponents(AccessInterface access) { |
---|
103 | assert currentTreeNode.path.getInstance().isActive(); |
---|
104 | assert currentTreeNode != null; |
---|
105 | LOGGER.debug("refreshing components"); |
---|
106 | |
---|
107 | final Map<ValueControl, Object> values = new HashMap<ValueControl, Object>(); |
---|
108 | for (ValueControl vc : valueComponents.values()) { |
---|
109 | Object value = access.get(vc.getParam().getId(), Object.class); |
---|
110 | values.put(vc, value); |
---|
111 | } |
---|
112 | |
---|
113 | frame.invokeLater(new Runnable() { |
---|
114 | @Override |
---|
115 | public void run() { |
---|
116 | for (Map.Entry<ValueControl, Object> e : values.entrySet()) { |
---|
117 | e.getKey().setValue(e.getValue()); |
---|
118 | } |
---|
119 | if (currentTreeNode.changedValues != null) { |
---|
120 | for (Map.Entry<ValueControl, Object> e : currentTreeNode.changedValues.entrySet()) { |
---|
121 | e.getKey().setValue(e.getValue()); |
---|
122 | } |
---|
123 | } |
---|
124 | refreshControlButtons(); |
---|
125 | ObjectPanel.this.revalidate(); |
---|
126 | } |
---|
127 | }); |
---|
128 | |
---|
129 | } |
---|
130 | |
---|
131 | |
---|
132 | |
---|
133 | /* public void updateValue() { |
---|
134 | //assert panel.getFrame().isActive(); |
---|
135 | |
---|
136 | final Node n = panel.getCurrentNode(); |
---|
137 | panel.getBrowser().getManager().invokeLater(new Runnable() { |
---|
138 | @Override |
---|
139 | public void run() { |
---|
140 | Object v = n.getAccess().get(param, Object.class); |
---|
141 | if (v == null) { |
---|
142 | v = param.getDef(Object.class); |
---|
143 | } |
---|
144 | final Object fv = v; |
---|
145 | panel.getBrowser().invokeLater(new Runnable() { |
---|
146 | @Override |
---|
147 | public void run() { |
---|
148 | setValueImpl(fv); |
---|
149 | } |
---|
150 | }); |
---|
151 | } |
---|
152 | }); |
---|
153 | }*/ |
---|
154 | |
---|
155 | } |
---|