From 119b109fd29f4efa4756cef4daa43398d22cf7a0 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sat, 24 May 2008 18:44:44 +0200 Subject: [PATCH] add node label --- src/net/pterodactylus/jsite/gui/NodeLabel.java | 220 +++++++++++++++++++++++++ 1 file changed, 220 insertions(+) create mode 100644 src/net/pterodactylus/jsite/gui/NodeLabel.java diff --git a/src/net/pterodactylus/jsite/gui/NodeLabel.java b/src/net/pterodactylus/jsite/gui/NodeLabel.java new file mode 100644 index 0000000..ca3bbfd --- /dev/null +++ b/src/net/pterodactylus/jsite/gui/NodeLabel.java @@ -0,0 +1,220 @@ +/* + * jSite2 - NodeLabel.java - + * Copyright © 2008 David Roden + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +package net.pterodactylus.jsite.gui; + +import java.awt.FlowLayout; +import java.awt.event.MouseEvent; +import java.awt.event.MouseListener; +import java.beans.PropertyChangeEvent; +import java.beans.PropertyChangeListener; + +import javax.swing.BorderFactory; +import javax.swing.Icon; +import javax.swing.JLabel; +import javax.swing.JPanel; +import javax.swing.JPopupMenu; +import javax.swing.SwingConstants; +import javax.swing.border.EtchedBorder; + +import net.pterodactylus.jsite.core.Node; +import net.pterodactylus.jsite.i18n.I18nable; +import net.pterodactylus.jsite.i18n.gui.I18nLabel; + +/** + * A node label is a small component that sits in the status bar, displays the + * current status of a node and offers a context menu to connect and disconnect + * from the node. + * + * @author David ‘Bombe’ Roden <bombe@freenetproject.org> + */ +public class NodeLabel extends JLabel implements PropertyChangeListener, MouseListener, I18nable { + + /** The Swing interface. */ + private final SwingInterface swingInterface; + + /** The node that is shows. */ + private final Node node; + + /** The online state icon. */ + private final Icon onlineIcon; + + /** The offline state icon. */ + private final Icon offlineIcon; + + /** The error state icon. */ + private final Icon errorIcon; + + /** The context menu. */ + private JPopupMenu contextMenu; + + /** The menu label. */ + private I18nLabel menuLabel; + + /** + * Creates a new node label. + * + * @param swingInterface + * The Swing interface + * @param node + * The node + * @param onlineIcon + * The icon for the “online” state + * @param offlineIcon + * The icon for the “offline” state + * @param errorIcon + * The icon for the “error” state + */ + public NodeLabel(SwingInterface swingInterface, Node node, Icon onlineIcon, Icon offlineIcon, Icon errorIcon) { + super(node.getName(), SwingConstants.LEADING); + this.swingInterface = swingInterface; + this.node = node; + this.onlineIcon = onlineIcon; + this.offlineIcon = offlineIcon; + this.errorIcon = errorIcon; + initComponents(); + } + + // + // ACTIONS + // + + /** + * Displays the icon for the “online” state. + */ + public void setOnline() { + setIcon(onlineIcon); + } + + /** + * Displays the icon for the “offline” state. + */ + public void setOffline() { + setIcon(offlineIcon); + } + + /** + * Displays the icon for the “error” state. + */ + public void setError() { + setIcon(errorIcon); + } + + // + // PRIVATE ACTIONS + // + + /** + * Initializes the component. + */ + private void initComponents() { + setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), BorderFactory.createEmptyBorder(1, 6, 1, 6))); + setIcon(offlineIcon); + contextMenu = new JPopupMenu(); + menuLabel = new I18nLabel("mainWindow.statusBar.nodeLabel", node.getName()); + JPanel menuPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 0)); + menuPanel.add(menuLabel); + contextMenu.add(menuPanel); + contextMenu.addSeparator(); + contextMenu.add(swingInterface.getNodeConnectAction(node)); + contextMenu.add(swingInterface.getNodeDisconnectAction(node)); + addMouseListener(this); + } + + /** + * Checks whether the given mouse event is a trigger for popup menues and + * shows the popup menu if it is. + * + * @param mouseEvent + * The mouse event to check for being a popup trigger + */ + private void maybeShowContextMenu(MouseEvent mouseEvent) { + if (mouseEvent.isPopupTrigger()) { + contextMenu.show(this, mouseEvent.getX(), mouseEvent.getY()); + } + } + + // + // INTERFACE PropertyChangeListener + // + + /** + * {@inheritDoc} + */ + public void propertyChange(PropertyChangeEvent propertyChangeEvent) { + if (propertyChangeEvent.getSource() != node) { + return; + } + if (propertyChangeEvent.getPropagationId().equals(Node.PROPERTY_NAME)) { + setText(node.getName()); + menuLabel.updateI18n(); + } + } + + // + // INTERFACE I18nable + // + + /** + * {@inheritDoc} + */ + public void updateI18n() { + menuLabel.updateI18n(); + } + + // + // INTERFACE MouseListener + // + + /** + * {@inheritDoc} + */ + public void mouseClicked(MouseEvent mouseEvent) { + maybeShowContextMenu(mouseEvent); + } + + /** + * {@inheritDoc} + */ + public void mouseEntered(MouseEvent mouseEvent) { + /* ignore. */ + } + + /** + * {@inheritDoc} + */ + public void mouseExited(MouseEvent mouseEvent) { + /* ignore. */ + } + + /** + * {@inheritDoc} + */ + public void mousePressed(MouseEvent mouseEvent) { + maybeShowContextMenu(mouseEvent); + } + + /** + * {@inheritDoc} + */ + public void mouseReleased(MouseEvent mouseEvent) { + maybeShowContextMenu(mouseEvent); + } + +} -- 2.7.4