Package ca.cgjennings.apps.arkham
Class ContextBar.CommandButton
- java.lang.Object
-
- ca.cgjennings.apps.arkham.ContextBar.CommandButton
-
- All Implemented Interfaces:
ContextBar.Button
,IconProvider
,java.awt.event.ActionListener
,java.util.EventListener
- Enclosing class:
- ContextBar
public static class ContextBar.CommandButton extends java.lang.Object implements ContextBar.Button
A context bar button that delegates to anAbstractCommand
. The button will activate the command when clicked, and be enabled or disabled based on the current state of the command. The base class implementation will create a button that is always visible and performs no action when the context bar is attached or detached.
-
-
Constructor Summary
Constructors Constructor Description CommandButton(AbstractCommand command)
Creates a newCommandButton
for the specified command.CommandButton(java.lang.String id, AbstractCommand command)
Creates a newCommandButton
for the specified command.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
actionPerformed(java.awt.event.ActionEvent e)
Activates the command associated with this button.AbstractCommand
getCommand()
Returns the command that this button is a proxy for.javax.swing.Icon
getIcon()
Returns the button's icon.java.lang.String
getID()
Returns a unique ID for this button, such as "BOLD".java.lang.String
getName()
Returns the button's name, ornull
.ContextBar.CommandButton
hideIfDisabled()
Causes the default implementation ofisVisibleInCurrentContext(ca.cgjennings.apps.arkham.ContextBar.Context)
to return the same value asisEnabledInCurrentContext(ca.cgjennings.apps.arkham.ContextBar.Context)
.boolean
isEnabledInCurrentContext(ContextBar.Context context)
Returnstrue
if the button should be enabled in the current context.boolean
isVisibleInCurrentContext(ContextBar.Context context)
Returnstrue
if the button should be visible in the current context.void
onAttach(ContextBar.Context context)
This method is called to notify the button when the tool bar is about to be attached to a new target component (the component that it floats over and that its commands normally apply to).void
onDetach(ContextBar.Context context)
This method is called to notify the button when the tool bar is about to be detached from the current target component (the component that it floats over and that its commands normally apply to).java.lang.String
toString()
Returns a string representation of the button suitable for debugging.protected void
update()
Updates the state of the command that this button is a proxy for.
-
-
-
Constructor Detail
-
CommandButton
public CommandButton(AbstractCommand command)
Creates a newCommandButton
for the specified command. The button's unique ID will be determined by reading the command'sAbstractCommand.BUTTON_ID_KEY
key.- Parameters:
command
- the command to generate a button for- Throws:
java.lang.NullPointerException
- if the command isnull
or the command does not define a button ID value
-
CommandButton
public CommandButton(java.lang.String id, AbstractCommand command)
Creates a newCommandButton
for the specified command. The command will use the specified ID.- Parameters:
command
- the command to generate a button forid
- the unique ID to use for the button- Throws:
java.lang.NullPointerException
- if the command isnull
-
-
Method Detail
-
getCommand
public final AbstractCommand getCommand()
Returns the command that this button is a proxy for.- Returns:
- the command used to create this button
-
getID
public java.lang.String getID()
Description copied from interface:ContextBar.Button
Returns a unique ID for this button, such as "BOLD". Only letters, digits, underscores, and currency symbols may appear in an ID.- Specified by:
getID
in interfaceContextBar.Button
- Returns:
- a short ID that will identify this button in the application preferences
-
getName
public java.lang.String getName()
Description copied from interface:ContextBar.Button
Returns the button's name, ornull
.- Specified by:
getName
in interfaceContextBar.Button
- Returns:
- a localized description of the button's function
-
getIcon
public javax.swing.Icon getIcon()
Description copied from interface:ContextBar.Button
Returns the button's icon.- Specified by:
getIcon
in interfaceContextBar.Button
- Specified by:
getIcon
in interfaceIconProvider
- Returns:
- a small transparent icon used to represent the button
-
onAttach
public void onAttach(ContextBar.Context context)
Description copied from interface:ContextBar.Button
This method is called to notify the button when the tool bar is about to be attached to a new target component (the component that it floats over and that its commands normally apply to).- Specified by:
onAttach
in interfaceContextBar.Button
- Parameters:
context
- information about the current context
-
onDetach
public void onDetach(ContextBar.Context context)
Description copied from interface:ContextBar.Button
This method is called to notify the button when the tool bar is about to be detached from the current target component (the component that it floats over and that its commands normally apply to).- Specified by:
onDetach
in interfaceContextBar.Button
- Parameters:
context
- information about the current context
-
isEnabledInCurrentContext
public boolean isEnabledInCurrentContext(ContextBar.Context context)
Description copied from interface:ContextBar.Button
Returnstrue
if the button should be enabled in the current context.- Specified by:
isEnabledInCurrentContext
in interfaceContextBar.Button
- Parameters:
context
- information about the current context- Returns:
true
if the button should be enabled (ignored if not visible)
-
isVisibleInCurrentContext
public boolean isVisibleInCurrentContext(ContextBar.Context context)
Description copied from interface:ContextBar.Button
Returnstrue
if the button should be visible in the current context. A button should be invisible if it can never be enabled in the current context, and disabled if it is not currently usable due to a temporary restriction. For example, any text field can be copied from if there is an active selection, so a copy button should always be visible but should be enabled only if there is a selection. On the other hand, a command that only works for script code should be visible only when editing a script.- Specified by:
isVisibleInCurrentContext
in interfaceContextBar.Button
- Parameters:
context
- information about the current context- Returns:
true
if the button should be visible
-
hideIfDisabled
public ContextBar.CommandButton hideIfDisabled()
Causes the default implementation ofisVisibleInCurrentContext(ca.cgjennings.apps.arkham.ContextBar.Context)
to return the same value asisEnabledInCurrentContext(ca.cgjennings.apps.arkham.ContextBar.Context)
. (The default behaviour is to simply returntrue
.)- Returns:
- this command button
-
update
protected void update()
Updates the state of the command that this button is a proxy for. If this is called more than once while the same event is being dispatched, it will only update the actual command once (the first time it is called for that event). This allows this method to be called multiple times while determining the button state without a significant performance penalty, even if the command's update process is complex.
-
actionPerformed
public void actionPerformed(java.awt.event.ActionEvent e)
Activates the command associated with this button.- Specified by:
actionPerformed
in interfacejava.awt.event.ActionListener
- Parameters:
e
- the action event for the button activation
-
toString
public java.lang.String toString()
Returns a string representation of the button suitable for debugging. The returned string is Button<id>, where id is the button's ID as returned byContextBar.Button.getID()
.- Overrides:
toString
in classjava.lang.Object
- Returns:
- a string representation of the button
-
-