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.JComponent; |
---|
10 | import javax.swing.JLabel; |
---|
11 | import javax.swing.JPanel; |
---|
12 | import javax.swing.border.TitledBorder; |
---|
13 | |
---|
14 | import org.apache.logging.log4j.Logger; |
---|
15 | import org.apache.logging.log4j.LogManager; |
---|
16 | |
---|
17 | import com.framsticks.gui.controls.CheckBoxControl; |
---|
18 | import com.framsticks.gui.controls.Control; |
---|
19 | import com.framsticks.gui.controls.ControlOwner; |
---|
20 | import com.framsticks.gui.controls.EnumControl; |
---|
21 | import com.framsticks.gui.controls.EventControl; |
---|
22 | import com.framsticks.gui.controls.ProcedureControl; |
---|
23 | import com.framsticks.gui.controls.SliderControl; |
---|
24 | import com.framsticks.gui.controls.TextAreaControl; |
---|
25 | import com.framsticks.gui.controls.TextFieldControl; |
---|
26 | import com.framsticks.params.CompositeParam; |
---|
27 | import com.framsticks.params.Param; |
---|
28 | import com.framsticks.params.ParamFlags; |
---|
29 | import com.framsticks.params.PrimitiveParam; |
---|
30 | import com.framsticks.params.types.BinaryParam; |
---|
31 | import com.framsticks.params.types.BooleanParam; |
---|
32 | import com.framsticks.params.types.ColorParam; |
---|
33 | import com.framsticks.params.types.DecimalParam; |
---|
34 | import com.framsticks.params.types.EnumParam; |
---|
35 | import com.framsticks.params.types.EventParam; |
---|
36 | import com.framsticks.params.types.FloatParam; |
---|
37 | import com.framsticks.params.types.ProcedureParam; |
---|
38 | import com.framsticks.params.types.StringParam; |
---|
39 | import com.framsticks.params.types.UniversalParam; |
---|
40 | import com.framsticks.util.FramsticksException; |
---|
41 | import com.framsticks.util.lang.Strings; |
---|
42 | |
---|
43 | public final class Gui { |
---|
44 | |
---|
45 | private static final Logger log = LogManager.getLogger(Gui.class.getName()); |
---|
46 | |
---|
47 | private Gui() { |
---|
48 | } |
---|
49 | |
---|
50 | public static Control createComponentForText(PrimitiveParam<?> valueParam) { |
---|
51 | if (valueParam.getMax(Object.class) != null) { |
---|
52 | return new TextFieldControl(valueParam); |
---|
53 | } |
---|
54 | return new TextAreaControl(valueParam); |
---|
55 | |
---|
56 | |
---|
57 | // if (valueParam.getMin(Object.class) != null) { |
---|
58 | // return new TextAreaControl(valueParam); |
---|
59 | // } |
---|
60 | // return new TextFieldControl(valueParam); |
---|
61 | } |
---|
62 | |
---|
63 | public static Control createSuitable(Param param) { |
---|
64 | |
---|
65 | if (param instanceof EnumParam) { |
---|
66 | return new EnumControl((EnumParam) param); |
---|
67 | } |
---|
68 | if (param instanceof BooleanParam) { |
---|
69 | return new CheckBoxControl((BooleanParam) param); |
---|
70 | } |
---|
71 | if (param instanceof DecimalParam) { |
---|
72 | DecimalParam decimalParam = (DecimalParam)param; |
---|
73 | if (decimalParam.getMin(Integer.class) != null && decimalParam.getMax(Integer.class) != null) { |
---|
74 | return new SliderControl(decimalParam); |
---|
75 | } |
---|
76 | return new TextFieldControl(decimalParam); |
---|
77 | } |
---|
78 | if (param instanceof FloatParam) { |
---|
79 | FloatParam floatParam = (FloatParam)param; |
---|
80 | if (floatParam.getMin(Double.class) != null && floatParam.getMax(Double.class) != null) { |
---|
81 | return new SliderControl(floatParam); |
---|
82 | } |
---|
83 | return new TextFieldControl(floatParam); |
---|
84 | } |
---|
85 | if (param instanceof StringParam) { |
---|
86 | return createComponentForText((StringParam) param); |
---|
87 | } |
---|
88 | if (param instanceof ProcedureParam) { |
---|
89 | return new ProcedureControl((ProcedureParam) param); |
---|
90 | } |
---|
91 | if (param instanceof BinaryParam) { |
---|
92 | return new TextFieldControl((BinaryParam) param); |
---|
93 | } |
---|
94 | if (param instanceof ColorParam) { |
---|
95 | return new TextFieldControl((ColorParam) param); |
---|
96 | } |
---|
97 | if (param instanceof UniversalParam) { |
---|
98 | return new TextAreaControl((UniversalParam) param); |
---|
99 | } |
---|
100 | if (param instanceof EventParam) { |
---|
101 | return new EventControl((EventParam) param); |
---|
102 | } |
---|
103 | return null; |
---|
104 | } |
---|
105 | |
---|
106 | public static <P extends Param, C extends Control> void fillWithControls(ControlOwner owner, JPanel panel, Collection<P> params, Map<String, C> components, Class<C> controlType) { |
---|
107 | panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS)); |
---|
108 | for (P param : params) { |
---|
109 | if (param.hasFlag(ParamFlags.USERHIDDEN)) { |
---|
110 | continue; |
---|
111 | } |
---|
112 | assert !(param instanceof CompositeParam); |
---|
113 | Control c = Gui.createSuitable(param); |
---|
114 | |
---|
115 | if (!controlType.isInstance(c)) { |
---|
116 | throw new FramsticksException().msg("created control is not of required type").arg("control", c).arg("type", controlType); |
---|
117 | } |
---|
118 | |
---|
119 | C control = controlType.cast(c); |
---|
120 | |
---|
121 | control.setOwner(owner); |
---|
122 | |
---|
123 | log.debug("add component for {}", param); |
---|
124 | JPanel line = new JPanel(); |
---|
125 | line.setLayout(new BoxLayout(line, BoxLayout.LINE_AXIS)); |
---|
126 | line.setAlignmentX(JPanel.LEFT_ALIGNMENT); |
---|
127 | |
---|
128 | line.add(control); |
---|
129 | line.revalidate(); |
---|
130 | panel.add(line); |
---|
131 | panel.add(Box.createRigidArea(new Dimension(0, 8))); |
---|
132 | components.put(param.getId(), control); |
---|
133 | //component.setAlignmentX(LEFT_ALIGNMENT); |
---|
134 | // components.put(param.getId(), control); |
---|
135 | } |
---|
136 | |
---|
137 | } |
---|
138 | |
---|
139 | public static String getBestName(Param param) { |
---|
140 | if (Strings.notEmpty(param.getName())) { |
---|
141 | return param.getName(); |
---|
142 | } |
---|
143 | if (Strings.notEmpty(param.getId())) { |
---|
144 | return param.getId(); |
---|
145 | } |
---|
146 | return "?"; |
---|
147 | } |
---|
148 | |
---|
149 | public static void setupTitledControl(Control control, JComponent... components) { |
---|
150 | |
---|
151 | control.setLayout(new BoxLayout(control, BoxLayout.PAGE_AXIS)); |
---|
152 | control.setBorder(new TitledBorder(Gui.getBestName(control.getParam()))); |
---|
153 | for (JComponent c : components) { |
---|
154 | // control.add(Box.createRigidArea(new Dimension(0, 4))); |
---|
155 | control.add(c); |
---|
156 | } |
---|
157 | } |
---|
158 | |
---|
159 | public static void layoutInRow(JPanel panel, JComponent first, JComponent... components) { |
---|
160 | panel.setLayout(new BoxLayout(panel, BoxLayout.LINE_AXIS)); |
---|
161 | panel.setAlignmentX(JPanel.LEFT_ALIGNMENT); |
---|
162 | |
---|
163 | panel.add(first); |
---|
164 | for (JComponent c : components) { |
---|
165 | panel.add(Box.createRigidArea(new Dimension(8, 0))); |
---|
166 | panel.add(c); |
---|
167 | } |
---|
168 | } |
---|
169 | |
---|
170 | public static void addLeftToLabel(Control control, JComponent... components) { |
---|
171 | |
---|
172 | JLabel label = new JLabel(getBestName(control.getParam())); |
---|
173 | label.setToolTipText(control.getToolTipText()); |
---|
174 | label.setHorizontalAlignment(JLabel.LEFT); |
---|
175 | Dimension labelSize = new Dimension(150, 30); |
---|
176 | label.setMaximumSize(labelSize); |
---|
177 | label.setMinimumSize(labelSize); |
---|
178 | label.setPreferredSize(labelSize); |
---|
179 | |
---|
180 | layoutInRow(control, label, components); |
---|
181 | |
---|
182 | } |
---|
183 | } |
---|