Source code: com/flexstor/common/gui/addressbook/EmailTree.java
1 /*
2 * EmailTree.java
3 *
4 * Copyright $Date: 2003/08/11 02:22:32 $ FLEXSTOR.net Inc.
5 *
6 * This work is licensed for use and distribution under license terms found at
7 * http://www.flexstor.org/license.html
8 *
9 */
10
11 package com.flexstor.common.gui.addressbook;
12
13 import java.awt.event.ActionEvent;
14 import java.awt.event.ActionListener;
15 import java.util.ArrayList;
16 import java.util.Iterator;
17
18 import com.flexstor.common.awt.event.SettingsEvent;
19 import com.flexstor.common.awt.tree.FlexTreeNode;
20 import com.flexstor.common.awt.tree.TreeActionEvent;
21 import com.flexstor.common.data.ejb.SettingData;
22 import com.flexstor.common.data.ejb.address.AddressData;
23 import com.flexstor.common.data.ejb.address.EmailAddressData;
24 import com.flexstor.common.gateway.exceptions.TransactionFailedException;
25
26 /**
27 * An EmailTree for Address Book Component.
28 * 12/11/2000.
29 * @author Mahesh Gidwani
30 */
31
32 public class EmailTree extends AddressBookTree implements ActionListener
33 {
34 EmailAddressData mailData = null;
35
36 /**
37 * @param AddressBookModelI a model address book component to manage the email data.
38 */
39 public EmailTree(FlexTreeNode rootNode, AddressBookModelI model )
40 {
41 super();
42 this.model = model;
43 //setPreviousNode(rootNode);
44 addRootNode(rootNode);
45 addActionListener(this);
46 populateTree();
47 }
48
49 /**
50 * @return AddressData current selected address.
51 */
52 public SettingData getDataObject() //throws TransactionFailedException
53 {
54 if(getCurrentNode() == null || getCurrentNode().equals(getRootNode()))
55 return null;
56 //System.out.println("DBG:emailTree:getdataojbect "+getData(getCurrentNode().getLabel()));
57 mailData = (EmailAddressData)getData(getCurrentNode().getLabel());//( (AddressBookModel)model ).getCurrentAddress();//mailData;//
58 return mailData;
59 }
60
61 /*
62 This is the helper method for this class which takes the name of the address and
63 returns the address.
64 */
65 private AddressData getData(String name) //throws TransactionFailedException
66 {
67 EmailAddressData data = null;
68 try
69 {
70 //System.out.println("DBG1.0:EmaiPanel:getData:name "+name);
71 ArrayList list = model.getAddresses(AddressData.EMAIL_ADDRESS) ;
72 //System.out.println("DBG1.0:EmaiPanel:getData:list "+list);
73 Iterator it = list.iterator();
74 //System.out.println("DBG1.1:EmaiPanel:getData:name "+name);
75 while(it.hasNext())
76 {
77 data = (EmailAddressData)it.next();
78 //System.out.println("DBG1.2:EmaiPanel:getData:data "+data);
79 if(data.getName() != null && data.getName().equals(name))
80 return data;
81 }
82 //System.out.println("DBG1.3:EmaiPanel:getData:data "+data);
83 return data;
84 }
85 catch(TransactionFailedException e)
86 {
87 e.printStackTrace();
88 return null;
89 }
90 catch(Exception e)
91 {
92 System.out.println("DBG1.2:EmaiPanel:getData:data "+data);
93 e.printStackTrace();
94 return null;
95 }
96 }
97
98 /**
99 * populates the tree with the addresses.
100 */
101 public void populateTree()
102 {
103 try
104 {
105 /*if( getRootNode() == null )
106 return ;*/
107 getRootNode().removeChildren();
108 ArrayList list = model.getAddresses(AddressData.EMAIL_ADDRESS);
109 //System.out.println(list.size());
110 Iterator it = list.iterator();
111 FlexTreeNode node = null ;
112
113 while(it.hasNext())
114 {
115 String name = ( (AddressData)it.next() ).getName();
116 node = new FlexTreeNode(name);
117 addChild(node,FlexTreeNode.LAST,false);
118 }
119 }
120 catch(TransactionFailedException e)
121 {
122 e.printStackTrace();
123 }
124 }
125
126 /**
127 * Implementation for ActionListener class. It will get the TreeActionEvent and
128 * fire the AddressBookEvent so that intersted parties get notified.
129 * @param ActionEvent
130 */
131 public void actionPerformed(ActionEvent e)
132 {
133 TreeActionEvent e1 =(TreeActionEvent)e;
134 //System.out.println("DBG1:mailTree:actionPerformed");
135 if(previousNode != e1.getNode() && e1.getID()== TreeActionEvent.END_NODE_SELECT )
136 {
137 //System.out.println("DBG2:mailTree:actionPerformed");
138 setCurrentNode(e1.getNode());
139 SettingsEvent event = new SettingsEvent();
140 event.setSourceComponent(this);
141 event.setEventId(SettingsEvent.NEW_SELECTION);
142 event.setEventType(SettingsEvent.EMAIL_EVENT);
143 //mailData = (EmailAddressData)getData(getCurrentNode().getLabel());
144 //System.out.println("DBG2:mailTree:actionPerformed:data name "+mailData.getName());
145 fireSettingsEvent(event);
146 previousNode = getCurrentNode();
147 }
148 }
149
150 }//Class
151
152
153 /*
154
155 public AddressData getDataObject() //throws TransactionFailedException
156 {
157 if(getCurrentNode() == null)
158 return null;
159 try
160 {
161 ArrayList list = model.getAddresses(AddressData.EMAIL_ADDRESS);
162 for(int i = 0; i < list.size(); i++)
163 {
164 AddressData data = ( AddressData ) list.get(i);
165 if(data.getName().equals(getCurrentNode().getLabel()));
166 return data;
167 }
168 }
169 catch(TransactionFailedException e)
170 {
171 //throw e;
172 }
173 return null;
174 }
175 */