Siba/src/org/dclermonte/siba/gui/SibaGUI.java

280 lines
9.0 KiB
Java
Raw Normal View History

2016-04-19 22:34:25 +02:00
/*
2016-04-23 17:50:13 +02:00
* Copyright (C) 2016 Didier Clermonté <dclermonte@april.org>
2016-04-19 22:34:25 +02:00
* Copyright (C) 2016 Christian Pierre Momon <christian.momon@devinsy.fr>
*
* This file is part of Siba.
*
* Siba is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2016-03-29 23:25:18 +02:00
package org.dclermonte.siba.gui;
2016-03-27 12:50:01 +02:00
2016-04-19 22:34:25 +02:00
import java.awt.BorderLayout;
import java.awt.Color;
2016-04-26 23:02:26 +02:00
import java.awt.Cursor;
2016-04-19 22:34:25 +02:00
import java.awt.Dimension;
2016-03-27 12:50:01 +02:00
import java.awt.EventQueue;
2016-04-19 22:34:25 +02:00
import java.awt.Font;
2016-04-26 23:02:26 +02:00
import java.awt.Point;
2016-05-25 18:10:34 +02:00
import java.awt.Toolkit;
2016-04-19 22:34:25 +02:00
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Locale;
import java.util.ResourceBundle;
2016-03-27 12:50:01 +02:00
import javax.swing.JFrame;
2016-04-19 22:34:25 +02:00
import javax.swing.JMenu;
2016-03-29 23:25:18 +02:00
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
2016-04-19 22:34:25 +02:00
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
2016-04-26 23:02:26 +02:00
import javax.swing.SwingConstants;
2016-04-19 22:34:25 +02:00
import javax.swing.UIManager;
2016-04-26 23:02:26 +02:00
import javax.swing.UnsupportedLookAndFeelException;
2016-03-27 12:50:01 +02:00
2016-04-19 22:34:25 +02:00
import org.apache.commons.lang3.StringUtils;
2016-05-31 16:25:35 +02:00
import org.dclermonte.siba.Siba;
2016-05-19 18:40:00 +02:00
import org.dclermonte.siba.SibaException;
2016-04-23 17:50:13 +02:00
import org.dclermonte.siba.gui.utils.GUIToolBox;
2016-03-27 12:50:01 +02:00
/**
* The main class for GUI (Graphical User Interface )
*
2016-05-22 21:47:41 +02:00
* @author dclermonte
*
*/
2016-03-27 12:50:01 +02:00
public class SibaGUI
{
2016-04-26 23:02:26 +02:00
private static final ResourceBundle BUNDLE = ResourceBundle.getBundle("org.dclermonte.siba.gui.messages"); //$NON-NLS-1$
2016-04-19 22:34:25 +02:00
/**
* Launch the application.
*/
Locale locale;
private JFrame frmSimpleBackup;
2016-03-27 12:50:01 +02:00
/**
2016-05-19 18:40:00 +02:00
* This is the constructor of this application.
2016-04-26 23:02:26 +02:00
*
* @throws UnsupportedLookAndFeelException
* @throws IllegalAccessException
* @throws InstantiationException
* @throws ClassNotFoundException
2016-03-27 12:50:01 +02:00
*/
2016-04-26 23:02:26 +02:00
public SibaGUI() throws ClassNotFoundException, InstantiationException, IllegalAccessException,
2016-05-19 18:40:00 +02:00
UnsupportedLookAndFeelException, SibaException
2016-03-27 12:50:01 +02:00
{
2016-04-23 17:50:13 +02:00
// Set default GUI catch.
2016-04-19 22:34:25 +02:00
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler()
{
/**
*
*/
@Override
public void uncaughtException(final Thread thread, final Throwable exception)
{
String message;
if (exception instanceof OutOfMemoryError)
{
2016-04-26 23:02:26 +02:00
message = "Java ran out of memory!\n\n";
2016-04-19 22:34:25 +02:00
}
else
{
message = "An error occured: " + exception.getClass() + "(" + exception.getMessage() + ")";
}
JOptionPane.showMessageDialog(null, message, "Error", JOptionPane.ERROR_MESSAGE);
exception.printStackTrace();
}
});
2016-04-23 17:50:13 +02:00
2016-04-19 22:34:25 +02:00
// Remove BOLD on default font.
2016-04-20 10:28:03 +02:00
UIManager.put("swing.boldMetal", Boolean.FALSE);
2016-04-19 22:34:25 +02:00
// Set LookAndFeel.
System.out.println("System lookAndFeel property:" + System.getProperty("swing.defaultlaf"));
2016-04-20 10:28:03 +02:00
System.out.println("Available lookAndFeel: " + GUIToolBox.availableLookAndFeels().toString());
2016-04-19 22:34:25 +02:00
System.out.println("System lookAndFeel: " + UIManager.getSystemLookAndFeelClassName());
System.out.println("Current lookAndFeel: " + UIManager.getLookAndFeel().getName());
if (!StringUtils.equals(UIManager.getSystemLookAndFeelClassName(), "javax.swing.plaf.metal.MetalLookAndFeel"))
{
try
{
System.out.println("Metal LAF setted and system LAF detected, try to set system LAF.");
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (final Exception exception)
{
System.out.println("Failed to set the system LookAndFeel.");
}
}
2016-04-20 10:28:03 +02:00
else if (GUIToolBox.availableLookAndFeels().toString().contains("GTK+"))
2016-04-19 22:34:25 +02:00
{
try
{
System.out.println("Metal LAF setted and GTK+ LAF detected, try to set GTK+ LAF.");
UIManager.setLookAndFeel("com.sun.java.swing.plaf.gtk.GTKLookAndFeel");
}
catch (final Exception exception)
{
System.out.println("Failed to set the system LookAndFeel.");
}
}
2016-04-26 23:02:26 +02:00
/* UIManager.setLookAndFeel("com.sun.java.swing.plaf.metal.MetalLookAndFeel"); */
2016-04-19 22:34:25 +02:00
System.out.println("Activated lookAndFeel: " + UIManager.getLookAndFeel().getName());
2016-04-23 17:50:13 +02:00
// Set default locale.
2016-04-19 22:34:25 +02:00
this.locale = Locale.getDefault();
updateLanguage(this.locale);
System.out.println(this.locale.getDisplayLanguage());
2016-03-27 12:50:01 +02:00
initialize();
2016-04-19 22:34:25 +02:00
2016-03-27 12:50:01 +02:00
}
/**
* Initialize the contents of the frame.
2016-05-19 18:40:00 +02:00
*
* @throws SibaException
2016-03-27 12:50:01 +02:00
*/
2016-05-19 18:40:00 +02:00
private void initialize() throws SibaException
2016-03-27 12:50:01 +02:00
{
2016-04-19 22:34:25 +02:00
this.frmSimpleBackup = new JFrame();
2016-05-25 18:10:34 +02:00
this.frmSimpleBackup.setIconImage(Toolkit.getDefaultToolkit()
2016-05-26 22:37:55 +02:00
.getImage(SibaGUI.class.getResource("/org/dclermonte/siba/gui/Cat-Box-32x32.png")));
2016-04-26 23:02:26 +02:00
this.frmSimpleBackup.getContentPane().setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
2016-04-19 22:34:25 +02:00
this.frmSimpleBackup.setMinimumSize(new Dimension(600, 400));
this.frmSimpleBackup.getContentPane().setBackground(new Color(173, 255, 47));
2016-03-27 12:50:01 +02:00
JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
2016-04-26 23:02:26 +02:00
tabbedPane.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
2016-04-19 22:34:25 +02:00
this.frmSimpleBackup.getContentPane().add(tabbedPane, BorderLayout.CENTER);
2016-03-27 22:32:35 +02:00
JPanel panel = new BackupPanel();
2016-04-26 23:02:26 +02:00
panel.setBackground(new Color(0, 255, 0));
String backup = BackupPanel.BUNDLE.getString("BackupPanel.TitleBackupPanel.text");
tabbedPane.addTab(backup, null, panel, null);
2016-04-19 22:34:25 +02:00
2016-03-27 22:32:35 +02:00
JPanel panel_1 = new CheckPanel();
2016-04-26 23:02:26 +02:00
panel_1.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
2016-03-27 12:50:01 +02:00
panel_1.setBackground(new Color(144, 238, 144));
2016-04-26 23:02:26 +02:00
String check = BackupPanel.BUNDLE.getString("BackupPanel.TitleCheckPanel.text");
tabbedPane.addTab(check, null, panel_1, null);
2016-04-19 22:34:25 +02:00
this.frmSimpleBackup.setFont(new Font("DejaVu Sans", Font.BOLD, 12));
this.frmSimpleBackup.setForeground(new Color(0, 100, 0));
this.frmSimpleBackup.setBackground(new Color(102, 205, 170));
this.frmSimpleBackup.setTitle("Simple Backup");
this.frmSimpleBackup.setBounds(100, 100, 450, 300);
this.frmSimpleBackup.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
2016-03-29 23:25:18 +02:00
JMenuBar menuBar = new JMenuBar();
2016-04-26 23:02:26 +02:00
menuBar.setBackground(new Color(152, 251, 152));
2016-04-19 22:34:25 +02:00
this.frmSimpleBackup.setJMenuBar(menuBar);
2016-04-26 23:02:26 +02:00
JMenuItem mntmNewMenuItem_1 = new JMenuItem(BUNDLE.getString("SibaGUI.mntmNewMenuItem_1.text")); //$NON-NLS-1$
mntmNewMenuItem_1.setMaximumSize(new Dimension(100, 32767));
mntmNewMenuItem_1.setHorizontalTextPosition(SwingConstants.LEFT);
mntmNewMenuItem_1.setHorizontalAlignment(SwingConstants.LEFT);
mntmNewMenuItem_1.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
2016-04-19 22:34:25 +02:00
mntmNewMenuItem_1.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(final ActionEvent e)
{
SibaGUI.this.frmSimpleBackup.dispose();
2016-03-29 23:25:18 +02:00
}
});
menuBar.add(mntmNewMenuItem_1);
2016-04-19 22:34:25 +02:00
2016-04-26 23:02:26 +02:00
JMenu mnNewMenu = new JMenu(BUNDLE.getString("SibaGUI.mnNewMenu.text")); //$NON-NLS-1$
mnNewMenu.setLocation(new Point(500, 0));
mnNewMenu.setHorizontalTextPosition(SwingConstants.RIGHT);
mnNewMenu.setHorizontalAlignment(SwingConstants.RIGHT);
mnNewMenu.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
mnNewMenu.setForeground(new Color(0, 0, 0));
2016-03-29 23:25:18 +02:00
mnNewMenu.setBackground(new Color(127, 255, 212));
mnNewMenu.setOpaque(true);
menuBar.add(mnNewMenu);
2016-04-19 22:34:25 +02:00
2016-04-26 23:02:26 +02:00
JMenuItem mntmNewMenuItem_2 = new JMenuItem(BUNDLE.getString("SibaGUI.mntmNewMenuItem_2.text")); //$NON-NLS-1$
mntmNewMenuItem_2.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
2016-04-19 22:34:25 +02:00
mntmNewMenuItem_2.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(final ActionEvent e)
{
2016-03-29 23:25:18 +02:00
AboutDialog.main("");
}
});
mnNewMenu.add(mntmNewMenuItem_2);
2016-03-27 12:50:01 +02:00
}
2016-04-19 22:34:25 +02:00
/**
2016-05-19 18:40:00 +02:00
* This method set the local language.
2016-04-19 22:34:25 +02:00
*/
public void updateLanguage(final Locale source)
{
// Change JVM default locale.
java.util.Locale.setDefault(source);
// Change LookAndFeel default locale.
javax.swing.UIManager.getDefaults().setDefaultLocale(source);
// Change new component default locale.
javax.swing.JComponent.setDefaultLocale(source);
//
ResourceBundle.clearCache();
}
/**
2016-05-19 18:40:00 +02:00
* This method launch the GUI.
*/
2016-05-16 23:40:07 +02:00
public static void run()
{
EventQueue.invokeLater(new Runnable()
{
@Override
public void run()
{
try
{
SibaGUI window = new SibaGUI();
window.frmSimpleBackup.setVisible(true);
}
2016-05-22 21:47:41 +02:00
catch (SibaException sibaException)
{
2016-05-31 16:25:35 +02:00
sibaException.printStackTrace();
2016-05-22 21:47:41 +02:00
String dataMissing = sibaException.getMessage();
if (!(sibaException.getCause() == null))
{
2016-05-31 16:25:35 +02:00
Siba.logger.info("SibaException chainée = " + sibaException.getCause().getMessage());
// dataMissing.concat(sibaException.getCause().getMessage());
2016-05-22 21:47:41 +02:00
}
String titleWarning = BUNDLE.getString("BackupPanel.TitleWarning.text");
JOptionPane.showMessageDialog(null, dataMissing, titleWarning, JOptionPane.INFORMATION_MESSAGE);
}
2016-05-16 23:40:07 +02:00
catch (Exception e)
{
e.printStackTrace();
}
}
});
}
2016-03-27 12:50:01 +02:00
}