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  import java.util.Observer;
24  
25  import javax.swing.table.AbstractTableModel;
26  
27  import ui.Messages;
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 NAddressTableModel extends AbstractTableModel implements Observer {
37  
38  	String[] columnNames = { Messages.getString("common.id"), Messages.getString("common.city"), Messages.getString("common.nation"), Messages.getString("common.street"), Messages.getString("common.region"), //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
39  			Messages.getString("common.postalcode"), Messages.getString("common.description") }; //$NON-NLS-1$ //$NON-NLS-2$
40  
41  	public static final int NADDRESS_ID_COLUMN = 0;
42  
43  	private static final int NADDRESS_CITY_COLUMN = 1;
44  
45  	private static final int NADDRESS_NATION_COLUMN = 2;
46  
47  	private static final int NADDRESS_STREET_COLUMN = 3;
48  
49  	private static final int NADDRESS_REGION_COLUMN = 4;
50  
51  	private static final int NADDRESS_POSTAL_CODE_COLUMN = 5;
52  
53  	private static final int NADDRESS_DESCRIPTION_COLUMN = 6;
54  
55  	private User user;
56  
57  	public NAddressTableModel() {
58  		super();
59  	}
60  
61  	public NAddressTableModel(User user) {
62  		super();
63  		this.user = user;
64  		// FUK! WHY I CAN'T WRITE THAT ?
65  		// this.user.addObserver(this);
66  	}
67  
68  	public int getColumnCount() {
69  		return columnNames.length;
70  	}
71  
72  	public int getRowCount() {
73  		return user.getNAddress().length;
74  	}
75  
76  	public String getColumnName(int col) {
77  		return columnNames[col];
78  	}
79  
80  	public Object getValueAt(int row, int column) {
81  		if (row < 0 || column < 0)
82  			return null;
83  		NAddress[] address = user.getNAddress();
84  		Object result = null;
85  		switch (column) {
86  		case NADDRESS_ID_COLUMN:
87  			result = address[row].getId();
88  			break;
89  		case NADDRESS_CITY_COLUMN:
90  			result = address[row].getCity();
91  			break;
92  		case NADDRESS_NATION_COLUMN:
93  			result = address[row].getNation();
94  			break;
95  		case NADDRESS_STREET_COLUMN:
96  			result = address[row].getStreet();
97  			break;
98  		case NADDRESS_REGION_COLUMN:
99  			result = address[row].getRegion();
100 			break;
101 		case NADDRESS_POSTAL_CODE_COLUMN:
102 			result = address[row].getPostalCode();
103 			break;
104 		case NADDRESS_DESCRIPTION_COLUMN:
105 			result = address[row].getDescription();
106 			break;
107 		}
108 		return result;
109 	}
110 
111 	@SuppressWarnings("unchecked") //$NON-NLS-1$
112 	public Class getColumnClass(int column) {
113 		if (column < 0)
114 			return null;
115 		Class result = null;
116 		switch (column) {
117 		case NADDRESS_ID_COLUMN:
118 			result = Integer.class;
119 			break;
120 		case NADDRESS_CITY_COLUMN:
121 			result = String.class;
122 			break;
123 		case NADDRESS_NATION_COLUMN:
124 			result = String.class;
125 			break;
126 		case NADDRESS_STREET_COLUMN:
127 			result = String.class;
128 			break;
129 		case NADDRESS_REGION_COLUMN:
130 			result = String.class;
131 			break;
132 		case NADDRESS_POSTAL_CODE_COLUMN:
133 			result = String.class;
134 			break;
135 		case NADDRESS_DESCRIPTION_COLUMN:
136 			result = String.class;
137 			break;
138 		}
139 		return result;
140 	}
141 
142 	/*
143 	 * Don't need to implement this method unless your table's editable.
144 	 */
145 	public boolean isCellEditable(int row, int column) {
146 		if (row < 0 || column < 0)
147 			return false;
148 		// Note that the data/cell address is constant,
149 		// no matter where the cell appears onscreen.
150 		boolean result = false;
151 		switch (column) {
152 		case NADDRESS_ID_COLUMN:
153 			result = false;
154 			break;
155 		case NADDRESS_CITY_COLUMN:
156 			result = true;
157 			break;
158 		case NADDRESS_NATION_COLUMN:
159 			result = true;
160 			break;
161 		case NADDRESS_STREET_COLUMN:
162 			result = true;
163 			break;
164 		case NADDRESS_REGION_COLUMN:
165 			result = true;
166 			break;
167 		case NADDRESS_POSTAL_CODE_COLUMN:
168 			result = true;
169 			break;
170 		case NADDRESS_DESCRIPTION_COLUMN:
171 			result = true;
172 			break;
173 		}
174 		return result;
175 	}
176 
177 	/*
178 	 * Don't need to implement this method unless your table's data can change.
179 	 */
180 	public void setValueAt(Object value, int row, int column) {
181 		if (row < 0 || column < 0)
182 			return;
183 		switch (column) {
184 		case NADDRESS_ID_COLUMN:
185 			break;
186 		case NADDRESS_CITY_COLUMN:
187 			user.getNAddress()[row].setCity(value.toString());
188 			break;
189 		case NADDRESS_NATION_COLUMN:
190 			user.getNAddress()[row].setNation(value.toString());
191 			break;
192 		case NADDRESS_STREET_COLUMN:
193 			user.getNAddress()[row].setStreet(value.toString());
194 			break;
195 		case NADDRESS_REGION_COLUMN:
196 			user.getNAddress()[row].setRegion(value.toString());
197 			break;
198 		case NADDRESS_POSTAL_CODE_COLUMN:
199 			user.getNAddress()[row].setPostalCode(value.toString());
200 			break;
201 		case NADDRESS_DESCRIPTION_COLUMN:
202 			user.getNAddress()[row].setDescription(value.toString());
203 			break;
204 		}
205 		fireTableCellUpdated(row, column);
206 	}
207 
208 	/*
209 	 * (non-Javadoc)
210 	 * 
211 	 * @see java.util.Observer#update(java.util.Observable, java.lang.Object)
212 	 */
213 	public void update(Observable observable, Object changedObject) {
214 		if (changedObject instanceof NAddress)
215 			this.fireTableDataChanged();
216 	}
217 }