Skip to content

Package: FolderModel

FolderModel

nameinstructionbranchcomplexitylinemethod
FolderModel()
M: 35 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
getCachedData(int)
M: 73 C: 0
0%
M: 10 C: 0
0%
M: 6 C: 0
0%
M: 20 C: 0
0%
M: 1 C: 0
0%
getColumnClass(int)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getColumnCount()
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getColumnName(int)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getMessage(int)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getRowCount()
M: 9 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
getValueAt(int, int)
M: 16 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
setFolder(Folder)
M: 36 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 12 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * Copyright (c) 1997, 2021 Oracle and/or its affiliates. All rights reserved.
3: *
4: * This program and the accompanying materials are made available under the
5: * terms of the Eclipse Distribution License v. 1.0, which is available at
6: * http://www.eclipse.org/org/documents/edl-v10.php.
7: *
8: * SPDX-License-Identifier: BSD-3-Clause
9: */
10:
11: import jakarta.mail.*;
12: import java.util.Date;
13: import javax.swing.table.AbstractTableModel;
14:
15: /**
16: * Maps the messages in a Folder to the Swing's Table Model
17: *
18: * @author        Christopher Cotton
19: * @author        Bill Shannon
20: */
21:
22: public class FolderModel extends AbstractTableModel {
23:
24: Folder        folder;
25: Message[]        messages;
26:
27: String[]        columnNames = { "Date", "From", "Subject"};
28: Class[]        columnTypes = { String.class, String.class, String.class };
29:
30: public void setFolder(Folder what) throws MessagingException {
31:•        if (what != null) {
32:
33:          // opened if needed
34:•         if (!what.isOpen()) {
35:                 what.open(Folder.READ_WRITE);
36:          }
37:
38:          // get the messages
39:          messages = what.getMessages();
40:          cached = new String[messages.length][];
41:         } else {
42:          messages = null;
43:          cached = null;
44:         }
45:         // close previous folder and switch to new folder
46:•        if (folder != null)
47:          folder.close(true);
48:         folder = what;
49:         fireTableDataChanged();
50: }
51:
52: public Message getMessage(int which) {
53:         return messages[which];
54: }
55:
56: //---------------------
57: // Implementation of the TableModel methods
58: //---------------------
59:
60: public String getColumnName(int column) {
61:         return columnNames[column];
62: }
63:
64: public Class getColumnClass(int column) {
65:         return columnTypes[column];
66: }
67:
68:
69: public int getColumnCount() {
70:         return columnNames.length;
71: }
72:
73: public int getRowCount() {
74:•        if (messages == null)
75:          return 0;
76:         
77:         return messages.length;
78: }
79:
80: public Object getValueAt(int aRow, int aColumn) {
81:•        switch(aColumn) {
82:         case 0:        // date
83:         case 1: // From                String[] what = getCachedData(aRow);
84:         case 2: // Subject
85:          String[] what = getCachedData(aRow);
86:•         if (what != null) {
87:                 return what[aColumn];
88:          } else {
89:                 return "";
90:          }
91:         
92:         default:
93:          return "";
94:         }
95: }
96:
97: protected static String[][]        cached;
98:
99: protected String[] getCachedData(int row) {
100:•        if (cached[row] == null) {
101:          try{
102:                 Message m = messages[row];
103:         
104:                 String[] theData = new String[4];
105:         
106:                 // Date
107:                 Date date = m.getSentDate();
108:•                if (date == null) {
109:                  theData[0] = "Unknown";
110:                 } else {
111:                  theData[0] = date.toString();
112:                 }
113:         
114:                 // From
115:                 Address[] adds = m.getFrom();
116:•                if (adds != null && adds.length != 0) {
117:                  theData[1] = adds[0].toString();        
118:                 } else {
119:                  theData[1] = "";
120:                 }
121:                 
122:                 // Subject
123:                 String subject = m.getSubject();
124:•                if (subject != null) {
125:                  theData[2] = subject;
126:                 } else {
127:                  theData[2] = "(No Subject)";
128:                 }
129:
130:                 cached[row] = theData;
131:          }
132:          catch (MessagingException e) {
133:                 e.printStackTrace();
134:          }
135:         }
136:         
137:         return cached[row];
138: }
139: }