View Javadoc

1   /*******************************************************************************
2    * Copyright 2013 Universidad Politécnica de Madrid
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * An alternative to drawing Repeats, Specially indicated for editable repeats, since forms whould be modeled and rendered with in a 
43   * {@link JPanel} fitted with a {@link GridLayout}.
44   * @author amedrano
45   *
46   */
47  public class RepeatModelGrid extends RepeatModel {
48  
49  	/**
50  	 * The {@link GridLayout}, useful for extenders of this class to access and change the v and h gaps.
51  	 */
52  	private GridLayout layout;
53  	
54      /**
55       * container for children of {@link #repeat}
56       */
57      private FormControl[] elems;
58  
59  	private JPanel grid;
60  
61  	private RepeatSubdivider subDivider;
62  
63  	/**
64  	 * @param control
65  	 * @param render
66  	 */
67  	public RepeatModelGrid(Repeat control, Renderer render) {
68  		super(control, render);
69  	}
70  
71  	/** {@ inheritDoc}	 */
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  		//TODO implement MyGridLayout which adjusts columns independently.
84  		grid.setLayout(layout);
85  	}
86  	
87  	private void reDrawPanel(){
88  		update();
89  		grid.revalidate();
90  	}
91  	
92  	/** {@ inheritDoc}	 */
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 	 * See {@link RepeatModelGrid#layout}.
127 	 * @return The layout used for the JPanel containing the repeat sub components.
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 	 * Class representing the Add button for Tables.
148 	 * @author amedrano
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 		 * Java Serializer Variable
172 		 */
173 		private static final long serialVersionUID = 1L;
174 
175 		/**
176 		 * Constructor for Add button
177 		 */
178 		public AddTableButton() {
179 			super();
180 			this.addActionListener(this);
181 			this.setName(fc.getURI()+"_Add");
182 		}
183 
184 		/** {@inheritDoc}*/
185 		public void actionPerformed(ActionEvent e) {
186 			SwingUtilities.invokeLater(new Runnable() {
187 				public void run() {
188 					if (((Repeat)fc).addValue()){
189 						// re draw the whole Panel.
190 						reDrawPanel();
191 					}
192 				}
193 			});
194 		}
195 	}
196 
197 	/**
198 	 * Class representing the Delete button for Tables.
199 	 * @author amedrano
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 		 * Java Serializer Variable
223 		 */
224 		private static final long serialVersionUID = 1L;
225 
226 		
227 		/**
228 		 * Constructor for Remove Button
229 		 */
230 		public DeleteTableButton() {
231 			super();
232 			this.addActionListener(this);
233 			this.setName(fc.getURI()+"_Delete");
234 		}
235 		
236 		/** {@inheritDoc}*/
237 		public void actionPerformed(ActionEvent e) {
238 			SwingUtilities.invokeLater(new Runnable() {
239 				public void run() {
240 					if (((Repeat)fc).addValue()){
241 						// re draw the whole Panel.
242 						reDrawPanel();
243 					}
244 				}
245 			});
246 		}
247 	}
248 	
249 	private class RowChanger implements FocusListener {
250 		
251 		private int row;
252 		
253 		
254 
255 		/**
256 		 * Create a Row listener that will change the selected row when the element is selected.
257 		 * @param row the row at which the listened element is at.
258 		 */
259 		public RowChanger(int row) {
260 			super();
261 			this.row = row;
262 		}
263 
264 		/** {@ inheritDoc}	 */
265 		public void focusGained(FocusEvent e) {
266 			TaskQueue.addTask(new Runnable() {
267 				public void run() {
268 					// change the repeat's lselected row to the row of this component.
269 					Repeat r = (Repeat) fc;
270 					r.setSelection(row);
271 				}
272 			});
273 		}
274 
275 		/** {@ inheritDoc}	 */
276 		public void focusLost(FocusEvent e) {
277 			// Nothing, another component will be called to focusGained().
278 		}
279 		
280 	}
281 }