View Javadoc

1   /*
2    *  XNap - A P2P framework and client.
3    *
4    *  See the file AUTHORS for copyright information.
5    *
6    *  This program is free software; you can redistribute it and/or modify
7    *  it under the terms of the GNU General Public License as published by
8    *  the Free Software Foundation.
9    *
10   *  This program is distributed in the hope that it will be useful,
11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   *  GNU General Public License for more details.
14   *
15   *  You should have received a copy of the GNU General Public License
16   *  along with this program; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  
20  package org.xnap.plugin.joscar;
21  
22  import java.awt.event.ActionEvent;
23  
24  import javax.swing.AbstractAction;
25  import javax.swing.Action;
26  import javax.swing.Icon;
27  import javax.swing.JOptionPane;
28  
29  import org.apache.log4j.Logger;
30  import org.xnap.XNap;
31  import org.xnap.event.StateEvent;
32  import org.xnap.event.StateListener;
33  import org.xnap.event.StateSupport;
34  import org.xnap.gui.XNapFrame;
35  import org.xnap.gui.util.FocusManager;
36  import org.xnap.peer.AbstractPeer;
37  import org.xnap.peer.HotlistItem;
38  import org.xnap.peer.action.AbstractChatAction;
39  import org.xnap.peer.action.AbstractRemoveFromHotlistAction;
40  import org.xnap.util.State;
41  
42  public class JOscarPeer extends AbstractPeer
43  	implements HotlistItem, StateListener
44  {
45  	//--- Constant(s) ---
46  
47  	//--- Data field(s) ---
48  	
49  	private StateSupport listeners = new StateSupport(this);
50  
51  	private String status;
52  
53      private static Logger logger = Logger.getLogger(JOscarPeer.class);
54  	/***
55  	 * Keeps track if actions are enabled or not.
56  	 */
57  	private boolean enabled = false;
58  	
59  	//--- Constructor(s) ---
60  
61      public JOscarPeer(String uin)
62  	{
63  		super(uin);
64  		setStatus(XNap.tr("Offline"));
65  		stateChanged(null);
66  	}
67  
68  	//--- Method(s) ---
69  
70  	public void stateChanged(StateEvent e)
71  	{
72  		State s = JOscarPlugin.getInstance().getConnection().getState();
73  		if (s == State.DISCONNECTED) {
74  			setStatus(XNap.tr("Offline"));
75  			enabled = false;
76  		}
77  		else if (s == State.CONNECTED) {
78  			enabled = true;
79  		}
80  	}
81  
82  	public void addStateListener(StateListener listener)
83  	{
84  		listeners.addStateListener(listener);
85  	}
86  
87  	public Action[] getActions()
88  	{
89  		return new Action[] { 
90  			new ChatAction(), new SendSMSAction(), new RemoveAction(),
91  		};
92  	}
93  
94  	public String getCategory()
95  	{
96  		return null;
97  	}
98  
99  	/* (non-Javadoc)
100 	 * @see xnap.peer.HotlistItem#getComment()
101 	 */
102 	public String getComment() {
103 		// TODO Auto-generated method stub
104 		return null;
105 	}
106 
107 	public Icon getIcon()
108 	{
109 		return JOscarPlugin.ICON_16;
110 	}
111 
112 	/***
113 	 * @see xnap.peer.HotlistItem#getStatus()
114 	 */
115 	public String getStatus()
116 	{
117 		return status;
118 	}
119 
120 	public void setStatus(String newStatus)
121 	{
122 		status = newStatus;
123 		listeners.fireStateChanged();
124 	}
125 
126 	public void removeStateListener(StateListener listener)
127 	{
128 		listeners.removeStateListener(listener);
129 	}
130 
131 	public String getHost()
132 	{
133 		// TODO Auto-generated method stub
134 		return null;
135 	}
136 
137 	/* (non-Javadoc)
138 	 * @see xnap.peer.Peer#getLocalDownloadCount()
139 	 */
140 	public int getLocalDownloadCount() {
141 		// TODO Auto-generated method stub
142 		return 0;
143 	}
144 
145 	/* (non-Javadoc)
146 	 * @see xnap.peer.Peer#getLocalUploadCount()
147 	 */
148 	public int getLocalUploadCount() {
149 		// TODO Auto-generated method stub
150 		return 0;
151 	}
152 
153     //--- Inner Class(es) ---
154 
155 	private class ChatAction extends AbstractChatAction
156 	{
157 		public ChatAction()
158 		{
159 			setEnabled(JOscarPeer.this.enabled);
160 		}
161 
162 		public void actionPerformed(ActionEvent event) 
163 		{
164 			JOscarPlugin.getInstance().addChannel
165 				(new JOscarChannel(JOscarPeer.this));
166 			FocusManager.setFocusTo("chat", event);
167 		}
168 	}
169 
170 	private class RemoveAction extends AbstractRemoveFromHotlistAction
171 	{
172 		public void actionPerformed(ActionEvent event)
173 		{
174 			JOscarPlugin.getInstance().removeBuddy(JOscarPeer.this);
175 		}
176 	}
177 
178 	private class SendSMSAction extends AbstractAction
179 	{
180 		public SendSMSAction()
181 		{
182 			putValue(Action.NAME, XNap.tr("Send SMS"));
183 			setEnabled(JOscarPeer.this.enabled);
184 		}
185 
186 		public void actionPerformed(ActionEvent event)
187 		{
188 			String message = JOptionPane.showInputDialog
189 				(XNapFrame.getInstance(), XNap.tr("Enter SMS:"),
190 				 XNap.tr("Send SMS"), JOptionPane.QUESTION_MESSAGE);
191 
192 			if (message != null) {
193 				JOscarPlugin.getInstance().sendSMS(JOscarPeer.this.getName(),
194 												   message);
195 			}
196 		}
197 	}
198 }