mergedoc.ui.PreferencePanel.java Source code

Java tutorial

Introduction

Here is the source code for mergedoc.ui.PreferencePanel.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.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.LinkedList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;

import mergedoc.MergeDocException;
import mergedoc.core.FastStringUtils;
import mergedoc.core.Preference;
import mergedoc.xml.ConfigManager;
import mergedoc.xml.Persister;
import mergedoc.xml.ReplaceEntry;

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

/**
 * ???
 * @author Shinji Kashihara
 */
public class PreferencePanel extends JPanel {

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

    /** ?API  ? */
    private FileChooserField docField = new FileChooserField();

    /** ? ? */
    private FileChooserField srcField = new FileChooserField();

    /** ? ? */
    private FileChooserField outField = new FileChooserField();

    /** ? */
    private JSplitPane splitPane = new JSplitPane();

    /** ???? */
    private List<EntryCheckBox> entryCheckList = new LinkedList<EntryCheckBox>();

    /** ?  */
    private PreviewScrollPane previewScrollPane;

    /**
     * ??
     * @throws MergeDocException ????????
     */
    public PreferencePanel() throws MergeDocException {

        // 
        setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
        setMaximumSize(ComponentFactory.createMaxDimension());

        // ????
        add(createUpperPanel());
        add(ComponentFactory.createSpacer(0, 7));
        add(createLowerPanel());
    }

