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