Skip to content

Package: SimpleClient$1

SimpleClient$1

nameinstructionbranchcomplexitylinemethod
windowClosing(WindowEvent)
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
{...}
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * Copyright (c) 1997, 2021 Oracle and/or its affiliates. All rights reserved.
3: *
4: * This program and the accompanying materials are made available under the
5: * terms of the Eclipse Distribution License v. 1.0, which is available at
6: * http://www.eclipse.org/org/documents/edl-v10.php.
7: *
8: * SPDX-License-Identifier: BSD-3-Clause
9: */
10:
11: import java.util.*;
12: import java.io.*;
13: import jakarta.mail.*;
14: import jakarta.mail.internet.*;
15: import jakarta.activation.*;
16: import java.awt.*;
17: import java.awt.event.*;
18: import javax.swing.*;
19: import javax.swing.table.*;
20: import javax.swing.tree.*;
21: import javax.swing.event.*;
22:
23:
24: /**
25: * Demo app that shows a very simple Mail Client
26: *
27: * @author Christopher Cotton
28: * @author Bill Shannon
29: */
30:
31: public class SimpleClient {
32:
33: static Vector url = new Vector();
34: static FolderViewer fv;
35: static MessageViewer mv;
36:
37: public static void main(String argv[]) {
38:         boolean usage = false;
39:
40:         for (int optind = 0; optind < argv.length; optind++) {
41:          if (argv[optind].equals("-L")) {
42:                 url.addElement(argv[++optind]);
43:          } else if (argv[optind].startsWith("-")) {
44:                 usage = true;
45:                 break;
46:          } else {
47:                 usage = true;
48:                 break;
49:          }
50:         }
51:
52:         if (usage || url.size() == 0) {
53:          System.out.println("Usage: SimpleClient -L url");
54:          System.out.println(" where url is protocol://username:password@hostname/");
55:          System.exit(1);
56:         }
57:
58:         try {
59:          // Set up our Mailcap entries. This will allow the JAF
60:          // to locate our viewers.
61:          File capfile = new File("simple.mailcap");
62:          if (!capfile.isFile()) {
63:                 System.out.println(
64:                  "Cannot locate the \"simple.mailcap\" file.");
65:                 System.exit(1);
66:          }
67:         
68:          CommandMap.setDefaultCommandMap( new MailcapCommandMap(
69:                 new FileInputStream(capfile)));
70:                 
71:          JFrame frame = new JFrame("Simple Jakarta Mail Client");
72:          frame.addWindowListener(new WindowAdapter() {
73:                 public void windowClosing(WindowEvent e) {System.exit(0);}});
74:          //frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
75:                 
76:          // Get a Store object
77:          SimpleAuthenticator auth = new SimpleAuthenticator(frame);
78:          Session session =
79:                 Session.getDefaultInstance(System.getProperties(), auth);
80:          //session.setDebug(true);
81:
82:          DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
83:
84:          // create a node for each store we have
85:          for (Enumeration e = url.elements() ; e.hasMoreElements() ;) {
86:                 String urlstring = (String) e.nextElement();
87:                 URLName urln = new URLName(urlstring);
88:                 Store store = session.getStore(urln);
89:                 
90:                 StoreTreeNode storenode = new StoreTreeNode(store);
91:                 root.add(storenode);
92:          }        
93:
94:          DefaultTreeModel treeModel = new DefaultTreeModel(root);
95:          JTree tree = new JTree(treeModel);
96:          tree.addTreeSelectionListener(new TreePress());
97:
98:          /* Put the Tree in a scroller. */
99:          JScrollPane sp = new JScrollPane();
100:          sp.setPreferredSize(new Dimension(250, 300));
101:          sp.getViewport().add(tree);
102:
103:          /* Create a double buffered JPanel */
104:          JPanel sv = new JPanel(new BorderLayout());
105:          sv.add("Center", sp);
106:
107:          fv = new FolderViewer(null);
108:
109:          JSplitPane jsp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
110:                                 sv, fv);
111:          jsp.setOneTouchExpandable(true);
112:          mv = new MessageViewer();
113:          JSplitPane jsp2 = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
114:                                 jsp, mv);
115:          jsp2.setOneTouchExpandable(true);
116:
117:          frame.getContentPane().add(jsp2);
118:          frame.pack();
119:          frame.show();
120:
121:         } catch (Exception ex) {
122:          System.out.println("SimpletClient caught exception");
123:          ex.printStackTrace();
124:          System.exit(1);
125:         }
126: }
127:
128: }
129:
130: class TreePress implements TreeSelectionListener {
131:
132: public void valueChanged(TreeSelectionEvent e) {
133:         TreePath path = e.getNewLeadSelectionPath();
134:         if (path != null) {
135:          Object o = path.getLastPathComponent();
136:          if (o instanceof FolderTreeNode) {
137:                 FolderTreeNode node = (FolderTreeNode)o;
138:                 Folder folder = node.getFolder();
139:
140:                 try {
141:                  if ((folder.getType() & Folder.HOLDS_MESSAGES) != 0) {
142:                         SimpleClient.fv.setFolder(folder);
143:                  }
144:                 } catch (MessagingException me) { }
145:          }
146:         }
147: }
148: }