ArduinoAbout.java

/*******************************************************************************
 * jArduino: Arduino C++ Code Generation From Java
 * Copyright 2020 Tony Washer
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package net.sourceforge.jarduino.gui;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dialog.ModalityType;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.Border;

import net.sourceforge.jarduino.ArduinoException;

/**
 * Swing About Box.
 */
public class ArduinoAbout {
    /**
     * The Strut size.
     */
    private static final int STRUT_SIZE = 8;

    /**
     * The Inset size.
     */
    private static final int INSET_SIZE = 5;

    /**
     * APP Key.
     */
    private static final String PFX_APP = "App.";

    /**
     * Name Key.
     */
    private static final String KEY_NAME = PFX_APP + "name";

    /**
     * Version Key.
     */
    private static final String KEY_VERSION = PFX_APP + "version";

    /**
     * Revision Key.
     */
    private static final String KEY_REVISION = PFX_APP + "revision";

    /**
     * Copyright Key.
     */
    private static final String KEY_COPYRIGHT = PFX_APP + "copyright";

    /**
     * TimeStamp Key.
     */
    private static final String KEY_BUILTON = PFX_APP + "timeStamp";
    /**
     * The frame.
     */
    private final JFrame theFrame;

    /**
     * The panel.
     */
    private final JPanel thePanel;

    /**
     * The dialog.
     */
    private JDialog theDialog;

    /**
     * Constructor.
     * @param pFrame the frame
     */
    ArduinoAbout(final JFrame pFrame) throws ArduinoException {
        /* Store parameters */
        theFrame = pFrame;

        /* Build the OK button */
        final JButton myOKButton = new JButton();
        myOKButton.setText("OK");
        myOKButton.addActionListener(e -> closeDialog());

        /* Create the components */
        final Map<String, String> myMap = loadProperties();
        final JLabel myProduct = new JLabel(myMap.get(KEY_NAME));
        final JLabel myVersion = new JLabel("Version: " + myMap.get(KEY_VERSION));
        final JLabel myRevision = new JLabel("Revision: " + myMap.get(KEY_REVISION));
        final JLabel myBuild = new JLabel("BuiltOn: " + myMap.get(KEY_BUILTON));
        final JLabel myCopyright = new JLabel(myMap.get(KEY_COPYRIGHT));
        final JLabel myImages = new JLabel("Icons by mysitemyway.com");

        /* Layout the panel */
        thePanel = new JPanel();
        thePanel.setLayout(new BoxLayout(thePanel, BoxLayout.Y_AXIS));

        /* Add the components */
        thePanel.add(createContainer(myProduct));
        thePanel.add(createContainer(myCopyright));
        thePanel.add(Box.createVerticalStrut(STRUT_SIZE));
        thePanel.add(createContainer(myVersion));
        thePanel.add(createContainer(myRevision));
        thePanel.add(createContainer(myBuild));
        thePanel.add(Box.createVerticalStrut(STRUT_SIZE));
        thePanel.add(createContainer(myImages));
        thePanel.add(Box.createVerticalStrut(STRUT_SIZE));
        thePanel.add(createContainer(myOKButton));
    }

    /**
     * Create container panel.
     * @param pNode the panel
     * @return the container panel
     */
    private static JPanel createContainer(final JComponent pNode) {
        final JPanel myPanel = new JPanel();
        myPanel.setLayout(new BoxLayout(myPanel, BoxLayout.Y_AXIS));
        pNode.setAlignmentX(Component.CENTER_ALIGNMENT);
        myPanel.add(pNode);
        return myPanel;
    }

    /**
     * load properties.
     * @return the properties map.
     * @throws ArduinoException on error
     */
    private static Map<String, String> loadProperties() throws ArduinoException {
        /* Create the maps */
        final Map<String, String> myMap = new HashMap<>();

        /* Protect against exceptions */
        try (InputStream myInput = ArduinoAbout.class.getResourceAsStream("jArduino.properties")) {
            /* Load the properties */
            final Properties myProperties = new Properties();
            myProperties.load(myInput);

            /* Load property names into map */
            for (String myName : myProperties.stringPropertyNames()) {
               myMap.put(myName, myProperties.getProperty(myName));
            }

            /* Return the map */
            return myMap;

            /* Catch Exceptions */
        } catch (IOException e) {
            throw new ArduinoException("Failed to load properties", e);
        }
    }

    /**
     * Show the dialog.
     */
    public void showDialog() {
        /* If we have not made the dialog yet */
        if (theDialog == null) {
            makeDialog();
        }

        /* Show the dialog */
        theDialog.setVisible(true);
    }

    /**
     * Make the dialog.
     */
    private void makeDialog() {
        /* Create the dialog */
        theDialog = new JDialog(theFrame);
        theDialog.setUndecorated(true);
        theDialog.setModalityType(ModalityType.APPLICATION_MODAL);

        /* Create a border */
        final Border myPadded = BorderFactory.createEmptyBorder(INSET_SIZE, INSET_SIZE, INSET_SIZE, INSET_SIZE);
        final Border myLine = BorderFactory.createLineBorder(Color.BLACK, 2);
        thePanel.setBorder(BorderFactory.createCompoundBorder(myLine, myPadded));

        /* Attach the node to the dialog */
        theDialog.getContentPane().add(thePanel);
        theDialog.pack();
        theDialog.setLocationRelativeTo(theFrame);
    }

    /**
     * Close the dialog.
     */
    private void closeDialog() {
        theDialog.setVisible(false);
    }
}