-
Notifications
You must be signed in to change notification settings - Fork 0
/
retrieve_roster.java
197 lines (176 loc) · 6.08 KB
/
retrieve_roster.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.Collection;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import org.jivesoftware.smack.chat2.Chat;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.roster.Roster;
import org.jivesoftware.smack.roster.RosterEntry;
import org.jivesoftware.smack.roster.RosterListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Presence;
public class retrieve_roster
{
JFrame frame;
ArrayList<String> buttons;
XMPPConnection connection;
JButton connect;
Collection<RosterEntry> list; //Iterator<RemoteRosterEntry>
public retrieve_roster(String username, String password)throws XMPPException
{
System.out.println("retrieve roster begining...");
frame = ley.frame;
connection = ley.connection;
frame.removeAll();
buttons = new ArrayList<String>();
System.out.println("Send information");
ChatClient.login(username, password);
ChatClient.setStatus("Available");
Roster roster = ( ley.connection).getRoster();
list = roster.getEntries();
System.out.println("List is: " + list);
final JPanel panel = new JPanel();
panel.setBackground(Color.WHITE);
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
//final JPanel subpane1 = new JPanel();
//subpanel.setLayout(new BoxLayout(subpanel, BoxLayout.Y_AXIS));
roster.addRosterListener(new RosterListener(){
@Override
public void entriesAdded(Collection<String> arg0)
{
System.out.println("Entries added");
}
@Override
public void entriesDeleted(Collection<String> arg0)
{
System.out.println("Entries removed");
}
@Override
public void entriesUpdated(Collection<String> arg0){}
@Override
public void presenceChanged(Presence presence)
{
System.out.println("Presence changed: " + presence.getFrom() + " " + presence.getStatus());
String newcomer = presence.getFrom().split("/")[0];
//the entry is available from the begining only its status has changed...
if((presence.isAvailable()) && buttons.contains(newcomer)){
presence.getMode(); //should be setMode i think
System.out.println("Was available and has changed" + presence.getMode());
}
//entry is not available, we need to add it to list and make it display...
else if(presence.isAvailable() && buttons.contains(newcomer))
{
System.out.println("Someone new has come");
JPanel subpanel = new JPanel();
subpanel.setLayout(new BorderLayout());
subpanel.add((connect = new JButton(newcomer)));
buttons.add(newcomer);
connect.setActionCommand(newcomer);
connect.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent event){
new_chat chat_box = new new_chat(event.getActionCommand());
chat_box.start();
}
});
panel.add(subpanel);
panel_to_frame(panel);
new add_to_list_refresh(newcomer);
}
else if(!presence.isAvailable() && buttons.contains(newcomer))
{
System.out.println("Has gone offline");
int index = buttons.indexOf(newcomer);
System.out.println("Index is:" + index);
panel.remove(panel.getComponent(index));
panel_to_frmae(panel);
}
}
});
//This will make an ArrayList<String> of all the online users
for(RosterEntry r : list)
{
System.out.println("In for loop of retrieve roster");
if(roster.getPresence(r.getUser()).isAvailable()){
System.out.println("Name of friend is: " + r);
buttons.add(r.getUser());
}
}
show_list(panel);
panel_to_frame(panel);
new create_listener_threads();
// new add_to list();
// new receive_file();
}
void show_list(JPanel panel)
{
System.out.println("In retrieve roster, show_list");
for(String s : buttons)
{
JPanel subpanel = new JPanel();
subpanel.setLayout(new BorderLayout());
subpanel.add((connect = new JButton(s)));
connect.setActionCommand(s);
connect.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent event)
{
new_chat chat_box = new new_chat(event.getActionCommand());
chat_box.start();
}
});
panel.add(subpanel);
}
}
void panel_to_frame(JPanel panel)
{
System.out.println("In retrieve roster, Panel to Frame");
frame.removeAll();
frame.setVisible(false);
frame = new JFrame("BUDDY LIST");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
/* frame add.WindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
connection.disconnect();
System.out.println("in window closing...");
ley.make_exit();
System.exit(ABORT);
}
});*/
JScrollPane scrollar = new JScrollPane(panel);
//scrollar.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
//scrollar.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
frame.getContentPane().add(BorderLayout.NORTH, scrollar);
frame.setBackground(Color.WHITE);
frame.setSize(350, 500);
frame.setVisible(true);
}
/*public class add_to_list_refresh implements MessageListener{
public add_to_list_refresh(String newcomer)
{
System.out.println("In add to list refresh");
connection.getChatManager().createChat(newcomer, this);
}
@Override
public void processMessage(Chat arg, Message msg)
{
if(msg.getType() == Message.Type.chat)
{
new receive_new_chat(msg.getBody(), arg);
arg.removeMessageListener(this);
}
}
}*/
}