1   /*
2    * Copyright 2008 motej
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  package motej.demos.balanceboard;
17  
18  import java.awt.BorderLayout;
19  
20  import javax.swing.JPanel;
21  import javax.swing.JTable;
22  import javax.swing.table.AbstractTableModel;
23  
24  import motejx.extensions.balanceboard.BalanceBoard;
25  import motejx.extensions.balanceboard.BalanceBoardEvent;
26  import motejx.extensions.balanceboard.BalanceBoardListener;
27  
28  /**
29   * 
30   * <p>
31   * @author <a href="mailto:vfritzsch@users.sourceforge.net">Volker Fritzsch</a>
32   */
33  public class BalanceBoardListenerPanel implements BalanceBoardListener {
34  
35  	private JPanel panel;
36  	
37  	private JTable table;
38  	
39  	private BalanceBoardEvent event;
40  	
41  	private class Model extends AbstractTableModel {
42  	
43  		public Object getValueAt(int rowIndex, int columnIndex) {
44  			if (columnIndex == 0) {
45  			switch(rowIndex) {
46  			case 0:
47  				return "Sensor A";
48  			case 1:
49  				return "Sensor B";
50  			case 2:
51  				return "Sensor C";
52  			case 3:
53  				return "Sensor D";
54  			}}
55  			
56  			if (columnIndex == 1) {
57  				if (event == null) {
58  					return "-";
59  				}
60  				
61  				switch (rowIndex) {
62  				case 0:
63  					return event.getTopRight();
64  				case 1:
65  					return event.getBottomRight();
66  				case 2:
67  					return event.getTopLeft();
68  				case 3:
69  					return event.getBottomLeft();
70  				}
71  			}
72  			
73  			if (columnIndex == 2) {
74  				if (event == null) {
75  					return "-";
76  				}
77  				
78  				switch (rowIndex) {
79  				case 0:
80  					return event.getTopRightInterpolated();
81  				case 1:
82  					return event.getBottomRightInterpolated();
83  				case 2:
84  					return event.getTopLeftInterpolated();
85  				case 3:
86  					return event.getBottomLeftInterpolated();
87  				}
88  			}
89  			return null;
90  		}
91  	
92  		public int getRowCount() {
93  			return 4;
94  		}
95  	
96  		public int getColumnCount() {
97  			return 3;
98  		}
99  		
100 		public void changed() {
101 			fireTableDataChanged();
102 		}
103 	};
104 	
105 	private Model model = new Model();
106 	
107 	public BalanceBoardListenerPanel(BalanceBoard board) {
108 		board.addBalanceBoardListener(this);
109 		
110 		table = new JTable(model);
111 		panel = new JPanel(new BorderLayout());
112 		panel.add(table, BorderLayout.CENTER);
113 	}
114 	
115 	public void balanceBoardChanged(BalanceBoardEvent evt) {
116 		this.event = evt;
117 		model.changed();
118 	}
119 
120 	public JPanel getPanel() {
121 		return panel;
122 	}
123 }