    /**
     * ??????
     * @return ??
     * @throws MergeDocException ????????
     */
    private JComponent createUpperPanel() throws MergeDocException {

        // 
        JLabel docLabel = docField.getLabel();
        JLabel srcLabel = srcField.getLabel();
        JLabel outLabel = outField.getLabel();
        docLabel.setText("API ");
        srcLabel.setText("");
        outLabel.setText("");
        JLabel[] labels = { docLabel, srcLabel, outLabel };
        ComponentFactory.ensureMaxFontWidth(labels);

        // 
        JComboBox docCombo = docField.getComboBox();
        JComboBox srcCombo = srcField.getComboBox();
        JComboBox outCombo = outField.getComboBox();
        docCombo.addItem(FileChooserField.ENCODING_AUTO);
        srcCombo.addItem(FileChooserField.ENCODING_AUTO);
        docCombo.setSelectedItem("UTF-8");
        srcCombo.setSelectedItem(FileChooserField.ENCODING_DEFAULT);
        outCombo.setSelectedItem(FileChooserField.ENCODING_DEFAULT);
        JComboBox[] combos = { docCombo, srcCombo, outCombo };
        ComponentFactory.ensureMaxFontWidth(combos);

        // ??
        docField.setSelectionMode(FileChooserField.DIRECTORIES);
        srcField.setSelectionMode(FileChooserField.ZIP_TGZ_FILES);
        outField.setSelectionMode(FileChooserField.ZIP_FILES);
        docField.setChooseListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                resolveArchivePath(docField.getFile());
            }
        });

        // ??
        JPanel panel = new TitledPanel("");
        panel.add(docField);
        panel.add(srcField);
        panel.add(outField);

        // ??
        final String OPTION_KEY = "target.directory";
        String targetStr = System.getProperty(OPTION_KEY);
        if (targetStr != null) {
            File targetDir = new File(targetStr);
            if (!targetDir.exists() || targetDir.isFile()) {
                throw new MergeDocException(" " + OPTION_KEY + " ??? " + targetStr
                        + " ?\n?????????");
            }
            File docDir = searchDocDirectory(targetDir);
            if (docDir != null) {
                docField.setFile(docDir);
            }
            srcField.setFile(new File(""));
            outField.setFile(new File(""));
            resolveArchivePath(targetDir);
        }

        // ??
        loadPersister(docField, Persister.DOC_DIR, Persister.DOC_ENC);
        loadPersister(srcField, Persister.IN_FILE, Persister.IN_ENC);
        loadPersister(outField, Persister.OUT_FILE, Persister.OUT_ENC);

        // ?
        String docPath = docField.getFile().getPath();
        if (docPath.equals("")) {

            File home = new File(System.getProperty("java.home"));
            if (home.getName().equals("jre")) {
                home = home.getParentFile();
            }
            File docDir = new File(home, "docs/ja/api");
            if (docDir.exists()) {
                docField.setFile(docDir);
                resolveArchivePath(home);
            }
        }

        return panel;
    }

    /**
     * ??????
     * @return ??
     * @throws MergeDocException ????????
     */
    private JComponent createLowerPanel() throws MergeDocException {

        // ???
        JPanel checkPanel = new JPanel();
        checkPanel.setLayout(new BoxLayout(checkPanel, BoxLayout.Y_AXIS));
        List<ReplaceEntry> list = ConfigManager.getInstance().getGlobalEntries();
        for (ReplaceEntry entry : list) {
            EntryCheckBox cb = new EntryCheckBox(entry);
            cb.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    previewScrollPane.updatePreview(entryCheckList);
                }
            });
            checkPanel.add(cb);
            entryCheckList.add(cb);
        }
        JScrollPane checkScrollPane = ComponentFactory.createScrollPane(checkPanel);
        checkScrollPane.getVerticalScrollBar().setUnitIncrement(10);

        // ?
        previewScrollPane = new PreviewScrollPane();

        // ??
        splitPane.setBorder(BorderFactory.createEmptyBorder());
        splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
        splitPane.setContinuousLayout(true);
        splitPane.setTopComponent(checkScrollPane);
        splitPane.setBottomComponent(previewScrollPane);
        JPanel splitPanel = new JPanel();
        splitPanel.setLayout(new BoxLayout(splitPanel, BoxLayout.X_AXIS));
        splitPanel.add(splitPane);

        // ??
        JPanel panel = new TitledPanel("");
        panel.setMaximumSize(ComponentFactory.createMaxDimension());
        panel.add(splitPanel);

        // ???
        Persister psst = Persister.getInstance();
        int loc = psst.getInt(Persister.DETAIL_PANEL_HEIGHT, 88);
        splitPane.setDividerLocation(loc);

        // ???
        String[] pDescs = psst.getStrings(Persister.REPLACE_DESCRIPTION_ARRAY);
        for (EntryCheckBox ecb : entryCheckList) {
            ReplaceEntry entry = ecb.getReplaceEntry();
            String desc = entry.getDescription();
            for (String pDesc : pDescs) {
                if (desc.equals(pDesc)) {
                    ecb.setSelected(true);
                    break;
                }
            }
        }
        previewScrollPane.updatePreview(entryCheckList);

        return panel;
    }

    /**
     * ?????
     * ??????? 1 ???
     * ????????
     * @param baseDir 
     */
    private void resolveArchivePath(File baseDir) {

        String src = srcField.getFile().getPath();
        String out = outField.getFile().getPath();
        if (!src.equals("") || !out.equals("")) {
            return;
        }
        for (File dir = baseDir; dir != null; dir = dir.getParentFile()) {
            File zip = new File(dir, "src.zip");
            if (zip.exists()) {
                srcField.setFile(zip);
                break;
            }
            File jar = new File(dir, "src.jar");
            if (jar.exists()) {
                srcField.setFile(jar);
                break;
            }
        }
        src = srcField.getFile().getPath();
        if (!src.equals("")) {
            String outName = FastStringUtils.replaceFirst(src, "\\.(zip|jar)$", "ja.zip");
            outField.setFile(new File(outName));
        }
    }

    /**
     * ???API ???
     * @param baseDir 
     * @return API 
     */
    private File searchDocDirectory(File baseDir) {

        File dir = null;

        for (File file : baseDir.listFiles()) {
            if (file.isFile()) {
                if (file.getName().equals("allclasses-frame.html")) {
                    dir = baseDir;
                }
            } else {
                dir = searchDocDirectory(file); //?
            }
            if (dir != null) {
                break;
            }
        }
        return dir;
    }

    /**
     * ?????????
     * @param field ?
     * @param pathKey ??
     * @param charKey ??
     * @throws MergeDocException ????????
     */
    private void loadPersister(FileChooserField field, Persister.Key pathKey, Persister.Key charKey)
            throws MergeDocException {
        Persister psst = Persister.getInstance();
        if (field.getFile().getPath().equals("")) {
            String path = psst.getString(pathKey, "");
            if (path.length() > 0) {
                field.setFile(new File(path));
            }
        }
        try {
            String enc = psst.getString(charKey);
            field.getComboBox().setSelectedItem(enc);
        } catch (IllegalArgumentException e) {
        }
    }

    /**
     * ??????????
     * @return ??????
     */
    public ReplaceEntry[] getSelectedEntries() {

        List<ReplaceEntry> enables = new LinkedList<ReplaceEntry>();
        for (EntryCheckBox cb : entryCheckList) {
            if (cb.isSelected()) {
                ReplaceEntry entry = cb.getReplaceEntry();
                enables.add(entry);
            }
        }
        ReplaceEntry[] entries = enables.toArray(new ReplaceEntry[enables.size()]);
        return entries;
    }

    /**
     * ????
     * @return 
     */
    public Preference getPreference() {

        return new Preference() {

            File docDir = docField.getFile();
            File srcFile = srcField.getFile();
            File outFile = outField.getFile();
            String docEnc = docField.getComboBox().getSelectedItem().toString();
            String srcEnc = srcField.getComboBox().getSelectedItem().toString();
            String outEnc = outField.getComboBox().getSelectedItem().toString();
            ReplaceEntry[] entries = getSelectedEntries();

            @Override
            public File getDocDirectory() {
                return docDir;
            }

            @Override
            public File getInputArchive() {
                return srcFile;
            }

            @Override
            public File getOutputArchive() {
                return outFile;
            }

            @Override
            public String getDocEncoding() {
                return docEnc;
            }

            @Override
            public String getInputEncoding() {
                return srcEnc;
            }

            @Override
            public String getOutputEncoding() {
                return outEnc;
            }

            @Override
            public ReplaceEntry[] getGlobalEntries() {
                return entries;
            }
        };
    }

    /**
     * ???? Persister ????
     * @throws Persister ???????
     */
    public void persistent() throws MergeDocException {

        Preference pref = getPreference();
        Persister psst = Persister.getInstance();

        psst.setString(Persister.DOC_DIR, pref.getDocDirectory().getPath());
        psst.setString(Persister.IN_FILE, pref.getInputArchive().getPath());
        psst.setString(Persister.OUT_FILE, pref.getOutputArchive().getPath());
        psst.setString(Persister.DOC_ENC, pref.getDocEncoding());
        psst.setString(Persister.IN_ENC, pref.getInputEncoding());
        psst.setString(Persister.OUT_ENC, pref.getOutputEncoding());
        psst.setInt(Persister.DETAIL_PANEL_HEIGHT, splitPane.getDividerLocation());

        List<String> descList = new LinkedList<String>();
        for (ReplaceEntry entry : pref.getGlobalEntries()) {
            descList.add(entry.getDescription());
        }
        String[] descs = descList.toArray(new String[descList.size()]);
        psst.setStrings(Persister.REPLACE_DESCRIPTION_ARRAY, descs);
    }
}