2010 => 2011
[scilab.git] / scilab / modules / gui / src / java / org / scilab / modules / gui / utils / ScilabAboutBox.java
1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2009 - DIGITEO - Vincent COUVERT
4  *
5  * This file must be used under the terms of the CeCILL.
6  * This source file is licensed as described in the file COPYING, which
7  * you should have received as part of this distribution.  The terms
8  * are also available at
9  * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
10  *
11  */
12
13 package org.scilab.modules.gui.utils;
14
15 import java.awt.Graphics;
16 import java.awt.Image;
17 import java.awt.Toolkit;
18 import java.awt.event.ActionEvent;
19 import java.awt.event.ActionListener;
20 import java.io.BufferedReader;
21 import java.io.File;
22 import java.io.FileInputStream;
23 import java.io.IOException;
24 import java.io.InputStreamReader;
25
26 import javax.swing.Icon;
27 import javax.swing.ImageIcon;
28 import javax.swing.JButton;
29 import javax.swing.JFrame;
30 import javax.swing.JScrollPane;
31 import javax.swing.JTextPane;
32
33 import org.scilab.modules.gui.utils.ScilabSwingUtilities;
34 import org.scilab.modules.localization.Messages;
35
36 public class ScilabAboutBox {
37
38         private static Icon scilabIcon = new ImageIcon(System.getenv("SCI") + "/modules/gui/images/icons/scilab.png");
39         private static Image imageForIcon = ((ImageIcon) scilabIcon).getImage();
40         /**
41          * Constructor
42          */
43         protected ScilabAboutBox() {
44                 throw new UnsupportedOperationException(); /* Prevents calls from subclass */
45         }
46
47         public static void displayAndWait() {
48
49                 String[] contents = {Messages.gettext("The Scilab Consortium (DIGITEO)"),
50                                 Messages.gettext("Copyright (c) 1989-2011 (INRIA)"),
51                                 Messages.gettext("Copyright (c) 1989-2007 (ENPC)"),
52                                 "",
53                                 Messages.gettext("CeCILL License")};
54
55                 String filename = System.getenv("SCI") + "/ACKNOWLEDGEMENTS"; // Source version
56                 if (!new File(filename).exists()) {
57                         filename = System.getenv("SCI") + "/../../ACKNOWLEDGEMENTS"; // Linux binary version
58                 }
59
60                 createAboutBox(Messages.gettext("About Scilab..."), contents, filename, Messages.gettext("Acknowledgements"));
61
62         }
63
64         public static void createAboutBox(String aboutTitle, String[] contents, String ackFile, String ackTitle) {
65
66                 ImageIcon icon = new ImageIcon(System.getenv("SCI") + "/modules/gui/images/icons/aboutscilab.png");
67                 icon.setImage(icon.getImage().getScaledInstance(icon.getIconWidth() / 2, icon.getIconHeight() / 2, 0));
68
69                 final JFrame ackBox = new JFrame();
70                 ackBox.setIconImage(imageForIcon);
71                 final JFrame aboutBox = new JFrame();
72                 aboutBox.setIconImage(imageForIcon);
73                 aboutBox.setTitle(Messages.gettext(aboutTitle));
74
75                 aboutBox.setLayout(null);
76                 aboutBox.setAlwaysOnTop(true);
77                 aboutBox.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
78                 aboutBox.setLocationRelativeTo(null);
79                 aboutBox.setSize(icon.getIconWidth(), icon.getIconHeight() + 20);
80
81                 ScilabSwingUtilities.closeOnEscape(aboutBox);
82                 ScilabSwingUtilities.closeOnEscape(ackBox);
83
84                 ImagePanel text = new ImagePanel();
85
86                 String theText = new String("\n\n\n\n\n\n");
87                 for (int i = 0; i < contents.length; i++) {
88                         theText += "\n\t" + contents[i];
89                 }
90
91                 text.setText(theText);
92
93                 text.setOpaque(false);
94                 text.setEditable(false);
95                 text.setSize(icon.getIconWidth(), icon.getIconHeight());
96                 text.setVisible(true);
97
98                 aboutBox.setContentPane(text);
99                 aboutBox.setResizable(false);
100
101                 JButton close = new JButton(Messages.gettext("Close"));
102                 close.addActionListener(new ActionListener() {
103                         public void actionPerformed(ActionEvent event) {
104                                 aboutBox.dispose();
105                                 ackBox.dispose();
106                         }
107                 });
108                 aboutBox.add(close, 0);
109                 close.setSize(80, 30);
110                 close.setLocation(text.getWidth() - 90, text.getHeight() - 40);
111
112                 if (ackFile != null && ackTitle != null) {
113                 JButton acknowledgements = new JButton(ackTitle);
114                 final String filename = ackFile;
115                 final String finalAckTitle = ackTitle;
116                 acknowledgements.addActionListener(new ActionListener() {
117                         public void actionPerformed(ActionEvent event) {
118
119                                 StringBuilder contents = new StringBuilder();
120                             try {
121                               BufferedReader input = new BufferedReader(new InputStreamReader(new FileInputStream(filename), "UTF8"));
122                               try {
123                                 String line = null;
124                                 while ((line = input.readLine()) != null) {
125                                   contents.append(line);
126                                   contents.append(System.getProperty("line.separator"));
127                                 }
128                               } finally {
129                                 input.close();
130                               }
131                             } catch (IOException ex) {
132                               ex.printStackTrace();
133                             }
134
135                                 ackBox.setTitle(finalAckTitle);
136
137                                 ackBox.setLayout(null);
138                                 ackBox.setAlwaysOnTop(true);
139                                 ackBox.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
140                                 ackBox.setLocationRelativeTo(null);
141                                 ackBox.setSize(600, 400);
142
143                                 JTextPane text = new JTextPane();
144                                 text.setText(contents.toString());
145                                 text.setCaretPosition(0);
146
147                                 JScrollPane pane = new JScrollPane(text);
148                                 pane.setSize(600, 400);
149                                 ackBox.setContentPane(pane);
150
151                                 text.setEditable(false);
152
153                                 ackBox.setLocation((int) Toolkit.getDefaultToolkit().getScreenSize().getWidth() / 2 - (aboutBox.getWidth() / 2),
154                                                 (int) Toolkit.getDefaultToolkit().getScreenSize().getHeight() / 2 - (aboutBox.getHeight() / 2));
155                                 ackBox.setVisible(true);
156                         }
157                 });
158                 aboutBox.add(acknowledgements, 0);
159                 acknowledgements.setSize(120, 30);
160                 acknowledgements.setLocation(10, text.getHeight() - 40);
161                 }
162
163                 aboutBox.setLocation((int) Toolkit.getDefaultToolkit().getScreenSize().getWidth() / 2 - (aboutBox.getWidth() / 2),
164                                 (int) Toolkit.getDefaultToolkit().getScreenSize().getHeight() / 2 - (aboutBox.getHeight() / 2));
165                 aboutBox.setVisible(true);
166
167         }
168
169 }
170
171 final class ImagePanel extends JTextPane {
172
173         private static final long serialVersionUID = -4896563054481112056L;
174
175         Image background = new ImageIcon(System.getenv("SCI") + "/modules/gui/images/icons/aboutscilab.png").getImage();
176
177         public void paintComponent(Graphics g) {
178                 g.drawImage(background, 0, 0, (int)getSize().getWidth(), (int)getSize().getHeight(), this);
179                 super.paintComponent(g);
180
181         }
182 }