Save This Page
Home » openjdk-7 » net.jbeans » ui » border » [javadoc | source]
    1   /* -------------------------------------------------------------------
    2    * Java source file for the class CustomBorderFactory
    3    * 
    4    * Copyright (c), 2002, Masahiro Takatsuka.
    5    * All Rights Researved.
    6    * 
    7    * Original Author: Masahiro Takatsuka (masa@jbeans.net)
    8    * $Author: takatsukam $
    9    * 
   10    * $Date: 2003/07/25 04:51:46 $
   11    * 
   12    * $Id: CustomBorderFactory.java,v 1.1.1.1 2003/07/25 04:51:46 takatsukam Exp $
   13    * 
   14    * Reference:		Document no:
   15    * ___				___
   16    * 
   17    * To Do:
   18    * ___
   19    * 
   20   ------------------------------------------------------------------- */
   21   
   22   /* --------------------------- Package ---------------------------- */
   23   package net.jbeans.ui.border;
   24   
   25   /* ------------------ Import classes (packages) ------------------- */
   26   import java.awt;
   27   
   28   import javax.swing;
   29   import javax.swing.border;
   30   import javax.swing.plaf.basic;
   31   
   32   /*====================================================================
   33                   Implementation of class CustomBorderFactory                 
   34   ====================================================================*/
   35   /**
   36    * CustomBorderFactory can be used to create a border object.
   37    * 
   38    * @version $Revision: 1.1.1.1 $
   39    * @author Masahiro Takatsuka (masa@jbeans.net)
   40    */
   41   
   42   public final class CustomBorderFactory {
   43   	private CustomBorderFactory() {
   44   	}
   45   	
   46       /** 
   47        * Creates an etched border with the displayed text
   48        */
   49       public static Border createBorder(String text, int justification, int orientation) {
   50           Border border = BorderFactory.createEtchedBorder();
   51           
   52           return BorderFactory.createTitledBorder(border, text, 
   53   												justification,
   54   												orientation);
   55       }
   56   
   57       /** 
   58        * Creates an etched border with the displayed text
   59        */
   60       public static Border createBorder(String text) {
   61   		return createBorder(text, TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.TOP);
   62       }
   63       
   64       /** 
   65        * Creates an blank border.
   66        */
   67       public static Border createBorder() {
   68           return BorderFactory.createEmptyBorder(4, 4, 4, 4);
   69       }
   70   
   71   	private static Border flatButtonBorder;
   72   
   73   	public static Border createFlatButtonBorder() {
   74   		if(flatButtonBorder == null) {
   75   			flatButtonBorder
   76   				= new CompoundBorder(new FlatButtonBorder(UIManager.getColor("controlShadow"), UIManager.getColor("controlLtHighlight")), new BasicBorders.MarginBorder());
   77   		}
   78   		return flatButtonBorder;
   79   	}
   80   	
   81   	public static abstract class AbstractFlatBorder extends AbstractBorder {
   82   		protected Color darkShadow;
   83   		protected Color highlight;
   84   		public final int FLAT = 0;
   85   		public final int RAISED = 1;
   86   		public final int LOWERED = 2;
   87   		
   88   		
   89   		protected AbstractFlatBorder(Color darkShadow, Color highlight) {
   90   			this.darkShadow = darkShadow;
   91   			this.highlight = highlight;
   92   		}
   93   		
   94   		public void paintBorder(Component c, Graphics g, int x, int y,
   95   								int width, int height) {
   96   			switch(getBorderType(c)) {
   97   			case FLAT:
   98   				break;
   99   			case RAISED:
  100   				paintFlatBorder(g, false, x, y, width, height,
  101   								darkShadow, highlight);
  102   				break;
  103   			case LOWERED:
  104   				paintFlatBorder(g, true, x, y, width, height,
  105   								darkShadow, highlight);
  106   				break;
  107   			}
  108   		}
  109   		
  110   		protected abstract int getBorderType(Component c);
  111   		
  112   		protected void paintFlatBorder(Graphics g, boolean isLowered,
  113   									   int x, int y, int w, int h,
  114   									   Color darkShadow, Color highlight) {
  115   			Color oldColor = g.getColor();
  116   			g.translate(x, y);
  117   			
  118   			if(isLowered) {
  119   				g.setColor(darkShadow);
  120   			} else {
  121   				g.setColor(highlight);
  122   			}
  123   			g.drawLine(0, 0, 0, h-1);
  124   			g.drawLine(1, 0, w-2, 0);
  125   			
  126   			if(isLowered) {
  127   				g.setColor(highlight);
  128   			} else {
  129   				g.setColor(darkShadow);
  130   			}
  131   			g.drawLine(0, h-1, w-1, h-1);
  132   			g.drawLine(w-1, h-1, w-1, 0);
  133   			
  134   			g.translate(-x, -y);
  135   			g.setColor(oldColor);
  136   		}
  137   	}
  138   	
  139   	public static class FlatButtonBorder
  140   		extends AbstractFlatBorder {
  141   		public FlatButtonBorder(Color darkShadow, Color highlight) {
  142   			super(darkShadow, highlight);
  143   		}
  144   		
  145   		protected int getBorderType(Component c) {
  146   			boolean isRollover = false;
  147   			boolean isPressed = false;
  148   			boolean isSelected = false;
  149   			
  150   			if (c instanceof AbstractButton) {
  151   				AbstractButton b = (AbstractButton)c;
  152   				ButtonModel model = b.getModel();
  153   				isRollover = model.isRollover();
  154   				isPressed = model.isPressed() && model.isArmed();
  155   				isSelected = model.isSelected();
  156   			}
  157   			
  158   			if(isSelected) {
  159   				return LOWERED;
  160   			} else {
  161   				if(isRollover) {
  162   					if(isPressed) {
  163   						return LOWERED;
  164   					} else {
  165   						return RAISED;
  166   					}
  167   				} else {
  168   					return FLAT;
  169   				}
  170   			}
  171   		}  
  172   	}
  173   }

Save This Page
Home » openjdk-7 » net.jbeans » ui » border » [javadoc | source]