1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package net.sourceforge.jarduino.gui;
18
19 import java.awt.Color;
20 import java.awt.Component;
21 import java.awt.Dialog.ModalityType;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.util.HashMap;
25 import java.util.Map;
26 import java.util.Properties;
27 import javax.swing.BorderFactory;
28 import javax.swing.Box;
29 import javax.swing.BoxLayout;
30 import javax.swing.JButton;
31 import javax.swing.JComponent;
32 import javax.swing.JDialog;
33 import javax.swing.JFrame;
34 import javax.swing.JLabel;
35 import javax.swing.JPanel;
36 import javax.swing.border.Border;
37
38 import net.sourceforge.jarduino.ArduinoException;
39
40
41
42
43 public class ArduinoAbout {
44
45
46
47 private static final int STRUT_SIZE = 8;
48
49
50
51
52 private static final int INSET_SIZE = 5;
53
54
55
56
57 private static final String PFX_APP = "App.";
58
59
60
61
62 private static final String KEY_NAME = PFX_APP + "name";
63
64
65
66
67 private static final String KEY_VERSION = PFX_APP + "version";
68
69
70
71
72 private static final String KEY_REVISION = PFX_APP + "revision";
73
74
75
76
77 private static final String KEY_COPYRIGHT = PFX_APP + "copyright";
78
79
80
81
82 private static final String KEY_BUILTON = PFX_APP + "timeStamp";
83
84
85
86 private final JFrame theFrame;
87
88
89
90
91 private final JPanel thePanel;
92
93
94
95
96 private JDialog theDialog;
97
98
99
100
101
102 ArduinoAbout(final JFrame pFrame) throws ArduinoException {
103
104 theFrame = pFrame;
105
106
107 final JButton myOKButton = new JButton();
108 myOKButton.setText("OK");
109 myOKButton.addActionListener(e -> closeDialog());
110
111
112 final Map<String, String> myMap = loadProperties();
113 final JLabel myProduct = new JLabel(myMap.get(KEY_NAME));
114 final JLabel myVersion = new JLabel("Version: " + myMap.get(KEY_VERSION));
115 final JLabel myRevision = new JLabel("Revision: " + myMap.get(KEY_REVISION));
116 final JLabel myBuild = new JLabel("BuiltOn: " + myMap.get(KEY_BUILTON));
117 final JLabel myCopyright = new JLabel(myMap.get(KEY_COPYRIGHT));
118 final JLabel myImages = new JLabel("Icons by mysitemyway.com");
119
120
121 thePanel = new JPanel();
122 thePanel.setLayout(new BoxLayout(thePanel, BoxLayout.Y_AXIS));
123
124
125 thePanel.add(createContainer(myProduct));
126 thePanel.add(createContainer(myCopyright));
127 thePanel.add(Box.createVerticalStrut(STRUT_SIZE));
128 thePanel.add(createContainer(myVersion));
129 thePanel.add(createContainer(myRevision));
130 thePanel.add(createContainer(myBuild));
131 thePanel.add(Box.createVerticalStrut(STRUT_SIZE));
132 thePanel.add(createContainer(myImages));
133 thePanel.add(Box.createVerticalStrut(STRUT_SIZE));
134 thePanel.add(createContainer(myOKButton));
135 }
136
137
138
139
140
141
142 private static JPanel createContainer(final JComponent pNode) {
143 final JPanel myPanel = new JPanel();
144 myPanel.setLayout(new BoxLayout(myPanel, BoxLayout.Y_AXIS));
145 pNode.setAlignmentX(Component.CENTER_ALIGNMENT);
146 myPanel.add(pNode);
147 return myPanel;
148 }
149
150
151
152
153
154
155 private static Map<String, String> loadProperties() throws ArduinoException {
156
157 final Map<String, String> myMap = new HashMap<>();
158
159
160 try (InputStream myInput = ArduinoAbout.class.getResourceAsStream("jArduino.properties")) {
161
162 final Properties myProperties = new Properties();
163 myProperties.load(myInput);
164
165
166 for (String myName : myProperties.stringPropertyNames()) {
167 myMap.put(myName, myProperties.getProperty(myName));
168 }
169
170
171 return myMap;
172
173
174 } catch (IOException e) {
175 throw new ArduinoException("Failed to load properties", e);
176 }
177 }
178
179
180
181
182 public void showDialog() {
183
184 if (theDialog == null) {
185 makeDialog();
186 }
187
188
189 theDialog.setVisible(true);
190 }
191
192
193
194
195 private void makeDialog() {
196
197 theDialog = new JDialog(theFrame);
198 theDialog.setUndecorated(true);
199 theDialog.setModalityType(ModalityType.APPLICATION_MODAL);
200
201
202 final Border myPadded = BorderFactory.createEmptyBorder(INSET_SIZE, INSET_SIZE, INSET_SIZE, INSET_SIZE);
203 final Border myLine = BorderFactory.createLineBorder(Color.BLACK, 2);
204 thePanel.setBorder(BorderFactory.createCompoundBorder(myLine, myPadded));
205
206
207 theDialog.getContentPane().add(thePanel);
208 theDialog.pack();
209 theDialog.setLocationRelativeTo(theFrame);
210 }
211
212
213
214
215 private void closeDialog() {
216 theDialog.setVisible(false);
217 }
218 }