All Downloads are FREE. Search and download functionalities are using the official Maven repository.

it.tidalwave.netbeans.lookandfeel.LookAndFeel Maven / Gradle / Ivy

The newest version!
/***********************************************************************************************************************
 *
 * SolidBlue - open source safe data
 * Copyright (C) 2011-2011 by Tidalwave s.a.s. (http://www.tidalwave.it)
 *
 ***********************************************************************************************************************
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations under the License.
 *
 ***********************************************************************************************************************
 *
 * WWW: http://solidblue.java.net
 * SCM: https://bitbucket.org/tidalwave/solidblue-src
 *
 **********************************************************************************************************************/
package it.tidalwave.netbeans.lookandfeel;

import javax.annotation.Nonnull;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;
import java.awt.Component;
import javax.swing.plaf.ComponentUI;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.plaf.ScrollBarUI;
import it.tidalwave.netbeans.lookandfeel.ui.BlueMarineScrollBarUI;
import it.tidalwave.netbeans.lookandfeel.ui.HeaderPanelUI;

/*******************************************************************************
 *
 * A facility class with methods that contains some useful constants and can
 * install UI properties for the blueMarine  Look & Feel.
 *
 * @author  Fabrizio Giudici
 * @version $Id$
 *
 ******************************************************************************/
