001: // The contents of this file are subject to the Mozilla Public License Version
002: // 1.1
003: //(the "License"); you may not use this file except in compliance with the
004: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
005: //
006: //Software distributed under the License is distributed on an "AS IS" basis,
007: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
008: //for the specific language governing rights and
009: //limitations under the License.
010: //
011: //The Original Code is "The Columba Project"
012: //
013: //The Initial Developers of the Original Code are Frederik Dietz and Timo
014: // Stich.
015: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
016: //
017: //All Rights Reserved.
018: package org.columba.chat.ui.presence;
019:
020: import java.awt.BorderLayout;
021: import java.awt.Component;
022: import java.awt.event.ItemEvent;
023: import java.awt.event.ItemListener;
024:
025: import javax.swing.BorderFactory;
026: import javax.swing.ImageIcon;
027: import javax.swing.JComboBox;
028: import javax.swing.JComponent;
029: import javax.swing.JLabel;
030: import javax.swing.JList;
031: import javax.swing.JPanel;
032: import javax.swing.ListCellRenderer;
033: import javax.swing.UIManager;
034:
035: import org.columba.api.statusbar.IStatusBarExtension;
036: import org.columba.chat.MainInterface;
037: import org.columba.chat.command.ChangePresenceCommand;
038: import org.columba.chat.command.ChatCommandReference;
039: import org.columba.chat.conn.api.ConnectionChangedEvent;
040: import org.columba.chat.conn.api.IConnectionChangedListener;
041: import org.columba.chat.conn.api.IConnection.STATUS;
042: import org.columba.chat.resourceloader.ResourceLoader;
043: import org.columba.chat.ui.frame.api.IChatFrameMediator;
044: import org.columba.chat.ui.presence.api.IPresenceController;
045: import org.columba.core.command.CommandProcessor;
046: import org.jivesoftware.smack.packet.Presence;
047:
048: /**
049: * @author fdietz
050: *
051: */
052: public class PresenceComboBox extends JPanel implements ItemListener,
053: IPresenceController, IStatusBarExtension,
054: IConnectionChangedListener {
055:
056: private JLabel label;
057:
058: private JComboBox comboBox;
059:
060: private ImageIcon available = ResourceLoader
061: .getMiscIcon("online.png");
062:
063: private ImageIcon extendedaway = ResourceLoader
064: .getMiscIcon("extended_away.png");
065:
066: private ImageIcon away = ResourceLoader.getMiscIcon("away.png");
067:
068: private ImageIcon busy = ResourceLoader
069: .getMiscIcon("unavailable.png");
070:
071: private ImageIcon offline = ResourceLoader
072: .getMiscIcon("offline.png");
073:
074: private IChatFrameMediator mediator;
075:
076: public PresenceComboBox(IChatFrameMediator mediator) {
077: super ();
078:
079: this .mediator = mediator;
080:
081: setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
082:
083: setBackground(UIManager.getColor("Tree.background"));
084:
085: comboBox = new JComboBox();
086: // comboBox.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
087: comboBox.addItem("Available");
088: comboBox.addItem("Busy");
089: comboBox.addItem("Away");
090: comboBox.addItem("Extended Aways");
091: // checkBox.addItem("Custom Message...", null);
092: // checkBox.addItem("Leave...");
093:
094: comboBox.setRenderer(new ItemRenderer());
095:
096: label = new JLabel();
097: label.setBorder(BorderFactory.createEmptyBorder(0, 2, 0, 2));
098: label.setIcon(offline);
099:
100: setLayout(new BorderLayout());
101:
102: add(label, BorderLayout.WEST);
103:
104: add(comboBox, BorderLayout.CENTER);
105:
106: comboBox.addItemListener(this );
107:
108: setEnabled(false);
109: comboBox.setEnabled(false);
110: label.setEnabled(false);
111:
112: MainInterface.connection.addConnectionChangedListener(this );
113: }
114:
115: // private void addStatus(String tooltip, ImageIcon icon) {
116: // JLabel label = new JLabel(icon);
117: // label.setToolTipText(tooltip);
118: //
119: // comboBox.addItem(tooltip);
120: // }
121:
122: public void addItemListener(ItemListener l) {
123: comboBox.addItemListener(l);
124: }
125:
126: /**
127: * @see java.awt.event.ItemListener#itemStateChanged(java.awt.event.ItemEvent)
128: */
129: public void itemStateChanged(ItemEvent e) {
130: Object source = e.getItemSelectable();
131:
132: int index = ((JComboBox) source).getSelectedIndex();
133:
134: Presence p = null;
135: switch (index) {
136: case 0: {
137: label.setIcon(available);
138: p = new Presence(Presence.Type.AVAILABLE);
139: p.setStatus("Available");
140: CommandProcessor.getInstance().addOp(
141: new ChangePresenceCommand(mediator,
142: new ChatCommandReference(p)));
143: break;
144: }
145: case 1: {
146: label.setIcon(busy);
147: p = new Presence(Presence.Type.UNAVAILABLE);
148: p.setStatus("Busy");
149: CommandProcessor.getInstance().addOp(
150: new ChangePresenceCommand(mediator,
151: new ChatCommandReference(p)));
152:
153: break;
154: }
155: case 2: {
156: label.setIcon(away);
157: p = new Presence(Presence.Type.UNAVAILABLE);
158: p.setStatus("Away");
159: CommandProcessor.getInstance().addOp(
160: new ChangePresenceCommand(mediator,
161: new ChatCommandReference(p)));
162:
163: break;
164: }
165: case 3: {
166: label.setIcon(extendedaway);
167: p = new Presence(Presence.Type.UNAVAILABLE);
168: p.setStatus("Extended Away");
169: CommandProcessor.getInstance().addOp(
170: new ChangePresenceCommand(mediator,
171: new ChatCommandReference(p)));
172:
173: break;
174: }
175: }
176:
177: }
178:
179: public JComponent getView() {
180: return this ;
181: }
182:
183: public class ItemRenderer extends JLabel implements
184: ListCellRenderer {
185:
186: public ItemRenderer() {
187: /*
188: * setIconTextGap(5); setVerticalAlignment(JLabel.CENTER);
189: */
190: }
191:
192: /** {@inheritDoc} */
193: public Component getListCellRendererComponent(JList list,
194: Object value, int index, boolean isSelected,
195: boolean cellHasFocus) {
196:
197: setText((String) value);
198:
199: return this ;
200: }
201: }
202:
203: /**
204: * @see org.columba.chat.conn.api.IConnectionChangedListener#connectionChanged(org.columba.chat.conn.api.ConnectionChangedEvent)
205: */
206: public void connectionChanged(ConnectionChangedEvent object) {
207: // IAccount account = object.getAccount();
208: STATUS status = object.getStatus();
209:
210: if (status == STATUS.ONLINE) {
211: setEnabled(true);
212: comboBox.setEnabled(true);
213: label.setEnabled(true);
214: } else if (status == STATUS.OFFLINE) {
215: setEnabled(false);
216: comboBox.setEnabled(false);
217: label.setEnabled(false);
218:
219: }
220: }
221: }
|