Package com.GestDB.swing

Source Code of com.GestDB.swing.Skins

/*
* Skins.java
*
* Created on 25 de septiembre de 2006, 17:04
* Created by Arsenio Molinero
* Copyright (C) 2004, 2005, 2006 Arsenio Molinero
*
* GestDB - a Java multi database client
*
* This file is part of GestDB.
*
* GestDB is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* GestDB 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GestDB; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

package com.GestDB.swing;

import com.GestDB.Gestor;
import com.GestDB.general.Trackbug;
import com.GestDB.swing.skins.PropertiesMetal;
import com.nilo.plaf.nimrod.NimRODLookAndFeel;
import com.nilo.plaf.nimrod.NimRODTheme;
import com.pagosoft.plaf.PlafOptions;
import com.pagosoft.plaf.themes.NativeColorTheme;
import com.pagosoft.plaf.themes.SilverTheme;
import com.pagosoft.plaf.themes.VistaTheme;
import de.muntjak.tinylookandfeel.ThemeDescription;
import de.muntjak.tinylookandfeel.TinyLookAndFeel;
import javax.swing.plaf.metal.*;
import java.util.*;
import java.io.*;
import javax.swing.SwingUtilities;

/**
* Se encarga de cargar los skin y los temas, tambi�n se utiliza para cargar un skin en concreto
* @author seni
*/
public class Skins extends Thread
{
    private Gestor gestor = null;
   
    /** Creates a new instance of Skins */
    public Skins(String sskin, String stema, Gestor gestor)
    {
        this.gestor = gestor;
        sskinSelected = sskin;
        sskinThemeSelected = stema;
        setSkin(sskin, stema);
    }
   
    public String getSkinSelected() { return sskinSelected; }
    public String getThemeSelected() { return sskinThemeSelected; }
    public Object [] list() { return hmSkins.keySet().toArray(); }
    public Object [] listThemes(String sskin)
    {
        HashMap hskin = getSkin(sskin);
        if(hskin != null && hskin.containsKey("hasTheme") &&
                ((String)hskin.get("hasTheme")).equals("true") && hskin.containsKey("themes"))
            return ((HashMap)hskin.get("themes")).keySet().toArray();

        return null;
    }

    public Properties getTheme(String sskin, String stema)
    {
        HashMap hskin = getSkin(sskin);
        if(hskin != null && hskin.containsKey("hasTheme") &&
                ((String)hskin.get("hasTheme")).equals("true") &&
                hskin.containsKey("themes") && ((HashMap)hskin.get("themes")).containsKey(stema))
            return (Properties)((HashMap)hskin.get("themes")).get(stema);

        return null;
    }

    public HashMap getSkin(String sskin)
    {
        if(hmSkins.containsKey(sskin))
            return (HashMap)hmSkins.get(sskin);
        else
            return null;
    }
   
    public void run()
    {
        try {
            javax.swing.UIManager.setLookAndFeel((String) hskinSel.get("class"));
            SwingUtilities.updateComponentTreeUI(gestor);
        } catch (Exception ex) {
            Trackbug.error(ex, ex.getMessage());
            try {
                javax.swing.UIManager.setLookAndFeel("com.birosoft.liquid.LiquidLookAndFeel");
                SwingUtilities.updateComponentTreeUI(gestor);
            }catch(Exception cnfe)
            {
                ;
            }
        }
    }

