extend core listener
[jSite2.git] / src / net / pterodactylus / jsite / gui / MainWindow.java
1 /*
2  * jSite2 - MainWindow.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.BorderLayout;
23 import java.awt.Container;
24 import java.awt.Dimension;
25 import java.awt.event.WindowAdapter;
26 import java.awt.event.WindowEvent;
27
28 import javax.swing.Action;
29 import javax.swing.Box;
30 import javax.swing.BoxLayout;
31 import javax.swing.JButton;
32 import javax.swing.JFrame;
33 import javax.swing.JMenuBar;
34 import javax.swing.JMenuItem;
35 import javax.swing.JPanel;
36 import javax.swing.JTabbedPane;
37 import javax.swing.JToolBar;
38 import javax.swing.SwingConstants;
39 import javax.swing.border.EmptyBorder;
40
41 import net.pterodactylus.jsite.core.Project;
42 import net.pterodactylus.jsite.i18n.I18n;
43 import net.pterodactylus.jsite.i18n.I18nable;
44 import net.pterodactylus.jsite.i18n.gui.I18nAction;
45 import net.pterodactylus.jsite.i18n.gui.I18nMenu;
46 import net.pterodactylus.jsite.main.Version;
47 import net.pterodactylus.util.swing.StatusBar;
48 import net.pterodactylus.util.swing.SwingUtils;
49
50 /**
51  * Defines the main window of the application.
52  *
53  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
54  * @version $Id$
55  */
56 public class MainWindow extends JFrame implements I18nable {
57
58         /** The swing interface that receives all actions. */
59         private final SwingInterface swingInterface;
60
61         /** The status bar. */
62         private StatusBar statusBar = new StatusBar();
63
64         /** The content pane. */
65         private JPanel contentPane = new JPanel(new BorderLayout(12, 12));
66
67         /** The jSite menu. */
68         private I18nMenu jSiteMenu;
69
70         /** The node menu. */
71         private I18nMenu nodeMenu;
72
73         /** The “connect” (advanced mode) menu. */
74         private I18nMenu connectMenu;
75
76         /** The “connect” (simple mode) menu. */
77         private JMenuItem connectMenuItem;
78
79         /** The “disconnect” (advanced mode) menu. */
80         private I18nMenu disconnectMenu;
81
82         /** The “diconnect” (simple mode) menu item. */
83         private JMenuItem disconnectMenuItem;
84
85         /** The language menu. */
86         private I18nMenu languageMenu;
87
88         /** The about menu. */
89         private I18nMenu helpMenu;
90
91         /** The tabbed project pane. */
92         private JTabbedPane projectPane;
93
94         /** The project overview panel. */
95         private Box projectOverviewPanel;
96
97         /**
98          * Creates a new main window that redirects all actions to the given swing
99          * interface.
100          *
101          * @param swingInterface
102          *            The swing interface to receive all actions
103          */
104         public MainWindow(SwingInterface swingInterface) {
105                 super("jSite " + Version.getVersion());
106                 this.swingInterface = swingInterface;
107                 initWindow();
108                 setPreferredSize(new Dimension(480, 280));
109                 pack();
110                 SwingUtils.center(this);
111                 I18n.registerI18nable(this);
112                 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
113         }
114
115         //
116         // ACCESSORS
117         //
118
119         /**
120          * Sets the text of the status bar.
121          *
122          * @param text
123          *            The text of the status bar
124          */
125         public void setStatusBarText(String text) {
126                 statusBar.setText(text);
127         }
128
129         /**
130          * Sets whether the advanced mode is activated.
131          *
132          * @param advancedMode
133          *            <code>true</code> if the advanced mode is activated,
134          *            <code>false</code> if the simple mode is activated
135          */
136         public void setAdvancedMode(boolean advancedMode) {
137                 connectMenu.setVisible(advancedMode);
138                 connectMenuItem.setVisible(!advancedMode);
139                 disconnectMenu.setVisible(advancedMode);
140                 disconnectMenuItem.setVisible(!advancedMode);
141         }
142
143         /**
144          * {@inheritDoc}
145          */
146         @Override
147         public Container getContentPane() {
148                 return contentPane;
149         }
150
151         /**
152          * Returns the currently selected project.
153          *
154          * @return The currently selected project
155          */
156         public Project getSelectedProject() {
157                 return null;
158         }
159
160         //
161         // ACTIONS
162         //
163
164         /**
165          * Refreshes the menu items in the “connect” and “disconnect” menus.
166          */
167         void refreshNodeMenuItems() {
168                 connectMenu.removeAll();
169                 for (Action nodeConnectAction: swingInterface.getNodeConnectActions()) {
170                         connectMenu.add(nodeConnectAction);
171                 }
172                 disconnectMenu.removeAll();
173                 for (Action nodeDisconnectAction: swingInterface.getNodeDisconnectActions()) {
174                         disconnectMenu.add(nodeDisconnectAction);
175                 }
176         }
177
178         //
179         // PRIVATE METHODS
180         //
181
182         /**
183          * Initializes the window by creating all its components.
184          */
185         private void initWindow() {
186                 JMenuBar menuBar = new JMenuBar();
187
188                 jSiteMenu = new I18nMenu("mainWindow.menu.jSite");
189                 menuBar.add(jSiteMenu);
190
191                 jSiteMenu.add(new FixedJMenuItem(swingInterface.getConfigureAction()));
192                 jSiteMenu.addSeparator();
193                 jSiteMenu.add(new FixedJMenuItem(swingInterface.getImportConfigAction()));
194                 jSiteMenu.addSeparator();
195                 jSiteMenu.add(new FixedJMenuItem(swingInterface.getQuitAction()));
196
197                 connectMenu = new I18nMenu("mainWindow.menu.connect");
198                 disconnectMenu = new I18nMenu("mainWindow.menu.disconnect");
199
200                 nodeMenu = new I18nMenu("mainWindow.menu.node");
201                 menuBar.add(nodeMenu);
202
203                 nodeMenu.add(new FixedJMenuItem(swingInterface.getManageNodesAction()));
204                 nodeMenu.addSeparator();
205                 nodeMenu.add(connectMenuItem = new FixedJMenuItem(swingInterface.getNodeConnectAction()));
206                 nodeMenu.add(connectMenu);
207                 nodeMenu.add(disconnectMenuItem = new FixedJMenuItem(swingInterface.getNodeDisconnectAction()));
208                 nodeMenu.add(disconnectMenu);
209
210                 languageMenu = new I18nMenu("mainWindow.menu.language");
211                 menuBar.add(languageMenu);
212
213                 for (I18nAction languageAction: swingInterface.getLanguageActions()) {
214                         languageMenu.add(new FixedJMenuItem(languageAction));
215                 }
216
217                 JPanel spacerPanel = new JPanel();
218                 spacerPanel.setOpaque(false);
219                 menuBar.add(spacerPanel);
220
221                 helpMenu = new I18nMenu("mainWindow.menu.help");
222                 menuBar.add(helpMenu);
223
224                 helpMenu.add(new FixedJMenuItem(swingInterface.getHelpAboutAction()));
225
226                 setJMenuBar(menuBar);
227
228                 JToolBar toolBar = new JToolBar(I18n.get("mainWindow.toolbar.name"));
229                 toolBar.add(swingInterface.getManageNodesAction());
230                 toolBar.addSeparator();
231                 toolBar.add(swingInterface.getNodeConnectAction());
232                 toolBar.add(swingInterface.getNodeDisconnectAction());
233                 super.getContentPane().add(toolBar, BorderLayout.PAGE_START);
234
235                 super.getContentPane().add(contentPane, BorderLayout.CENTER);
236
237                 addWindowListener(new WindowAdapter() {
238
239                         /**
240                          * {@inheritDoc}
241                          */
242                         @SuppressWarnings("synthetic-access")
243                         @Override
244                         public void windowClosing(WindowEvent windowEvent) {
245                                 swingInterface.getQuitAction().actionPerformed(null);
246                         }
247                 });
248
249                 initComponents();
250         }
251
252         /**
253          * Initializes all components of this window.
254          */
255         private void initComponents() {
256                 super.getContentPane().add(statusBar, BorderLayout.PAGE_END);
257
258                 /*
259                  * the main window consists of two panels which are vertically oriented.
260                  * the upper panel contains of a tabbed pane, the lower panel consists
261                  * of a table that lists the running requests.
262                  */
263
264                 JPanel upperPanel = new JPanel(new BorderLayout(12, 12));
265                 getContentPane().add(upperPanel, BorderLayout.PAGE_START);
266                 contentPane.setBorder(new EmptyBorder(12, 12, 12, 12));
267
268                 projectPane = new JTabbedPane(SwingConstants.TOP, JTabbedPane.SCROLL_TAB_LAYOUT);
269                 upperPanel.add(projectPane, BorderLayout.CENTER);
270
271                 projectOverviewPanel = new Box(BoxLayout.PAGE_AXIS);
272                 projectOverviewPanel.setName(I18n.get("mainWindow.pane.overview.title"));
273                 projectPane.add(projectOverviewPanel);
274                 projectOverviewPanel.setBorder(new EmptyBorder(12, 12, 12, 12));
275                 projectOverviewPanel.add(Box.createVerticalGlue());
276                 JButton addProjectButton = new JButton(swingInterface.getAddProjectAction());
277                 addProjectButton.setAlignmentX(0.5f);
278                 projectOverviewPanel.add(addProjectButton);
279                 projectOverviewPanel.add(Box.createVerticalGlue());
280
281 // JPanel lowerPanel = new JPanel(new BorderLayout(12, 12));
282 // getContentPane().add(lowerPanel, BorderLayout.CENTER);
283         }
284
285         //
286         // INTERFACE I18nable
287         //
288
289         /**
290          * {@inheritDoc}
291          */
292         public void updateI18n() {
293                 swingInterface.getConfigureAction().updateI18n();
294                 swingInterface.getImportConfigAction().updateI18n();
295                 swingInterface.getQuitAction().updateI18n();
296                 swingInterface.getManageNodesAction().updateI18n();
297                 swingInterface.getNodeConnectAction().updateI18n();
298                 connectMenu.updateI18n();
299                 swingInterface.getNodeDisconnectAction().updateI18n();
300                 disconnectMenu.updateI18n();
301                 swingInterface.getAddProjectAction().updateI18n();
302                 swingInterface.getCloneProjectAction().updateI18n();
303                 swingInterface.getDeleteProjectAction().updateI18n();
304                 swingInterface.getHelpAboutAction().updateI18n();
305                 jSiteMenu.updateI18n();
306                 nodeMenu.updateI18n();
307                 languageMenu.updateI18n();
308                 for (I18nAction languageAction: swingInterface.getLanguageActions()) {
309                         languageAction.updateI18n();
310                 }
311                 helpMenu.updateI18n();
312                 getJMenuBar().revalidate();
313                 projectOverviewPanel.setName(I18n.get("mainWindow.pane.overview.title"));
314                 for (int componentIndex = 0; componentIndex < projectPane.getTabCount(); componentIndex++) {
315                         projectPane.setTitleAt(componentIndex, projectPane.getComponentAt(componentIndex).getName());
316                 }
317                 SwingUtils.repackCentered(this);
318         }
319
320 }