1 | package com.framsticks.gui; |
---|
2 | |
---|
3 | import com.framsticks.gui.controls.Control; |
---|
4 | import com.framsticks.gui.controls.ControlOwner; |
---|
5 | import com.framsticks.gui.controls.ValueControl; |
---|
6 | import com.framsticks.params.Access; |
---|
7 | import com.framsticks.params.Param; |
---|
8 | |
---|
9 | import org.apache.logging.log4j.Logger; |
---|
10 | import org.apache.logging.log4j.LogManager; |
---|
11 | |
---|
12 | import javax.swing.*; |
---|
13 | |
---|
14 | import java.util.Collection; |
---|
15 | import java.util.HashMap; |
---|
16 | import java.util.Map; |
---|
17 | |
---|
18 | import static com.framsticks.structure.TreeOperations.*; |
---|
19 | import static com.framsticks.util.lang.Containers.filterInstanceof; |
---|
20 | |
---|
21 | import com.framsticks.structure.Path; |
---|
22 | import com.framsticks.util.FramsticksException; |
---|
23 | |
---|
24 | |
---|
25 | @SuppressWarnings("serial") |
---|
26 | public class ObjectPanel extends ModifiablePanel implements ControlOwner { |
---|
27 | |
---|
28 | private static final Logger log = LogManager.getLogger(ObjectPanel.class); |
---|
29 | |
---|
30 | final protected Map<String, Control> controls = new HashMap<String, Control>(); |
---|
31 | final protected Map<String, ValueControl> valueControls = new HashMap<String, ValueControl>(); |
---|
32 | |
---|
33 | protected final JPanel contentPanel; |
---|
34 | protected final JScrollPane scrollPane; |
---|
35 | |
---|
36 | public ObjectPanel(TreePanel.Parameters parameters, Collection<Param> params) { |
---|
37 | super(parameters); |
---|
38 | |
---|
39 | contentPanel = new JPanel(); |
---|
40 | scrollPane = new JScrollPane(contentPanel); |
---|
41 | |
---|
42 | setupContentComponent(scrollPane); |
---|
43 | |
---|
44 | Gui.fillWithControls(this, contentPanel, params, controls, Control.class); |
---|
45 | setName(framsClass.getId()); |
---|
46 | |
---|
47 | for (final ValueControl c : filterInstanceof(controls.values(), ValueControl.class)) { |
---|
48 | valueControls.put(c.getParam().getId(), c); |
---|
49 | c.setUserEnabled(true); |
---|
50 | } |
---|
51 | |
---|
52 | contentPanel.add(Box.createVerticalGlue()); |
---|
53 | this.revalidate(); |
---|
54 | } |
---|
55 | |
---|
56 | @Override |
---|
57 | protected void applyChanges() { |
---|
58 | assert frame.isActive(); |
---|
59 | assert currentPath != null; |
---|
60 | treeAtFrame.pushUserChangesToTree(currentPath); |
---|
61 | refreshControlButtons(); |
---|
62 | } |
---|
63 | |
---|
64 | |
---|
65 | @Override |
---|
66 | protected void revertChanges() { |
---|
67 | assert currentPath != null; |
---|
68 | removeSideNote(currentPath, treeAtFrame.getUserChangesKey()); |
---|
69 | pullValuesFromLocalToUser(bindAccess(currentPath)); |
---|
70 | } |
---|
71 | |
---|
72 | @Override |
---|
73 | public void pullValuesFromLocalToUser(Access access) { |
---|
74 | assert currentPath != null; |
---|
75 | log.debug("refreshing components"); |
---|
76 | |
---|
77 | UserChanges userChanges = getSideNote(currentPath, treeAtFrame.getUserChangesKey()); |
---|
78 | |
---|
79 | |
---|
80 | for (Map.Entry<String, ValueControl> e : valueControls.entrySet()) { |
---|
81 | String id = e.getKey(); |
---|
82 | Object value; |
---|
83 | if (userChanges != null && userChanges.changes.containsKey(id)) { |
---|
84 | value = userChanges.changes.get(id); |
---|
85 | } else { |
---|
86 | value = access.get(id, Object.class); |
---|
87 | } |
---|
88 | |
---|
89 | e.getValue().pushValueToUserInterface(value); |
---|
90 | } |
---|
91 | |
---|
92 | for (Map.Entry<String, Control> e : controls.entrySet()) { |
---|
93 | e.getValue().refreshState(); |
---|
94 | } |
---|
95 | |
---|
96 | refreshControlButtons(); |
---|
97 | // ObjectPanel.this.revalidate(); |
---|
98 | } |
---|
99 | |
---|
100 | @Override |
---|
101 | public String getTitle() { |
---|
102 | return "Properties"; |
---|
103 | } |
---|
104 | |
---|
105 | @Override |
---|
106 | public void handle(FramsticksException exception) { |
---|
107 | frame.handle(exception); |
---|
108 | } |
---|
109 | |
---|
110 | @Override |
---|
111 | public Path getCurrentPath() { |
---|
112 | return super.getCurrentPath(); |
---|
113 | } |
---|
114 | |
---|
115 | @Override |
---|
116 | public boolean onValueChange(ValueControl control, Object newValue) { |
---|
117 | if (currentPath == null) { |
---|
118 | return true; |
---|
119 | } |
---|
120 | boolean result = treeAtFrame.changeValue(currentPath.assureResolved().getTopObject(), control, newValue); |
---|
121 | refreshControlButtons(); |
---|
122 | return result; |
---|
123 | } |
---|
124 | |
---|
125 | } |
---|