1 | package com.framsticks.gui; |
---|
2 | |
---|
3 | import java.awt.Dimension; |
---|
4 | import java.util.Collection; |
---|
5 | import java.util.Map; |
---|
6 | |
---|
7 | import javax.swing.Box; |
---|
8 | import javax.swing.BoxLayout; |
---|
9 | import javax.swing.JLabel; |
---|
10 | import javax.swing.JPanel; |
---|
11 | |
---|
12 | import org.apache.log4j.Logger; |
---|
13 | |
---|
14 | import com.framsticks.gui.controls.CheckBoxControl; |
---|
15 | import com.framsticks.gui.controls.Control; |
---|
16 | import com.framsticks.gui.controls.ControlOwner; |
---|
17 | import com.framsticks.gui.controls.EnumControl; |
---|
18 | import com.framsticks.gui.controls.EventControl; |
---|
19 | import com.framsticks.gui.controls.ProcedureControl; |
---|
20 | import com.framsticks.gui.controls.SliderControl; |
---|
21 | import com.framsticks.gui.controls.TextAreaControl; |
---|
22 | import com.framsticks.gui.controls.TextFieldControl; |
---|
23 | import com.framsticks.params.CompositeParam; |
---|
24 | import com.framsticks.params.Param; |
---|
25 | import com.framsticks.params.PrimitiveParam; |
---|
26 | import com.framsticks.params.types.BinaryParam; |
---|
27 | import com.framsticks.params.types.BooleanParam; |
---|
28 | import com.framsticks.params.types.ColorParam; |
---|
29 | import com.framsticks.params.types.DecimalParam; |
---|
30 | import com.framsticks.params.types.EnumParam; |
---|
31 | import com.framsticks.params.types.EventParam; |
---|
32 | import com.framsticks.params.types.FloatParam; |
---|
33 | import com.framsticks.params.types.ProcedureParam; |
---|
34 | import com.framsticks.params.types.StringParam; |
---|
35 | import com.framsticks.params.types.UniversalParam; |
---|
36 | import com.framsticks.util.FramsticksException; |
---|
37 | import com.framsticks.util.lang.Strings; |
---|
38 | |
---|
39 | public final class Gui { |
---|
40 | |
---|
41 | private static final Logger log = Logger.getLogger(Gui.class.getName()); |
---|
42 | |
---|
43 | private Gui() { |
---|
44 | } |
---|
45 | |
---|
46 | public static Control createComponentForText(PrimitiveParam<?> valueParam) { |
---|
47 | if (valueParam.getMax(Object.class) != null) { |
---|
48 | return new TextFieldControl(valueParam); |
---|
49 | } |
---|
50 | return new TextAreaControl(valueParam); |
---|
51 | |
---|
52 | |
---|
53 | // if (valueParam.getMin(Object.class) != null) { |
---|
54 | // return new TextAreaControl(valueParam); |
---|
55 | // } |
---|
56 | // return new TextFieldControl(valueParam); |
---|
57 | } |
---|
58 | |
---|
59 | public static Control createSuitable(Param param) { |
---|
60 | |
---|
61 | if (param instanceof EnumParam) { |
---|
62 | return new EnumControl((EnumParam) param); |
---|
63 | } |
---|
64 | if (param instanceof BooleanParam) { |
---|
65 | return new CheckBoxControl((BooleanParam) param); |
---|
66 | } |
---|
67 | if (param instanceof DecimalParam) { |
---|
68 | DecimalParam decimalParam = (DecimalParam)param; |
---|
69 | if (decimalParam.getMin(Integer.class) != null && decimalParam.getMax(Integer.class) != null) { |
---|
70 | return new SliderControl(decimalParam); |
---|
71 | } |
---|
72 | return new TextFieldControl(decimalParam); |
---|
73 | } |
---|
74 | if (param instanceof FloatParam) { |
---|
75 | FloatParam floatParam = (FloatParam)param; |
---|
76 | if (floatParam.getMin(Double.class) != null && floatParam.getMax(Double.class) != null) { |
---|
77 | return new SliderControl(floatParam); |
---|
78 | } |
---|
79 | return new TextFieldControl(floatParam); |
---|
80 | } |
---|
81 | if (param instanceof StringParam) { |
---|
82 | return createComponentForText((StringParam) param); |
---|
83 | } |
---|
84 | if (param instanceof ProcedureParam) { |
---|
85 | return new ProcedureControl((ProcedureParam) param); |
---|
86 | } |
---|
87 | if (param instanceof BinaryParam) { |
---|
88 | return new TextFieldControl((BinaryParam) param); |
---|
89 | } |
---|
90 | if (param instanceof ColorParam) { |
---|
91 | return new TextFieldControl((ColorParam) param); |
---|
92 | } |
---|
93 | if (param instanceof UniversalParam) { |
---|
94 | return new TextAreaControl((UniversalParam) param); |
---|
95 | } |
---|
96 | if (param instanceof EventParam) { |
---|
97 | return new EventControl((EventParam) param); |
---|
98 | } |
---|
99 | return null; |
---|
100 | } |
---|
101 | |
---|
102 | public static <P extends Param, C extends Control> void fillWithControls(ControlOwner owner, Collection<P> params, Map<P, C> components, Class<C> controlType) { |
---|
103 | JPanel panel = owner.getPanelForControls(); |
---|
104 | for (P param : params) { |
---|
105 | if (param.isUserHidden()) { |
---|
106 | continue; |
---|
107 | } |
---|
108 | assert !(param instanceof CompositeParam); |
---|
109 | Control c = Gui.createSuitable(param); |
---|
110 | |
---|
111 | if (!controlType.isInstance(c)) { |
---|
112 | throw new FramsticksException().msg("created control is not of required type").arg("control", c).arg("type", controlType); |
---|
113 | } |
---|
114 | |
---|
115 | C control = controlType.cast(c); |
---|
116 | |
---|
117 | control.setOwner(owner); |
---|
118 | |
---|
119 | log.debug("add component for " + param); |
---|
120 | JPanel line = new JPanel(); |
---|
121 | line.setLayout(new BoxLayout(line, BoxLayout.LINE_AXIS)); |
---|
122 | line.setAlignmentX(JPanel.LEFT_ALIGNMENT); |
---|
123 | JLabel label = new JLabel(Strings.notEmpty(param.getName()) ? param.getName() : (Strings.notEmpty(param.getId()) ? param.getId() : "?")); |
---|
124 | label.setToolTipText(control.getToolTipText()); |
---|
125 | label.setHorizontalAlignment(JLabel.RIGHT); |
---|
126 | Dimension labelSize = new Dimension(150, 30); |
---|
127 | label.setMaximumSize(labelSize); |
---|
128 | label.setMinimumSize(labelSize); |
---|
129 | label.setPreferredSize(labelSize); |
---|
130 | line.add(label); |
---|
131 | line.add(Box.createRigidArea(new Dimension(8, 0))); |
---|
132 | line.add(control); |
---|
133 | line.revalidate(); |
---|
134 | panel.add(line); |
---|
135 | panel.add(Box.createRigidArea(new Dimension(0, 8))); |
---|
136 | //component.setAlignmentX(LEFT_ALIGNMENT); |
---|
137 | components.put(param, control); |
---|
138 | } |
---|
139 | |
---|
140 | } |
---|
141 | } |
---|