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