001: // The contents of this file are subject to the Mozilla Public License Version
002: // 1.1
003: //(the "License"); you may not use this file except in compliance with the
004: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
005: //
006: //Software distributed under the License is distributed on an "AS IS" basis,
007: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
008: //for the specific language governing rights and
009: //limitations under the License.
010: //
011: //The Original Code is "The Columba Project"
012: //
013: //The Initial Developers of the Original Code are Frederik Dietz and Timo
014: // Stich.
015: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
016: //
017: //All Rights Reserved.
018: package org.columba.addressbook.gui.table.model;
019:
020: import java.util.Iterator;
021: import java.util.Map;
022: import java.util.logging.Logger;
023:
024: import javax.swing.table.AbstractTableModel;
025:
026: import org.columba.addressbook.model.ContactModelPartial;
027: import org.columba.addressbook.model.IContactModelPartial;
028:
029: /**
030: * Simple table model, using an extended TableModel interface.
031: *
032: * @author fdietz
033: */
034: public class AddressbookTableModel extends AbstractTableModel implements
035: ContactItemTableModel {
036:
037: /** JDK 1.4+ logging framework logger, used for logging. */
038: private static final Logger LOG = Logger
039: .getLogger("org.columba.addressbook.gui.table.model");
040:
041: private String[] columns = { "displayname", "given", "family",
042: "email;internet", "url" };
043: private ContactModelPartial[] rows;
044:
045: private Map<String, IContactModelPartial> headerItemList;
046:
047: public AddressbookTableModel() {
048: super ();
049:
050: }
051:
052: public void setContactItemMap(Map<String, IContactModelPartial> list) {
053:
054: if (list == null) {
055: LOG.fine("map == null");
056:
057: rows = new ContactModelPartial[0];
058:
059: fireTableDataChanged();
060:
061: return;
062:
063: }
064:
065: this .headerItemList = list;
066:
067: if (list.size() == 0)
068: LOG.fine("map is empty");
069:
070: rows = new ContactModelPartial[list.size()];
071:
072: Iterator it = list.values().iterator();
073: int i = 0;
074: while (it.hasNext()) {
075: rows[i++] = (ContactModelPartial) it.next();
076: }
077:
078: fireTableDataChanged();
079: }
080:
081: public void update() {
082: rows = new ContactModelPartial[headerItemList.size()];
083:
084: Iterator it = headerItemList.values().iterator();
085: int i = 0;
086: while (it.hasNext()) {
087: rows[i++] = (ContactModelPartial) it.next();
088: }
089:
090: fireTableDataChanged();
091: }
092:
093: /**
094: * @see javax.swing.table.TableModel#getColumnCount()
095: */
096: public int getColumnCount() {
097: return columns.length;
098: }
099:
100: /**
101: * @see javax.swing.table.TableModel#getRowCount()
102: */
103: public int getRowCount() {
104: if (rows == null)
105: return 0;
106:
107: return rows.length;
108: }
109:
110: /**
111: * @see javax.swing.table.TableModel#getValueAt(int, int)
112: */
113: public Object getValueAt(int row, int column) {
114: ContactModelPartial item = rows[row];
115:
116: switch (column) {
117: case 0:
118: return item.getName() != null ? item.getName() : "";
119: case 1:
120: return item.getFirstname() != null ? item.getFirstname()
121: : "";
122: case 2:
123: return item.getLastname() != null ? item.getLastname() : "";
124: case 3:
125: return item.getAddress();
126: case 4:
127: return item.getWebsite() != null ? item.getWebsite() : "";
128: default:
129: return "";
130:
131: }
132:
133: }
134:
135: /**
136: * @see org.columba.addressbook.gui.table.model.ContactItemTableModel#getHeaderItem(int)
137: */
138: public IContactModelPartial getContactItem(int index) {
139:
140: return rows[index];
141: }
142:
143: /**
144: * @return Returns the headerItemList.
145: */
146: public Map<String, IContactModelPartial> getContactItemMap() {
147: return headerItemList;
148: }
149: }
|