1 | package com.framsticks.gui.controls; |
---|
2 | |
---|
3 | import org.apache.logging.log4j.Logger; |
---|
4 | import org.apache.logging.log4j.LogManager; |
---|
5 | |
---|
6 | import com.framsticks.params.CastFailure; |
---|
7 | import com.framsticks.params.ParamFlags; |
---|
8 | import com.framsticks.params.PrimitiveParam; |
---|
9 | import com.framsticks.params.ReassignResult; |
---|
10 | import com.framsticks.params.SetStateFlags; |
---|
11 | import com.framsticks.util.FramsticksException; |
---|
12 | import com.framsticks.util.lang.FlagsUtil; |
---|
13 | import com.framsticks.util.swing.TooltipConstructor; |
---|
14 | // import static com.framsticks.core.TreeOperations.*; |
---|
15 | |
---|
16 | /** |
---|
17 | * @author Piotr Sniegowski |
---|
18 | */ |
---|
19 | @SuppressWarnings("serial") |
---|
20 | public abstract class ValueControl extends Control { |
---|
21 | private static final Logger log = |
---|
22 | LogManager.getLogger(ValueControl.class); |
---|
23 | |
---|
24 | public ValueControl(PrimitiveParam<?> primitiveParam) { |
---|
25 | super(primitiveParam); |
---|
26 | |
---|
27 | this.setToolTipText(new TooltipConstructor() |
---|
28 | .append("name", primitiveParam.getName()) |
---|
29 | .append("id", primitiveParam.getId()) |
---|
30 | .append("help", primitiveParam.getHelp()) |
---|
31 | .append("def", primitiveParam.getDef(Object.class)) |
---|
32 | .append("min", primitiveParam.getMin(Object.class)) |
---|
33 | .append("max", primitiveParam.getMax(Object.class)) |
---|
34 | .append("flags", FlagsUtil.write(ParamFlags.class, primitiveParam.getFlags(), null)) |
---|
35 | .append("group", primitiveParam.getGroup()) |
---|
36 | .append("extra", primitiveParam.getExtra()) |
---|
37 | .build()) |
---|
38 | ; |
---|
39 | } |
---|
40 | |
---|
41 | @Override |
---|
42 | public PrimitiveParam<?> getParam() { |
---|
43 | return (PrimitiveParam<?>) param; |
---|
44 | } |
---|
45 | |
---|
46 | protected abstract void pushValueToUserInterfaceImpl(Object value); |
---|
47 | |
---|
48 | /** I consider this an ugly solution, but I was unable to find proper |
---|
49 | * action listeners for underlying swing controls, that would only fire |
---|
50 | * on user change and on programmatic change. |
---|
51 | */ |
---|
52 | protected boolean programmaticChange = false; |
---|
53 | |
---|
54 | public void pushValueToUserInterface(Object value) { |
---|
55 | programmaticChange = true; |
---|
56 | pushValueToUserInterfaceImpl(value); |
---|
57 | programmaticChange = false; |
---|
58 | } |
---|
59 | |
---|
60 | public abstract Object pullValueFromUserInterface(); |
---|
61 | |
---|
62 | protected Object filterValueThroughConstraints(Object candidate) { |
---|
63 | Object oldValue = pullValueFromUserInterface();//bindAccess(owner.getCurrentPath()).get(getParam(), Object.class); |
---|
64 | try { |
---|
65 | ReassignResult<?> res = getParam().reassign(candidate, oldValue); |
---|
66 | if (res.getFlags() != 0) { |
---|
67 | log.warn("filter of param {} failed: {}", param, FlagsUtil.write(SetStateFlags.class, res.getFlags(), "0")); |
---|
68 | } |
---|
69 | return res.getValue(); |
---|
70 | } catch (CastFailure e) { |
---|
71 | //TODO just throw here, but check where that function is being used |
---|
72 | handle(new FramsticksException().msg("invalid value in control").arg("param", param).arg("value", candidate).cause(e)); |
---|
73 | } |
---|
74 | return oldValue; |
---|
75 | } |
---|
76 | |
---|
77 | /** This method is meant as a public interface to obtain current and correct value from control. |
---|
78 | * |
---|
79 | */ |
---|
80 | public final Object getCurrentValue() { |
---|
81 | return filterValueThroughConstraints(pullValueFromUserInterface()); |
---|
82 | } |
---|
83 | |
---|
84 | protected boolean notifyOfChange() { |
---|
85 | if (!programmaticChange) { |
---|
86 | return owner.onValueChange(this, getCurrentValue()); |
---|
87 | } |
---|
88 | return true; |
---|
89 | } |
---|
90 | |
---|
91 | |
---|
92 | } |
---|