Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 * Copyright (C) 2009 Klaus Reimer <k@ailis.de> 
 * See LICENSE.md for licensing information.
 */

import java.util.Locale;

import javax.swing.UIManager;

public class Main {
    /**
     * Prepares the Swing GUI.
     *
     * @param appName
     *            The application name. This is used to create the names of environment variables check for locale and
     *            theme overrides. appName "FooBar" for example uses the override environment variables FOOBAR_LANG and
     *            FOOBAR_THEME.
     * @throws Exception
     *             When preparing the GUI failed
     */

    public static void prepareGUI(final String appName) throws Exception {
        prepareLocale(appName.toUpperCase() + "_LANG");
        prepareTheme(appName.toUpperCase() + "_THEME");
    }

    /**
     * Prepares the locale. The language can be overridden with the specified environment variable. Set it to "de" to
     * enforce German language for example. The default is the system locale.
     *
     * @param overrideEnvVar
     *            The environment variable to check for override value. Must not be null.
     */

    public static void prepareLocale(final String overrideEnvVar) {
        final String language = System.getenv().get(overrideEnvVar);
        if (language != null) {
            Locale.setDefault(new Locale(language));
        }
    }

    /**
     * Prepares the theme. The theme can be overridden with the specified environment variable. The default is the
     * system look and feel.
     *
     * @param overrideEnvVar
     *            The environment variable to check for override value. Specify null for don't use override variable
     * @throws Exception
     *             When setting the theme failed
     */

    public static void prepareTheme(final String overrideEnvVar) throws Exception {
        final String sysThemeStr = overrideEnvVar == null ? null : System.getenv().get(overrideEnvVar);
        System.setProperty("apple.laf.useScreenMenuBar", "true");
        if (sysThemeStr == null || Boolean.parseBoolean(sysThemeStr)) {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        }
    }
}