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