Package xplanetconfigurator.gui

Source Code of xplanetconfigurator.gui.JDialogAbout

package xplanetconfigurator.gui;

import javax.swing.*;


import java.awt.*;
import java.awt.event.*;
import java.io.*;
import xplanetconfigurator.util.OwnPreferences;

/**
* @author @author@
*
* To change this generated comment edit the template variable "typecomment":
* Window>Preferences>Java>Templates.
* To enable and disable the creation of type comments go to
* Window>Preferences>Java>Code Generation.
*/
public class JDialogAbout extends JDialog {

    private static String FRAME_X = "dialog_about_x";
    private static String FRAME_Y = "dialog_about_y";
    private static String FRAME_HEIGHT = "dialog_about_h";
    private static String FRAME_WITH = "dialog_about_w";
    private JButton jButtonOK;
    private JScrollPane jScrollPaneMain;
    private JEditorPane jEditorPane;
    private JPanel jPanelMain;
    private String url;

    public JDialogAbout(JFrame parent, String url) {
        super(parent, true);
        this.url = url;
        this.initGui();
        this.loadUserPrefs();
        Rectangle r = new Rectangle(0, 0, 1, 1);
        this.jPanelMain.scrollRectToVisible(r);
    }

    public JDialogAbout(String url) {
        this.url = url;
        this.initGui();
        this.loadUserPrefs();
        Rectangle r = new Rectangle(0, 0, 1, 1);
        this.jPanelMain.scrollRectToVisible(r);
    }

    private void initGui() {
        this.setTitle("About");
        this.setSize(new Dimension(500, 300));
        this.addWindowListener(new UtilWindowClosingAdapter());
        this.getRootPane().setDefaultButton(this.jButtonOK);

        this.jButtonOK = new JButton("OK");
        jButtonOK.setMnemonic('O');

        this.jPanelMain = new JPanel();
        this.jEditorPane = new JEditorPane();
        this.jEditorPane.setContentType("text/html");
        this.loadText();
        this.jEditorPane.setCaretPosition(0);
        this.jEditorPane.setEditable(false);

        this.jScrollPaneMain = new JScrollPane(this.jEditorPane);

        GridBagLayout gridbagMainPanel = new GridBagLayout();
        jPanelMain.setLayout(gridbagMainPanel);

        GridBagConstraints c = new GridBagConstraints();
        c.fill = GridBagConstraints.BOTH;
        c.gridx = 0;
        c.gridy = 0;
        c.weightx = 1.0;
        c.weighty = 1.0;
        c.insets = new Insets(0, 0, 0, 0)// int top, int left, int bottom, int right
        gridbagMainPanel.setConstraints(this.jScrollPaneMain, c);
        jPanelMain.add(jScrollPaneMain);

        c = new GridBagConstraints();
        c.anchor = GridBagConstraints.SOUTHEAST;
        c.gridx = 0;
        c.gridy = 1;
        c.insets = new Insets(11, 12, 12, 12)// int top, int left, int bottom, int right
        gridbagMainPanel.setConstraints(this.jButtonOK, c);
        jPanelMain.add(jButtonOK);

        this.getContentPane().add(this.jPanelMain, BorderLayout.CENTER);

        this.jButtonOK.addActionListener(
                new ActionListenerOK());

        // this.setLocationRelativeTo(null); //center it
        // this.setVisible(true);
    }

    private void loadText() {
        StringBuffer buf = new StringBuffer();
        BufferedReader in = null;
        try {
            in = new BufferedReader(
                    new InputStreamReader(this.getClass().getResourceAsStream(url)));
            String s = null;
            while ((s = in.readLine()) != null) {
                buf.append(s).append("\n");
            }
        } catch (Exception e) {
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (Exception eio) {
                }
            }
        }
        this.jEditorPane.setText(buf.toString());
    }

    private void storeUserPrefs() {
        int x = this.getX();
        int y = this.getY();
        Dimension d = this.getSize();
        int h = (int) d.getHeight();
        int w = (int) d.getWidth();

        OwnPreferences prefs =
                OwnPreferences.userNodeForPackage(this.getClass());

        prefs.putInt(FRAME_X, x);
        prefs.putInt(FRAME_Y, y);
        prefs.putInt(FRAME_HEIGHT, h);
        prefs.putInt(FRAME_WITH, w);
    }

    private void loadUserPrefs() {
        OwnPreferences prefs =
                OwnPreferences.userNodeForPackage(this.getClass());

        int x = prefs.getInt(FRAME_X, 10);
        int y = prefs.getInt(FRAME_Y, 10);
        int h = prefs.getInt(FRAME_HEIGHT, 600);
        int w = prefs.getInt(FRAME_WITH, 500);

        this.setSize(new Dimension(w, h));
        // System.out.println("Start info = " + info);
        if (x == 10 && y == 10) // First use
        {
            // System.out.println("Start info in if() = " + info);
            this.setLocationRelativeTo(null); //center it
        } else {
            this.setLocation(x, y);
        }
    }

    private void closeMySelf() {
        this.storeUserPrefs();
        this.setVisible(false);
        this.dispose();
    }

    class UtilWindowClosingAdapter extends WindowAdapter {

        @Override
        public void windowClosing(WindowEvent e) {
            closeMySelf();
        }
    }

    class ActionListenerOK implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            closeMySelf();
        }
    }
}
TOP

Related Classes of xplanetconfigurator.gui.JDialogAbout

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.