1 | package com.framsticks.gui.components; |
---|
2 | |
---|
3 | import com.framsticks.params.Flags; |
---|
4 | import com.framsticks.params.Param; |
---|
5 | import com.framsticks.params.types.*; |
---|
6 | import org.apache.log4j.Logger; |
---|
7 | |
---|
8 | import javax.swing.*; |
---|
9 | import java.awt.*; |
---|
10 | |
---|
11 | /** |
---|
12 | * Interface that each class that want to be component must implement. |
---|
13 | */ |
---|
14 | public abstract class Control extends JPanel { |
---|
15 | |
---|
16 | public static final int LINE_HEIGHT = 36; |
---|
17 | |
---|
18 | private static final Logger LOGGER = Logger.getLogger(Control.class.getName()); |
---|
19 | |
---|
20 | protected Frame frame; |
---|
21 | protected Param param; |
---|
22 | |
---|
23 | public Control(Param param) { |
---|
24 | this.param = param; |
---|
25 | |
---|
26 | String help = param.getHelp(); |
---|
27 | if (help != null) { |
---|
28 | this.setToolTipText(help); |
---|
29 | } |
---|
30 | |
---|
31 | if (param.getHelp() != null) { |
---|
32 | this.setToolTipText(param.getHelp()); |
---|
33 | } |
---|
34 | this.setEnabled(!param.hasFlag(Flags.READONLY)); |
---|
35 | // TODO: take care of textField exception: setEditable |
---|
36 | } |
---|
37 | |
---|
38 | public Param getParam() { |
---|
39 | return param; |
---|
40 | } |
---|
41 | |
---|
42 | public void setFrame(Frame frame) { |
---|
43 | this.frame = frame; |
---|
44 | } |
---|
45 | |
---|
46 | public static Control createComponentForText(ValueParam valueParam) { |
---|
47 | if (valueParam.getMin(Object.class) != null) { |
---|
48 | return new TextAreaControl(valueParam); |
---|
49 | } |
---|
50 | return new TextFieldControl(valueParam); |
---|
51 | } |
---|
52 | |
---|
53 | public static Control createSuitable(Param param) { |
---|
54 | |
---|
55 | if (param instanceof EnumParam) { |
---|
56 | return new EnumControl((EnumParam) param); |
---|
57 | } |
---|
58 | if (param instanceof BooleanParam) { |
---|
59 | return new CheckBoxControl((BooleanParam) param); |
---|
60 | } |
---|
61 | if (param instanceof DecimalParam) { |
---|
62 | DecimalParam decimalParam = (DecimalParam)param; |
---|
63 | if (param.getMin(Integer.class) != null && param.getMax(Integer.class) != null) { |
---|
64 | return new SliderControl(decimalParam); |
---|
65 | } |
---|
66 | return createComponentForText(decimalParam); |
---|
67 | } |
---|
68 | if (param instanceof FloatParam) { |
---|
69 | FloatParam floatParam = (FloatParam)param; |
---|
70 | if (param.getMin(Double.class) != null && param.getMax(Double.class) != null) { |
---|
71 | return new SliderControl(floatParam); |
---|
72 | } |
---|
73 | return createComponentForText(floatParam); |
---|
74 | } |
---|
75 | if (param instanceof StringParam) { |
---|
76 | return createComponentForText((StringParam)param); |
---|
77 | } |
---|
78 | if (param instanceof ProcedureParam) { |
---|
79 | return new ProcedureControl((ProcedureParam)param); |
---|
80 | } |
---|
81 | if (param instanceof BinaryParam) { |
---|
82 | return createComponentForText((BinaryParam)param); |
---|
83 | } |
---|
84 | if (param instanceof ColorParam) { |
---|
85 | return createComponentForText((ColorParam) param); |
---|
86 | } |
---|
87 | if (param instanceof UniversalParam) { |
---|
88 | return new TextAreaControl((UniversalParam)param); |
---|
89 | } |
---|
90 | if (param instanceof EventParam) { |
---|
91 | return new EventControl((EventParam)param); |
---|
92 | } |
---|
93 | return null; |
---|
94 | } |
---|
95 | |
---|
96 | protected void addAsOnlyChild(JComponent component) { |
---|
97 | this.setLayout(new BorderLayout()); |
---|
98 | this.add(component, BorderLayout.CENTER); |
---|
99 | } |
---|
100 | |
---|
101 | @Override |
---|
102 | public String toString() { |
---|
103 | return param.toString(); |
---|
104 | } |
---|
105 | } |
---|