KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SimpleClient


1 /*
2  * @(#)SimpleClient.java 1.24 01/05/23
3  *
4  * Copyright 1997-2000 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * - Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * - Redistribution in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the distribution.
16  *
17  * Neither the name of Sun Microsystems, Inc. or the names of contributors
18  * may be used to endorse or promote products derived from this software
19  * without specific prior written permission.
20  *
21  * This software is provided "AS IS," without a warranty of any kind. ALL
22  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
23  * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
24  * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND
25  * ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES OR LIABILITIES
26  * SUFFERED BY LICENSEE AS A RESULT OF OR RELATING TO USE, MODIFICATION
27  * OR DISTRIBUTION OF THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL
28  * SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
29  * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE
30  * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
31  * ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS
32  * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
33  *
34  * You acknowledge that Software is not designed, licensed or intended
35  * for use in the design, construction, operation or maintenance of any
36  * nuclear facility.
37  */

38 /*
39  * @(#)SimpleClient.java 1.24, 01/05/23
40  *
41  * Copyright (c) 1997-1998 by Sun Microsystems, Inc.
42  * All Rights Reserved.
43  */

44
45 import java.util.*;
46 import java.io.*;
47 import javax.mail.*;
48 import javax.mail.internet.*;
49 import javax.activation.*;
50 import java.awt.*;
51 import java.awt.event.*;
52 import javax.swing.*;
53 import javax.swing.table.*;
54 import javax.swing.tree.*;
55 import javax.swing.event.*;
56
57
58 /**
59  * Demo app that shows a very simple Mail Client
60  *
61  * @version 1.24, 01/05/23
62  * @author Christopher Cotton
63  * @author Bill Shannon
64  */

65
66 public class SimpleClient {
67
68     static Vector url = new Vector();
69     static FolderViewer fv;
70     static MessageViewer mv;
71
72     public static void main(String JavaDoc argv[]) {
73     boolean usage = false;
74
75     for (int optind = 0; optind < argv.length; optind++) {
76         if (argv[optind].equals("-L")) {
77         url.addElement(argv[++optind]);
78         } else if (argv[optind].startsWith("-")) {
79         usage = true;
80         break;
81         } else {
82         usage = true;
83         break;
84         }
85     }
86
87     if (usage || url.size() == 0) {
88         System.out.println("Usage: SimpleClient -L url");
89         System.out.println(" where url is protocol://username:password@hostname/");
90         System.exit(1);
91     }
92
93         try {
94         // Set up our Mailcap entries. This will allow the JAF
95
// to locate our viewers.
96
File capfile = new File("simple.mailcap");
97         if (!capfile.isFile()) {
98         System.out.println(
99             "Cannot locate the \"simple.mailcap\" file.");
100         System.exit(1);
101         }
102         
103         CommandMap.setDefaultCommandMap( new MailcapCommandMap(
104         new FileInputStream(capfile)));
105         
106         JFrame frame = new JFrame("Simple JavaMail Client");
107         frame.addWindowListener(new WindowAdapter() {
108         public void windowClosing(WindowEvent e) {System.exit(0);}});
109         //frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
110

111         // Get a Store object
112
SimpleAuthenticator auth = new SimpleAuthenticator(frame);
113         Session session =
114         Session.getDefaultInstance(System.getProperties(), auth);
115         //session.setDebug(true);
116

117         DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
118
119         // create a node for each store we have
120
for (Enumeration e = url.elements() ; e.hasMoreElements() ;) {
121         String JavaDoc urlstring = (String JavaDoc) e.nextElement();
122         URLName urln = new URLName(urlstring);
123         Store store = session.getStore(urln);
124         
125         StoreTreeNode storenode = new StoreTreeNode(store);
126         root.add(storenode);
127         }
128
129         DefaultTreeModel treeModel = new DefaultTreeModel(root);
130         JTree tree = new JTree(treeModel);
131         tree.addTreeSelectionListener(new TreePress());
132
133         /* Put the Tree in a scroller. */
134         JScrollPane sp = new JScrollPane();
135         sp.setPreferredSize(new Dimension(250, 300));
136         sp.getViewport().add(tree);
137
138         /* Create a double buffered JPanel */
139         JPanel sv = new JPanel(new BorderLayout());
140         sv.add("Center", sp);
141
142         fv = new FolderViewer(null);
143
144         JSplitPane jsp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
145                 sv, fv);
146         jsp.setOneTouchExpandable(true);
147         mv = new MessageViewer();
148         JSplitPane jsp2 = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
149                 jsp, mv);
150         jsp2.setOneTouchExpandable(true);
151
152         frame.getContentPane().add(jsp2);
153         frame.pack();
154         frame.show();
155
156     } catch (Exception JavaDoc ex) {
157         System.out.println("SimpletClient caught exception");
158         ex.printStackTrace();
159         System.exit(1);
160     }
161     }
162
163 }
164
165 class TreePress implements TreeSelectionListener {
166     
167     public void valueChanged(TreeSelectionEvent e) {
168     TreePath path = e.getNewLeadSelectionPath();
169     if (path != null) {
170         Object JavaDoc o = path.getLastPathComponent();
171         if (o instanceof FolderTreeNode) {
172         FolderTreeNode node = (FolderTreeNode)o;
173         Folder folder = node.getFolder();
174
175         try {
176             if ((folder.getType() & Folder.HOLDS_MESSAGES) != 0) {
177             SimpleClient.fv.setFolder(folder);
178             }
179         } catch (MessagingException me) { }
180         }
181     }
182     }
183 }
184
Popular Tags