alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 *                 Sun Public License Notice
 *
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 *
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2000 Sun
 * Microsystems, Inc. All Rights Reserved.
 */

package org.netbeans.core.ui;

import java.awt.event.*;
import java.awt.Graphics;
import java.awt.Dimension;
import javax.swing.UIManager;
import javax.swing.JLabel;

/**
 * A lightweight component creating a focusable Label. 
 * NbLabelA11y can be focusable or not.
 * Accessibility Name and Description could be set by
 * two parameters A11yName and A11yDesc.
 *
 * @author  Dusan Pavlica
 */
public class NbLabelA11y extends JLabel {
    
    private boolean isFocusable = false;
    
    private boolean isFocused = false;
 
   /** Creates new NbLabelA11y and not focusable*/
    public NbLabelA11y() {
        super();
    }

   /** Creates new NbLabelA11y and set text of label*/
    public NbLabelA11y(String label) {
        this();
        setText(label);
    }
 
    /** Creates new NbLabelA11y and set his style of focusing.
     * Accessibility Name and Description could be set too.
     */
    public NbLabelA11y(boolean focusable) {
        this();
        setA11yFocus(focusable);
    }
    
    /** Creates new NbLabelA11y and set his style of focusing.
     * Accessibility Name and Description could be set too.
     */
    public NbLabelA11y(boolean isFocusable, String a11yName, String a11yDesc) {
        this(isFocusable);
        setA11yContext(a11yName, a11yDesc);
    }
 
    public void setA11yContext(String a11yName, String a11yDesc) {
        if (a11yName == "" && (getAccessibleContext().getAccessibleName() == null) && getText() != "")
            getAccessibleContext().setAccessibleName(getText());
        if (a11yDesc == "" && (getAccessibleContext().getAccessibleDescription() == null) && getToolTipText() != "")
            getAccessibleContext().setAccessibleDescription(getToolTipText());        
        if (a11yName != "") getAccessibleContext().setAccessibleName(a11yName); 
        if (a11yDesc != "") getAccessibleContext().setAccessibleDescription(a11yDesc);
    }    
   
    /** Set if component is focusable
     */    
    public void setA11yFocus(boolean a11yFocus) {
        isFocusable = a11yFocus;
        setRequestFocusEnabled(a11yFocus);
        //add listeners if a11yFocus is true
        if (a11yFocus) {
           addFocusListener(
            new FocusListener() {
                public void focusGained(FocusEvent event) {
                    isFocused = true;
                    repaint();
                }
                public void focusLost(FocusEvent event) {
                    isFocused = false;
                    repaint();
                }
            });
        }
        repaint();
    }    

    /* Used for setting of focus and accessibility together
     */
    public void setA11yAll(boolean a11yFocus, String a11yName, String a11yDesc) {
        setA11yFocus(a11yFocus);
        setA11yContext(a11yName, a11yDesc);
    }
    
    /**
     * Identifies whether or not this component can receive the focus.
     *
     * @return true if this component can receive the focus (isFocusable=true)
     */
    public boolean isFocusTraversable() {
        return (isFocusable);
    }
    
    /**
     * If the UI delegate is non-null, calls its paint
     * method. If isFocusable=true and showFocuse=true then
     * Border will be painted.
     *
     */
    public void paintComponent(Graphics g) {
        if (ui != null) {
            try {
                ui.update(g, this);
            }
            finally {
                g.dispose();
            }
        }
        
        if (isFocused) {
            Dimension size = getSize();
            g.setColor(UIManager.getColor("Button.focus")); // NOI18N
            g.drawRect(0, 0, size.width-1, size.height-1);
        }
    }
    
}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.