Subversion Repositories javautils

Rev

Blame | Last modification | View Log | RSS feed

  1. /*
  2.  * Copyright 1997-2007 Sun Microsystems, Inc. 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 Sun Microsystems 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.  * Copyright (c) 1997-2007 by Sun Microsystems, Inc.
  33.  * All Rights Reserved.
  34.  */
  35.  
  36. import java.util.*;
  37. import java.io.*;
  38. import javax.mail.*;
  39. import javax.mail.internet.*;
  40. import javax.activation.*;
  41. import java.awt.*;
  42. import java.awt.event.*;
  43. import javax.swing.*;
  44. import javax.swing.table.*;
  45. import javax.swing.tree.*;
  46. import javax.swing.event.*;
  47.  
  48.  
  49. /**
  50.  * Demo app that shows a very simple Mail Client
  51.  *
  52.  * @author Christopher Cotton
  53.  * @author Bill Shannon
  54.  */
  55.  
  56. public class SimpleClient {
  57.  
  58.     static Vector url = new Vector();
  59.     static FolderViewer fv;
  60.     static MessageViewer mv;
  61.  
  62.     public static void main(String argv[]) {
  63.         boolean usage = false;
  64.  
  65.         for (int optind = 0; optind < argv.length; optind++) {
  66.             if (argv[optind].equals("-L")) {
  67.                 url.addElement(argv[++optind]);
  68.             } else if (argv[optind].startsWith("-")) {
  69.                 usage = true;
  70.                 break;
  71.             } else {
  72.                 usage = true;
  73.                 break;
  74.             }
  75.         }
  76.  
  77.         if (usage || url.size() == 0) {
  78.             System.out.println("Usage: SimpleClient -L url");
  79.             System.out.println("   where url is protocol://username:password@hostname/");
  80.             System.exit(1);
  81.         }
  82.  
  83.         try {
  84.             // Set up our Mailcap entries.  This will allow the JAF
  85.             // to locate our viewers.
  86.             File capfile = new File("simple.mailcap");
  87.             if (!capfile.isFile()) {
  88.                 System.out.println(
  89.                     "Cannot locate the \"simple.mailcap\" file.");
  90.                 System.exit(1);
  91.             }
  92.            
  93.             CommandMap.setDefaultCommandMap( new MailcapCommandMap(
  94.                 new FileInputStream(capfile)));
  95.                
  96.             JFrame frame = new JFrame("Simple JavaMail Client");
  97.             frame.addWindowListener(new WindowAdapter() {
  98.                 public void windowClosing(WindowEvent e) {System.exit(0);}});
  99.             //frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  100.                
  101.             // Get a Store object
  102.             SimpleAuthenticator auth = new SimpleAuthenticator(frame);
  103.             Session session =
  104.                 Session.getDefaultInstance(System.getProperties(), auth);
  105.             //session.setDebug(true);
  106.  
  107.             DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
  108.  
  109.             // create a node for each store we have
  110.             for (Enumeration e = url.elements() ; e.hasMoreElements() ;) {
  111.                 String urlstring = (String) e.nextElement();
  112.                 URLName urln = new URLName(urlstring);
  113.                 Store store = session.getStore(urln);
  114.                
  115.                 StoreTreeNode storenode = new StoreTreeNode(store);
  116.                 root.add(storenode);
  117.             }      
  118.  
  119.             DefaultTreeModel treeModel = new DefaultTreeModel(root);
  120.             JTree tree = new JTree(treeModel);
  121.             tree.addTreeSelectionListener(new TreePress());
  122.  
  123.             /* Put the Tree in a scroller. */
  124.             JScrollPane        sp = new JScrollPane();
  125.             sp.setPreferredSize(new Dimension(250, 300));
  126.             sp.getViewport().add(tree);
  127.  
  128.             /* Create a double buffered JPanel */
  129.             JPanel sv = new JPanel(new BorderLayout());
  130.             sv.add("Center", sp);
  131.  
  132.             fv = new FolderViewer(null);
  133.  
  134.             JSplitPane jsp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
  135.                                 sv, fv);
  136.             jsp.setOneTouchExpandable(true);
  137.             mv = new MessageViewer();
  138.             JSplitPane jsp2 = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
  139.                                 jsp, mv);
  140.             jsp2.setOneTouchExpandable(true);
  141.  
  142.             frame.getContentPane().add(jsp2);
  143.             frame.pack();
  144.             frame.show();
  145.  
  146.         } catch (Exception ex) {
  147.             System.out.println("SimpletClient caught exception");
  148.             ex.printStackTrace();
  149.             System.exit(1);
  150.         }
  151.     }
  152.  
  153. }
  154.  
  155. class TreePress implements TreeSelectionListener {
  156.    
  157.     public void valueChanged(TreeSelectionEvent e) {
  158.         TreePath path = e.getNewLeadSelectionPath();
  159.         if (path != null) {
  160.             Object o = path.getLastPathComponent();
  161.             if (o instanceof FolderTreeNode) {
  162.                 FolderTreeNode node = (FolderTreeNode)o;
  163.                 Folder folder = node.getFolder();
  164.  
  165.                 try {
  166.                     if ((folder.getType() & Folder.HOLDS_MESSAGES) != 0) {
  167.                         SimpleClient.fv.setFolder(folder);
  168.                     }
  169.                 } catch (MessagingException me) { }
  170.             }
  171.         }
  172.     }
  173. }
  174.