View Javadoc
1   /*******************************************************************************
2    * jArduino: Arduino C++ Code Generation From Java
3    * Copyright 2020 Tony Washer
4    *
5    * Licensed under the Apache License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    *   http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * Swing About Box.
42   */
43  public class ArduinoAbout {
44      /**
45       * The Strut size.
46       */
47      private static final int STRUT_SIZE = 8;
48  
49      /**
50       * The Inset size.
51       */
52      private static final int INSET_SIZE = 5;
53  
54      /**
55       * APP Key.
56       */
57      private static final String PFX_APP = "App.";
58  
59      /**
60       * Name Key.
61       */
62      private static final String KEY_NAME = PFX_APP + "name";
63  
64      /**
65       * Version Key.
66       */
67      private static final String KEY_VERSION = PFX_APP + "version";
68  
69      /**
70       * Revision Key.
71       */
72      private static final String KEY_REVISION = PFX_APP + "revision";
73  
74      /**
75       * Copyright Key.
76       */
77      private static final String KEY_COPYRIGHT = PFX_APP + "copyright";
78  
79      /**
80       * TimeStamp Key.
81       */
82      private static final String KEY_BUILTON = PFX_APP + "timeStamp";
83      /**
84       * The frame.
85       */
86      private final JFrame theFrame;
87  
88      /**
89       * The panel.
90       */
91      private final JPanel thePanel;
92  
93      /**
94       * The dialog.
95       */
96      private JDialog theDialog;
97  
98      /**
99       * Constructor.
100      * @param pFrame the frame
101      */
102     ArduinoAbout(final JFrame pFrame) throws ArduinoException {
103         /* Store parameters */
104         theFrame = pFrame;
105 
106         /* Build the OK button */
107         final JButton myOKButton = new JButton();
108         myOKButton.setText("OK");
109         myOKButton.addActionListener(e -> closeDialog());
110 
111         /* Create the components */
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         /* Layout the panel */
121         thePanel = new JPanel();
122         thePanel.setLayout(new BoxLayout(thePanel, BoxLayout.Y_AXIS));
123 
124         /* Add the components */
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      * Create container panel.
139      * @param pNode the panel
140      * @return the container panel
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      * load properties.
152      * @return the properties map.
153      * @throws ArduinoException on error
154      */
155     private static Map<String, String> loadProperties() throws ArduinoException {
156         /* Create the maps */
157         final Map<String, String> myMap = new HashMap<>();
158 
159         /* Protect against exceptions */
160         try (InputStream myInput = ArduinoAbout.class.getResourceAsStream("jArduino.properties")) {
161             /* Load the properties */
162             final Properties myProperties = new Properties();
163             myProperties.load(myInput);
164 
165             /* Load property names into map */
166             for (String myName : myProperties.stringPropertyNames()) {
167                myMap.put(myName, myProperties.getProperty(myName));
168             }
169 
170             /* Return the map */
171             return myMap;
172 
173             /* Catch Exceptions */
174         } catch (IOException e) {
175             throw new ArduinoException("Failed to load properties", e);
176         }
177     }
178 
179     /**
180      * Show the dialog.
181      */
182     public void showDialog() {
183         /* If we have not made the dialog yet */
184         if (theDialog == null) {
185             makeDialog();
186         }
187 
188         /* Show the dialog */
189         theDialog.setVisible(true);
190     }
191 
192     /**
193      * Make the dialog.
194      */
195     private void makeDialog() {
196         /* Create the dialog */
197         theDialog = new JDialog(theFrame);
198         theDialog.setUndecorated(true);
199         theDialog.setModalityType(ModalityType.APPLICATION_MODAL);
200 
201         /* Create a border */
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         /* Attach the node to the dialog */
207         theDialog.getContentPane().add(thePanel);
208         theDialog.pack();
209         theDialog.setLocationRelativeTo(theFrame);
210     }
211 
212     /**
213      * Close the dialog.
214      */
215     private void closeDialog() {
216         theDialog.setVisible(false);
217     }
218 }