1 package SnowMailClient.model.multipart; 2 3 import SnowMailClient.model.*; 4 import SnowMailClient.utils.*; 5 import java.awt.*; 6 import java.awt.event.*; 7 import javax.swing.*; 8 import javax.swing.tree.*; 9 import java.util.*; 10 11 17 public final class MimeTreeModel extends DefaultTreeModel 18 { 19 private MimePart rootPart; 20 21 int i1 = (int)(Math.random()*1e8); 23 int i2 = (int)(Math.random()*1e8); 24 25 public MimeTreeModel(MimePart mp) 26 { 27 super(mp); 28 rootPart = mp; 29 } 30 31 public MimePart getRootPart() { return rootPart; } 32 33 35 public void clearMimeTree() 36 { 37 rootPart = new MimePart(); 38 this.setRoot(rootPart); 39 } 40 41 43 public MimePart[] searchInTextParts(String txtUP) 44 { 45 Vector<MimePart> foundParts = new Vector<MimePart>(); 46 searchTextRecurse(foundParts, this.rootPart, txtUP); 47 return foundParts.toArray(new MimePart[foundParts.size()]); 48 } 49 50 private void searchTextRecurse(Vector<MimePart> found, MimePart node, String txtUP) 51 { 52 if(node.containText(txtUP)) 53 { 54 found.addElement(node); 55 } 56 57 for(int i=0; i<node.getChildCount(); i++) 58 { 59 searchTextRecurse(found, node.getPartAt(i), txtUP); 60 } 61 } 62 63 64 67 public boolean isMimeMessageFormat() 68 { 69 if(rootPart.getChildCount()>0) return true; 70 if(rootPart.lookIfContentIsAnAttachment()) return true; return false; 72 } 73 74 75 77 public MimePart getFirstPlainTextPart() 78 { 79 return getFirstNodeOfType(this.rootPart, MimePart.ContentType.TEXT, "PLAIN"); 80 } 81 82 84 public MimePart getFirstHTMLTextPart() 85 { 86 return getFirstNodeOfType(this.rootPart, MimePart.ContentType.TEXT, "HTML"); 87 } 88 89 92 public void setMessagePlainText(Address from, Vector<Address> tos, String subject, String text) 93 { 94 MimePart partText = this.getFirstPlainTextPart(); 96 if(partText==null) 97 { 98 partText = new MimePart(); 100 rootPart.insert(partText, 0); 102 } 103 104 try 105 { 106 partText.setContentAsPlainTextMailMessage(text); 107 rootPart.setContentAsMIME_ROOT(from, tos, subject); 108 } 109 catch(Exception e) 110 { 111 e.printStackTrace(); 113 } 114 } 115 116 117 private MimePart getFirstNodeOfType(MimePart node, MimePart.ContentType type, String subtype) 118 { 119 if(node.getContentTYPE()==type && node.getContentSubType().equals(subtype)) 120 { 121 return node; 122 } 123 for(int i=0; i<node.getChildCount(); i++) 124 { 125 MimePart mp = getFirstNodeOfType(node.getPartAt(i), type, subtype); 127 if(mp!=null) return mp; 128 } 129 return null; 130 } 131 132 public MimePart getPartWithID(String id) 133 { 134 return getNodeWithID(this.getRootPart(), id); 135 } 136 137 private MimePart getNodeWithID(MimePart node, String id) 138 { 139 if(node.getID().equalsIgnoreCase(id)) return node; 140 141 for(int i=0; i<node.getChildCount(); i++) 142 { 143 MimePart mp = getNodeWithID(node.getPartAt(i), id); 145 if(mp!=null) return mp; 146 } 147 return null; 148 } 149 150 153 public void addAttachment(MimePart mp) 154 { 155 this.insertNodeInto(mp, this.rootPart, rootPart.getChildCount()); 156 if(rootPart.getChildCount()>0) 157 { 158 rootPart.setContentType(MimePart.ContentType.MULTIPART, "Mixed"); 159 } 160 } 161 162 165 public void removePart(MimePart mp) 166 { 167 this.removeNodeFromParent(mp); 168 } 169 170 171 public Vector<MimePart> getAllAttachments() 172 { 173 Vector<MimePart> allAtt = new Vector<MimePart>(); 174 getAllAttachmentsRecurse(allAtt, this.rootPart); 175 return allAtt; 176 } 177 178 private void getAllAttachmentsRecurse(Vector<MimePart> atts, MimePart node) 179 { 180 if(node.isLeaf()) 181 { 182 if(node.lookIfContentIsAnAttachment()) 184 { 185 atts.add(node); 186 return; 187 } 188 } 189 190 for(int i=0; i<node.getChildCount(); i++) 191 { 192 getAllAttachmentsRecurse(atts, node.getPartAt(i)); 194 } 195 } 196 197 200 public String createMessageStringContent() 201 { 202 return rootPart.createStringRepresentation(1, i1, i2); 203 } 204 205 206 public byte[] getContent_For_Sending() 207 { 208 try 209 { 210 return rootPart.getContent_For_Sending(1, i1, i2); 211 } 212 catch(Exception ex) 213 { 214 ex.printStackTrace(); 216 return createMessageStringContent().getBytes(); 218 } 219 220 } 221 } | Popular Tags |