1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.universAAL.ui.handler.gui.swing.model.FormControl;
18
19 import java.awt.GridLayout;
20 import java.awt.event.ActionEvent;
21 import java.awt.event.ActionListener;
22 import java.awt.event.FocusEvent;
23 import java.awt.event.FocusListener;
24 import java.util.Iterator;
25 import java.util.List;
26
27 import javax.swing.Icon;
28 import javax.swing.JButton;
29 import javax.swing.JComponent;
30 import javax.swing.JLabel;
31 import javax.swing.JPanel;
32 import javax.swing.SwingUtilities;
33
34 import org.universAAL.middleware.ui.rdf.Form;
35 import org.universAAL.middleware.ui.rdf.FormControl;
36 import org.universAAL.middleware.ui.rdf.Repeat;
37 import org.universAAL.ui.handler.gui.swing.Renderer;
38 import org.universAAL.ui.handler.gui.swing.model.FormControl.support.RepeatSubdivider;
39 import org.universAAL.ui.handler.gui.swing.model.FormControl.support.TaskQueue;
40
41
42
43
44
45
46
47 public class RepeatModelGrid extends RepeatModel {
48
49
50
51
52 private GridLayout layout;
53
54
55
56
57 private FormControl[] elems;
58
59 private JPanel grid;
60
61 private RepeatSubdivider subDivider;
62
63
64
65
66
67 public RepeatModelGrid(Repeat control, Renderer render) {
68 super(control, render);
69 }
70
71
72 public JComponent getNewComponent() {
73 Repeat r = (Repeat)fc;
74 subDivider = new RepeatSubdivider(r);
75 elems = subDivider.getElems();
76 grid = new JPanel();
77 updateLayout();
78 return grid;
79 }
80
81 private void updateLayout(){
82 layout = new GridLayout(0,elems.length);
83
84 grid.setLayout(layout);
85 }
86
87 private void reDrawPanel(){
88 update();
89 grid.revalidate();
90 }
91
92
93 public void update(){
94 grid.removeAll();
95 Repeat r = (Repeat)fc;
96 subDivider = new RepeatSubdivider(r);
97 elems = subDivider.getElems();
98 updateLayout();
99 updateContents();
100 }
101
102
103 protected void updateContents() {
104 int ii = 0;
105 for (int i = 0; i < elems.length; i++) {
106 JLabel colLabel =
107 getRenderer().getModelMapper()
108 .getModelFor(elems[i].getLabel()).getComponent();
109 grid.add(colLabel);
110 }
111 List forms = subDivider.generateSubForms();
112 for (Iterator i = forms.iterator(); i.hasNext();) {
113 Form f = (Form) i.next();
114 FormControl[] fcs = f.getIOControls().getChildren();
115 for (int j = 0; j < fcs.length; j++) {
116 JComponent jc = getRenderer()
117 .getModelMapper().getModelFor(fcs[j]).getComponent();
118 jc.addFocusListener(new RowChanger(ii));
119 grid.add(jc);
120 }
121 ii++;
122 }
123 }
124
125
126
127
128
129 protected GridLayout getLayout(){
130 return layout;
131 }
132
133 protected JPanel getButtonPanel() {
134 Repeat r = (Repeat)fc;
135
136 JPanel buttonPanel = new JPanel();
137 if (r.listAcceptsNewEntries()) {
138 buttonPanel.add(new AddTableButton());
139 }
140 if (r.listEntriesDeletable()) {
141 buttonPanel.add(new DeleteTableButton());
142 }
143 return buttonPanel;
144 }
145
146
147
148
149
150 public class AddTableButton extends JButton implements ActionListener {
151
152 public AddTableButton(Icon icon) {
153 super(icon);
154 this.addActionListener(this);
155 this.setName(fc.getURI()+"_Add");
156 }
157
158 public AddTableButton(String text, Icon icon) {
159 super(text, icon);
160 this.addActionListener(this);
161 this.setName(fc.getURI()+"_Add");
162 }
163
164 public AddTableButton(String text) {
165 super(text);
166 this.addActionListener(this);
167 this.setName(fc.getURI()+"_Add");
168 }
169
170
171
172
173 private static final long serialVersionUID = 1L;
174
175
176
177
178 public AddTableButton() {
179 super();
180 this.addActionListener(this);
181 this.setName(fc.getURI()+"_Add");
182 }
183
184
185 public void actionPerformed(ActionEvent e) {
186 SwingUtilities.invokeLater(new Runnable() {
187 public void run() {
188 if (((Repeat)fc).addValue()){
189
190 reDrawPanel();
191 }
192 }
193 });
194 }
195 }
196
197
198
199
200
201 public class DeleteTableButton extends JButton implements ActionListener {
202
203 public DeleteTableButton(Icon icon) {
204 super(icon);
205 this.addActionListener(this);
206 this.setName(fc.getURI()+"_Delete");
207 }
208
209 public DeleteTableButton(String text, Icon icon) {
210 super(text, icon);
211 this.addActionListener(this);
212 this.setName(fc.getURI()+"_Delete");
213 }
214
215 public DeleteTableButton(String text) {
216 super(text);
217 this.addActionListener(this);
218 this.setName(fc.getURI()+"_Delete");
219 }
220
221
222
223
224 private static final long serialVersionUID = 1L;
225
226
227
228
229
230 public DeleteTableButton() {
231 super();
232 this.addActionListener(this);
233 this.setName(fc.getURI()+"_Delete");
234 }
235
236
237 public void actionPerformed(ActionEvent e) {
238 SwingUtilities.invokeLater(new Runnable() {
239 public void run() {
240 if (((Repeat)fc).addValue()){
241
242 reDrawPanel();
243 }
244 }
245 });
246 }
247 }
248
249 private class RowChanger implements FocusListener {
250
251 private int row;
252
253
254
255
256
257
258
259 public RowChanger(int row) {
260 super();
261 this.row = row;
262 }
263
264
265 public void focusGained(FocusEvent e) {
266 TaskQueue.addTask(new Runnable() {
267 public void run() {
268
269 Repeat r = (Repeat) fc;
270 r.setSelection(row);
271 }
272 });
273 }
274
275
276 public void focusLost(FocusEvent e) {
277
278 }
279
280 }
281 }