[84] | 1 | package com.framsticks.gui.controls; |
---|
| 2 | |
---|
| 3 | import org.apache.log4j.Logger; |
---|
| 4 | |
---|
| 5 | import com.framsticks.params.CastFailure; |
---|
| 6 | import com.framsticks.params.Flags; |
---|
| 7 | import com.framsticks.params.PrimitiveParam; |
---|
| 8 | import com.framsticks.params.ReassignResult; |
---|
| 9 | import com.framsticks.util.swing.TooltipConstructor; |
---|
| 10 | |
---|
| 11 | /** |
---|
| 12 | * @author Piotr Sniegowski |
---|
| 13 | */ |
---|
| 14 | @SuppressWarnings("serial") |
---|
| 15 | public abstract class ValueControl extends Control { |
---|
| 16 | private static final Logger log = |
---|
| 17 | Logger.getLogger(ValueControl.class); |
---|
| 18 | |
---|
| 19 | /** |
---|
| 20 | * |
---|
| 21 | */ |
---|
| 22 | protected ValueControlListener listener; |
---|
| 23 | |
---|
[87] | 24 | public ValueControl(PrimitiveParam<?> primitiveParam) { |
---|
[84] | 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", Flags.write(primitiveParam.getFlags(), null)) |
---|
| 35 | .append("group", primitiveParam.getGroup()) |
---|
| 36 | .append("extra", primitiveParam.getExtra()) |
---|
| 37 | .build()) |
---|
| 38 | ; |
---|
| 39 | } |
---|
| 40 | |
---|
| 41 | @Override |
---|
[87] | 42 | public PrimitiveParam<?> getParam() { |
---|
| 43 | return (PrimitiveParam<?>) param; |
---|
[84] | 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 | public void setListener(ValueControlListener listener) { |
---|
| 63 | this.listener = listener; |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | protected Object filterValueThroughConstraints(Object candidate) { |
---|
| 67 | Object oldValue = pullValueFromUserInterface(); |
---|
| 68 | try { |
---|
| 69 | ReassignResult<?> res = getParam().reassign(candidate, oldValue); |
---|
| 70 | if (res.getFlags() != 0) { |
---|
| 71 | log.warn("filter of param " + param + " failed: " + Flags.write(res.getFlags(), "0")); |
---|
| 72 | } |
---|
| 73 | return res.getValue(); |
---|
| 74 | } catch (CastFailure e) { |
---|
| 75 | log.error("cast failure when setting param " + param + " to value " + candidate + ": " + e); |
---|
| 76 | } |
---|
| 77 | return oldValue; |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | protected boolean notifyOfChange() { |
---|
| 81 | if (!programmaticChange) { |
---|
| 82 | if (listener == null) { |
---|
| 83 | return true; |
---|
| 84 | } |
---|
| 85 | return listener.onChange(filterValueThroughConstraints(pullValueFromUserInterface())); |
---|
| 86 | } |
---|
| 87 | return true; |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | public final boolean isReadOnly() { |
---|
| 91 | return (param.getFlags() & Flags.READONLY) != 0; |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | |
---|
| 95 | |
---|
| 96 | } |
---|