001 /* 002 * Copyright 2002-2006 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 005 * use this file except in compliance with the License. You may obtain a copy of 006 * the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 012 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 013 * License for the specific language governing permissions and limitations under 014 * the License. 015 */ 016 package org.springframework.richclient.samples.simple.ui; 017 018 import javax.swing.JTable; 019 import javax.swing.table.TableColumnModel; 020 021 import org.springframework.richclient.samples.simple.domain.Contact; 022 import org.springframework.richclient.samples.simple.domain.ContactDataStore; 023 import org.springframework.richclient.table.support.AbstractObjectTable; 024 025 /** 026 * This class provides a concrete implementation of a table showing {@link Contact} objects. 027 * @author lstreepy 028 */ 029 public class ContactTable extends AbstractObjectTable { 030 031 /** The data store holding all our contacts. */ 032 private ContactDataStore dataStore; 033 034 /** 035 * Default constructor. 036 */ 037 public ContactTable(ContactDataStore dataStore) { 038 super("contacts", new String[] { "lastName", "firstName", "address.address1", "address.city", "address.state", 039 "address.zip" }); 040 this.dataStore = dataStore; 041 } 042 043 protected void configureTable(JTable table) { 044 // Adjust the table column widths 045 TableColumnModel tcm = table.getColumnModel(); 046 tcm.getColumn(0).setPreferredWidth(100); 047 tcm.getColumn(1).setPreferredWidth(100); 048 tcm.getColumn(2).setPreferredWidth(200); 049 tcm.getColumn(3).setPreferredWidth(50); 050 tcm.getColumn(4).setPreferredWidth(10); 051 tcm.getColumn(5).setPreferredWidth(50); 052 } 053 054 /** 055 * Provide the initial data for the table. Note that this is hard coded for this sample. You would normally access a 056 * persistent store, or some other source to get the data for the table. 057 */ 058 protected Object[] getDefaultInitialData() { 059 return dataStore.getAllContacts(); 060 } 061 062 /** 063 * Get the array of selected Contact objects in the table. 064 * @return array of Contacts, zero length if nothing is selected 065 */ 066 public Contact[] getSelectedContacts() { 067 int[] selected = getTable().getSelectedRows(); 068 Contact[] contacts = new Contact[selected.length]; 069 for (int i = 0; i < selected.length; i++) { 070 contacts[i] = (Contact) getTableModel().getElementAt(selected[i]); 071 } 072 return contacts; 073 } 074 075 public Contact getSelectedContact() { 076 return (Contact) getSelectedContacts()[0]; 077 } 078 }