Package pdfdb.gui.frames

Source Code of pdfdb.gui.frames.MainFrame

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package pdfdb.gui.frames;

import pdfdb.gui.panels.LoadingPanel;
import pdfdb.gui.panels.MainPanel;
import java.io.IOException;
import pdfdb.app.*;
import java.awt.*;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.sql.SQLException;
import pdfdb.settings.UserSettingsManager;

/** The main GUI frame class.
* @author ug22cmg */
public class MainFrame extends JFrame
{

    private static final String KEY_HEIGHT = "HEIGHT";
    private static final String KEY_WIDTH = "WIDTH";
    private static final String KEY_X = "X";
    private static final String KEY_Y = "Y";
    private MainPanel panel = null;
    private MenuBar menu = new MenuBar();

    /** Initializes the frame
     * @param connected Whether to immediately connect to the data pipeline.
     * @throws java.sql.SQLException If an error occurs. */
    public MainFrame(boolean connected) throws SQLException
    {
        init(connected);
    }

    /** Initializes the frame.
     * @param connected Whether to connect to the data pipeline.
     * @throws java.sql.SQLException If an error occured. */
    private void init(boolean connected) throws SQLException
    {
        this.panel = new MainPanel(connected);
        this.initSize();
        super.setLayout(new BorderLayout());
        super.add(this.panel, BorderLayout.CENTER);
        super.setTitle("ADRS");
        super.addWindowListener(EventController.getWindowListener());
        super.setMenuBar(this.menu);
        super.pack();
    }

    /** Gets the current main-frame. Used for getting the parent frame when
     * the program is running.
     * @return The MainFrame instance or null. */
    public synchronized static MainFrame getMainFrame()
    {
        Frame[] frames = Frame.getFrames();
        if (frames == null) return null;
        for (Frame f : frames)
        {
            if (f instanceof MainFrame)
            {
                return (MainFrame) f;
            }
        }
        return null;
    }

    /** Connects the frame to the data pipeline.
     * @throws java.sql.SQLException If an error occurs. */
    public void connect() throws SQLException
    {
        if (!panel.isConnected()) panel.connect();
    }

    /** Initializes the size of the frame from configuration settings. */
    private void initSize()
    {
        UserSettingsManager manager = UserSettingsManager.getInstance();
        String sHeight = manager.get(KEY_HEIGHT);
        String sWidth = manager.get(KEY_WIDTH);
        String sX = manager.get(KEY_X);
        String sY = manager.get(KEY_Y);
        Dimension dim = null;
        Point p = null;
        boolean error = false;

        if (sHeight == null || sWidth == null || sX == null || sY == null)
        {
            error = true;
        }
        else
        {
            try
            {
                int height = Integer.parseInt(sHeight);
                int width = Integer.parseInt(sWidth);
                int x = Integer.parseInt(sX);
                int y = Integer.parseInt(sY);
                p = new Point(x, y);
                dim = new Dimension(width, height);
            }
            catch (Exception e)
            {
                error = true;
            }
        }

        if (error)
        {
            dim = new Dimension(750, 600);
            p = new Point(10, 10);
        }
        super.setSize(dim);
        super.setPreferredSize(dim);
        super.setLocation(p);
    }

    /** Initializes the menu */
    private void initMenu()
    {
        Menu fileMenu = new Menu("File");
        Menu toolsMenu = new Menu("Tools");
        Menu helpMenu = new Menu("Help");
        ActionListener a = EventController.getActionListener();

        String cmd1 = String.valueOf(EventController.OP_MENU_EXIT);
        String cmd3 = String.valueOf(EventController.OP_PREFS_OPEN);
        String cmd4 = String.valueOf(EventController.OP_MENU_HELP_MAIN);
        String cmd5 = String.valueOf(EventController.OP_MENU_HELP_DEV);
        String cmd6 = String.valueOf(EventController.OP_MENU_ABOUT);

        // Action listeners
        fileMenu.addActionListener(a);
        toolsMenu.addActionListener(a);
        helpMenu.addActionListener(a);

        // Items
        MenuItem exitItem = new MenuItem("Exit");
        MenuItem prefsItem = new MenuItem("Preferences");
        MenuItem helpItem = new MenuItem("Online Help");
        MenuItem devHelpItem = new MenuItem("Developer Help");
        MenuItem aboutHelpItem = new MenuItem("About");

        // Command for the action listeners

        exitItem.setActionCommand(cmd1);
        prefsItem.setActionCommand(cmd3);
        helpItem.setActionCommand(cmd4);
        devHelpItem.setActionCommand(cmd5);
        aboutHelpItem.setActionCommand(cmd6);

        // Add to menus
        fileMenu.add(exitItem);
        toolsMenu.add(prefsItem);
        helpMenu.add(helpItem);
        helpMenu.add(devHelpItem);
        helpMenu.add(aboutHelpItem);

        // Add submenus to main menu
        this.menu.add(fileMenu);
        this.menu.add(toolsMenu);
        this.menu.add(helpMenu);
        this.menu.setHelpMenu(helpMenu);
    }

    /** Shows or hide's the loading glass pane.
     * @param visible The new visibility of the loading pane. */
    public void setLoadingIsVisible(boolean visible)
    {
        if (visible)
        {
            this.setGlassPane(new LoadingPanel());
            this.getGlassPane().setVisible(true);
        }
        else
        {
            this.getGlassPane().setVisible(false);
            this.initMenu();
        }
    }

    /** Shuts down the panel when notified of a shutdown */
    public void shutdown()
    {
        try
        {
            UserSettingsManager manager = UserSettingsManager.getInstance();
            Dimension dim = this.getSize();
            String width = String.valueOf(dim.width);
            String height = String.valueOf(dim.height);
            String x = String.valueOf(getX());
            String y = String.valueOf(getY());
            manager.set(KEY_WIDTH, width);
            manager.set(KEY_HEIGHT, height);
            manager.set(KEY_X, x);
            manager.set(KEY_Y, y);
            manager.save();
        }
        catch (IOException ex)
        {
            ex.printStackTrace();
        }
    }
}
TOP

Related Classes of pdfdb.gui.frames.MainFrame

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.