1 /*
2  * Copyright (c) 1997-2010 Oracle and/or its affiliates. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  *   - Redistributions of source code must retain the above copyright
9  *     notice, this list of conditions and the following disclaimer.
10  *
11  *   - Redistributions in binary form must reproduce the above copyright
12  *     notice, this list of conditions and the following disclaimer in the
13  *     documentation and/or other materials provided with the distribution.
14  *
15  *   - Neither the name of Oracle nor the names of its
16  *     contributors may be used to endorse or promote products derived
17  *     from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
20  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 import java.util.*;
33 import java.io.*;
34 import javax.mail.*;
35 import javax.mail.internet.*;
36 import javax.activation.*;
37 import java.awt.*;
38 import java.awt.event.*;
39 import javax.swing.*;
40 import javax.swing.table.*;
41 import javax.swing.tree.*;
42 import javax.swing.event.*;
43 
44 
45 /**
46  * Demo app that shows a very simple Mail Client
47  *
48  * @author Christopher Cotton
49  * @author Bill Shannon
50  */
51 
52 public class SimpleClient {
53 
54     static Vector url = new Vector();
55     static FolderViewer fv;
56     static MessageViewer mv;
57 
main(String argv[])58     public static void main(String argv[]) {
59 	boolean usage = false;
60 
61 	for (int optind = 0; optind < argv.length; optind++) {
62 	    if (argv[optind].equals("-L")) {
63 		url.addElement(argv[++optind]);
64 	    } else if (argv[optind].startsWith("-")) {
65 		usage = true;
66 		break;
67 	    } else {
68 		usage = true;
69 		break;
70 	    }
71 	}
72 
73 	if (usage || url.size() == 0) {
74 	    System.out.println("Usage: SimpleClient -L url");
75 	    System.out.println("   where url is protocol://username:password@hostname/");
76 	    System.exit(1);
77 	}
78 
79 	try {
80 	    // Set up our Mailcap entries.  This will allow the JAF
81 	    // to locate our viewers.
82 	    File capfile = new File("simple.mailcap");
83 	    if (!capfile.isFile()) {
84 		System.out.println(
85 		    "Cannot locate the \"simple.mailcap\" file.");
86 		System.exit(1);
87 	    }
88 
89 	    CommandMap.setDefaultCommandMap( new MailcapCommandMap(
90 		new FileInputStream(capfile)));
91 
92 	    JFrame frame = new JFrame("Simple JavaMail Client");
93 	    frame.addWindowListener(new WindowAdapter() {
94 		public void windowClosing(WindowEvent e) {System.exit(0);}});
95 	    //frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
96 
97 	    // Get a Store object
98 	    SimpleAuthenticator auth = new SimpleAuthenticator(frame);
99 	    Session session =
100 		Session.getDefaultInstance(System.getProperties(), auth);
101 	    //session.setDebug(true);
102 
103 	    DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
104 
105 	    // create a node for each store we have
106 	    for (Enumeration e = url.elements() ; e.hasMoreElements() ;) {
107 		String urlstring = (String) e.nextElement();
108 		URLName urln = new URLName(urlstring);
109 		Store store = session.getStore(urln);
110 
111 		StoreTreeNode storenode = new StoreTreeNode(store);
112 		root.add(storenode);
113 	    }
114 
115 	    DefaultTreeModel treeModel = new DefaultTreeModel(root);
116 	    JTree tree = new JTree(treeModel);
117 	    tree.addTreeSelectionListener(new TreePress());
118 
119 	    /* Put the Tree in a scroller. */
120 	    JScrollPane        sp = new JScrollPane();
121 	    sp.setPreferredSize(new Dimension(250, 300));
122 	    sp.getViewport().add(tree);
123 
124 	    /* Create a double buffered JPanel */
125 	    JPanel sv = new JPanel(new BorderLayout());
126 	    sv.add("Center", sp);
127 
128 	    fv = new FolderViewer(null);
129 
130 	    JSplitPane jsp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
131 				sv, fv);
132 	    jsp.setOneTouchExpandable(true);
133 	    mv = new MessageViewer();
134 	    JSplitPane jsp2 = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
135 				jsp, mv);
136 	    jsp2.setOneTouchExpandable(true);
137 
138 	    frame.getContentPane().add(jsp2);
139 	    frame.pack();
140 	    frame.show();
141 
142 	} catch (Exception ex) {
143 	    System.out.println("SimpletClient caught exception");
144 	    ex.printStackTrace();
145 	    System.exit(1);
146 	}
147     }
148 
149 }
150 
151 class TreePress implements TreeSelectionListener {
152 
valueChanged(TreeSelectionEvent e)153     public void valueChanged(TreeSelectionEvent e) {
154 	TreePath path = e.getNewLeadSelectionPath();
155 	if (path != null) {
156 	    Object o = path.getLastPathComponent();
157 	    if (o instanceof FolderTreeNode) {
158 		FolderTreeNode node = (FolderTreeNode)o;
159 		Folder folder = node.getFolder();
160 
161 		try {
162 		    if ((folder.getType() & Folder.HOLDS_MESSAGES) != 0) {
163 			SimpleClient.fv.setFolder(folder);
164 		    }
165 		} catch (MessagingException me) { }
166 	    }
167 	}
168     }
169 }
170