KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SnowMailClient > model > multipart > MimeTreeModel


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 /** This should be used to deal with mime messages
12     to create from a received content, use the parse method of the root part.
13     To create a sendable message from tree content, ue createMessageStringContent()
14
15
16 */

17 public final class MimeTreeModel extends DefaultTreeModel
18 {
19   private MimePart rootPart;
20
21   // used to generate a random boundary delimiter
22
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   /** this delete all the tree
34   */

35   public void clearMimeTree()
36   {
37     rootPart = new MimePart();
38     this.setRoot(rootPart);
39   }
40
41   /** @return the parts containing the given text
42   */

43   public MimePart[] searchInTextParts(String JavaDoc 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 JavaDoc 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   /** return true if this is a mime message format
65      (i.e. if there are childs)
66   */

67   public boolean isMimeMessageFormat()
68   {
69      if(rootPart.getChildCount()>0) return true;
70      if(rootPart.lookIfContentIsAnAttachment()) return true; // [Jan2006]: occur when the content is made of a pure attachment!, for example outlook
71
return false;
72   }
73
74
75   /** null if none, return the message part or the first plain text part
76   */

77   public MimePart getFirstPlainTextPart()
78   {
79     return getFirstNodeOfType(this.rootPart, MimePart.ContentType.TEXT, "PLAIN");
80   }
81
82   /** null if none
83   */

84   public MimePart getFirstHTMLTextPart()
85   {
86     return getFirstNodeOfType(this.rootPart, MimePart.ContentType.TEXT, "HTML");
87   }
88
89   /** called when editing a plain text message with attachments,
90      this creates a text node and fill it with the text
91   */

92   public void setMessagePlainText(Address from, Vector<Address> tos, String JavaDoc subject, String JavaDoc text)
93   {
94     //System.out.println("setMessagePlainText("+text+")");
95
MimePart partText = this.getFirstPlainTextPart();
96     if(partText==null)
97     {
98        // create a text part and add it at the begining
99
partText = new MimePart();
100        //rootPart.addPart(partText);
101
rootPart.insert(partText, 0);
102     }
103
104     try
105     {
106       partText.setContentAsPlainTextMailMessage(text);
107       rootPart.setContentAsMIME_ROOT(from, tos, subject);
108     }
109     catch(Exception JavaDoc e)
110     {
111       // should not appear
112
e.printStackTrace();
113     }
114   }
115
116
117   private MimePart getFirstNodeOfType(MimePart node, MimePart.ContentType type, String JavaDoc 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       // depth first
126
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 JavaDoc id)
133   {
134      return getNodeWithID(this.getRootPart(), id);
135   }
136
137   private MimePart getNodeWithID(MimePart node, String JavaDoc id)
138   {
139     if(node.getID().equalsIgnoreCase(id)) return node;
140
141     for(int i=0; i<node.getChildCount(); i++)
142     {
143       // depth first
144
MimePart mp = getNodeWithID(node.getPartAt(i), id);
145       if(mp!=null) return mp;
146     }
147     return null;
148   }
149
150   /** tree bug ??
151      events are made, but one need to updateUI the tree !!
152   */

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   /** remove any part
163    use this in preference to the MutableTree, because the events are here generated
164   */

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        //System.out.println("Node "+node.getTextForTreeRepresentation()+": "+node.lookIfContentIsAnAttachment());
183
if(node.lookIfContentIsAnAttachment())
184        {
185           atts.add(node);
186           return;
187        }
188     }
189
190     for(int i=0; i<node.getChildCount(); i++)
191     {
192        // depth first
193
getAllAttachmentsRecurse(atts, node.getPartAt(i));
194     }
195   }
196
197   /** Creates the content of this mime tree that will be the body of the mail message
198       this will be stored as string in the MailMessage model.
199   */

200   public String JavaDoc 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 JavaDoc ex)
213     {
214       // should never occur
215
ex.printStackTrace();
216       // may help
217
return createMessageStringContent().getBytes();
218     }
219
220   }
221 } // MimeTreeModel
Popular Tags