    public void setSkin(String sskin, String stema)
    {
        sskinSelected = sskin;
        sskinThemeSelected = stema;
        try {
            if(skins == null)
                loadSkins(this.getClass());
           
            // ponemos el skin a trabajar
            if(skins != null)
            {
                if(hmSkins.containsKey(sskinSelected))
                {
                    hskinSel = (HashMap)hmSkins.get(sskinSelected);
                    if(hskinSel != null && hskinSel.containsKey("hasTheme") && hskinSel.containsKey("class"))
                    {
                        if(((String)hskinSel.get("hasTheme")).equals("false"))
                        {
                            gestor.setDefaultLookAndFeelDecorated(true);
                            SwingUtilities.invokeLater(this);
                       
                        else
                        {
                            if(hskinSel.get("themes") != null && ((HashMap)hskinSel.get("themes")).get(sskinThemeSelected) != null)
                            {
                                // Carga el tema del skin
                                if(sskin.equals("Metal"))
                                {
                                    MetalTheme theme = new PropertiesMetal((Properties)((HashMap)hskinSel.get("themes")).get(sskinThemeSelected));
                                    MetalLookAndFeel.setCurrentTheme(theme);
                                    // vamos con el skin
                                    javax.swing.UIManager.setLookAndFeel((String)hskinSel.get("class"));
                                    SwingUtilities.updateComponentTreeUI(gestor);
                                }
                                else if(sskin.equals("nimrodlf"))
                                {
                                    File ftemp = new File(Gestor.getRutaBaseSkin() + "themes/nimrod/" + sskinThemeSelected + ".theme");
                                    if(ftemp.exists() && ftemp.canRead())
                                    {
                                        NimRODTheme nt = new NimRODTheme(Gestor.getRutaBaseSkin() + "themes/nimrod/" + sskinThemeSelected + ".theme");
                                        NimRODLookAndFeel nf = new NimRODLookAndFeel();
                                        nf.setCurrentTheme( nt);

                                        // vamos con el skin
                                        javax.swing.UIManager.setLookAndFeel(nf);
                                        SwingUtilities.updateComponentTreeUI(gestor);
                                    }
                                }
                                else if(sskin.equals("pgslookandfeel"))
                                {
                                    PlafOptions.setAsLookAndFeel();
                                    if(sskinThemeSelected.equals("NativeColor"))
                                        PlafOptions.setCurrentTheme(new NativeColorTheme());
                                    else if(sskinThemeSelected.equals("Vista"))
                                        PlafOptions.setCurrentTheme(new VistaTheme());
                                    else
                                        PlafOptions.setCurrentTheme(new SilverTheme());
                                    SwingUtilities.updateComponentTreeUI(gestor);
                                }
                                else if(sskin.equals("TinyLaF"))
                                {
                                    TinyLookAndFeel tlf = new TinyLookAndFeel();
                                    ThemeDescription[] availableThemes = de.muntjak.tinylookandfeel.Theme.getAvailableThemes();
                                    for(int i=0; i < availableThemes.length; i++)
                                    {
                                        if(availableThemes[i].getName().equals(sskinThemeSelected))
                                        {
                                            de.muntjak.tinylookandfeel.Theme.loadTheme(availableThemes[i]);
                                            break;
                                        }
                                    }

                                    javax.swing.UIManager.setLookAndFeel(tlf);
                                    SwingUtilities.updateComponentTreeUI(gestor);
                                }
                            }
                            else
                            {
                                gestor.setDefaultLookAndFeelDecorated(true);
                                javax.swing.UIManager.setLookAndFeel((String)hskinSel.get("class"));
                                SwingUtilities.updateComponentTreeUI(gestor);
                            }
                           
                        }
                    }
                    else
                    {
                        javax.swing.UIManager.setLookAndFeel("com.birosoft.liquid.LiquidLookAndFeel");
                        SwingUtilities.updateComponentTreeUI(gestor);
                    }
                }
            }
            else
            {
                // cargamos el de por defecto
                javax.swing.UIManager.setLookAndFeel("com.birosoft.liquid.LiquidLookAndFeel");
                SwingUtilities.updateComponentTreeUI(gestor);
            }
        } catch(Exception e)
        {
            Trackbug.error(e, e.getMessage());
            try {
                javax.swing.UIManager.setLookAndFeel("com.birosoft.liquid.LiquidLookAndFeel");
                SwingUtilities.updateComponentTreeUI(gestor);
            }catch(Exception cnfe)
            {
                ;
            }       
        }
    }
   
    private static boolean loadSkins(Class clase) throws IOException, Exception
    {
        File ftemp = new File(Gestor.getRutaBaseSkin(),"skin.properties");
        if(!ftemp.exists() || !ftemp.canRead())
            return false;
       
        skins = new Properties();
        FileInputStream fin = new FileInputStream(ftemp);
        skins.load(fin);
        fin.close();
       
        if(skins.containsKey("skins.count"))
        {
            try {
                int nskins = Integer.parseInt((String)skins.get("skins.count"));
                hmSkins = new HashMap(nskins);
                HashMap htmp = null;
                HashMap htmp2 = null;
                StringTokenizer temas = null;
                String stemp = null;
                Properties ptmp = null;
                for(int i=1; i <= nskins; i++)
                {
                    htmp = new HashMap(3);
                    htmp.put("name",(String)skins.get("skins." + i + ".name"));
                    htmp.put("class",(String)skins.get("skins." + i + ".class"));
                    htmp.put("hasTheme",(String)skins.get("skins." + i + ".hasTheme"));
                   
                    if(((String)skins.get("skins." + i + ".hasTheme")).toLowerCase().equals("true"))
                    {
                        // los temas vienen separados por comas
                        htmp2 = new HashMap();
                        temas = new StringTokenizer((String)skins.get("skins." + i + ".themes"),",",false);
                        while(temas.hasMoreTokens())
                        {
                            stemp = temas.nextToken();
                            if(((String)skins.get("skins." + i + ".name")).equals("nimrodlf") ||
                               ((String)skins.get("skins." + i + ".name")).equals("pgslookandfeel") ||
                               ((String)skins.get("skins." + i + ".name")).equals("TinyLaF"))
                            {
                                htmp2.put(stemp,new Properties());
                            }
                            else
                            {
                                // cargamos el archivo de propiedades y lo a�adimos
                                try {
                                    ptmp = new Properties();
                                    ftemp = new File(Gestor.getRutaBaseSkin() + "themes/" + stemp + ".theme");
                                    if(ftemp.exists() && ftemp.canRead())
                                    {
                                        try {
                                            fin = new FileInputStream(ftemp);
                                            ptmp.load(fin);
                                            fin.close();
                                            htmp2.put(stemp,ptmp);
                                        }catch(Exception eee)
                                        {
                                            Trackbug.error(eee, eee.getMessage());
                                        }
                                    }
                                }catch(Exception ee)
                                {
                                    ;
                                }
                            }
                        }
                        htmp.put("themes",htmp2);
                    }
                   
                    hmSkins.put((String)skins.get("skins." + i + ".name"),htmp);
                }
                return true;
            }catch(Exception e)
            {
                skins = null;
            }           
        }
           
        return false;
    }

    public boolean createTheme(String sskin, String stheme, Properties ptheme) throws IOException, Exception
    {
        // tenemos que abrir el archivo de skins para buscar el metal y a�adir el tema a la propiedad "themes"
        File ftemp = new File(Gestor.getRutaBaseSkin(),"skin.properties");
        if(!ftemp.exists() || !ftemp.canRead())
            return false;
       
        Properties pskins = new Properties();
        FileInputStream fin = new FileInputStream(ftemp);
        pskins.load(fin);
        fin.close();
       
        if(pskins.containsKey("skins.count"))
        {
            try {
                int nskins = Integer.parseInt((String)pskins.get("skins.count"));
                String temas = null;
                String stemp = null;
                for(int i=1; i <= nskins; i++)
                {
                    if(((String)pskins.get("skins." + i + ".hasTheme")).toLowerCase().equals("true") &&
                            ((String)pskins.get("skins." + i + ".name")).toLowerCase().equals("metal"))
                    {
                        // los temas vienen separados por comas comprobamos si hay alguno, de existir ponemos una coma detras sino
                        // simplemente a�adimos el nombre del tema.
                        temas = (String)pskins.get("skins." + i + ".themes");
                        if(temas.trim().length() > 0)
                            temas += ",";
                        temas += stheme;
                       
                        // actualizamos en pskins
                        pskins.put("skins." + i + ".themes",temas);
                       
                        // estamos preparados para guardar el archivo de skins
                        try {
                            FileOutputStream fout = new FileOutputStream(ftemp);
                            pskins.store(fout,"Skins control properties");
                            fout.close();
                           
                            // el siguiente paso es crear el archivo del tema
                            ftemp = new File(Gestor.getRutaBaseSkin() + "themes/" + stheme + ".theme");
                            try {
                                fout = new FileOutputStream(ftemp);
                                ptheme.store(fout,stheme + ". Theme control properties");
                                fout.close();
                            }catch(Exception eee)
                            {
                                Trackbug.error(eee, eee.getMessage());
                            }

                            // tenemos que crear el directorio de las imagenes
                            ftemp = new File(Gestor.getRutaBaseSkin() + "themes/" + stheme);
                            if(!ftemp.exists())
                            {
                                if(!ftemp.mkdir())
                                    Trackbug.error(null, "No se ha creado: " + ftemp.getAbsolutePath());
                            }
                           
                            // cogemos las imagenes del directorio tmp que existan y las ponerlas en el directorio recien creado
                            // para ello nos recorremos el properties sacando las propiedades que son del tipo imagen
                            Object claves[] = ptheme.keySet().toArray();
                            String stemp2 = null;
                            File ftemp2 = null;
                            for(int j=0 ; j < claves.length ; j++)
                            {
                                stemp2 = claves[j].toString().substring(0,3); // coge los tres primeros caracteres del nombre de la propiedad
                                if(stemp2.equals("Int")) // icono
                                {
                                    // lo que recibimos es el nombre de la imagen.
                                    // ftemp2 = new File(Gestor.getRutaBaseSkin() + "themes/" + stheme + "/",ptheme.getProperty(claves[j].toString()));
                                    ftemp2 = new File(Gestor.getRutaBaseTmp(),ptheme.getProperty(claves[j].toString()));
                                    if(ftemp2.exists() || ftemp2.canRead())
                                    {
                                        if(com.utils.utilidades.copyFile(ftemp2,ftemp))
                                            ftemp2.delete();
                                    }
                                }
                               
                            }
                           
                            // forzamos la recarga de los skins y de los temas
                            loadSkins(this.getClass());
                           
                            return true;
                        }catch(Exception eee)
                        {
                            Trackbug.error(eee, eee.getMessage());
                        }
                    }
                }
            }catch(Exception e)
            {
                pskins = null;
            }           
        }
           
        return false;
    }
   
    public boolean saveTheme(String sskin, String stheme, String sOldTheme,Properties ptheme) throws IOException, Exception
    {
        // Guardamos el archivo del tema
        File ftemp = new File(Gestor.getRutaBaseSkin() + "themes/" + stheme + ".theme");
        try {
            FileOutputStream fout = new FileOutputStream(ftemp);
            ptheme.store(fout,stheme + ". Theme control properties");
            fout.close();

            // si stheme es distinto de sOldTheme, tenemos que a�adir el tema al archivo de skins, pues es como un guardar como
            if(!sOldTheme.equals(stheme))
            {
                ftemp = new File(Gestor.getRutaBaseSkin(),"skin.properties");
                if(!ftemp.exists() || !ftemp.canRead())
                    return false;

                Properties pskins = new Properties();
                FileInputStream fin = new FileInputStream(ftemp);
                pskins.load(fin);
                fin.close();

                if(pskins.containsKey("skins.count"))
                {
                    try {
                        int nskins = Integer.parseInt((String)pskins.get("skins.count"));
                        String temas = null;
                        String stemp = null;
                        for(int i=1; i <= nskins; i++)
                        {
                            if(((String)pskins.get("skins." + i + ".hasTheme")).toLowerCase().equals("true") &&
                                    ((String)pskins.get("skins." + i + ".name")).toLowerCase().equals("metal"))
                            {
                                // los temas vienen separados por comas comprobamos si hay alguno, de existir ponemos una coma detras sino
                                // simplemente a�adimos el nombre del tema.
                                temas = (String)pskins.get("skins." + i + ".themes");
                                if(temas.trim().length() > 0)
                                    temas += ",";
                                temas += stheme;

                                // actualizamos en pskins
                                pskins.put("skins." + i + ".themes",temas);

                                // estamos preparados para guardar el archivo de skins
                                try {
                                    fout = new FileOutputStream(ftemp);
                                    pskins.store(fout,"Skins control properties");
                                    fout.close();
                                }catch(Exception eee)
                                {
                                    Trackbug.error(eee, eee.getMessage());
                                    return false;
                                }
                               
                                // tenemos que crear el directorio de imagenes
                                ftemp = new File(Gestor.getRutaBaseSkin() + "themes/" + stheme);
                                if(!ftemp.exists())
                                {
                                    if(!ftemp.mkdir())
                                        Trackbug.error(null, "No se ha creado: " + ftemp.getAbsolutePath());
                                }
                               
                                // cogemos las imagenes del directorio tmp que existan y las ponerlas en el directorio recien creado
                                // para ello nos recorremos el properties sacando las propiedades que son del tipo imagen
                                Object claves[] = ptheme.keySet().toArray();
                                String stemp2 = null;
                                File ftemp2 = null;
                                for(int j=0 ; j < claves.length ; j++)
                                {
                                    stemp2 = claves[j].toString().substring(0,3); // coge los tres primeros caracteres del nombre de la propiedad
                                    if(stemp2.equals("Int")) // icono
                                    {
                                        // lo que recibimos es el nombre de la imagen.
                                        // ftemp2 = new File(Gestor.getRutaBaseSkin() + "themes/" + stheme + "/",ptheme.getProperty(claves[j].toString()));
                                        ftemp2 = new File(Gestor.getRutaBaseTmp(),ptheme.getProperty(claves[j].toString()));
                                        if(ftemp2.exists() || ftemp2.canRead())
                                        {
                                            if(com.utils.utilidades.copyFile(ftemp2,ftemp))
                                                ftemp2.delete();
                                        }
                                    }

                                }
                               
                                break;
                            } // fin del if metal
                        } // fin del for
                    }catch(Exception e)
                    {
                        pskins = null;
                        return false;
                    }           
                }

            }
            else
            {
                // Comprobamos si hay que copiar los iconos
                // directorio de imagenes
                ftemp = new File(Gestor.getRutaBaseSkin() + "themes/" + stheme);
                // cogemos las imagenes del directorio tmp que existan y las ponerlas en el directorio recien creado
                // para ello nos recorremos el properties sacando las propiedades que son del tipo imagen
                Object claves[] = ptheme.keySet().toArray();
                String stemp2 = null;
                File ftemp2 = null;
                for(int j=0 ; j < claves.length ; j++)
                {
                    stemp2 = claves[j].toString().substring(0,3); // coge los tres primeros caracteres del nombre de la propiedad
                    if(stemp2.equals("Int")) // icono
                    {
                        // lo que recibimos es el nombre de la imagen.
                        // ftemp2 = new File(Gestor.getRutaBaseSkin() + "themes/" + stheme + "/",ptheme.getProperty(claves[j].toString()));
                        ftemp2 = new File(Gestor.getRutaBaseTmp(),ptheme.getProperty(claves[j].toString()));
                        if(ftemp2.exists() || ftemp2.canRead())
                        {
                            if(com.utils.utilidades.copyFile(ftemp2,ftemp))
                                ftemp2.delete();
                        }
                    }

                }
            }
           
            // forzamos la recarga de los skins y de los temas
            loadSkins(this.getClass());

            return true;
        }catch(Exception eee)
        {
            Trackbug.error(eee, eee.getMessage());
        }
       
        return false;
    }

    public boolean saveSkin(String sskin, String clase) throws IOException, Exception
    {
        // tenemos que abrir el archivo de skins para a�adirlo
        File ftemp = new File(Gestor.getRutaBaseSkin(),"skin.properties");
        if(!ftemp.exists() || !ftemp.canRead())
            return false;
       
        Properties pskins = new Properties();
        FileInputStream fin = new FileInputStream(ftemp);
        pskins.load(fin);
        fin.close();
       
        if(pskins.containsKey("skins.count"))
        {
            try {
                int nskins = Integer.parseInt((String)pskins.get("skins.count"));
               
                // creamos la nueva piel
                pskins.put("skins." + nskins + ".hasTheme","false");
                pskins.put("skins." + nskins + ".name",sskin);
                pskins.put("skins." + nskins + ".class",clase);

                // incrementamos el numero de skins
                pskins.put("skins.count",Integer.toString(++nskins));
               
                FileOutputStream fout = new FileOutputStream(ftemp);
                pskins.store(fout,"Skins control properties");
                fout.close();
               
                // recargamos los skins
                loadSkins(this.getClass());
               
                return true;
               
            }catch(Exception e)
            {
                pskins = null;
            }           
        }
           
        return false;
    }
   
    public static boolean isLoaded()
    {
        return (skins != null);
    }
   
    private static String sskinSelected = "Liquid";
    private static String sskinThemeSelected = "";
   
    /*
     * Este hashmap contiene los skins. la estructura de este es la siguiente:
     * La clave es el nombre del skin.
     * Como valor tiene un hasmap con las siguientes claves:
     * name     Nombre del skin
     * class    Clase necesaria para cargarlo
     * hasTheme indica si permite temas
     * themes   hasmap con los temas. S�lo si hasTheme es true.
     * El hashmap del los temas tiene como clave el nombre del tema. este se tiene que corresponder con el archivo
     * ubicado en el directorio skin/themes con las extensi�n theme, por ejemplo dark.theme
     * El valor del hashmap es un Properties.
     *
     * El archivo de tema puede tener las siguientes propiedades:
     *
     */
    private static HashMap hmSkins = null;
    private static Properties skins = null;
    private HashMap hskinSel = null;

}
TOP

Related Classes of com.GestDB.swing.Skins

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.