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.EAddress;
28  import base.user.NAddress;
29  import base.user.User;
30  
31  /***
32   * @author skunk
33   * 
34   */
35  @SuppressWarnings("serial") //$NON-NLS-1$
36  public class EAddressTableModel extends AbstractTableModel {
37  
38  	String[] columnNames = { Messages.getString("common.id"), Messages.getString("common.address"), Messages.getString("common.description") }; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
39  
40  	public static final int EADDRESS_ID_COLUMN = 0;
41  
42  	private static final int EADDRESS_ADDRESS_COLUMN = 1;
43  
44  	private static final int EADDRESS_DESCRIPTION_COLUMN = 2;
45  
46  	private User user;
47  
48  	public EAddressTableModel(User user) {
49  		this.user = user;
50  	}
51  
52  	public int getColumnCount() {
53  		return columnNames.length;
54  	}
55  
56  	public int getRowCount() {
57  		return user.getEAddress().length;
58  	}
59  
60  	public String getColumnName(int col) {
61  		return columnNames[col];
62  	}
63  
64  	public Object getValueAt(int row, int column) {
65  		if (row < 0 || column < 0)
66  			return null;
67  		EAddress[] address = user.getEAddress();
68  		Object result = null;
69  		switch (column) {
70  		case EADDRESS_ID_COLUMN:
71  			result = address[row].getId();
72  			break;
73  		case EADDRESS_ADDRESS_COLUMN:
74  			result = address[row].toString();
75  			break;
76  		case EADDRESS_DESCRIPTION_COLUMN:
77  			result = address[row].getDescription();
78  			break;
79  		}
80  		return result;
81  	}
82  
83  	@SuppressWarnings("unchecked") //$NON-NLS-1$
84  	public Class getColumnClass(int column) {
85  		if (column < 0)
86  			return null;
87  		Class result = null;
88  		switch (column) {
89  		case EADDRESS_ID_COLUMN:
90  			result = Integer.class;
91  			break;
92  		case EADDRESS_ADDRESS_COLUMN:
93  			result = String.class;
94  			break;
95  		case EADDRESS_DESCRIPTION_COLUMN:
96  			result = String.class;
97  			break;
98  		}
99  		return result;
100 	}
101 
102 	/*
103 	 * Don't need to implement this method unless your table's editable.
104 	 */
105 	public boolean isCellEditable(int row, int column) {
106 		if (row < 0 || column < 0)
107 			return false;
108 		// Note that the data/cell address is constant,
109 		// no matter where the cell appears onscreen.
110 		boolean result = false;
111 		switch (column) {
112 		case EADDRESS_ID_COLUMN:
113 			result = false;
114 			break;
115 		case EADDRESS_ADDRESS_COLUMN:
116 			result = false;
117 			break;
118 		case EADDRESS_DESCRIPTION_COLUMN:
119 			result = true;
120 			break;
121 		}
122 		return result;
123 	}
124 
125 	/*
126 	 * Don't need to implement this method unless your table's data can change.
127 	 */
128 	public void setValueAt(Object value, int row, int column) {
129 		if (row < 0 || column < 0)
130 			return;
131 		EAddress[] address = user.getEAddress();
132 		switch (column) {
133 		case EADDRESS_DESCRIPTION_COLUMN:
134 			address[row].equals(value);
135 			break;
136 		}
137 		fireTableCellUpdated(row, column);
138 	}
139 
140 	/*
141 	 * (non-Javadoc)
142 	 * 
143 	 * @see java.util.Observer#update(java.util.Observable, java.lang.Object)
144 	 */
145 	public void update(Observable observable, Object changedObject) {
146 		if (changedObject instanceof NAddress)
147 			this.fireTableDataChanged();
148 	}
149 }