Package DisplayProject

Source Code of DisplayProject.WindowSystem

/*
Copyright (c) 2003-2009 ITerative Consulting Pty Ltd. All Rights Reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted
provided that the following conditions are met:

o Redistributions of source code must retain the above copyright notice, this list of conditions and
the following disclaimer.
 
o Redistributions in binary form must reproduce the above copyright notice, this list of conditions
and the following disclaimer in the documentation and/or other materials provided with the distribution.
   
o This jcTOOL Helper Class software, whether in binary or source form may not be used within,
or to derive, any other product without the specific prior written permission of the copyright holder

 
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


*/
package DisplayProject;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.print.PageFormat;
import java.awt.print.PrinterJob;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.swing.UIManager;

import org.apache.log4j.Logger;

import Framework.File;
/**
* This class provide many, but not all, of the capabilities provided in the Forte WindowSystem class.
*
* It is not a complete implementation
*
*
*/
public class WindowSystem {
    protected static final int cREG_COL_OFFSET = 9600;
    private static ArrayList<Color> regColors = new ArrayList<Color>();
    private static ArrayList<Cursor> regCursors = new ArrayList<Cursor>();
    /**
     * The fonts hash table holds the fonts keyed by the font name. It used to be
     * stored by family, but issues arose with fonts like SansSerif where there are
     * multiple fonts with different names, and the non-plain font comes first in the
     * array (eg SansSerif.Bold comes before SansSerif.Plain)
     */
    private static Map<String, Font> fonts;
    private static Logger _log = Logger.getLogger(WindowSystem.class);
    private WindowSystem() {
        super();
    }
    static {
        // Load up the fonts hash table, keyed by name
        Font[] fontList = GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts();
        fonts = new HashMap<String, Font>(fontList.length*2)
        for (Font f : fontList) {
            fonts.put(f.getName().toLowerCase(), f);
        }
    }

    private static Font findFont(String name){
        Font result = fonts.get(name.toLowerCase());
        if (result == null) {
            result = (Font)UIManager.get("TextField.font");
        }
        return result;
    }
    /**
     * The GetPortableFont method returns a cached PortableFontDescriptor object, or, if necessary, creates a new PortableFontDescriptor object based on specifications you provide.
     * @param pointSize The pointSize parameter specifies the size of the font, in tenths of a point. To set a font size to 12 points, for example, you set the pointSize parameter to 120.
     * @param typeFace The typeFace parameter specifies the typeface itself. It accepts the following values:
     * <table>
     * <tr><th>Value</th><th>Definition</th></tr>
     * <tr><td>TF_SYSTEM_DEFAULT</td><td>The window system�s standard proportionally spaced font.<td></tr>
     * <tr><td>TF_COURIER</td><td>The window system�s Courier font.<td></tr>
     * <tr><td>TF_SYSTEM</td><td>The window system�s standard proportionally spaced font.<td></tr>
     * <tr><td>TF_SYSTEM_DATAENTRY</td><td>The window system�s data entry font.<td></tr>
     * <tr><td>TF_SYSTEM_ICONLABEL</td><td>The window system�s icon label font.<td></tr>
     * <tr><td>TF_SYSTEM_MONOSPACE</td><td>The window system�s standard monospaced font.<td></tr>
     * <tr><td>TF_SYSTEM_PROPORTIONAL</td><td>The window system�s standard proportionally spaced font.<td></tr>
     * <tr><td>TF_SYSTEM_TERMINAL</td><td>The window system�s standard terminal emulation font.<td></tr>
     * <tr><td>TF_TIMESROMAN</td><td>The window system�s Times Roman font.<td></tr>
     * </table>
     * @param isBold The isBold parameter, if TRUE, sets the font to a bold variation. The default setting for the isBold parameter is FALSE.
     * @param isItalic The isItalic parameter, if TRUE, sets the font to an italic variation. The default setting for the isItalic parameter is FALSE.
     * @return
     */
    public static Font getPortableFont(int pointSize, int typeFace, boolean isBold, boolean isItalic){
        Font result;
        float size = 11;
        if (pointSize != -1) {
          // TF:26/07/2009:Java font points assume 72 dpi scaled by a factor of 10. Most machines typically
          // are set to 96dpi, so we need a ratio of 72 * 10 / 96 = 7.5
          size = pointSize / 7.5f;
        }
        int style = Font.PLAIN;
        if (isBold)
            style |= Font.BOLD;
        if (isItalic)
            style |= Font.ITALIC;
        switch (typeFace){
        case Constants.TF_TIMESROMAN:
            result = findFont("Times New Roman").deriveFont(style, size);
            break;
        case Constants.TF_COURIER:
            result = findFont("Courier New").deriveFont(style, size);
            break;
        case Constants.TF_HELVETICA:
            result = findFont("Helvetica").deriveFont(style, size);
            break;
        case Constants.TF_SYSTEM_DATAENTRY:
            result = findFont("Tahoma").deriveFont(style, size);
            break;
        case Constants.TF_SYSTEM_DEFAULT:
            result = findFont("Tahoma").deriveFont(style, size);
            break;
        case Constants.TF_SYSTEM_ICONLABEL:
            result = findFont("SansSerif.plain").deriveFont(style, size);
            break;
        case Constants.TF_SYSTEM_MONOSPACE:
          // String[] availableFonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
         
          // CraigM:16/12/2008 - Best match is the GulimChe font (that I found anyway)
          result = fonts.get("gulimche");
         
          if (result == null) {
            result = fonts.get("bitstream vera sans mono"); // This font is also close to Fortes
          }

          if (result != null) {
            // Best default size is 12 points
            if (pointSize == -1) {
              size = 12;
            }
            result = result.deriveFont(style, size);
          }
          else {
            // Default back to the standard Java font if GulimChe is not installed
            result = findFont("Monospaced.plain").deriveFont(style, size);
          }
            break;
        case Constants.TF_SYSTEM_PROPORTIONAL:
            result = findFont("SansSerif.plain").deriveFont(style, size);
            break;
        case Constants.TF_SYSTEM_TERMINAL:
            result = findFont("Monospaced.plain").deriveFont(style, size);
            break;
        default:
            result = findFont("Tahoma").deriveFont(style, size);
        break;
        }

        return result;
    }

