|
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.FocusManager;
import javax.swing.UIManager;
import javax.swing.JTextArea;
/**
* A lightweight component creating a focusable TextArea.
* NbTextAreaA11y can be focusable or not.
* Accessibility Name and Description could be set by
* two parameters A11yName and A11yDesc.
*
* @author Dusan Pavlica
*/
public class NbTextAreaA11y extends JTextArea {
private boolean isFocusable = false;
private boolean isFocused = false;
private boolean showFocus = false;
private boolean tabWasReleased = false;
/** Creates new NbTextAreaA11y without parameters
*/
public NbTextAreaA11y() {
super();
}
/** Creates new NbTextAreaA11y and set text of label
*/
public NbTextAreaA11y(String label) {
this();
this.setText(label);
}
/** Creates new NbTextAreaA11y and set his focusing
*/
public NbTextAreaA11y(boolean focusable) {
this();
isFocusable = showFocus = focusable;
setA11yFocus(isFocusable);
}
/** Creates new NbTextAreaA11y and set focusing.
* Accessibility Name and Description could be set too.
*/
public NbTextAreaA11y(boolean isFocusable, String a11yName, String a11yDesc) {
this(isFocusable);
setA11yContext(a11yName, a11yDesc);
}
/** Set accessibility Name and accessibility Description
*/
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 focus by component and parameter showFocus by calling setA11FocusShow()
*/
public void setA11yFocus(boolean a11yFocus) {
isFocusable = a11yFocus;
setA11yFocusShow (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();
}
});
// Add Listener because of issue #15139
addKeyListener(new KeyListener() {
public void keyTyped(KeyEvent e) {
if (e.getKeyCode()==java.awt.event.KeyEvent.VK_TAB && tabWasReleased) {
a11yFocusNext();
tabWasReleased=false;
}
}
public void keyReleased(KeyEvent e) {
if (e.getKeyCode()==java.awt.event.KeyEvent.VK_TAB && tabWasReleased) {
e.consume();
a11yFocusNext();
tabWasReleased=false;
}
}
public void keyPressed(KeyEvent e) {
tabWasReleased=true;
if (e.getKeyCode()==java.awt.event.KeyEvent.VK_TAB) e.consume();
}
});
repaint();
}
}
/* This method could be used for set border of component when is focudsed
*/
public void setA11yFocusShow (boolean showFocusBorder) {
showFocus = showFocusBorder;
}
/* Used for setting of focus and accessibility together
*/
public void setA11yAll(boolean a11yFocus, String a11yName, String a11yDesc) {
setA11yFocus(a11yFocus);
setA11yContext(a11yName, a11yDesc);
repaint();
}
/** Provide focus of next component
*/
public void a11yFocusNext() {
FocusManager focusManager = FocusManager.getCurrentManager();
focusManager.focusNextComponent(this);
}
/** 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);
}
/** About manages focus.
*/
public boolean isManagingFocus() {
return false; //this has to be used because of issue #15139
}
/**
* If the UI delegate is non-null, calls its paint
* method. If isFocus=true then Border will be painted.
*/
public void paintComponent(Graphics g) {
if (ui != null) {
try {
ui.update(g, this);
}
finally {
g.dispose();
}
}
if (isFocused && showFocus) {
Dimension size = getSize();
g.setColor(UIManager.getColor("Button.focus")); // NOI18N
g.drawRect(0, 0, size.width-1, size.height-1);
}
}
}
|