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.gui;
21  
22  import java.awt.Color;
23  import java.awt.Graphics;
24  import java.awt.event.ActionEvent;
25  import java.awt.event.MouseAdapter;
26  import java.awt.event.MouseEvent;
27  import java.beans.PropertyChangeEvent;
28  import java.beans.PropertyChangeListener;
29  
30  import javax.swing.AbstractAction;
31  import javax.swing.Action;
32  import javax.swing.Icon;
33  import javax.swing.JPopupMenu;
34  
35  import org.xnap.XNap;
36  import org.xnap.gui.component.XNapMenuItem;
37  import org.xnap.gui.util.IconHelper;
38  import org.xnap.util.Preferences;
39  
40  /***
41   *
42   */
43  public class PresenceStatusPanel extends StatusPanel 
44  	implements PropertyChangeListener {
45  
46  	// --- Constant(s) ---
47  
48  	private static final Icon ICON_ONLINE 
49  		= IconHelper.getIcon("noatunplay.png", 16);
50  
51  	private static final Icon ICON_AWAY
52  		= IconHelper.getIcon("player_pause.png", 16);
53  
54  	// --- Data Field(s) ---
55  
56  	private JPopupMenu jpPresence;
57  
58      // --- Constructor(s) ---
59  
60  	public PresenceStatusPanel() 
61      {
62  		jpPresence = new JPopupMenu();
63  		jpPresence.add(new XNapMenuItem(new OnlineAction()));
64  		jpPresence.add(new XNapMenuItem(new AwayAction()));
65  
66  		propertyChange(null);
67  
68  		addMouseListener(new PopupListener());
69  		Preferences.getInstance().addPropertyChangeListener
70  			("sendChatAwayMessage", this);
71      }
72  
73      // --- Method(s) ---
74  
75  	protected void paintComponent(Graphics g)
76  	{
77  		super.paintComponent(g);
78  
79  		int w = 16 + getHorizontalTextPosition();
80  		int h = getHeight();
81  		
82  		g.setColor(Color.black);
83  		
84  		// draw bottom up: yoff = -3, xoff = -8
85  		for (int i = 0; i < 4; i++) {
86  			g.drawLine(w - 8 - i, h - 3 - i, w - 8 + i, h - 3 - i);
87  		}
88  		g.drawLine(w - 8 - 4, h - 3 - 4, w - 8 + 4, h - 3 - 4);
89  	}
90  
91      public void propertyChange(PropertyChangeEvent e)
92  	{
93  		if (Preferences.getInstance().getSendChatAwayMessage()) {
94  			setIcon(ICON_AWAY);
95  			setText(XNap.tr("Away"));
96  		}
97  		else {
98  			setIcon(ICON_ONLINE);
99  			setText(XNap.tr("Online"));
100 		}
101 	}
102 
103 	//--- Inner Class(es) ---
104 
105 	public class PopupListener extends MouseAdapter {
106 
107 		public void mousePressed(MouseEvent e) 
108 		{	
109 			if (!jpPresence.isVisible()) {
110 				jpPresence.show(PresenceStatusPanel.this, 0,
111 								-jpPresence.getHeight());
112 			}
113 			else {
114 				jpPresence.setVisible(false);
115 			}
116 		}
117 	}
118 
119 	public class OnlineAction extends AbstractAction {
120 
121 		//--- Constructor(s) ---
122 		
123 		public OnlineAction()
124 		{
125 			putValue(Action.NAME, XNap.tr("Online"));
126 			putValue(IconHelper.XNAP_ICON, "noatunplay.png");
127 		}
128 		
129 		//--- Method(s) ---
130         
131 		public void actionPerformed(ActionEvent event) 
132 		{
133 			Preferences.getInstance().setSendChatAwayMessage(false);
134 		}
135 
136 	}
137 
138 	public class AwayAction extends AbstractAction {
139 
140 		//--- Constructor(s) ---
141 		
142 		public AwayAction()
143 		{
144 			putValue(Action.NAME, XNap.tr("Away"));
145 			putValue(IconHelper.XNAP_ICON, "player_pause.png");
146 		}
147 		
148 		//--- Method(s) ---
149         
150 		public void actionPerformed(ActionEvent event) 
151 		{
152 			Preferences.getInstance().setSendChatAwayMessage(true);
153 		}
154 
155 	}
156 
157 }