1 38 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 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 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 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 111 SimpleAuthenticator auth = new SimpleAuthenticator(frame); 113 Session session = 114 Session.getDefaultInstance(System.getProperties(), auth); 115 117 DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root"); 118 119 for (Enumeration e = url.elements() ; e.hasMoreElements() ;) { 121 String urlstring = (String ) 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 134 JScrollPane sp = new JScrollPane(); 135 sp.setPreferredSize(new Dimension(250, 300)); 136 sp.getViewport().add(tree); 137 138 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 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 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 |