public final class LookAndFeel 
  {
    private static final String CLASS = LookAndFeel.class.getName();    
    private static final Logger logger = Logger.getLogger(CLASS);
    
    /** The UIManager key for the Border to be used with Explorers. */
    public static final String BORDER_EXPLORER = "it.tidalwave.netbeans.explorer.border";
    
    /** The UIManager key for the Color to be used for the window borders. */
    public static final String COLOR_FRAME_FOREGROUND = "it.tidalwave.netbeans.frame.foreground";
    
    /** The UIManager key for the Color to be used for the desktop background. */
    public static final String COLOR_DESKTOP_BACKGROUND = "it.tidalwave.netbeans.desktop.background";
    
    /** The UIManager key for the Color to be used for the background of Explorers. */
    public static final String COLOR_EXPLORER_BACKGROUND = "it.tidalwave.netbeans.explorer.background";
    
    /** The UIManager key for the Color to be used for the foreground of Explorers. */
    public static final String COLOR_EXPLORER_FOREGROUND = "it.tidalwave.netbeans.explorer.foreground";
    
    /** The UIManager key for the Color to be used for the background of Explorers. */
    public static final String COLOR_EXPLORER_DARK_BACKGROUND = "it.tidalwave.netbeans.explorer.darkBackground";
    
    /** The UIManager key for the Color to be used for the foreground of Explorers. */
    public static final String COLOR_EXPLORER_DARK_FOREGROUND = "it.tidalwave.netbeans.explorer.darkForeground";
    
    /** The UIManager key for the Color to be used for the foreground of selected items in Explorers. */
    public static final String COLOR_EXPLORER_SELECTION_BACKGROUND = "it.tidalwave.netbeans.explorer.selectionBackground";
    
    /** The UIManager key for the Color to be used for the background of selected items in Explorers. */
    public static final String COLOR_EXPLORER_SELECTION_FOREGROUND = "it.tidalwave.netbeans.explorer.selectionForeground";
    
    /** The UIManager key for the Color to be used for the background of headers. */
    public static final String COLOR_HEADER_BACKGROUND = "it.tidalwave.netbeans.header.background";
    
    /** The UIManager key for the Color to be used for the foreground of headers. */
    public static final String COLOR_HEADER_FOREGROUND = "it.tidalwave.netbeans.header.foreground";
    
    /** The UIManager key for the Color to be used for the background of headers (dark variant). */
    public static final String COLOR_HEADER_DARK_BACKGROUND = "it.tidalwave.netbeans.darkheader.background";
    
    /** The UIManager key for the Color to be used for the foreground of headers (dark variant). */
    public static final String COLOR_HEADER_DARK_FOREGROUND = "it.tidalwave.netbeans.darkheader.foreground";
    
    public static final String COLOR_HEADER_DARK_FOREGROUND2 = "it.tidalwave.netbeans.darkheader.foreground2";
    
    /** The UIManager key for the Font to be used with Explorers. */
    public static final String FONT_EXPLORER = "it.tidalwave.netbeans.explorer.font";
    
    /** The UIManager key for the Font to be used with headers. */
    public static final String FONT_HEADER = "it.tidalwave.netbeans.header.font";
    
    /** The UIManager key for the Font to be used with headers (smnall variant). */
    public static final String FONT_HEADER_SMALL = "it.tidalwave.netbeans.header.smallfont";
    
    public static final String FONT_SMALL = "it.tidalwave.netbeans.smallfont";
    
    public static final String UI_HEADER_PANEL = "it.tidalwave.netbeans.headerpanelui";

    public static final String UI_DARK_VIEW_PANEL = "it.tidalwave.netbeans.darkviewpanelui";

    private static final Map, ComponentUI> UI_CLASS_MAP =
            new HashMap, ComponentUI>();

    /***************************************************************************
     *
     *
     **************************************************************************/
    private LookAndFeel() 
      {
      }

    /***************************************************************************
     *
     *
     **************************************************************************/
    public static void registerDarkUI (final Class componentClass,
                                       final ComponentUI componentUI)
      {
        UI_CLASS_MAP.put(componentClass, componentUI);
      }
    
    /***************************************************************************
     *
     * Sets the properties for a View that lies in the main window.
     *
     **************************************************************************/
    public static void installDarkViewLAF (final JComponent parent)
      {
        logger.info("installDarkViewLAF(" + parent + ")");
        final JComponent view = findView(parent);
        logger.fine(">>>>> view: " + view);

        if (view != null)
          {
            view.setFont(UIManager.getFont(FONT_EXPLORER));
            view.setBackground(UIManager.getColor(COLOR_EXPLORER_BACKGROUND));
            view.setForeground(UIManager.getColor(COLOR_EXPLORER_FOREGROUND));
            view.putClientProperty("Tree.selectionForeground", UIManager.getColor(COLOR_EXPLORER_SELECTION_FOREGROUND));
            view.putClientProperty("Tree.selectionBackground", UIManager.getColor(COLOR_EXPLORER_SELECTION_BACKGROUND));
            installDarkLAF(view);

            final Border border = UIManager.getBorder(BORDER_EXPLORER);

            if (border != null)
              {
                view.setBorder(border);
              }
          }
      }

    /***************************************************************************
     *
     *
     **************************************************************************/
    private static JComponent findView (final JComponent component)
      {
        logger.finer("findView(" + component + ")");

        if (isView(component.getClass()))
          {
            return component;
          }

        for (final Component child : component.getComponents())
          {
            if (child instanceof JComponent)
              {
                final JComponent view = findView((JComponent)child);

                if (view != null)
                  {
                    return view;
                  }
              }
          }

        return null;
      }

    /***************************************************************************
     *
     *
     **************************************************************************/
    private static boolean isView (final Class clazz)
      {
        logger.finer("isView(" + clazz + ")");
        final String className = clazz.getName();
        
        if (className.equals("org.openide.explorer.view.ListView") || className.equals("org.openide.explorer.view.TreeView"))
          {
            return true;
          }

        if (clazz.getSuperclass() == null)
          {
            return false;
          }

        return isView(clazz.getSuperclass());
      }

    /***************************************************************************
     *
     *
     **************************************************************************/
    public static void installHeaderUI (final JPanel pnHeader) 
      { 
        pnHeader.setUI(HeaderPanelUI.createUI(pnHeader));
      }

    /***************************************************************************
     *
     *
     **************************************************************************/
    @Nonnull
    private static Method findSetUIMethod (@Nonnull final Class componentClass,
                                           @Nonnull final Class uiClass)
      throws NoSuchMethodException
      {
        for (Class c = uiClass; c != null; c = c.getSuperclass())
          {
            try
              {
                return componentClass.getMethod("setUI", c);
              }
            catch (NoSuchMethodException e)
              {
//                Exceptions.printStackTrace(ex);
              }
            catch (SecurityException e)
              {
//                Exceptions.printStackTrace(ex);
              }
          }

        throw new NoSuchMethodException();
      }

    /***************************************************************************
     *
     *
     **************************************************************************/
    public static void installDarkLAF (final Component component)
      {
        final Class clazz = component.getClass();
        final ComponentUI componentUI = UI_CLASS_MAP.get(clazz);

        if (componentUI != null)
          {
            try
              {
                final Method setUIMethod = findSetUIMethod(clazz, componentUI.getClass());
                setUIMethod.invoke(component, componentUI);
              }
            catch (NoSuchMethodException e)
              {
              }
            catch (Exception e)
              {
                e.printStackTrace(); // FIXME
              }
          }

//        if ((c instanceof JScrollBar) || c.getClass().getName().equals("javax.swing.JScrollPane$ScrollBar"))
        else if (component instanceof JComponent)
          {
            try 
              {
                final Method setUIMethod = clazz.getMethod("setUI", ScrollBarUI.class);
                setUIMethod.invoke(component, BlueMarineScrollBarUI.createUI((JComponent)component));
              } 
            catch (NoSuchMethodException e) 
              {
              }
            catch (Exception e) 
              {
                e.printStackTrace(); // FIXME
              }

//            if (c instanceof JToggleButton) FIXME doesn't look good
//              {
//                ((JToggleButton)c).setUI(BlueMarineToggleButtonUI.createUI((JComponent)c));
//              }
            
            for (final Component child : ((JComponent) component).getComponents())
              {
                installDarkLAF(child);
              }
          }
      }
  }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy