source: java/main/src/main/java/com/framsticks/gui/components/ValueControl.java @ 77

Last change on this file since 77 was 77, checked in by psniegowski, 11 years ago

Add new java codebase.

File size: 1.9 KB
Line 
1package com.framsticks.gui.components;
2
3import com.framsticks.params.CastFailure;
4import com.framsticks.params.Flags;
5import com.framsticks.params.types.ValueParam;
6
7import javax.swing.event.DocumentEvent;
8import javax.swing.event.DocumentListener;
9
10/**
11 * @author Piotr Sniegowski
12 */
13public abstract class ValueControl extends Control {
14
15        protected ValueControlListener listener;
16
17        public ValueControl(ValueParam valueParamEntry) {
18                super(valueParamEntry);
19        }
20
21        protected abstract void setValueImpl(Object value);
22
23        /** I consider this an ugly solution, but I was unable to find proper
24         * action listeners for underlying swing controls, that would only fire
25         * on user change and on programmatic change.
26         */
27        protected boolean programmaticChange = false;
28
29        public void setValue(Object value) {
30                programmaticChange = true;
31                setValueImpl(value);
32                programmaticChange = false;
33        }
34
35        public abstract Object getValue();
36
37        public void setListener(ValueControlListener listener) {
38                this.listener = listener;
39        }
40
41        protected boolean notifyOfChange() {
42                Object c = getValue();
43                try {
44                        Object v = param.reassign(c, null);
45                        if (!programmaticChange) {
46                                return listener == null || listener.onChange(v);
47                        }
48                        return true;
49                } catch (CastFailure castFailure) {
50                        return false;
51                }
52        }
53
54        public boolean isReadOnly() {
55                return (param.getFlags() & Flags.READONLY) != 0;
56        }
57
58    @Override
59    public ValueParam getParam() {
60        assert param instanceof ValueParam;
61        return (ValueParam)param;
62    }
63
64        protected static DocumentListener createDocumentListener(final ValueControl component) {
65                return new DocumentListener() {
66                        @Override
67                        public void insertUpdate(DocumentEvent documentEvent) {
68                                component.notifyOfChange();
69                        }
70
71                        @Override
72                        public void removeUpdate(DocumentEvent documentEvent) {
73                                component.notifyOfChange();
74                        }
75
76                        @Override
77                        public void changedUpdate(DocumentEvent documentEvent) {
78                                component.notifyOfChange();
79                        }
80                };
81        }
82
83}
Note: See TracBrowser for help on using the repository browser.