Package cz.mp.k3bg.gui

Source Code of cz.mp.k3bg.gui.GenerateColophonDialog

/*
* GenerateColophonDialog.java
*
*  created: 2.11.2011
*  charset: UTF-8
*  license: MIT (X11) (See LICENSE file for full license)
*/
package cz.mp.k3bg.gui;

import cz.mp.k3bg.log.LoggerManager;
import java.io.IOException;
import java.util.logging.Logger;
import cz.mp.k3bg.BookState;
import cz.mp.util.GuiUtils;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import net.miginfocom.swing.MigLayout;
import cz.mp.k3bg.Images;
import cz.mp.k3bg.core.BookFiles;
import cz.mp.k3bg.core.ColophonGenerator;
import cz.mp.k3bg.core.MimeType;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import static cz.mp.k3bg.Images.*;
import static cz.mp.k3bg.TextSource.*;

/**
* Dialog pro generování krátké tiráže.
*
* @author Martin Pokorný
* @version 0.1
* @see ColophonGenerator
*/
public class GenerateColophonDialog extends GenerateDialog {

    private static final boolean DEBUG = false;
    private static final Logger logger =
            LoggerManager.getLogger(GenerateColophonDialog.class, DEBUG);

    private static GenerateColophonDialog instance = null;

    private JLabel fileNameLabel = new JLabel(getLocText("gui.bookfiles.generate.file_name"));
    private JTextField fileNameField = new JTextField();

    private JLabel colophonLabel = new JLabel(getLocText("gui.bookfiles.generate.colophon"));
    private JEditorPane colophonArea = new JEditorPane();
    private JScrollPane colophonScrollArea = new JScrollPane(colophonArea);

    private JButton generateButton = new JButton(
            getLocText("gui.bookfiles.generate.generate"), Images.getImage(OK));
    private JButton cancelButton = new JButton(
            getLocText("gui.cancel"), Images.getImage(CANCEL));

    private ColophonGenerator colophonGen;

    // -----

    /**
     *
     */
    private GenerateColophonDialog() {
        super(MainFrame.getInstance());
        initComponents();
        initLayout();
        initEventHandlers();
        initDialog();
    }

    /**
     *
     * @return
     */
    private static GenerateColophonDialog getInstance() {
        if (instance == null) {
            instance = new GenerateColophonDialog();
        }
        return instance;
    }


    /**
     *
     * @param bookFiles
     */
    public static String showDialog(BookFiles bookFiles)  {
        getInstance();

        return instance.showThisDialog(bookFiles);
    }

    /**
     *
     * @param bookFiles
     */
    private String showThisDialog(BookFiles bookFiles) {
        generatedFileName = null;
        colophonArea.setText("");
        this.bookFiles = bookFiles;

        if (bookFiles != null &&
                BookState.getMainInstance().getMetadata() != null) {
            colophonGen = new ColophonGenerator(
                    bookFiles,
                    BookState.getMainInstance().getMetadata());
            colophonArea.setText(colophonGen.getTextOfCopyright());

            setVisible(true);
        }

        return generatedFileName;
    }

    /**
     *
     */
    private void initDialog() {
        setTitle(getLocText("gui.bookfiles.generate.colophon.title"));

        pack();
        setMinimumSize(new Dimension(400, getHeight()));
        setSize(MainFrame.getInstance().getWidth() - 60, MainFrame.getInstance().getHeight() - 150);

        setLocationRelativeTo(MainFrame.getInstance());

        GuiUtils.addHideActionWithEscapeKey(this);
    }

    /**
     *
     */
    private void initComponents() {
        fileNameLabel.setLabelFor(fileNameField);
        colophonLabel.setLabelFor(colophonArea);

        GuiUtils.adjustBackground(colophonArea);
        colophonArea.setContentType(MimeType.TXT.getValue());
        colophonScrollArea.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        colophonScrollArea.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

        fileNameField.setEditable(false);
        fileNameField.setText(BookFiles.DEFAULT_COLOPHON_FILENAME);

        getRootPane().setDefaultButton(generateButton);
    }

    /**
     *
     */
    private void initLayout() {
        this.setLayout(new MigLayout("",
                "unrel[label]rel[fill,grow]unrel",
                "unrel[]para[]rel[fill,grow]para[bottom,nogrid]unrel"));

        this.add(fileNameLabel);
        this.add(fileNameField, "wrap");

        this.add(colophonLabel, "wrap");
        this.add(colophonScrollArea, "spanx 2, growx, wrap");

        this.add(generateButton, "tag ok, sg");
        this.add(cancelButton, "tag cancel, sg");
    }

    /**
     *
     */
    private void initEventHandlers() {
        generateButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                generate();

                if (generatedFileName != null) {
                    setVisible(false);
                }
            }
        });

        cancelButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                setVisible(false);
            }
        });

        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                setVisible(false);
            }
        });
    }

    @Override
    protected void generate() {
        if (colophonGen == null) {
            throw new IllegalStateException("colophonGen = null");
        }

        try {
            bookFiles.setDefaultColophon();

            colophonGen.setTextOfCopyright(colophonArea.getText());

            colophonGen.generate();

            generatedFileName = bookFiles.getColophon().getPath();
            // ----

            generateBaseStyle();

        } catch (IOException ex) {
            logger.warning(ex.toString());
            if (DEBUG) {  ex.printStackTrace()}
            Dialogs.showErrorDialog(ex);
        }
    }

}   // GenerateColophonDialog.java
TOP

Related Classes of cz.mp.k3bg.gui.GenerateColophonDialog

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.