Source of MailEntry.java


  1: 
  2: package mail.gui;
  3: 
  4: import java.util.Date;
  5: import java.util.Comparator;
  6: import adapter.TableEntry;
  7: import mail.*;
  8: 
  9: public class MailEntry implements TableEntry { 
 10: 
 11:   // position of each column 
 12:   public static final int PRIORITY_COLUMN = 0; 
 13:   public static final int STATUS_COLUMN   = 1; 
 14:   public static final int FROM_COLUMN     = 2; 
 15:   public static final int RECEIVED_COLUMN = 3; 
 16:   public static final int SUBJECT_COLUMN  = 4; 
 17: 
 18:   public static final String[] columnNames = { 
 19:     "Priority",
 20:     "Status",
 21:     "From", 
 22:     "Received",
 23:     "Subject",
 24:   };
 25: 
 26:   public static final String[] columnTips = { 
 27:     "Priority",
 28:     "Status",
 29:     "From", 
 30:     "Received",
 31:     "Subject",
 32:   };
 33: 
 34:   public static final Comparator[] comparators = {
 35:     new MailEntryComparator(PRIORITY_COLUMN),
 36:     new MailEntryComparator(STATUS_COLUMN),
 37:     new MailEntryComparator(FROM_COLUMN),
 38:     new MailEntryComparator(RECEIVED_COLUMN),
 39:     new MailEntryComparator(SUBJECT_COLUMN),
 40:   };
 41: 
 42:   public static final int[] columnWidths = { 50, 50, 100, 150, 200}; 
 43: 
 44:   public MailEntry(Mail mail) { 
 45:     this.mail = mail; 
 46:   }
 47: 
 48:   public int getColumnCount() {
 49:     return columnNames.length;
 50:   }
 51: 
 52:   public String getColumnName(int col) {
 53:     if (col >= 0 && 
 54:         col < columnNames.length) { 
 55:       return columnNames[col]; 
 56:     }
 57:     return null;
 58:   }
 59: 
 60:   public Object getColumnValue(int col) {
 61:     if (mail != null && 
 62:         col >= 0 && 
 63:         col < columnNames.length) { 
 64:       switch (col) { 
 65:       case PRIORITY_COLUMN: return mail.getPriority();  
 66:       case STATUS_COLUMN:   return mail.getStatus();  
 67:       case FROM_COLUMN:     return mail.getFrom();  
 68:       case RECEIVED_COLUMN: return mail.getDate();  
 69:       case SUBJECT_COLUMN:  return mail.getSubject();  
 70:       } 
 71:     }
 72:     return null;
 73:   }
 74: 
 75:   public String getColumnTip(int col) {
 76:     if (col >= 0 && 
 77:         col < columnTips.length) { 
 78:       return columnTips[col]; 
 79:     }
 80:     return null;
 81:   }
 82: 
 83:   public Class getColumnClass(int col) {
 84:     if (col == PRIORITY_COLUMN) { 
 85:       return MailPriority.class;
 86:     } else if (col == STATUS_COLUMN) {
 87:       return MailStatus.class;
 88:     } else if (col == RECEIVED_COLUMN) {
 89:       return Date.class;
 90:     } else { 
 91:       return String.class;
 92:     }
 93:   }
 94: 
 95:   public Comparator getColumnComparator(int col) {
 96:     if (col >= 0 && 
 97:         col < comparators.length) { 
 98:       return comparators[col]; 
 99:     }
100:     return null;
101:   }
102: 
103:   public int getColumnWidth(int col) {
104:     if (col >= 0 && 
105:         col < columnWidths.length) { 
106:       return columnWidths[col]; 
107:     }
108:     return -1;
109:   }
110: 
111:   protected Mail mail; 
112: 
113:   static public class MailEntryComparator implements Comparator {
114: 
115:     public MailEntryComparator(int col) { 
116:       this.col = col; 
117:     }
118: 
119:     public int compare(Object o1, Object o2) {
120:       if (o1 != null && 
121:           o2 != null &&
122:           o1 instanceof MailEntry &&
123:           o2 instanceof MailEntry) {
124:         MailEntry e1 = (MailEntry) o1; 
125:         MailEntry e2 = (MailEntry) o2; 
126:         if (col == PRIORITY_COLUMN) { 
127:           return -((Comparable) e1.getColumnValue(col)).compareTo(e2.getColumnValue(col));
128:         } else if (col == STATUS_COLUMN) { 
129:           return ((Comparable) e1.getColumnValue(col)).compareTo(e2.getColumnValue(col));
130:         } else if (col == RECEIVED_COLUMN) { 
131:           return ((Date) e1.getColumnValue(col)).compareTo(e2.getColumnValue(col));
132:         } else { 
133:           return ((String) e1.getColumnValue(col)).compareTo(e2.getColumnValue(col)); 
134:         }        
135:       }
136:       return 0;
137:     }
138:    
139:     protected int col; 
140:   }
141:   
142: 
143: }