1
2
3
4
5
6
7
8
9
10
11
12 package sk.uniba.euromath.editor.widgets;
13 import java.util.ArrayList;
14 import java.util.HashMap;
15 import java.util.List;
16 import java.util.Map;
17 import org.eclipse.swt.widgets.Composite;
18 /***
19 * <p>
20 * Encapsulates multiple widgets into one.
21 * </p>
22 * <p>
23 * The widget's model is a {@link List} containing models of all child widgets,
24 * in the same order as in the {@link #getWidgets()} list. You may override this
25 * behaviour to return some composition bean or the like.
26 * </p>
27 * @author Martin Vysny
28 */
29 public class MultiWidget extends AbstractUserInputWidget {
30 /***
31 * Creates the encapsulator.
32 * @param parent composite where all widgets are already placed
33 * @param widgets list of all encapsulated widgets.
34 */
35 public MultiWidget(final Composite parent,
36 final List< ? extends IUserInputWidget> widgets) {
37 super();
38 this.parent = parent;
39 this.widgets = widgets;
40
41 for (final IUserInputWidget widget : widgets) {
42 widget.addModifyListener(modifyListener);
43 }
44 }
45 /***
46 * Modify listener that simply fires dataModified event.
47 */
48 private final IModifyListener modifyListener = new IModifyListener() {
49
50
51
52
53 public void dataModified() {
54 fireDataModified();
55 }
56 };
57 /***
58 * Returns a list of widgets provided to the constructor.
59 * @return list of widgets.
60 */
61 public final List< ? extends IUserInputWidget> getWidgets() {
62 return widgets;
63 }
64 /***
65 * composite where all widgets are placed
66 */
67 protected final Composite parent;
68 /***
69 * list of all encapsulated widgets.
70 */
71 protected final List< ? extends IUserInputWidget> widgets;
72
73
74
75
76 public final Composite getComposite() {
77 return parent;
78 }
79
80
81
82
83 public ValidityMessages getMessages() {
84 final Map<MessageLevelEnum, ValidityMessages> msgsLevels = new HashMap<MessageLevelEnum, ValidityMessages>();
85
86 for (final IUserInputWidget widget : widgets) {
87 final ValidityMessages msgs = widget.getMessages();
88 if (ValidityMessages.isError(msgs))
89 return msgs;
90 final MessageLevelEnum level = (msgs == null) ? null : msgs
91 .getLevel();
92 if ((level == MessageLevelEnum.INFO)
93 || (level == MessageLevelEnum.WARNING))
94 if (msgsLevels.get(level) == null)
95 msgsLevels.put(level, msgs);
96 }
97
98 if (msgsLevels.get(MessageLevelEnum.WARNING) != null)
99 return msgsLevels.get(MessageLevelEnum.WARNING);
100 return msgsLevels.get(MessageLevelEnum.INFO);
101 }
102
103
104
105
106 public List<Object> getState() {
107 final List<Object> result = new ArrayList<Object>(widgets.size());
108 for (final IUserInputWidget widget : widgets) {
109 result.add(widget.getState());
110 }
111 return result;
112 }
113
114
115
116
117 public Class< ? > getStateClass() {
118 return List.class;
119 }
120
121
122
123
124 @SuppressWarnings("unchecked")
125 public void setState(Object model) {
126 final List<Object> newModel = (List<Object>) model;
127 for (int i = 0; i < newModel.size(); i++) {
128 widgets.get(i).setState(newModel.get(i));
129 }
130 }
131 }