mergedoc.ui.MergeDocFrame.java Source code

Java tutorial

Introduction

Here is the source code for mergedoc.ui.MergeDocFrame.java

Source

/*
 * Copyright (c) 2003- Shinji Kashihara. All rights reserved.
 * This program are made available under the terms of the Common Public License
 * v1.0 which accompanies this distribution, and is available at cpl-v10.html.
 */
package mergedoc.ui;

import java.awt.Frame;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.BevelBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import mergedoc.MergeDocException;
import mergedoc.core.MergeManager;
import mergedoc.core.Preference;
import mergedoc.core.WorkingState;
import mergedoc.xml.ConfigManager;
import mergedoc.xml.Persister;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * ??????
 * @author Shinji Kashihara
 */
public class MergeDocFrame extends JFrame {

    /**  */
    private static final Log log = LogFactory.getLog(MergeDocFrame.class);

    /** ?? */
    private static final String SETTING_TITLE = "";

    /** ? */
    private JPanel mainPanel = new JPanel();

    /** ? */
    private PreferencePanel preferencePanel;

    /** ?? */
    private ProgressPanel progressPanel = new ProgressPanel();

    /** ? */
    private ButtonBar buttonBar = new ButtonBar();

    /**  */
    private MessageDialog dialog = new MessageDialog(this);

    /** ? */
    private MergeManager mergeManager;

    /**  Executor */
    private ExecutorService mergeExecutor = Executors.newSingleThreadExecutor();

    /**
     * ??
     */
    public MergeDocFrame() {
        try {
            initComponent();
        } catch (Exception e) {
            dialog.showErrorMessage(e.getMessage());
            System.exit(0);
        }
    }

