001: /*
002: * Copyright (c) 2003-2007 JGoodies Karsten Lentzsch. All Rights Reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * o Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * o Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * o Neither the name of JGoodies Karsten Lentzsch nor the names of
015: * its contributors may be used to endorse or promote products derived
016: * from this software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
020: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
021: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
022: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
025: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
026: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
027: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
028: * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030:
031: package com.jgoodies.validation.tutorial.util;
032:
033: import java.awt.Component;
034: import java.awt.FocusTraversalPolicy;
035:
036: import javax.swing.JRootPane;
037: import javax.swing.JScrollBar;
038: import javax.swing.LayoutFocusTraversalPolicy;
039: import javax.swing.text.JTextComponent;
040:
041: /**
042: * A FocusTraversalPolicy that determines traversal order based on the order
043: * of child Components in a Container. In addition to its superclass,
044: * this class excludes non-editable text components, scroll bars, and the
045: * root pane from the focus cycle. The JGoodies User Interface Framework (UIF)
046: * contains an extended version of this policy.
047: *
048: * @author Karsten Lentzsch
049: * @version $Revision: 1.8 $
050: * @since 1.4
051: *
052: * @see JTextComponent#isEditable()
053: */
054: public final class MyFocusTraversalPolicy extends
055: LayoutFocusTraversalPolicy {
056:
057: /**
058: * Holds the single instance of this focus traversal policy.
059: *
060: * @see java.awt.KeyboardFocusManager#setDefaultFocusTraversalPolicy(java.awt.FocusTraversalPolicy)
061: */
062: public static final FocusTraversalPolicy INSTANCE = new MyFocusTraversalPolicy();
063:
064: // Instance Creation ******************************************************
065:
066: /**
067: * Constructs a UIFFocusTraversalPolicy with no initial component set.
068: */
069: private MyFocusTraversalPolicy() {
070: // Overrides default constructor; ensuring non-instantiability.
071: }
072:
073: // ************************************************************************
074:
075: /**
076: * Determines whether a Component is an acceptable choice as the new
077: * focus owner. By default, this method will accept a Component if and
078: * only if it is visible, displayable, enabled, and focusable,
079: * no JScrollBar, no JRootPane, and in case of a JTextComponent,
080: * it must be editable.
081: *
082: * @param aComponent the Component whose fitness as a focus owner is to
083: * be tested
084: * @return <code>true</code> if aComponent is visible, displayable,
085: * enabled, and focusable; <code>false</code> otherwise
086: */
087: @Override
088: protected boolean accept(Component aComponent) {
089: if (!super .accept(aComponent)) {
090: return false;
091: } else if (aComponent instanceof JTextComponent) {
092: JTextComponent textComponent = (JTextComponent) aComponent;
093: return textComponent.isEditable();
094: } else if (aComponent instanceof JScrollBar) {
095: return false;
096: } else if (aComponent instanceof JRootPane) {
097: return false;
098: } else {
099: return true;
100: }
101: }
102:
103: }
|