[84] | 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; |
---|
[101] | 9 | import javax.swing.JComponent; |
---|
[84] | 10 | import javax.swing.JLabel; |
---|
| 11 | import javax.swing.JPanel; |
---|
[101] | 12 | import javax.swing.border.TitledBorder; |
---|
[84] | 13 | |
---|
[100] | 14 | import org.apache.logging.log4j.Logger; |
---|
| 15 | import org.apache.logging.log4j.LogManager; |
---|
[84] | 16 | |
---|
| 17 | import com.framsticks.gui.controls.CheckBoxControl; |
---|
| 18 | import com.framsticks.gui.controls.Control; |
---|
[90] | 19 | import com.framsticks.gui.controls.ControlOwner; |
---|
[84] | 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; |
---|
[101] | 28 | import com.framsticks.params.ParamFlags; |
---|
[84] | 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; |
---|
[90] | 40 | import com.framsticks.util.FramsticksException; |
---|
[84] | 41 | import com.framsticks.util.lang.Strings; |
---|
| 42 | |
---|
| 43 | public final class Gui { |
---|
| 44 | |
---|
[100] | 45 | private static final Logger log = LogManager.getLogger(Gui.class.getName()); |
---|
[84] | 46 | |
---|
| 47 | private Gui() { |
---|
| 48 | } |
---|
| 49 | |
---|
[87] | 50 | public static Control createComponentForText(PrimitiveParam<?> valueParam) { |
---|
[97] | 51 | if (valueParam.getMax(Object.class) != null) { |
---|
| 52 | return new TextFieldControl(valueParam); |
---|
[84] | 53 | } |
---|
[97] | 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); |
---|
[84] | 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 | } |
---|
[97] | 76 | return new TextFieldControl(decimalParam); |
---|
[84] | 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 | } |
---|
[97] | 83 | return new TextFieldControl(floatParam); |
---|
[84] | 84 | } |
---|
| 85 | if (param instanceof StringParam) { |
---|
[97] | 86 | return createComponentForText((StringParam) param); |
---|
[84] | 87 | } |
---|
| 88 | if (param instanceof ProcedureParam) { |
---|
[97] | 89 | return new ProcedureControl((ProcedureParam) param); |
---|
[84] | 90 | } |
---|
| 91 | if (param instanceof BinaryParam) { |
---|
[97] | 92 | return new TextFieldControl((BinaryParam) param); |
---|
[84] | 93 | } |
---|
| 94 | if (param instanceof ColorParam) { |
---|
[97] | 95 | return new TextFieldControl((ColorParam) param); |
---|
[84] | 96 | } |
---|
| 97 | if (param instanceof UniversalParam) { |
---|
[97] | 98 | return new TextAreaControl((UniversalParam) param); |
---|
[84] | 99 | } |
---|
| 100 | if (param instanceof EventParam) { |
---|
[97] | 101 | return new EventControl((EventParam) param); |
---|
[84] | 102 | } |
---|
| 103 | return null; |
---|
| 104 | } |
---|
| 105 | |
---|
[101] | 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)); |
---|
[90] | 108 | for (P param : params) { |
---|
[101] | 109 | if (param.hasFlag(ParamFlags.USERHIDDEN)) { |
---|
[84] | 110 | continue; |
---|
| 111 | } |
---|
| 112 | assert !(param instanceof CompositeParam); |
---|
[90] | 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); |
---|
[84] | 117 | } |
---|
[90] | 118 | |
---|
| 119 | C control = controlType.cast(c); |
---|
| 120 | |
---|
| 121 | control.setOwner(owner); |
---|
| 122 | |
---|
[100] | 123 | log.debug("add component for {}", param); |
---|
[84] | 124 | JPanel line = new JPanel(); |
---|
| 125 | line.setLayout(new BoxLayout(line, BoxLayout.LINE_AXIS)); |
---|
| 126 | line.setAlignmentX(JPanel.LEFT_ALIGNMENT); |
---|
[101] | 127 | |
---|
[84] | 128 | line.add(control); |
---|
| 129 | line.revalidate(); |
---|
| 130 | panel.add(line); |
---|
| 131 | panel.add(Box.createRigidArea(new Dimension(0, 8))); |
---|
[101] | 132 | components.put(param.getId(), control); |
---|
[84] | 133 | //component.setAlignmentX(LEFT_ALIGNMENT); |
---|
[101] | 134 | // components.put(param.getId(), control); |
---|
[84] | 135 | } |
---|
| 136 | |
---|
| 137 | } |
---|
[101] | 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 | } |
---|
[84] | 183 | } |
---|