net.yatomiya.nicherry.ui.preference.NPreferenceDialog.java Source code

Java tutorial

Introduction

Here is the source code for net.yatomiya.nicherry.ui.preference.NPreferenceDialog.java

Source

/*******************************************************************************
 * Copyright (c) 2014,2015 Hideki Yatomi
 * All rights reserved. This program and the accompanying materials are made
 * available under the terms of the Eclipse Public License v1.0 which
 * accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 ******************************************************************************/
package net.yatomiya.nicherry.ui.preference;

import java.io.*;
import org.eclipse.core.runtime.preferences.*;
import org.eclipse.jface.preference.*;
import org.eclipse.swt.widgets.*;
import net.yatomiya.e4.ui.preference.*;
import net.yatomiya.e4.util.*;
import net.yatomiya.nicherry.*;

public class NPreferenceDialog extends PreferenceDialog {
    public NPreferenceDialog(Shell parentShell) {
        super(parentShell, createPreferenceManager());

        IPreferenceStore store = new ScopedPreferenceStore(InstanceScope.INSTANCE, NPreferences.NODE) {
            @Override
            public void save() throws IOException {
                super.save();

                copyPreferenceMetaToData();
            }
        };

        setPreferenceStore(store);
    }

    static PreferenceManager createPreferenceManager() {
        PreferenceManager mgr = new PreferenceManager();
        PreferenceNode node;

        mgr.addToRoot(new PreferenceNode("", new AppearancePage()));
        mgr.addToRoot(new PreferenceNode("?", new NetworkPage()));
        node = new PreferenceNode("BBS", new BBSPage());
        node.add(new PreferenceNode("", new SiteViewPage()));
        node.add(new PreferenceNode("", new BoardViewPage()));
        node.add(new PreferenceNode("", new ThreadViewPage()));
        mgr.addToRoot(node);
        mgr.addToRoot(new PreferenceNode("", new BBSFilterPage()));
        mgr.addToRoot(new PreferenceNode("", new ImagePage()));

        return mgr;
    }

    static File prefPath;
    static File dataPrefPath;

    static {
        NApplication app = EUtils.get(NApplication.class);
        File prefDir = new File(app.getInstancePath(), ".metadata/.plugins/org.eclipse.core.runtime/.settings/");
        try {
            IOUtils.createDirectory(prefDir);
        } catch (IOException e) {
            throw new IllegalStateException(e);
        }
        prefPath = new File(prefDir, NConstants.PLUGIN_ID + ".prefs");
        dataPrefPath = new File(app.getDataPath(), NConstants.PLUGIN_ID + ".prefs");
    }

    public static void copyPreferenceDataToMeta() {
        if (IOUtils.isFileExists(dataPrefPath)) {
            try {
                IOUtils.copy(dataPrefPath, prefPath);
            } catch (IOException e) {
            }
        }
    }

    // .metadata ??? prefs ??
    public static void copyPreferenceMetaToData() {
        if (IOUtils.isFileExists(prefPath)) {
            try {
                IOUtils.copy(prefPath, dataPrefPath);
            } catch (IOException e) {
            }
        } else {
            // ????????? .prefs ??
            try {
                IOUtils.delete(dataPrefPath);
            } catch (IOException e) {
            }
        }
    }
}