1 | package com.framsticks.gui; |
---|
2 | |
---|
3 | import com.framsticks.gui.controls.Control; |
---|
4 | import com.framsticks.gui.controls.ValueControl; |
---|
5 | import com.framsticks.gui.controls.ValueControlListener; |
---|
6 | import com.framsticks.params.AccessInterface; |
---|
7 | import com.framsticks.params.Param; |
---|
8 | import com.framsticks.params.ValueParam; |
---|
9 | |
---|
10 | import org.apache.log4j.Logger; |
---|
11 | |
---|
12 | import javax.swing.*; |
---|
13 | import java.util.Collection; |
---|
14 | import java.util.HashMap; |
---|
15 | import java.util.Map; |
---|
16 | import static com.framsticks.util.lang.Containers.filterInstanceof; |
---|
17 | import com.framsticks.util.dispatching.RunAt; |
---|
18 | |
---|
19 | @SuppressWarnings("serial") |
---|
20 | public class ObjectPanel extends ModifiablePanel { |
---|
21 | |
---|
22 | private static final Logger log = Logger.getLogger(ObjectPanel.class.getName()); |
---|
23 | |
---|
24 | final protected Map<Param, Control> components = new HashMap<Param, Control>(); |
---|
25 | final protected Map<ValueParam, ValueControl> valueControls = new HashMap<ValueParam, ValueControl>(); |
---|
26 | |
---|
27 | public ObjectPanel(Panel.Parameters parameters, Collection<Param> params) { |
---|
28 | super(parameters); |
---|
29 | |
---|
30 | Gui.fillWithControls(contentPanel, params, components); |
---|
31 | |
---|
32 | for (final ValueControl c : filterInstanceof(components.values(), ValueControl.class)) { |
---|
33 | valueControls.put(c.getParam(), c); |
---|
34 | c.setListener(new ValueControlListener() { |
---|
35 | @Override |
---|
36 | public boolean onChange(Object newValue) { |
---|
37 | if (currentTreeNode == null) { |
---|
38 | return true; |
---|
39 | } |
---|
40 | boolean result = currentTreeNode.changeValue(c, newValue); |
---|
41 | refreshControlButtons(); |
---|
42 | return result; |
---|
43 | } |
---|
44 | }); |
---|
45 | } |
---|
46 | contentPanel.add(Box.createVerticalGlue()); |
---|
47 | this.revalidate(); |
---|
48 | } |
---|
49 | |
---|
50 | @Override |
---|
51 | protected void applyChanges() { |
---|
52 | assert frame.isActive(); |
---|
53 | assert currentTreeNode != null; |
---|
54 | currentTreeNode.pushLocalChanges(); |
---|
55 | } |
---|
56 | |
---|
57 | protected void refreshControlButtons() { |
---|
58 | assert frame.isActive(); |
---|
59 | applyButton.setEnabled(currentTreeNode.localChanges != null); |
---|
60 | } |
---|
61 | |
---|
62 | protected static void fillControlsWithValues(Map<ValueControl, Object> map) { |
---|
63 | } |
---|
64 | |
---|
65 | @Override |
---|
66 | public void pullValuesFromLocalToUser(AccessInterface access) { |
---|
67 | assert currentTreeNode != null; |
---|
68 | assert currentTreeNode.path.getInstance().isActive(); |
---|
69 | log.debug("refreshing components"); |
---|
70 | |
---|
71 | final Map<ValueControl, Object> values = new HashMap<ValueControl, Object>(); |
---|
72 | for (Map.Entry<ValueParam, ValueControl> e : valueControls.entrySet()) { |
---|
73 | values.put(e.getValue(), access.get(e.getKey().getId(), Object.class)); |
---|
74 | } |
---|
75 | |
---|
76 | frame.invokeLater(new RunAt<Frame>() { |
---|
77 | @Override |
---|
78 | public void run() { |
---|
79 | if (currentTreeNode.localChanges != null) { |
---|
80 | for (Map.Entry<ValueControl, Object> e : currentTreeNode.localChanges.entrySet()) { |
---|
81 | values.put(e.getKey(), e.getValue()); |
---|
82 | } |
---|
83 | } |
---|
84 | for (Map.Entry<ValueControl, Object> e : values.entrySet()) { |
---|
85 | e.getKey().pushValueToUserInterface(e.getValue()); |
---|
86 | } |
---|
87 | refreshControlButtons(); |
---|
88 | ObjectPanel.this.revalidate(); |
---|
89 | } |
---|
90 | }); |
---|
91 | |
---|
92 | } |
---|
93 | |
---|
94 | @Override |
---|
95 | public String getTitle() { |
---|
96 | return "Properties"; |
---|
97 | } |
---|
98 | |
---|
99 | |
---|
100 | // public void updateValue() { |
---|
101 | // //assert panel.getFrame().isActive(); |
---|
102 | |
---|
103 | // final Node n = panel.getCurrentNode(); |
---|
104 | // panel.getBrowser().getManager().invokeLater(new Runnable() { |
---|
105 | // @Override |
---|
106 | // public void run() { |
---|
107 | // Object v = n.getAccess().get(param, Object.class); |
---|
108 | // if (v == null) { |
---|
109 | // v = param.getDef(Object.class); |
---|
110 | // } |
---|
111 | // final Object fv = v; |
---|
112 | // panel.getBrowser().invokeLater(new Runnable() { |
---|
113 | // @Override |
---|
114 | // public void run() { |
---|
115 | // setValueImpl(fv); |
---|
116 | // } |
---|
117 | // }); |
---|
118 | // } |
---|
119 | // }); |
---|
120 | // } |
---|
121 | |
---|
122 | } |
---|