0N/A/*
292N/A * Copyright (c) 1997-2010 Oracle and/or its affiliates. All rights reserved.
0N/A *
0N/A * Redistribution and use in source and binary forms, with or without
0N/A * modification, are permitted provided that the following conditions
0N/A * are met:
0N/A *
0N/A * - Redistributions of source code must retain the above copyright
0N/A * notice, this list of conditions and the following disclaimer.
0N/A *
0N/A * - Redistributions in binary form must reproduce the above copyright
0N/A * notice, this list of conditions and the following disclaimer in the
0N/A * documentation and/or other materials provided with the distribution.
0N/A *
292N/A * - Neither the name of Oracle nor the names of its
0N/A * contributors may be used to endorse or promote products derived
0N/A * from this software without specific prior written permission.
0N/A *
0N/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
0N/A * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
0N/A * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
0N/A * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
0N/A * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
0N/A * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
0N/A * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
0N/A * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
0N/A * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
0N/A * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
0N/A * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0N/A */
0N/A
0N/Aimport javax.mail.*;
0N/Aimport java.util.Date;
0N/Aimport javax.swing.table.AbstractTableModel;
0N/A
0N/A/**
0N/A * Maps the messages in a Folder to the Swing's Table Model
0N/A *
0N/A * @author Christopher Cotton
0N/A * @author Bill Shannon
0N/A */
0N/A
0N/Apublic class FolderModel extends AbstractTableModel {
0N/A
0N/A Folder folder;
0N/A Message[] messages;
0N/A
0N/A String[] columnNames = { "Date", "From", "Subject"};
0N/A Class[] columnTypes = { String.class, String.class, String.class };
0N/A
0N/A public void setFolder(Folder what) throws MessagingException {
0N/A if (what != null) {
0N/A
0N/A // opened if needed
0N/A if (!what.isOpen()) {
0N/A what.open(Folder.READ_WRITE);
0N/A }
0N/A
0N/A // get the messages
0N/A messages = what.getMessages();
0N/A cached = new String[messages.length][];
0N/A } else {
0N/A messages = null;
0N/A cached = null;
0N/A }
0N/A // close previous folder and switch to new folder
0N/A if (folder != null)
0N/A folder.close(true);
0N/A folder = what;
0N/A fireTableDataChanged();
0N/A }
0N/A
0N/A public Message getMessage(int which) {
0N/A return messages[which];
0N/A }
0N/A
0N/A //---------------------
0N/A // Implementation of the TableModel methods
0N/A //---------------------
0N/A
0N/A public String getColumnName(int column) {
0N/A return columnNames[column];
0N/A }
0N/A
0N/A public Class getColumnClass(int column) {
0N/A return columnTypes[column];
0N/A }
0N/A
0N/A
0N/A public int getColumnCount() {
28N/A return columnNames.length;
0N/A }
0N/A
0N/A public int getRowCount() {
0N/A if (messages == null)
0N/A return 0;
0N/A
0N/A return messages.length;
0N/A }
0N/A
0N/A public Object getValueAt(int aRow, int aColumn) {
0N/A switch(aColumn) {
0N/A case 0: // date
0N/A case 1: // From String[] what = getCachedData(aRow);
0N/A case 2: // Subject
0N/A String[] what = getCachedData(aRow);
0N/A if (what != null) {
0N/A return what[aColumn];
0N/A } else {
0N/A return "";
0N/A }
0N/A
0N/A default:
0N/A return "";
0N/A }
0N/A }
0N/A
0N/A protected static String[][] cached;
0N/A
0N/A protected String[] getCachedData(int row) {
0N/A if (cached[row] == null) {
0N/A try{
0N/A Message m = messages[row];
0N/A
0N/A String[] theData = new String[4];
0N/A
0N/A // Date
0N/A Date date = m.getSentDate();
0N/A if (date == null) {
0N/A theData[0] = "Unknown";
0N/A } else {
0N/A theData[0] = date.toString();
0N/A }
0N/A
0N/A // From
0N/A Address[] adds = m.getFrom();
0N/A if (adds != null && adds.length != 0) {
0N/A theData[1] = adds[0].toString();
0N/A } else {
0N/A theData[1] = "";
0N/A }
0N/A
0N/A // Subject
0N/A String subject = m.getSubject();
0N/A if (subject != null) {
0N/A theData[2] = subject;
0N/A } else {
0N/A theData[2] = "(No Subject)";
0N/A }
0N/A
0N/A cached[row] = theData;
0N/A }
0N/A catch (MessagingException e) {
0N/A e.printStackTrace();
0N/A }
0N/A }
0N/A
0N/A return cached[row];
0N/A }
0N/A}