/*
 This file is part of the software framework JStatCom, 
 a library for building applications for data based analysis.

 Copyright (C) 2000-2005  Markus Kraetzig

 This library is free software; you can redistribute it and/or modify
 it under the terms of the GNU Lesser General Public License as
 published by the Free Software Foundation; either version 2.1 of the
 License, or (at your option) any later version.

 This library is distributed in the hope that it will be useful, but
 WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 Lesser General Public License for more details.

 You should have received a copy of the GNU Lesser General Public
 License along with this library; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 USA
 */

package com.jstatcom.component;

import java.awt.Color;
import java.awt.Font;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import javax.swing.SwingUtilities;

import org.apache.log4j.Logger;

/**
 * This class contains some publicly accessible fields that specify settings for
 * components used throughout JStatCom.
 * 
 * @author Markus Kraetzig
 */

public final class CompSettings {

    private static final Logger log = Logger.getLogger(CompSettings.class);

    /**
     * Default background color if error happened.
     */
    public static final Color errorBackground = Color.pink;

    /**
     * Default for small font.
     */
    public static final Font smallFontDefault = new Font("dialog",
            java.awt.Font.PLAIN, 10);

    /**
     * CompSettings constructor comment.
     */
    private CompSettings() {
        super();
    }

    /**
     * Later-Invokes the operation defined in runner within the
     * SWING event dispatching thread. If this method is already called within
     * the event dispatching thread, then simply runner.run is
     * called.O Otherwise SwingUtilities.invokeLater(runner) is
     * used.
     * 
     * @param runner
     *            wrapper for the method to be called within the SWING event
     *            dispatching thread
     * @throws IllegalArgumentException
     *             if (runner == null)
     */
    public static void syncWithSwing(Runnable runner) {

        if (runner == null)
            throw new IllegalArgumentException("Argument was null");

        if (SwingUtilities.isEventDispatchThread())
            runner.run();
        else
            SwingUtilities.invokeLater(runner);

    }

    /**
     * Dispatches event in Swing EDT. It first waits 2000 millis for the action
     * to return, then cancels and uses invokeLater.
     * 
     * @param runner
     * @throws IllegalArgumentException
     *              if (runner == null)
     */
    public static void dispatchEvent(final Runnable runner) {

        if (runner == null)
            throw new IllegalArgumentException("Argument was null.");

        if (SwingUtilities.isEventDispatchThread())
            runner.run();
        else {
            Thread waitThread = new Thread() {
                public void run() {
                    try {
                        SwingUtilities.invokeAndWait(runner);
                    } catch (Throwable e) {
                        log.error("Exception in invokeAndWait", e);
                    }
                }
            };
            waitThread.start();

            try { // do not wait forever
                waitThread.join(2000);
                if (waitThread.isAlive()) // still active, something is wrong
                {
                    SwingUtilities.invokeLater(runner);
                }

            } catch (Throwable e) {
                log
                        .error(
                                "Exception while waiting for SwingUtilities.invokeAndWait",
                                e);
            }
        }
    }

    /**
     * Gets a string with the formatted date and time.
     * 
     * @return formatted date string
     */
    public static String getDateString() {
        return getDefaultDateFormat().format(new Date());
    }

    /**
     * Gets a new instance of a default date formatter with the pattern
     * "EEE, d MMM yyyy HH:mm:ss".
     * 
     * @return date format
     */
    public static DateFormat getDefaultDateFormat() {
        return new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss", Locale.ENGLISH);
    }

    /**
     * Gets a new instance of a simple date formatter with the pattern
     * "HH:mm:ss".
     * 
     * @return date format
     */
    public static DateFormat getSimpleDateFormat() {
        return new SimpleDateFormat("HH:mm:ss", Locale.ENGLISH);
    }

}
 

Ads help maintain this website.