    /**
     * ??????
     * @throws MergeDocException ?????
     */
    private void initComponent() throws MergeDocException {

        // ????
        preferencePanel = new PreferencePanel();
        mergeManager = new MergeManager();

        // ???
        mainPanel.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
        mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.X_AXIS));
        mainPanel.add(preferencePanel);

        // ??????
        JPanel outerPanel = new JPanel();
        outerPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        outerPanel.setLayout(new BoxLayout(outerPanel, BoxLayout.Y_AXIS));
        outerPanel.add(mainPanel);
        outerPanel.add(ComponentFactory.createSpacer(0, 7));
        outerPanel.add(buttonBar);
        getContentPane().add(outerPanel);

        // ?
        initListener();
        buttonBar.setState(buttonBar.INIT_STATE);

        // 
        ConfigManager config = ConfigManager.getInstance();
        String iconPath = config.getFile("icon.png").toString();
        Image icon = Toolkit.getDefaultToolkit().createImage(iconPath);
        setIconImage(icon);

        // 
        setTitle(SETTING_TITLE);
        Persister psst = Persister.getInstance();
        setLocation(psst.getInt(Persister.WINDOW_X, 0), psst.getInt(Persister.WINDOW_Y, 0));
        setSize(psst.getInt(Persister.WINDOW_WIDTH, 700), psst.getInt(Persister.WINDOW_HEIGHT, 570));
        int state = psst.getInt(Persister.WINDOW_STATE, NORMAL);
        if ((state & Frame.ICONIFIED) != ICONIFIED) {
            setExtendedState(state);
        }
        setVisible(true);
    }

    /**
     * ????
     */
    private void initListener() {

        // ?
        buttonBar.setRunListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                mergeExecutor.submit(new Runnable() {
                    @Override
                    public void run() {
                        execute();
                    }
                });
            }
        });

        // ?
        buttonBar.setCancelListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                buttonBar.setEnabled(false);
                mergeManager.getWorkingState().cancel();
            }
        });

        // ?
        buttonBar.setBackListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                back();
            }
        });

        // ?
        buttonBar.setEndListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                end();
            }
        });

        // ??
        mergeManager.setChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                WorkingState state = mergeManager.getWorkingState();
                progressPanel.addListText(state.getWorkingText());
                setTitle(progressPanel.getBarText());
            }
        });

        // ?
        addComponentListener(new ComponentAdapter() {
            @Override
            public void componentResized(ComponentEvent e) {
                persistent();
            }
        });

        // ?
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                end();
            }
        });
    }

    /**
     * ???
     */
    public void execute() {

        buttonBar.setEnabled(false);
        Preference pref = preferencePanel.getPreference();
        mergeManager.setPreference(pref);

        // ??
        if (isCancelByConfirm(pref)) {
            buttonBar.setEnabled(true);
            return;
        }

        // ??
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                mainPanel.remove(preferencePanel);
                progressPanel.init();
                mainPanel.add(progressPanel);
                progressPanel.repaint();
            }
        });
        buttonBar.setState(buttonBar.WORKING_STATE);

        // ?
        try {
            mergeManager.validate();
            progressPanel.setMaximum(mergeManager.entrySize());
        } catch (MergeDocException e) {
            dialog.showErrorMessage(e.getMessage());
            back();
            return;
        } catch (Exception e) {
            String msg = "???????";
            log.error(msg, e);
            dialog.showErrorMessage(msg + "\n" + e);
            back();
            return;
        }

        // 
        try {
            mergeManager.execute();
        } catch (Exception e) {
            setTitle("?????");
            String msg = "???????";
            log.error(msg, e);
            dialog.showErrorMessage(msg + "\n" + e);
            buttonBar.setState(buttonBar.FINISH_STATE);
            progressPanel.setBarText("????");
            return;
        }

        // ???
        WorkingState state = mergeManager.getWorkingState();
        String message = null;
        if (state.isCanceled()) {
            buttonBar.setState(buttonBar.CANCEL_STATE);
            message = "????";
        } else {
            buttonBar.setState(buttonBar.FINISH_STATE);
            message = "???? " + state.getWorkTime() + "";
        }
        progressPanel.setBarText(message);
        setTitle(message);
    }

    /**
     * ???????
     * @param pref 
     * @return ???? true
     */
    private boolean isCancelByConfirm(Preference pref) {

        if (pref.getDocDirectory().getPath().length() == 0) {
            int result = dialog.showConfirmMessage(
                    "Javadoc API ?????????????\n"
                            + "????????????");
            if (result != JOptionPane.YES_OPTION) {
                return true;
            }
        }
        if (pref.getOutputArchive().exists()) {
            int result = dialog
                    .showConfirmMessage("?????\n"
                            + "??????");
            if (result != JOptionPane.YES_OPTION) {
                return true;
            }
        }
        return false;
    }

    /**
     * ????????<br>
     * ????????
     */
    private void back() {
        buttonBar.setState(buttonBar.INIT_STATE);
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                mainPanel.remove(progressPanel);
                mainPanel.add(preferencePanel);
                mainPanel.repaint();
            }
        });
        setTitle(SETTING_TITLE);
    }

    /**
     * ???
     * ??????
     */
    private void end() {
        persistent();
        try {
            preferencePanel.persistent();
            Persister.getInstance().store();
        } catch (MergeDocException e) {
            dialog.showErrorMessage(e.getMessage());
        }
        System.exit(0);
    }

    /**
     *  Persister ????
     * ?????
     */
    private void persistent() {
        try {
            Persister psst = Persister.getInstance();
            int state = getExtendedState();
            psst.setInt(Persister.WINDOW_STATE, state);
            if (state == Frame.NORMAL) {
                psst.setInt(Persister.WINDOW_WIDTH, (int) getSize().getWidth());
                psst.setInt(Persister.WINDOW_HEIGHT, (int) getSize().getHeight());
                psst.setInt(Persister.WINDOW_X, (int) getLocation().getX());
                psst.setInt(Persister.WINDOW_Y, (int) getLocation().getY());
            }
        } catch (MergeDocException e) {
            dialog.showErrorMessage(e.getMessage());
        }
    }

    /**
     * ??????????
     * ????????
     * @param   title ?
     * @see     Frame#setTitle(String)
     */
    @Override
    public void setTitle(final String title) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                MergeDocFrame.super.setTitle("MergeDoc7 - " + title);
            }
        });
    }
}