View Javadoc

1   /********************************************************************************
2    * InternetCafe is a software solution that helps the management of Cybercafes 
3    * according with the ITALIAN DECREE LAW ON ANTI-TERROR MEASURES, 27 JULY 2005.
4    * Copyright (C) 2006  Guido Angelo Ingenito
5   
6    * This program is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU General Public License
8    * as published by the Free Software Foundation; either version 2
9    * of the License, or (at your option) any later version.
10  
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   * 
16   * You should have received a copy of the GNU General Public License
17   * along with this program; if not, write to the Free Software
18   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19   *******************************************************************************/
20  package ui.table.model;
21  
22  import java.util.Observable;
23  
24  import javax.swing.table.AbstractTableModel;
25  
26  import ui.Messages;
27  import base.user.NAddress;
28  import base.user.PhoneNumber;
29  import base.user.User;
30  
31  @SuppressWarnings("serial") //$NON-NLS-1$
32  public class PhoneNumberTableModel extends AbstractTableModel {
33  
34  	String[] columnNames = { Messages.getString("common.id"), Messages.getString("common.areacode"), Messages.getString("common.exchange"), Messages.getString("common.number"), //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
35  			Messages.getString("common.description") }; //$NON-NLS-1$
36  
37  	public static final int PHONENUMBER_ID_COLUMN = 0;
38  
39  	public static final int PHONENUMBER_AREA_CODE_COLUMN = 1;
40  
41  	public static final int PHONENUMBER_EXCHANGE_COLUMN = 2;
42  
43  	public static final int PHONENUMBER_NUMBER_COLUMN = 3;
44  
45  	public static final int PHONENUMBER_DESCRIPTION_COLUMN = 4;
46  
47  	private User user;
48  
49  	public PhoneNumberTableModel(User user) {
50  		this.user = user;
51  	}
52  
53  	public int getColumnCount() {
54  		return columnNames.length;
55  	}
56  
57  	public int getRowCount() {
58  		return user.getPhoneNumber().length;
59  	}
60  
61  	public String getColumnName(int col) {
62  		return columnNames[col];
63  	}
64  
65  	public Object getValueAt(int row, int column) {
66  		if (row < 0 || column < 0)
67  			return null;
68  		PhoneNumber[] phoneNumber = user.getPhoneNumber();
69  		Object result = null;
70  		switch (column) {
71  		case PHONENUMBER_ID_COLUMN:
72  			result = phoneNumber[row].getId();
73  			break;
74  		case PHONENUMBER_AREA_CODE_COLUMN:
75  			result = phoneNumber[row].getAreaCode();
76  			break;
77  		case PHONENUMBER_EXCHANGE_COLUMN:
78  			result = phoneNumber[row].getExchange();
79  			break;
80  		case PHONENUMBER_NUMBER_COLUMN:
81  			result = phoneNumber[row].getNumber();
82  			break;
83  		case PHONENUMBER_DESCRIPTION_COLUMN:
84  			result = phoneNumber[row].getDescription();
85  			break;
86  		}
87  		return result;
88  	}
89  
90  	@SuppressWarnings("unchecked") //$NON-NLS-1$
91  	public Class getColumnClass(int column) {
92  		if (column < 0)
93  			return null;
94  		Class result = null;
95  		switch (column) {
96  		case PHONENUMBER_ID_COLUMN:
97  			result = Integer.class;
98  			break;
99  		case PHONENUMBER_AREA_CODE_COLUMN:
100 			result = String.class;
101 			break;
102 		case PHONENUMBER_EXCHANGE_COLUMN:
103 			result = String.class;
104 			break;
105 		case PHONENUMBER_NUMBER_COLUMN:
106 			result = String.class;
107 			break;
108 		case PHONENUMBER_DESCRIPTION_COLUMN:
109 			result = String.class;
110 			break;
111 		}
112 		return result;
113 	}
114 
115 	/*
116 	 * Don't need to implement this method unless your table's editable.
117 	 */
118 	public boolean isCellEditable(int row, int column) {
119 		if (row < 0 || column < 0)
120 			return false;
121 		// Note that the data/cell address is constant,
122 		// no matter where the cell appears onscreen.
123 		boolean result = false;
124 		switch (column) {
125 		case PHONENUMBER_ID_COLUMN:
126 			result = false;
127 			break;
128 		case PHONENUMBER_AREA_CODE_COLUMN:
129 			result = true;
130 			break;
131 		case PHONENUMBER_EXCHANGE_COLUMN:
132 			result = true;
133 			break;
134 		case PHONENUMBER_NUMBER_COLUMN:
135 			result = true;
136 			break;
137 		case PHONENUMBER_DESCRIPTION_COLUMN:
138 			result = true;
139 			break;
140 		}
141 		return result;
142 	}
143 
144 	/*
145 	 * Don't need to implement this method unless your table's data can change.
146 	 */
147 	public void setValueAt(Object value, int row, int column) {
148 		if (row < 0 || column < 0)
149 			return;
150 		PhoneNumber phoneNumber = user.getPhoneNumberById(row);
151 		switch (column) {
152 		case PHONENUMBER_ID_COLUMN:
153 			break;
154 		case PHONENUMBER_AREA_CODE_COLUMN:
155 			phoneNumber.setAreaCode(value.toString());
156 			break;
157 		case PHONENUMBER_EXCHANGE_COLUMN:
158 			phoneNumber.setExchange(value.toString());
159 			break;
160 		case PHONENUMBER_NUMBER_COLUMN:
161 			phoneNumber.setNumber(value.toString());
162 			break;
163 		case PHONENUMBER_DESCRIPTION_COLUMN:
164 			phoneNumber.setDescription(value.toString());
165 			break;
166 		}
167 		fireTableCellUpdated(row, column);
168 	}
169 
170 	/*
171 	 * (non-Javadoc)
172 	 * 
173 	 * @see java.util.Observer#update(java.util.Observable, java.lang.Object)
174 	 */
175 	public void update(Observable observable, Object changedObject) {
176 		if (changedObject instanceof NAddress)
177 			this.fireTableDataChanged();
178 	}
179 }