add node label
[jSite2.git] / src / net / pterodactylus / jsite / gui / NodeLabel.java
1 /*
2  * jSite2 - NodeLabel.java -
3  * Copyright © 2008 David Roden
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
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 net.pterodactylus.jsite.gui;
21
22 import java.awt.FlowLayout;
23 import java.awt.event.MouseEvent;
24 import java.awt.event.MouseListener;
25 import java.beans.PropertyChangeEvent;
26 import java.beans.PropertyChangeListener;
27
28 import javax.swing.BorderFactory;
29 import javax.swing.Icon;
30 import javax.swing.JLabel;
31 import javax.swing.JPanel;
32 import javax.swing.JPopupMenu;
33 import javax.swing.SwingConstants;
34 import javax.swing.border.EtchedBorder;
35
36 import net.pterodactylus.jsite.core.Node;
37 import net.pterodactylus.jsite.i18n.I18nable;
38 import net.pterodactylus.jsite.i18n.gui.I18nLabel;
39
40 /**
41  * A node label is a small component that sits in the status bar, displays the
42  * current status of a node and offers a context menu to connect and disconnect
43  * from the node.
44  * 
45  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
46  */
47 public class NodeLabel extends JLabel implements PropertyChangeListener, MouseListener, I18nable {
48
49         /** The Swing interface. */
50         private final SwingInterface swingInterface;
51
52         /** The node that is shows. */
53         private final Node node;
54
55         /** The online state icon. */
56         private final Icon onlineIcon;
57
58         /** The offline state icon. */
59         private final Icon offlineIcon;
60
61         /** The error state icon. */
62         private final Icon errorIcon;
63
64         /** The context menu. */
65         private JPopupMenu contextMenu;
66
67         /** The menu label. */
68         private I18nLabel menuLabel;
69
70         /**
71          * Creates a new node label.
72          * 
73          * @param swingInterface
74          *            The Swing interface
75          * @param node
76          *            The node
77          * @param onlineIcon
78          *            The icon for the “online” state
79          * @param offlineIcon
80          *            The icon for the “offline” state
81          * @param errorIcon
82          *            The icon for the “error” state
83          */
84         public NodeLabel(SwingInterface swingInterface, Node node, Icon onlineIcon, Icon offlineIcon, Icon errorIcon) {
85                 super(node.getName(), SwingConstants.LEADING);
86                 this.swingInterface = swingInterface;
87                 this.node = node;
88                 this.onlineIcon = onlineIcon;
89                 this.offlineIcon = offlineIcon;
90                 this.errorIcon = errorIcon;
91                 initComponents();
92         }
93
94         //
95         // ACTIONS
96         //
97
98         /**
99          * Displays the icon for the “online” state.
100          */
101         public void setOnline() {
102                 setIcon(onlineIcon);
103         }
104
105         /**
106          * Displays the icon for the “offline” state.
107          */
108         public void setOffline() {
109                 setIcon(offlineIcon);
110         }
111
112         /**
113          * Displays the icon for the “error” state.
114          */
115         public void setError() {
116                 setIcon(errorIcon);
117         }
118
119         //
120         // PRIVATE ACTIONS
121         //
122
123         /**
124          * Initializes the component.
125          */
126         private void initComponents() {
127                 setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), BorderFactory.createEmptyBorder(1, 6, 1, 6)));
128                 setIcon(offlineIcon);
129                 contextMenu = new JPopupMenu();
130                 menuLabel = new I18nLabel("mainWindow.statusBar.nodeLabel", node.getName());
131                 JPanel menuPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 0));
132                 menuPanel.add(menuLabel);
133                 contextMenu.add(menuPanel);
134                 contextMenu.addSeparator();
135                 contextMenu.add(swingInterface.getNodeConnectAction(node));
136                 contextMenu.add(swingInterface.getNodeDisconnectAction(node));
137                 addMouseListener(this);
138         }
139
140         /**
141          * Checks whether the given mouse event is a trigger for popup menues and
142          * shows the popup menu if it is.
143          * 
144          * @param mouseEvent
145          *            The mouse event to check for being a popup trigger
146          */
147         private void maybeShowContextMenu(MouseEvent mouseEvent) {
148                 if (mouseEvent.isPopupTrigger()) {
149                         contextMenu.show(this, mouseEvent.getX(), mouseEvent.getY());
150                 }
151         }
152
153         //
154         // INTERFACE PropertyChangeListener
155         //
156
157         /**
158          * {@inheritDoc}
159          */
160         public void propertyChange(PropertyChangeEvent propertyChangeEvent) {
161                 if (propertyChangeEvent.getSource() != node) {
162                         return;
163                 }
164                 if (propertyChangeEvent.getPropagationId().equals(Node.PROPERTY_NAME)) {
165                         setText(node.getName());
166                         menuLabel.updateI18n();
167                 }
168         }
169
170         //
171         // INTERFACE I18nable
172         //
173
174         /**
175          * {@inheritDoc}
176          */
177         public void updateI18n() {
178                 menuLabel.updateI18n();
179         }
180
181         //
182         // INTERFACE MouseListener
183         //
184
185         /**
186          * {@inheritDoc}
187          */
188         public void mouseClicked(MouseEvent mouseEvent) {
189                 maybeShowContextMenu(mouseEvent);
190         }
191
192         /**
193          * {@inheritDoc}
194          */
195         public void mouseEntered(MouseEvent mouseEvent) {
196                 /* ignore. */
197         }
198
199         /**
200          * {@inheritDoc}
201          */
202         public void mouseExited(MouseEvent mouseEvent) {
203                 /* ignore. */
204         }
205
206         /**
207          * {@inheritDoc}
208          */
209         public void mousePressed(MouseEvent mouseEvent) {
210                 maybeShowContextMenu(mouseEvent);
211         }
212
213         /**
214          * {@inheritDoc}
215          */
216         public void mouseReleased(MouseEvent mouseEvent) {
217                 maybeShowContextMenu(mouseEvent);
218         }
219
220 }