📄 bsmapview.java
字号:
package edu.ou.kmi.buddyspace.plugins.maps.gui;
/*
* BSMapView.java
*
* Project: BuddySpace
* (C) Copyright Knowledge Media Institute 2002
*
*
* Created on 19 August 2002, 9:54
*/
import java.util.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.*;
//import org.netbeans.lib.awtextra.*;
import org.jabber.jabberbeans.*;
import org.jabber.jabberbeans.Extension.*;
import org.jabber.jabberbeans.util.*;
import edu.ou.kmi.buddyspace.core.*;
import edu.ou.kmi.buddyspace.gui.*;
import edu.ou.kmi.buddyspace.utils.*;
import edu.ou.kmi.buddyspace.plugins.maps.xml.*;
/**
* <code>BSMapView</code> is view of map represented by a <code>MapTag</code>.
*
* @author Jiri Komzak, Knowledge Media Institute, Open University, United Kingdom
* BuddyFinder additions by Chris Denham
*/
public class BSMapView extends JScrollPane
implements BSPresenceListener,
MouseListener, ActionListener {
private Hashtable jidLabels = null;
private Vector clusterLabels = null;
private Collection emphasizedJIDs = null;
private BSPresenceBean presenceBean = null;
private BSRosterBean rosterBean = null;
private BSMapWindow mapPanel = null;
private JPanel helpPanel = null;
private JLayeredPane mainPanel = null;
private int layerNum = Integer.MAX_VALUE;
private static Map xicons = new HashMap(); // emphasized icon lookup
static {
// Build a map of emphasized icons
for (int i = 0; i < BSJIDLabel.icons.length; i++) {
Icon icon = BSJIDLabel.icons[i];
xicons.put(icon, new IconX(icon));
}
}
private int w = 10, h = 10; // icons width and height
private JPopupMenu jidPopupMenu;
private BSJIDActionMenuItem chatMenuItem;
private BSJIDActionMenuItem msgMenuItem;
private BSJIDActionMenuItem sendFileMenuItem;
private BSJIDActionMenuItem subscribeMenuItem;
private BSJIDActionMenuItem getPlanMenuItem;
private JMenuItem presenceMenuItem;
private JPopupMenu clusterPopupMenu;
private String CHAT_MENU_STR = "Chat";
private String MSG_MENU_STR = "Message";
private String FILE_MENU_STR = "Send file";
private String SUBSCRIBE_MENU_STR = "Subscribe to presence";
private String GET_PLAN_MENU_STR = "Get plan";
private String originID = null;
private String mapID = null;
private String mapPath;
/** Constructor */
public BSMapView(BSMapWindow mapPanel, MapTag map, String mapID, String originID,
String mapPath, BSRosterBean rosterBean) {
super();
jidLabels = new Hashtable();
clusterLabels = new Vector();
this.mapPanel = mapPanel;
this.mapPath = mapPath;
this.rosterBean = rosterBean;
if (rosterBean != null) rosterBean.addRosterListener(new RosterAdapter() {
public void replacedRoster(Roster r) {
refreshNicks();
}
public void changedRoster(Roster r) {
refreshNicks();
}
});
jidPopupMenu = new JPopupMenu();
clusterPopupMenu = new JPopupMenu();
chatMenuItem = new BSJIDActionMenuItem(CHAT_MENU_STR);
chatMenuItem.addActionListener(this);
msgMenuItem = new BSJIDActionMenuItem(MSG_MENU_STR);
msgMenuItem.addActionListener(this);
sendFileMenuItem = new BSJIDActionMenuItem(FILE_MENU_STR);
sendFileMenuItem.addActionListener(this);
subscribeMenuItem = new BSJIDActionMenuItem(SUBSCRIBE_MENU_STR);
subscribeMenuItem.addActionListener(this);
getPlanMenuItem = new BSJIDActionMenuItem(GET_PLAN_MENU_STR);
getPlanMenuItem.addActionListener(this);
presenceMenuItem = new JMenuItem("");
this.mapID = mapID;
this.originID = originID;
helpPanel = new JPanel();
//helpPanel.setLayout(new BorderLayout());
mainPanel = new JLayeredPane();
mainPanel.setDoubleBuffered(true);
//mainPanel.setLayout(null);
addMap(map, 0, 0, 1);
refreshPresences();
helpPanel.add(mainPanel);
//helpPanel.add(mainPanel, BorderLayout.CENTER);
setViewportView(helpPanel);
}
/** Sets presence bean */
public void setPresenceBean(BSPresenceBean presenceBean) {
if (this.presenceBean != null)
this.presenceBean.removePresenceListener(this);
this.presenceBean = presenceBean;
if (presenceBean != null) {
presenceBean.addPresenceListener(this);
refreshPresences();
}
}
/** Returns currently used presence bean */
public BSPresenceBean getPresenceBean() {
return presenceBean;
}
/** Returns map id */
public String getMapID() {
return mapID;
}
/** Returns origin id */
public String getOriginID() {
return originID;
}
/** Refreshes nicks according to current roster bean */
public void refreshNicks() {
// for all jid labels
Enumeration allLabels = BSMapView.this.jidLabels.elements();
while (allLabels.hasMoreElements()) {
BSJIDLabelList labels = (BSJIDLabelList) allLabels.nextElement();
JID jid = labels.getJID();
String nick = BSMapView.this.rosterBean.getFriendlyName(jid);
if (nick == null) nick = "?";
labels.setNick(nick);
}
}
// *** map view construction functions ***
/** Adds <code>map</code> into the view.
* This is used for inserting insets during the view construction.
*/
protected void addMap(MapTag map, int offsetX, int offsetY, float scale) {
Enumeration layers;
// creates temp vector
Vector tmpLayers = new Vector();
layers = map.layers();
while (layers.hasMoreElements()) {
tmpLayers.add(layers.nextElement());
}
// goes through all layers and inserts them according to their priority
int lastPriority = Integer.MAX_VALUE;
int curPriority = getNextHighestPriority(tmpLayers, lastPriority);
while (curPriority != -1) {
Vector tmp2Layers = (Vector) tmpLayers.clone();
layers = tmp2Layers.elements();
while (layers.hasMoreElements()) {
LayerTag l = (LayerTag) layers.nextElement();
if (l.getPriorityInt() == curPriority) {
addLayer(l, offsetX, offsetY, scale);
tmpLayers.remove(l);
}
}
lastPriority = curPriority;
curPriority = getNextHighestPriority(tmpLayers, lastPriority);
}
invalidate();
repaint();
}
/*public void repaint() {
super.repaint();
if (mapPanel != null)
mapPanel.repaint();
}
public void invalidate() {
super.invalidate();
if (mapPanel != null)
mapPanel.invalidate();
}*/
/**
* Returns the highest priority in vector lower than lastPriority.
*/
protected int getNextHighestPriority(Vector layersVector, int lastPriority) {
int newPriority = -1;
Enumeration layers = layersVector.elements();
while (layers.hasMoreElements()) {
int priority = ((LayerTag) layers.nextElement()).getPriorityInt();
if (priority < lastPriority && priority > newPriority)
newPriority = priority;
}
return newPriority;
}
/** Adds <code>layer</code> into the view.
* This is used during the view construction.
*/
protected void addLayer(LayerTag layer, int offsetX, int offsetY, float scale) {
Insets insets = mainPanel.getInsets();
// sets the new scale and offset inside the layer
float newScale = scale * layer.getScaleFloat();
int newOffsetX = Math.round(offsetX + layer.getOffsetXInt() * scale);
int newOffsetY = Math.round(offsetY + layer.getOffsetYInt() * scale);
// adds all the items
Enumeration items = layer.getItems();
while (items.hasMoreElements()) {
Object o = items.nextElement();
// if it's item
if (o instanceof ItemTag) {
ItemTag i = (ItemTag) o;
JID jid = i.getJID();
if (jid == null) break;
String nick = null;
if (rosterBean != null) nick = rosterBean.getFriendlyName(jid);
if (rosterBean == null || nick == null) nick = "?";
BSJIDLabel jidLabel = new BSJIDLabel(jid, nick, BSJIDLabel.lurkerIcon);
jidLabel.setOpaque(false);
if (BSJIDLabel.onlineIcon != null) {
// allow margin of 3 pixels for emphasis
w = BSJIDLabel.onlineIcon.getIconWidth() + 6;
h = BSJIDLabel.onlineIcon.getIconHeight() + 6;
}
int x = Math.round(i.getLonFloat()*newScale + newOffsetX - w/2);
int y = Math.round(i.getLatFloat()*newScale + newOffsetY - h/2);
//mainPanel.add(jidLabel, new AbsoluteConstraints(x, y, w, h));
//mainPanel.add(jidLabel, JLayeredPane.DEFAULT_LAYER, layerNum);
mainPanel.add(jidLabel, new Integer(layerNum));
jidLabel.setBounds(x + insets.left, y + insets.top, w, h);
jidLabel.addMouseListener(this);
registerJIDLabel(jidLabel);
}
// else if it's cluster
else if (o instanceof ClusterTag) {
ClusterTag c = (ClusterTag) o;
BSClusterLabel clusterLabel = new BSClusterLabel(c, BSJIDLabel.clusterIcon);
clusterLabel.setOpaque(false);
if (BSJIDLabel.onlineIcon != null) {
// allow margin of 3 pixels for emphasis
w = BSJIDLabel.onlineIcon.getIconWidth() + 6;
h = BSJIDLabel.onlineIcon.getIconHeight() + 6;
}
int x = Math.round(c.getLonFloat()*newScale + newOffsetX - w/2);
int y = Math.round(c.getLatFloat()*newScale + newOffsetY - h/2);
//mainPanel.add(jidLabel, new AbsoluteConstraints(x, y, w, h));
//mainPanel.add(clusterLabel, JLayeredPane.DEFAULT_LAYER, layerNum);
mainPanel.add(clusterLabel, new Integer(layerNum));
clusterLabel.setBounds(x + insets.left, y + insets.top, w, h);
clusterLabel.addMouseListener(this);
registerClusterLabel(clusterLabel);
}
}
layerNum--;
String completeMapPath = new String(mapPath + "/" + originID);
// loads image of map
ImgTag imgTag = layer.getImg();
if (imgTag != null) {
//Image image = getImage(ClassLoader.getSystemResource("images/" + imgTag.getSrc()));
/*Image image = Toolkit.getDefaultToolkit().getImage(
ClassLoader.getSystemResource(completeMapPath + "/" + imgTag.getSrc()));*/
Image image = Toolkit.getDefaultToolkit().getImage(completeMapPath + "/" + imgTag.getSrc());
ImagePanel imgPanel = new ImagePanel(image, newScale, mainPanel, imgTag.getWidthInt(), imgTag.getHeightInt());
//mainPanel.add(imgPanel, JLayeredPane.DEFAULT_LAYER, layerNum);
mainPanel.add(imgPanel, new Integer(layerNum));
imgPanel.setBounds(newOffsetX + insets.left, newOffsetY + insets.top,
imgPanel.getWidth(), imgPanel.getHeight());
imgPanel.maybeResizeParent();
Dimension d = mainPanel.getMinimumSize();
setMinimumSize(new Dimension(d.width + 30, d.height + 30));
}
layerNum--;
// loads map given in layer's src attribute (when the map is only referenced)
MapTag map = layer.getSrcMap();
if (map != null) {
addMap(map, newOffsetX, newOffsetY, newScale);
}
}
/** Registeres jid label to receive presence changes */
protected void registerJIDLabel(BSJIDLabel lbl) {
JID jid = lbl.getJID();
if (jid == null) return;
//String str = BSPresenceBean.getJIDHashString(jid, true);
//BSJIDLabelList labels = (BSJIDLabelList) jidLabels.get(str);
BSJIDLabelList labels = (BSJIDLabelList) jidLabels.get(jid);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -