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

Java example source code file (State.java)

This example Java source code file (State.java) is included in the alvinalexander.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Learn more about this Java project at its project page.

Java - Java tags/keywords

assertionerror, default, disabled, enabled, focused, gui, hashmap, mouseover, override, pressed, selected, standardstate, state, string, stringbuffer, swing, util

The State.java Java example source code

/*
 * Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */
package javax.swing.plaf.nimbus;

import java.util.HashMap;
import java.util.Map;
import javax.swing.JComponent;
import javax.swing.plaf.synth.SynthConstants;

/**
 * <p>Represents a built in, or custom, state in Nimbus.

* * <p>Synth provides several built in states, which are: * <ul> * <li>Enabled * <li>Mouse Over * <li>Pressed * <li>Disabled * <li>Focused * <li>Selected * <li>Default * </ul> * * <p>However, there are many more states that could be described in a LookAndFeel, and it * would be nice to style components differently based on these different states. * For example, a progress bar could be "indeterminate". It would be very convenient * to allow this to be defined as a "state".</p> * * <p>This class, State, is intended to be used for such situations. * Simply implement the abstract #isInState method. It returns true if the given * JComponent is "in this state", false otherwise. This method will be called * <em>many times in performance sensitive loops. It must execute * very quickly.</p> * * <p>For example, the following might be an implementation of a custom * "Indeterminate" state for JProgressBars:</p> * * <pre> * public final class IndeterminateState extends State<JProgressBar> { * public IndeterminateState() { * super("Indeterminate"); * } * * @Override * protected boolean isInState(JProgressBar c) { * return c.isIndeterminate(); * } * } * </code>
*/ public abstract class State<T extends JComponent>{ static final Map<String, StandardState> standardStates = new HashMap(7); static final State Enabled = new StandardState(SynthConstants.ENABLED); static final State MouseOver = new StandardState(SynthConstants.MOUSE_OVER); static final State Pressed = new StandardState(SynthConstants.PRESSED); static final State Disabled = new StandardState(SynthConstants.DISABLED); static final State Focused = new StandardState(SynthConstants.FOCUSED); static final State Selected = new StandardState(SynthConstants.SELECTED); static final State Default = new StandardState(SynthConstants.DEFAULT); private String name; /** * <p>Create a new custom State. Specify the name for the state. The name should * be unique within the states set for any one particular component. * The name of the state should coincide with the name used in UIDefaults.</p> * * <p>For example, the following would be correct:

* <pre> * defaults.put("Button.States", "Enabled, Foo, Disabled"); * defaults.put("Button.Foo", new FooState("Foo")); * </code> * * @param name a simple user friendly name for the state, such as "Indeterminate" * or "EmbeddedPanel" or "Blurred". It is customary to use camel case, * with the first letter capitalized. */ protected State(String name) { this.name = name; } @Override public String toString() { return name; } /** * <p>This is the main entry point, called by NimbusStyle.

* * <p>There are both custom states and standard states. Standard states * correlate to the states defined in SynthConstants. When a UI delegate * constructs a SynthContext, it specifies the state that the component is * in according to the states defined in SynthConstants. Our NimbusStyle * will then take this state, and query each State instance in the style * asking whether isInState(c, s).</p> * * <p>Now, only the standard states care about the "s" param. So we have * this odd arrangement:</p> * <ul> * <li>NimbusStyle calls State.isInState(c, s) * <li>State.isInState(c, s) simply delegates to State.isInState(c) * <li>EXCEPT, StandardState overrides State.isInState(c, s) and * returns directly from that method after checking its state, and * does not call isInState(c) (since it is not needed for standard states).</li> * </ul> */ boolean isInState(T c, int s) { return isInState(c); } /** * <p>Gets whether the specified JComponent is in the custom state represented * by this class. <em>This is an extremely performance sensitive loop. * Please take proper precautions to ensure that it executes quickly.</p> * * <p>Nimbus uses this method to help determine what state a JComponent is * in. For example, a custom State could exist for JProgressBar such that * it would return <code>true
when the progress bar is indeterminate. * Such an implementation of this method would simply be:</p> * * <pre> return c.isIndeterminate(); * * @param c the JComponent to test. This will never be null. * @return true if <code>c
is in the custom state represented by * this <code>State instance */ protected abstract boolean isInState(T c); String getName() { return name; } static boolean isStandardStateName(String name) { return standardStates.containsKey(name); } static StandardState getStandardState(String name) { return standardStates.get(name); } static final class StandardState extends State<JComponent> { private int state; private StandardState(int state) { super(toString(state)); this.state = state; standardStates.put(getName(), this); } public int getState() { return state; } @Override boolean isInState(JComponent c, int s) { return (s & state) == state; } @Override protected boolean isInState(JComponent c) { throw new AssertionError("This method should never be called"); } private static String toString(int state) { StringBuffer buffer = new StringBuffer(); if ((state & SynthConstants.DEFAULT) == SynthConstants.DEFAULT) { buffer.append("Default"); } if ((state & SynthConstants.DISABLED) == SynthConstants.DISABLED) { if (buffer.length() > 0) buffer.append("+"); buffer.append("Disabled"); } if ((state & SynthConstants.ENABLED) == SynthConstants.ENABLED) { if (buffer.length() > 0) buffer.append("+"); buffer.append("Enabled"); } if ((state & SynthConstants.FOCUSED) == SynthConstants.FOCUSED) { if (buffer.length() > 0) buffer.append("+"); buffer.append("Focused"); } if ((state & SynthConstants.MOUSE_OVER) == SynthConstants.MOUSE_OVER) { if (buffer.length() > 0) buffer.append("+"); buffer.append("MouseOver"); } if ((state & SynthConstants.PRESSED) == SynthConstants.PRESSED) { if (buffer.length() > 0) buffer.append("+"); buffer.append("Pressed"); } if ((state & SynthConstants.SELECTED) == SynthConstants.SELECTED) { if (buffer.length() > 0) buffer.append("+"); buffer.append("Selected"); } return buffer.toString(); } } }

Other Java examples (source code examples)

Here is a short list of links related to this Java State.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 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.