make delete and clone buttons project specific
[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.Component;
24 import java.awt.Container;
25 import java.awt.Dimension;
26 import java.awt.event.WindowAdapter;
27 import java.awt.event.WindowEvent;
28 import java.awt.event.WindowListener;
29 import java.beans.PropertyChangeEvent;
30 import java.beans.PropertyChangeListener;
31 import java.util.HashMap;
32 import java.util.Map;
33 import java.util.Timer;
34 import java.util.TimerTask;
35 import java.util.logging.Logger;
36
37 import javax.swing.Box;
38 import javax.swing.BoxLayout;
39 import javax.swing.Icon;
40 import javax.swing.JButton;
41 import javax.swing.JFrame;
42 import javax.swing.JMenu;
43 import javax.swing.JMenuBar;
44 import javax.swing.JOptionPane;
45 import javax.swing.JPanel;
46 import javax.swing.JTabbedPane;
47 import javax.swing.JToolBar;
48 import javax.swing.SwingConstants;
49 import javax.swing.border.EmptyBorder;
50
51 import net.pterodactylus.jsite.core.Node;
52 import net.pterodactylus.jsite.i18n.I18n;
53 import net.pterodactylus.jsite.i18n.I18nable;
54 import net.pterodactylus.jsite.i18n.gui.I18nAction;
55 import net.pterodactylus.jsite.i18n.gui.I18nMenu;
56 import net.pterodactylus.jsite.main.Version;
57 import net.pterodactylus.jsite.project.Project;
58 import net.pterodactylus.util.image.IconLoader;
59 import net.pterodactylus.util.logging.Logging;
60 import net.pterodactylus.util.swing.StatusBar;
61 import net.pterodactylus.util.swing.SwingUtils;
62
63 /**
64  * Defines the main window of the application.
65  * 
66  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
67  */
68 public class MainWindow extends JFrame implements WindowListener, I18nable, PropertyChangeListener {
69
70         /** Logger. */
71         @SuppressWarnings("unused")
72         private static final Logger logger = Logging.getLogger(MainWindow.class.getName());
73
74         /** The swing interface that receives all actions. */
75         private final SwingInterface swingInterface;
76
77         /** The status bar. */
78         private StatusBar statusBar = new StatusBar();
79
80         /** Timer for clearing the status bar. */
81         private Timer statusBarClearTimer = new Timer("StatusBar Cleaner", true);
82
83         /** Object for status bar clearing ticker event. */
84         private TimerTask statusBarClearTimerTask;
85
86         /** Delay (in seconds) after which to clear status bar. */
87         private int statusBarClearDelay = 5000;
88
89         /** The icon for offline nodes. */
90         private Icon offlineIcon;
91
92         /** The icon for online nodes. */
93         private Icon onlineIcon;
94
95         /** The icon for error nodes. */
96         private Icon errorIcon;
97
98         /** The content pane. */
99         private JPanel contentPane = new JPanel(new BorderLayout(12, 12));
100
101         /** The jSite menu. */
102         private I18nMenu jSiteMenu;
103
104         /** The node menu. */
105         private I18nMenu nodeMenu;
106
107         /** The language menu. */
108         private I18nMenu languageMenu;
109
110         /** The about menu. */
111         private I18nMenu helpMenu;
112
113         /** The tabbed project pane. */
114         private JTabbedPane projectPane;
115
116         /** The project overview panel. */
117         private Box projectOverviewPanel;
118
119         /** Maps from node to menus. */
120         private final Map<Node, JMenu> nodeMenus = new HashMap<Node, JMenu>();
121
122         /** Maps from nodes to node panels. */
123         private final Map<Node, NodeLabel> nodeLabels = new HashMap<Node, NodeLabel>();
124
125         /**
126          * Creates a new main window that redirects all actions to the given swing
127          * interface.
128          * 
129          * @param swingInterface
130          *            The swing interface to receive all actions
131          */
132         public MainWindow(SwingInterface swingInterface) {
133                 super("jSite " + Version.getVersion());
134                 this.swingInterface = swingInterface;
135                 initWindow();
136                 setPreferredSize(new Dimension(480, 280));
137                 pack();
138                 SwingUtils.center(this);
139                 I18n.registerI18nable(this);
140                 addWindowListener(this);
141                 setIconImage(IconLoader.loadImage("/jSite-frame-icon.png"));
142         }
143
144         //
145         // ACCESSORS
146         //
147
148         /**
149          * Sets the text of the status bar.
150          * 
151          * @param text
152          *            The text of the status bar
153          */
154         public void setStatusBarText(String text) {
155                 statusBar.setText(text);
156                 synchronized (statusBar) {
157                         if (statusBarClearTimerTask != null) {
158                                 statusBarClearTimerTask.cancel();
159                         }
160                         statusBarClearTimerTask = new TimerTask() {
161
162                                 @SuppressWarnings("synthetic-access")
163                                 @Override
164                                 public void run() {
165                                         statusBar.setText("\u00a0");
166                                 }
167
168                         };
169                         statusBarClearTimer.schedule(statusBarClearTimerTask, statusBarClearDelay);
170                 }
171         }
172
173         /**
174          * Returns the status bar clear delay (in milliseconds).
175          * 
176          * @return The status bar clear delay
177          */
178         public int getStatusBarClearDelay() {
179                 return statusBarClearDelay;
180         }
181
182         /**
183          * Sets the status bar clear delay (in milliseconds).
184          * 
185          * @param statusBarClearDelay
186          *            The status bar clear delay
187          */
188         public void setStatusBarClearDelay(int statusBarClearDelay) {
189                 this.statusBarClearDelay = statusBarClearDelay;
190         }
191
192         /**
193          * Sets whether the advanced mode is activated.
194          * 
195          * @param advancedMode
196          *            <code>true</code> if the advanced mode is activated,
197          *            <code>false</code> if the simple mode is activated
198          */
199         public void setAdvancedMode(boolean advancedMode) {
200                 /* doesn’t do anything right now. */
201         }
202
203         /**
204          * {@inheritDoc}
205          */
206         @Override
207         public Container getContentPane() {
208                 return contentPane;
209         }
210
211         /**
212          * Returns the currently selected project.
213          * 
214          * @return The currently selected project
215          */
216         public Project getSelectedProject() {
217                 return null;
218         }
219
220         /**
221          * Sets the given node to the “online” state.
222          * 
223          * @param node
224          *            The node to set online
225          */
226         public void setOnline(Node node) {
227                 nodeLabels.get(node).setOnline();
228         }
229
230         /**
231          * Sets the given node to the “offline” state in the status bar.
232          * 
233          * @param node
234          *            The node to set offline
235          */
236         public void setOffline(Node node) {
237                 nodeLabels.get(node).setOffline();
238         }
239
240         /**
241          * Sets the given node to the “error” state in the status bar.
242          * 
243          * @param node
244          *            The node to set the error state for
245          */
246         public void setError(Node node) {
247                 nodeLabels.get(node).setError();
248         }
249
250         //
251         // ACTIONS
252         //
253
254         /**
255          * Adds a node to the menu.
256          * 
257          * @param node
258          *            The node to add
259          */
260         void addNode(Node node) {
261                 JMenu newNodeMenu = new JMenu(node.getName());
262                 nodeMenus.put(node, newNodeMenu);
263                 newNodeMenu.add(swingInterface.getNodeConnectAction(node));
264                 newNodeMenu.add(swingInterface.getNodeDisconnectAction(node));
265                 newNodeMenu.addSeparator();
266                 newNodeMenu.add(swingInterface.getNodeEditAction(node));
267                 newNodeMenu.add(swingInterface.getNodeDeleteAction(node));
268                 nodeMenu.add(newNodeMenu);
269                 NodeLabel nodeLabel = new NodeLabel(swingInterface, node, onlineIcon, offlineIcon, errorIcon);
270                 nodeLabels.put(node, nodeLabel);
271                 statusBar.addSideComponent(nodeLabel);
272                 node.addPropertyChangeListener(this);
273         }
274
275         /**
276          * Removes a node from the menu.
277          * 
278          * @param node
279          *            The node to remove
280          */
281         void removeNode(Node node) {
282                 nodeMenu.remove(nodeMenus.remove(node));
283                 statusBar.removeSideComponent(nodeLabels.remove(node));
284                 node.removePropertyChangeListener(this);
285         }
286
287         /**
288          * Adds a project to the project pane.
289          * 
290          * @param project
291          *            The project to add
292          * @param switchToProject
293          *            <code>true</code> to switch to the new panel,
294          *            <code>false</code> to not change the current panel
295          */
296         void addProject(Project project, boolean switchToProject) {
297                 ProjectPanel projectPanel = new ProjectPanel(swingInterface, project);
298                 int newTabIndex = projectPane.getTabCount();
299                 projectPane.add(project.getName(), projectPanel);
300                 projectPane.setToolTipTextAt(newTabIndex, project.getDescription());
301                 project.addPropertyChangeListener(this);
302                 if (switchToProject) {
303                         projectPane.setSelectedIndex(newTabIndex);
304                         while (project.getBasePath().length() == 0) {
305                                 JOptionPane.showMessageDialog(this, I18n.get("mainWindow.information.changeProjectBasePath.message"), I18n.get("mainWindow.information.changeProjectBasePath.title"), JOptionPane.INFORMATION_MESSAGE);
306                                 projectPanel.changeBasePath();
307                         }
308                 }
309         }
310
311         /**
312          * @param project
313          */
314         void projectInsertStarted(Project project) {
315                 int projectIndex = getProjectIndex(project);
316                 if (projectIndex == -1) {
317                         return;
318                 }
319                 projectPane.setTitleAt(projectIndex, I18n.get("projectPanel.title.starting", project.getName()));
320         }
321
322         /**
323          * @param project
324          * @param totalBlocks
325          * @param requiredBlocks
326          * @param successfulBlocks
327          * @param failedBlocks
328          * @param fatallyFailedBlocks
329          * @param finalizedTotal
330          */
331         void projectInsertProgressed(Project project, int totalBlocks, int requiredBlocks, int successfulBlocks, int failedBlocks, int fatallyFailedBlocks, boolean finalizedTotal) {
332                 int projectIndex = getProjectIndex(project);
333                 if (projectIndex == -1) {
334                         return;
335                 }
336                 projectPane.setTitleAt(projectIndex, I18n.get("projectPanel.title.progress", project.getName(), requiredBlocks / (double) successfulBlocks));
337         }
338
339         /**
340          * @param project
341          */
342         void projectInsertGeneratedURI(Project project) {
343                 /* TODO - update panel. */
344         }
345
346         /**
347          * @param project
348          * @param success
349          */
350         void projectInsertFinished(Project project, boolean success) {
351                 int projectIndex = getProjectIndex(project);
352                 if (projectIndex == -1) {
353                         return;
354                 }
355                 projectPane.setTitleAt(projectIndex, project.getName());
356         }
357
358         //
359         // PRIVATE METHODS
360         //
361
362         /**
363          * Returns the index of the project panel that contains the given project.
364          * 
365          * @param project
366          *            The wanted project
367          * @return The index of {@link #projectPane}’s tab that contains the given
368          *         project, or <code>-1</code> if the project can not be found
369          */
370         private int getProjectIndex(Project project) {
371                 int tabCount = projectPane.getTabCount();
372                 for (int tabIndex = 1; tabIndex < tabCount; tabIndex++) {
373                         Component tabComponent = projectPane.getComponentAt(tabIndex);
374                         if (tabComponent instanceof ProjectPanel) {
375                                 if (((ProjectPanel) tabComponent).getProject() == project) {
376                                         return tabIndex;
377                                 }
378                         }
379                 }
380                 return -1;
381         }
382
383         /**
384          * Initializes the window by creating all its components.
385          */
386         private void initWindow() {
387                 onlineIcon = IconLoader.loadIcon("/node-online.png");
388                 offlineIcon = IconLoader.loadIcon("/node-offline.png");
389                 errorIcon = IconLoader.loadIcon("/node-error.png");
390
391                 JMenuBar menuBar = new JMenuBar();
392
393                 jSiteMenu = new I18nMenu("mainWindow.menu.jSite");
394                 menuBar.add(jSiteMenu);
395
396                 jSiteMenu.add(new FixedJMenuItem(swingInterface.getConfigureAction()));
397                 jSiteMenu.addSeparator();
398                 jSiteMenu.add(new FixedJMenuItem(swingInterface.getImportConfigAction()));
399                 jSiteMenu.addSeparator();
400                 jSiteMenu.add(new FixedJMenuItem(swingInterface.getQuitAction()));
401
402                 nodeMenu = new I18nMenu("mainWindow.menu.node");
403                 menuBar.add(nodeMenu);
404
405                 nodeMenu.add(new FixedJMenuItem(swingInterface.getAddNodeAction()));
406                 nodeMenu.addSeparator();
407
408                 languageMenu = new I18nMenu("mainWindow.menu.language");
409                 menuBar.add(languageMenu);
410
411                 for (I18nAction languageAction: swingInterface.getLanguageActions()) {
412                         languageMenu.add(new FixedJMenuItem(languageAction));
413                 }
414
415                 menuBar.add(Box.createHorizontalGlue());
416
417                 helpMenu = new I18nMenu("mainWindow.menu.help");
418                 menuBar.add(helpMenu);
419
420                 helpMenu.add(new FixedJMenuItem(swingInterface.getHelpAboutAction()));
421
422                 setJMenuBar(menuBar);
423
424                 JToolBar toolBar = new JToolBar(I18n.get("mainWindow.toolbar.name"));
425                 toolBar.add(swingInterface.getAddNodeAction());
426                 toolBar.addSeparator();
427                 toolBar.add(swingInterface.getQuitAction());
428                 super.getContentPane().add(toolBar, BorderLayout.PAGE_START);
429
430                 super.getContentPane().add(contentPane, BorderLayout.CENTER);
431
432                 addWindowListener(new WindowAdapter() {
433
434                         /**
435                          * {@inheritDoc}
436                          */
437                         @SuppressWarnings("synthetic-access")
438                         @Override
439                         public void windowClosing(WindowEvent windowEvent) {
440                                 swingInterface.getQuitAction().actionPerformed(null);
441                         }
442                 });
443
444                 initComponents();
445         }
446
447         /**
448          * Initializes all components of this window.
449          */
450         private void initComponents() {
451                 super.getContentPane().add(statusBar, BorderLayout.PAGE_END);
452                 contentPane.setBorder(new EmptyBorder(12, 12, 12, 12));
453
454                 projectPane = new JTabbedPane(SwingConstants.TOP, JTabbedPane.SCROLL_TAB_LAYOUT);
455                 contentPane.add(projectPane, BorderLayout.CENTER);
456
457                 projectOverviewPanel = new Box(BoxLayout.PAGE_AXIS);
458                 projectOverviewPanel.setName(I18n.get("mainWindow.pane.overview.title"));
459                 projectPane.add(projectOverviewPanel);
460                 projectOverviewPanel.setBorder(new EmptyBorder(12, 12, 12, 12));
461                 projectOverviewPanel.add(Box.createVerticalGlue());
462                 JButton addProjectButton = new JButton(swingInterface.getAddProjectAction());
463                 addProjectButton.setAlignmentX(0.5f);
464                 projectOverviewPanel.add(addProjectButton);
465                 projectOverviewPanel.add(Box.createVerticalGlue());
466         }
467
468         //
469         // INTERFACE I18nable
470         //
471
472         /**
473          * {@inheritDoc}
474          */
475         public void updateI18n() {
476                 swingInterface.getConfigureAction().updateI18n();
477                 swingInterface.getImportConfigAction().updateI18n();
478                 swingInterface.getQuitAction().updateI18n();
479                 swingInterface.getAddNodeAction().updateI18n();
480                 swingInterface.getAddProjectAction().updateI18n();
481                 swingInterface.getHelpAboutAction().updateI18n();
482                 jSiteMenu.updateI18n();
483                 nodeMenu.updateI18n();
484                 languageMenu.updateI18n();
485                 for (Node node: swingInterface.getNodes()) {
486                         swingInterface.getNodeConnectAction(node).updateI18n();
487                         swingInterface.getNodeDisconnectAction(node).updateI18n();
488                         swingInterface.getNodeEditAction(node).updateI18n();
489                         swingInterface.getNodeDeleteAction(node).updateI18n();
490                 }
491                 for (Project project: swingInterface.getProjects()) {
492                         swingInterface.getCloneProjectAction(project).updateI18n();
493                         swingInterface.getDeleteProjectAction(project).updateI18n();
494                 }
495                 for (I18nAction languageAction: swingInterface.getLanguageActions()) {
496                         languageAction.updateI18n();
497                 }
498                 helpMenu.updateI18n();
499                 getJMenuBar().revalidate();
500                 projectPane.setTitleAt(0, I18n.get("mainWindow.pane.overview.title"));
501                 for (int componentIndex = 0; componentIndex < projectPane.getTabCount(); componentIndex++) {
502                         Component tabComponent = projectPane.getComponentAt(componentIndex);
503                         if (tabComponent instanceof ProjectPanel) {
504                                 ((ProjectPanel) tabComponent).updateI18n();
505                         }
506                 }
507                 SwingUtils.repackCentered(this);
508         }
509
510         //
511         // INTERFACE WindowListener
512         //
513
514         /**
515          * {@inheritDoc}
516          */
517         public void windowActivated(WindowEvent e) {
518                 /* do nothing. */
519         }
520
521         /**
522          * {@inheritDoc}
523          */
524         public void windowClosed(WindowEvent e) {
525                 /* do nothing. */
526         }
527
528         /**
529          * {@inheritDoc}
530          */
531         public void windowClosing(WindowEvent e) {
532                 swingInterface.getQuitAction().actionPerformed(null);
533         }
534
535         /**
536          * {@inheritDoc}
537          */
538         public void windowDeactivated(WindowEvent e) {
539                 /* do nothing. */
540         }
541
542         /**
543          * {@inheritDoc}
544          */
545         public void windowDeiconified(WindowEvent e) {
546                 /* do nothing. */
547         }
548
549         /**
550          * {@inheritDoc}
551          */
552         public void windowIconified(WindowEvent e) {
553                 /* do nothing. */
554         }
555
556         /**
557          * {@inheritDoc}
558          */
559         public void windowOpened(WindowEvent e) {
560                 /* do nothing. */
561         }
562
563         //
564         // INTERFACE PropertyChangeListener
565         //
566
567         /**
568          * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
569          */
570         public void propertyChange(PropertyChangeEvent propertyChangeEvent) {
571                 Object eventSource = propertyChangeEvent.getSource();
572                 String propertyName = propertyChangeEvent.getPropertyName();
573                 if (eventSource instanceof Project) {
574                         /* if a project was changed, update the tab title and tooltip. */
575                         if (Project.PROPERTY_NAME.equals(propertyName) || Project.PROPERTY_DESCRIPTION.equals(propertyName)) {
576                                 Project project = (Project) eventSource;
577                                 int tabCount = projectPane.getTabCount();
578                                 for (int tabIndex = 0; tabIndex < tabCount; tabIndex++) {
579                                         Component tabComponent = projectPane.getComponentAt(tabIndex);
580                                         if (tabComponent instanceof ProjectPanel) {
581                                                 Project tabProject = ((ProjectPanel) tabComponent).getProject();
582                                                 if (tabProject.equals(project)) {
583                                                         projectPane.setTitleAt(tabIndex, project.getName());
584                                                         projectPane.setToolTipTextAt(tabIndex, project.getDescription());
585                                                         projectPane.repaint();
586                                                 }
587                                         }
588                                 }
589                         }
590                 } else if (eventSource instanceof Node) {
591                         if (propertyName.equals(Node.PROPERTY_NAME)) {
592                                 Node changedNode = (Node) eventSource;
593                                 nodeMenus.get(changedNode).setText(changedNode.getName());
594                         }
595                 }
596         }
597
598 }