[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; |
---|
| 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.EnumControl; |
---|
| 17 | import com.framsticks.gui.controls.EventControl; |
---|
| 18 | import com.framsticks.gui.controls.ProcedureControl; |
---|
| 19 | import com.framsticks.gui.controls.SliderControl; |
---|
| 20 | import com.framsticks.gui.controls.TextAreaControl; |
---|
| 21 | import com.framsticks.gui.controls.TextFieldControl; |
---|
| 22 | import com.framsticks.params.CompositeParam; |
---|
| 23 | import com.framsticks.params.Param; |
---|
| 24 | import com.framsticks.params.PrimitiveParam; |
---|
| 25 | import com.framsticks.params.types.BinaryParam; |
---|
| 26 | import com.framsticks.params.types.BooleanParam; |
---|
| 27 | import com.framsticks.params.types.ColorParam; |
---|
| 28 | import com.framsticks.params.types.DecimalParam; |
---|
| 29 | import com.framsticks.params.types.EnumParam; |
---|
| 30 | import com.framsticks.params.types.EventParam; |
---|
| 31 | import com.framsticks.params.types.FloatParam; |
---|
| 32 | import com.framsticks.params.types.ProcedureParam; |
---|
| 33 | import com.framsticks.params.types.StringParam; |
---|
| 34 | import com.framsticks.params.types.UniversalParam; |
---|
| 35 | import com.framsticks.util.lang.Strings; |
---|
| 36 | |
---|
| 37 | public final class Gui { |
---|
| 38 | |
---|
| 39 | private static final Logger log = Logger.getLogger(Gui.class.getName()); |
---|
| 40 | |
---|
| 41 | private Gui() { |
---|
| 42 | } |
---|
| 43 | |
---|
[87] | 44 | public static Control createComponentForText(PrimitiveParam<?> valueParam) { |
---|
[84] | 45 | if (valueParam.getMin(Object.class) != null) { |
---|
| 46 | return new TextAreaControl(valueParam); |
---|
| 47 | } |
---|
| 48 | return new TextFieldControl(valueParam); |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | public static Control createSuitable(Param param) { |
---|
| 52 | |
---|
| 53 | if (param instanceof EnumParam) { |
---|
| 54 | return new EnumControl((EnumParam) param); |
---|
| 55 | } |
---|
| 56 | if (param instanceof BooleanParam) { |
---|
| 57 | return new CheckBoxControl((BooleanParam) param); |
---|
| 58 | } |
---|
| 59 | if (param instanceof DecimalParam) { |
---|
| 60 | DecimalParam decimalParam = (DecimalParam)param; |
---|
| 61 | if (decimalParam.getMin(Integer.class) != null && decimalParam.getMax(Integer.class) != null) { |
---|
| 62 | return new SliderControl(decimalParam); |
---|
| 63 | } |
---|
| 64 | return createComponentForText(decimalParam); |
---|
| 65 | } |
---|
| 66 | if (param instanceof FloatParam) { |
---|
| 67 | FloatParam floatParam = (FloatParam)param; |
---|
| 68 | if (floatParam.getMin(Double.class) != null && floatParam.getMax(Double.class) != null) { |
---|
| 69 | return new SliderControl(floatParam); |
---|
| 70 | } |
---|
| 71 | return createComponentForText(floatParam); |
---|
| 72 | } |
---|
| 73 | if (param instanceof StringParam) { |
---|
| 74 | return createComponentForText((StringParam)param); |
---|
| 75 | } |
---|
| 76 | if (param instanceof ProcedureParam) { |
---|
| 77 | return new ProcedureControl((ProcedureParam)param); |
---|
| 78 | } |
---|
| 79 | if (param instanceof BinaryParam) { |
---|
| 80 | return createComponentForText((BinaryParam)param); |
---|
| 81 | } |
---|
| 82 | if (param instanceof ColorParam) { |
---|
| 83 | return createComponentForText((ColorParam) param); |
---|
| 84 | } |
---|
| 85 | if (param instanceof UniversalParam) { |
---|
| 86 | return new TextAreaControl((UniversalParam)param); |
---|
| 87 | } |
---|
| 88 | if (param instanceof EventParam) { |
---|
| 89 | return new EventControl((EventParam)param); |
---|
| 90 | } |
---|
| 91 | return null; |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | public static void fillWithControls(JPanel panel, Collection<Param> params, Map<Param, Control> components) { |
---|
| 95 | for (Param param : params) { |
---|
| 96 | if (param.isUserHidden()) { |
---|
| 97 | continue; |
---|
| 98 | } |
---|
| 99 | assert !(param instanceof CompositeParam); |
---|
| 100 | Control control = Gui.createSuitable(param); |
---|
| 101 | if (control == null) { |
---|
| 102 | log.error("component for param " + param + " of type " + param.getClass().getSimpleName() + " was not added"); |
---|
| 103 | continue; |
---|
| 104 | } |
---|
| 105 | log.debug("add component for " + param); |
---|
| 106 | JPanel line = new JPanel(); |
---|
| 107 | line.setLayout(new BoxLayout(line, BoxLayout.LINE_AXIS)); |
---|
| 108 | line.setAlignmentX(JPanel.LEFT_ALIGNMENT); |
---|
| 109 | JLabel label = new JLabel(Strings.notEmpty(param.getName()) ? param.getName() : "? (" + param.getId() + ")"); |
---|
| 110 | label.setToolTipText(control.getToolTipText()); |
---|
| 111 | label.setHorizontalAlignment(JLabel.RIGHT); |
---|
| 112 | Dimension labelSize = new Dimension(150, 30); |
---|
| 113 | label.setMaximumSize(labelSize); |
---|
| 114 | label.setMinimumSize(labelSize); |
---|
| 115 | label.setPreferredSize(labelSize); |
---|
| 116 | line.add(label); |
---|
| 117 | line.add(Box.createRigidArea(new Dimension(8, 0))); |
---|
| 118 | line.add(control); |
---|
| 119 | line.revalidate(); |
---|
| 120 | panel.add(line); |
---|
| 121 | panel.add(Box.createRigidArea(new Dimension(0, 8))); |
---|
| 122 | //component.setAlignmentX(LEFT_ALIGNMENT); |
---|
| 123 | components.put(param, control); |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | } |
---|
| 127 | } |
---|