1 package org.catacomb.druid.blocks;
2
3 import org.catacomb.druid.build.Context;
4 import org.catacomb.druid.build.GUIPath;
5 import org.catacomb.druid.gui.base.DruPanel;
6 import org.catacomb.druid.gui.edit.DruChoice;
7 import org.catacomb.interlish.structure.AddableTo;
8 import org.catacomb.report.E;
9
10 import java.util.ArrayList;
11
12
13 public class Choice extends Panel implements AddableTo {
14
15
16 public String label;
17
18 public String store;
19
20
21 public String action;
22 public String options;
23 public String labels;
24
25 public String from;
26 public int autoSelect;
27
28 public boolean required;
29 public String autoset;
30
31 private ArrayList<Option> optionsAL;
32 public ArrayList<BaseEffect> effects;
33
34
35 public Choice() {
36 autoSelect = -1;
37 }
38
39
40 public Choice(String slab) {
41 label = slab;
42 action = label;
43 autoSelect = -1;
44 }
45
46
47 public void add(Object obj) {
48 if (obj instanceof Option) {
49 if (optionsAL == null) {
50 optionsAL = new ArrayList<Option>();
51 }
52 optionsAL.add((Option)obj);
53
54 } else if (obj instanceof BaseEffect) {
55 if (effects == null) {
56 effects = new ArrayList<BaseEffect>();
57 }
58 effects.add((BaseEffect)obj);
59
60 } else {
61 E.error("cant add " + obj);
62 }
63 }
64
65
66
67 public DruPanel instantiatePanel() {
68 return new DruChoice(label, action);
69 }
70
71
72 public void populatePanel(DruPanel dp, Context ctx, GUIPath gpath) {
73
74 DruChoice drup = (DruChoice)dp;
75 String[] sopts = null;
76 String[] slabs = null;
77
78 if (optionsAL != null) {
79 int n = optionsAL.size();
80 sopts = new String[n];
81 slabs = new String[n];
82 for (int i = 0; i < n; i++) {
83 Option opt = optionsAL.get(i);
84 sopts[i] = (opt.label != null ? opt.label : opt.value);
85 slabs[i] = (opt.label != null ? opt.value : opt.label);
86 }
87
88 } else if (options != null) {
89 String[] sa = options.split(",");
90
91 sopts = new String[sa.length];
92 slabs = new String[sa.length];
93
94 for (int i = 0; i < sa.length; i++) {
95 String s = sa[i].trim();
96 sopts[i] = s;
97 slabs[i] = s;
98 }
99
100 if (labels != null) {
101 String[] sb = labels.split(",");
102 for (int i = 0; i < sa.length && i < sopts.length; i++) {
103 slabs[i] = sb[i].trim();
104 }
105 }
106 }
107
108 if (sopts != null) {
109 drup.setOptions(sopts, slabs);
110 if (autoSelect >= 0) {
111 drup.setAutoSelect(autoSelect);
112 } else if (autoset != null) {
113 for (int i = 0; i < sopts.length; i++) {
114 if (sopts[i].equals(autoset)) {
115 drup.setAutoSelect(i);
116 }
117 }
118 }
119 }
120
121
122 if (from != null && from.length() > 0) {
123 ctx.getMarketplace().addConsumer("ChoiceOptions", drup, from);
124 }
125
126 if (effects != null) {
127 drup.setEffects(realizeEffects(effects, ctx, gpath));
128 }
129
130 }
131
132 }