    public static Font getSystemFont(int typeFace) {
        return getPortableFont(-1, typeFace, true, false);
    }

    public static Font getExtendedFont(int pointSize, int typeFace, boolean isBold, boolean isItalic){
        return getPortableFont(pointSize, typeFace, isBold, isItalic);
    }
    public static PageFormat getDefaultPrintOptions(){
        PrinterJob pjob = PrinterJob.getPrinterJob();
        PageFormat pf = pjob.defaultPage();
        return pf;
    }
    /**
     * registers a new color and provides an index to it.
     * @param red
     * @param green
     * @param blue
     * @return
     */
    public static int registerColor(int red, int green, int blue){
        Color newColor = new Color(red, green, blue);
        regColors.add(newColor);
        return regColors.indexOf(newColor) + WindowSystem.cREG_COL_OFFSET;
    }

    /**
     * Given a Java colour, return the registered index of that colour if it exists as a registered colour, or -1 otherwsie.
     * @param pColor
     * @return
     */
    public static int getRegisteredColour(Color pColor) {
        int index = regColors.indexOf(pColor);
        if (index >= 0) {
            return index + WindowSystem.cREG_COL_OFFSET;
        }
        return -1;
    }

    /**
     * deregisters the Custom Forte colour
     * @param index
     */
    public static void unregisterColor(int index){
        regColors.remove(index - WindowSystem.cREG_COL_OFFSET);
    }
    /**
     * returns the Java Color for the registered colour index
     * @param index
     * @return
     */
    public static Color registeredJavaColor(int index){
        if ((index > -1)&&(index - WindowSystem.cREG_COL_OFFSET < regColors.size()))
            return regColors.get(index - WindowSystem.cREG_COL_OFFSET);
        else {
            _log.error("Invalid colour index of: " + index + " returning yellow instead");
            return UIutils.forteToJavaColor(Constants.C_YELLOW);
        }
    }
    /**
     * The RegisterCursor method creates a new cursor from a cursor stored in a file.
     * The cursor hot spot is assumed to be 0,0
     * @param file
     * @return
     */
    public static int registerCursor(File file){
        Image img = Toolkit.getDefaultToolkit().getImage( file.getFileName() );
        Cursor cuscur =
            Toolkit.getDefaultToolkit().createCustomCursor( img, new java.awt.Point(0,0), "custom" );
        regCursors.add(cuscur);
        return regCursors.indexOf(cuscur);

    }
    /**
     * The RegisterCursor method creates a new cursor from a cursor stored in a file.
     * @param file
     * @param hotSpot Location of the hot spot in the cursor
     * @return
     */
    public static int registerCursor(File file, java.awt.Point hotSpot){
        Image img = Toolkit.getDefaultToolkit().getImage( file.getFileName() );
        Cursor cuscur =
            Toolkit.getDefaultToolkit().createCustomCursor( img, hotSpot, "custom" );
        regCursors.add(cuscur);
        return regCursors.indexOf(cuscur);

    }
    /**
     * Deletes a custom system cursor that was registered with RegisterCursor.
     * @param index
     */
    public static void unregisterCursor(int index){
        regCursors.remove(index);
    }

    /**
     * returns the Java CUrsor for the registered Cursor index;
     * @param index
     * @return
     */
    public static Cursor registeredJavaCursor(int index){
        return regCursors.get(index);
    }
    /**
     * returns the System clipboard
     * @return
     */
    public static Clipboard getClipboard() {
        return Toolkit.getDefaultToolkit().getSystemClipboard();
    }
    /**
     * Returns the forte constant that represents the typeface
     * @param font
     * @return
     */
    public static int getTypeface(Font font){
        int type = 0;
        String family = font.getFamily();
        if (family.equals("TimesRoman")) {
            type =Constants.TF_TIMESROMAN;
        } else if (family.equals("Courier New")) {
            type = Constants.TF_COURIER;
        } else if (family.equals("Helvetica")) {
            type = Constants.TF_HELVETICA;
        } else if (family.equals("Tahoma")) {
            type = Constants.TF_SYSTEM_DEFAULT;
        } else if (family.equals("Monospaced")) {
            type = Constants.TF_SYSTEM_MONOSPACE;
        } else if (family.equals("Serif")) {
            type = Constants.TF_SYSTEM_PROPORTIONAL;
        } else if (family.equals("Monospaced")) {
            type = Constants.TF_SYSTEM_TERMINAL;
        } else {
            type = Constants.TF_SYSTEM_DEFAULT;
        }

        return type;
    }

}
TOP

Related Classes of DisplayProject.WindowSystem

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.