statusbar.java
来自「开源项目openfire的完整源程序」· Java 代码 · 共 552 行 · 第 1/2 页
JAVA
552 行
/**
* $Revision: $
* $Date: $
*
* Copyright (C) 2006 Jive Software. All rights reserved.
*
* This software is published under the terms of the GNU Lesser Public License (LGPL),
* a copy of which is included in this distribution.
*/
package org.jivesoftware.spark.ui.status;
import org.jivesoftware.resource.Default;
import org.jivesoftware.resource.Res;
import org.jivesoftware.resource.SparkRes;
import org.jivesoftware.smack.packet.PacketExtension;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smackx.packet.VCard;
import org.jivesoftware.spark.PresenceManager;
import org.jivesoftware.spark.SparkManager;
import org.jivesoftware.spark.ui.PresenceListener;
import org.jivesoftware.spark.util.GraphicUtils;
import org.jivesoftware.spark.util.ModelUtil;
import org.jivesoftware.spark.util.SwingTimerTask;
import org.jivesoftware.spark.util.SwingWorker;
import org.jivesoftware.spark.util.TaskEngine;
import org.jivesoftware.spark.util.log.Log;
import org.jivesoftware.sparkimpl.profile.VCardEditor;
import org.jivesoftware.sparkimpl.profile.VCardListener;
import org.jivesoftware.sparkimpl.profile.VCardManager;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.AffineTransform;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.TimerTask;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.SwingUtilities;
import javax.swing.border.Border;
//TODO: I need to remove the presence logic from this class.
public class StatusBar extends JPanel implements VCardListener {
private List<StatusItem> statusList = new ArrayList<StatusItem>();
private JLabel imageLabel = new JLabel();
private JLabel descriptiveLabel = new JLabel();
private JLabel nicknameLabel = new JLabel();
private StatusPanel statusPanel = new StatusPanel();
private Image backgroundImage;
private Presence currentPresence;
public StatusBar() {
setLayout(new GridBagLayout());
backgroundImage = Default.getImageIcon(Default.TOP_BOTTOM_BACKGROUND_IMAGE).getImage();
ImageIcon brandedImage = Default.getImageIcon(Default.BRANDED_IMAGE);
if (brandedImage != null && brandedImage.getIconWidth() > 1) {
final JLabel brandedLabel = new JLabel(brandedImage);
add(brandedLabel, new GridBagConstraints(3, 0, 1, 3, 1.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(2, 2, 2, 2), 0, 0));
}
add(imageLabel, new GridBagConstraints(0, 0, 1, 4, 0.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(2, 2, 2, 2), 0, 0));
add(nicknameLabel, new GridBagConstraints(1, 0, 2, 2, 1.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(5, 5, 0, 0), 0, 0));
add(descriptiveLabel, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(0, 5, 0, 0), 0, 0));
add(statusPanel, new GridBagConstraints(1, 3, 1, 1, 0.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(0, 2, 0, 0), 0, 0));
nicknameLabel.setToolTipText(SparkManager.getConnection().getUser());
nicknameLabel.setFont(new Font("Dialog", Font.BOLD, 12));
buildStatusItemList();
setStatus(Res.getString("available"));
currentPresence = new Presence(Presence.Type.available, Res.getString("available"), 1, Presence.Mode.available);
setBorder(BorderFactory.createLineBorder(new Color(197, 213, 230), 1));
SparkManager.getSessionManager().addPresenceListener(new PresenceListener() {
public void presenceChanged(Presence presence) {
changeAvailability(presence);
}
});
// Show profile on double click of image label
imageLabel.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent mouseEvent) {
if (mouseEvent.getClickCount() == 1) {
VCardManager vcardManager = SparkManager.getVCardManager();
VCardEditor editor = new VCardEditor();
editor.editProfile(vcardManager.getVCard(), SparkManager.getWorkspace());
}
}
public void mouseEntered(MouseEvent e) {
imageLabel.setCursor(GraphicUtils.HAND_CURSOR);
}
public void mouseExited(MouseEvent e) {
imageLabel.setCursor(GraphicUtils.DEFAULT_CURSOR);
}
});
final TimerTask task = new SwingTimerTask() {
public void doRun() {
SparkManager.getVCardManager().addVCardListener(SparkManager.getWorkspace().getStatusBar());
}
};
TaskEngine.getInstance().schedule(task, 3000);
}
public void setAvatar(Icon icon) {
imageLabel.setIcon(icon);
imageLabel.setBorder(null);
invalidate();
validateTree();
}
public void setNickname(String nickname) {
nicknameLabel.setText(nickname);
}
/**
* Sets the current status text in the Status Manager.
*
* @param status the status to set.
*/
public void setStatus(String status) {
statusPanel.setStatus(status);
}
public void showPopup(MouseEvent e) {
final JPopupMenu popup = new JPopupMenu();
List custom = CustomMessages.load();
if (custom == null) {
custom = new ArrayList();
}
// Build menu from StatusList
for (final StatusItem statusItem : statusList) {
final Action statusAction = new AbstractAction() {
public void actionPerformed(ActionEvent actionEvent) {
final String text = statusItem.getText();
final StatusItem si = getStatusItem(text);
if (si == null) {
// Custom status
Log.error("Unable to find status item for status - " + text);
return;
}
SwingWorker worker = new SwingWorker() {
public Object construct() {
SparkManager.getSessionManager().changePresence(si.getPresence());
return "ok";
}
public void finished() {
setStatus(text);
}
};
worker.start();
}
};
statusAction.putValue(Action.NAME, statusItem.getText());
statusAction.putValue(Action.SMALL_ICON, statusItem.getIcon());
// Has Children
boolean hasChildren = false;
Iterator customItemIterator = custom.iterator();
while (customItemIterator.hasNext()) {
final CustomStatusItem cItem = (CustomStatusItem)customItemIterator.next();
String type = cItem.getType();
if (type.equals(statusItem.getText())) {
hasChildren = true;
}
}
if (!hasChildren) {
// Add as Menu Item
popup.add(statusAction);
}
else {
final JMenu mainStatusItem = new JMenu(statusAction);
popup.add(mainStatusItem);
// Add Custom Messages
customItemIterator = custom.iterator();
while (customItemIterator.hasNext()) {
final CustomStatusItem customItem = (CustomStatusItem)customItemIterator.next();
String type = customItem.getType();
if (type.equals(statusItem.getText())) {
// Add Child Menu
Action action = new AbstractAction() {
public void actionPerformed(ActionEvent actionEvent) {
final String text = mainStatusItem.getText();
final StatusItem si = getStatusItem(text);
if (si == null) {
// Custom status
Log.error("Unable to find status item for status - " + text);
return;
}
SwingWorker worker = new SwingWorker() {
public Object construct() {
Presence oldPresence = si.getPresence();
Presence presence = copyPresence(oldPresence);
presence.setStatus(customItem.getStatus());
presence.setPriority(customItem.getPriority());
SparkManager.getSessionManager().changePresence(presence);
return "ok";
}
public void finished() {
String status = customItem.getType() + " - " + customItem.getStatus();
setStatus(status);
}
};
worker.start();
}
};
action.putValue(Action.NAME, customItem.getStatus());
action.putValue(Action.SMALL_ICON, statusItem.getIcon());
mainStatusItem.add(action);
}
}
// If menu has children, allow it to still be clickable.
mainStatusItem.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent mouseEvent) {
statusAction.actionPerformed(null);
popup.setVisible(false);
}
});
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?