public class Action extends Object
Usually, an application provides several ways to let users execute an operation, for example the "Open File..." shows up in Menus, Toolbars, and elsewhere. If an operation can be executed from several GUI places, it seems logical that such different places appear similar to the user. Namely, they should have the same textual label, the same icon, and certainly the same accelerator key. This way, if the user is accustomed to executing an operation from the Toolbar and then sees the equivalent operation in as a MenuItem with the same icon and label, she will know that both will do the same thing. It thus seems reasonable that both MenuItem and ToolButton share some Object where the common information is stored, and this is indeed the role of the Action class.
An Action holds information about an operation that could be executed from different places. This information includes the label that is shown to the user, the icon, and the tooltip (popup help message). It also stores state information, such as whether the action is "visible" or "sensitive". When this information changes, all the Action proxies (i.e. the Widgets such as MenuItems or ToolButtons related to it) are also all changed properly.
Each Action has an Action.Activate
signal emitted when any of its
proxies are triggered. Thus you can just connect one signal to start the
needed operation, instead of having to connect to the
Action.Activate
signal of each of various proxies.
Once you have created an Action, you can get proxies for it with the
createMenuItem()
and createToolItem()
methods.
Incidentally, you can still use Actions even if you only plan to let the user execute operations from a single place in your application; they're a useful technique to concentrate the code handling of user activity.
ActionGroup
Modifier and Type | Class and Description |
---|---|
static interface |
Action.Activate
Signal emitted when the Action is activated.
|
Constructor and Description |
---|
Action(String name,
Stock stock)
Create a new Action from a Stock item.
|
Action(String name,
Stock stock,
Action.Activate handler)
Create a new Action based on a Stock item, and connect a handler to its
Action.Activate signal. |
Action(String name,
String label)
Create a new Action.
|
Action(String name,
String label,
Action.Activate handler)
Create a new Action, and connect a handler to its
Action.Activate signal. |
Action(String name,
String label,
String tooltip,
Icon icon)
Create a new Action.
|
Action(String name,
String label,
String tooltip,
Icon icon,
Action.Activate handler)
Create a new Action, and connect a handler to its
Action.Activate signal. |
Action(String name,
String label,
String tooltip,
Stock stock)
Create a new Action.
|
Action(String name,
String label,
String tooltip,
Stock stock,
Action.Activate handler)
Create a new Action, and connect a handler to its
Action.Activate signal. |
Modifier and Type | Method and Description |
---|---|
void |
connect(Action.Activate handler)
Connect a handler to the
Action.Activate signal. |
MenuItem |
createMenuItem()
Create a MenuItem from this Action.
|
ToolItem |
createToolItem()
Creates a ToolItem from this Action.
|
void |
emitActivate()
Activates the Action.
|
boolean |
getSensitive()
Return whether this Action itself is enabled to be activated by the
user.
|
String |
getTooltip()
Get the tooltip for the Action.
|
boolean |
getVisible()
Return whether the Action itself is visible.
|
boolean |
isSensitive()
Returns whether the action is effectively sensitive, i.e., if both the
Action and the ActionGroup it belongs to are enabled.
|
boolean |
isVisible()
Is the Action visible on the screen?
|
void |
setAccelerator(AcceleratorGroup group,
Keyval keyval,
ModifierType modifier)
Specify a key binding to trigger this Action.
|
void |
setSensitive(boolean sensitive)
Set if the Action is available to be activated by the user.
|
void |
setTooltip(String tooltip)
Set a tooltip (little help message appearing in a hover) for the
Action.
|
void |
setVisible(boolean visible)
Set whether the Action is visible to users.
|
public Action(String name, Stock stock)
public Action(String name, Stock stock, Action.Activate handler)
Action.Activate
signal. Complements the
<init>(String, Stock)
constructor.public Action(String name, String label)
name
- A unique name for the Action.label
- The text that will be displayed in the proxy Widgets. You
usually will want to localize it to the user language.public Action(String name, String label, Action.Activate handler)
Action.Activate
signal.name
- A unique name for the Action.label
- The label that will be displayed in the proxy Widgets. You
usually will want to localize it to the user language.handler
- A handler to connect to the Action.Activate
signal. Usually will will start from here the operation
related to the Action.public Action(String name, String label, String tooltip, Icon icon)
name
- A unique name for the Action.label
- The text that will be displayed in the proxy Widgets. You
usually will want to localize it to the user language.tooltip
- A Tooltip or little help message for the Action. Also
localized.icon
- The Icon to display in proxy Widgets.public Action(String name, String label, String tooltip, Icon icon, Action.Activate handler)
Action.Activate
signal.name
- A unique name for the Action.label
- The text that will be displayed in the proxy Widgets. You
usually will want to localize it to the user language.tooltip
- A Tooltip or little help message for the Action. Also
localized.icon
- The Icon to display in proxy Widgets.handler
- A handler to connect to the Action.Activate
signal. Typically this will be used to actually start the
operation related to this Action.public Action(String name, String label, String tooltip, Stock stock)
name
- A unique name for the Action.label
- The text that will be displayed in the proxy Widgets. You
usually will want to localize it to the user language.tooltip
- A Tooltip or little help message for the Action. Also
localized.stock
- The Stock icon to display in proxy Widgets.public Action(String name, String label, String tooltip, Stock stock, Action.Activate handler)
Action.Activate
signal.name
- A unique name for the Action.label
- The text that will be displayed in the proxy Widgets. You
usually will want to localize it to the user language.tooltip
- A Tooltip or little help message for the Action. Also
localized.stock
- The stock icon to display in proxy Widgets.handler
- A handler to connect to the Action.Activate
signal. Typically this will be used to actually start the
operation related to this Action.public void connect(Action.Activate handler)
Action.Activate
signal.public MenuItem createMenuItem()
You can add the returned MenuItem to a Menu
. The MenuItem will
have the same label, icon or accelerator of this Action, and when the
user activates the MenuItem, this Action will
Action.Activate
too.
public ToolItem createToolItem()
You can add the returned ToolItem to a Toolbar
. The ToolItem
will have the same Label, icon, tooltips or accelerator of this Action,
and when the user clicks it, this Action will be
Action.Activate
d.
public void emitActivate()
Programmatically cause this Action to fire its
Action.Activate
signal. Note that this has no effect if
the Action is not currently sensitive.
Since the Action is automatically activated when user activates one of its proxies (selecting the specific MenuItem or clicking the ToolButton that goes with this Action), normally you don't need this. However, in some cases you want to activate the Action in your application code. Use this there.
public boolean getSensitive()
isSensitive()
for that.public String getTooltip()
public boolean getVisible()
isVisible()
,
setVisible(boolean)
public boolean isSensitive()
public boolean isVisible()
true
when both the Action and the ActionGroup it
belongs to are visible, false
otherwise.setVisible(boolean)
public void setAccelerator(AcceleratorGroup group, Keyval keyval, ModifierType modifier)
addAcceleratorGroup()
.public void setSensitive(boolean sensitive)
You usually will want to deactivate an Action when its operation makes no sense in current application status. For example, in a text editor you should probably disable the "Save" Action when there is no document is open or when current document hasn't been changed.
When not sensitive, Action proxies are displayed differently than enabled proxies (usually in grey color). That way, user knows the Action has no application in the current program state, and she needs to do something before the disabled Action becomes applicable.
However, when a full Menu on a MenuBar needs to be disabled, generally
a better idea is to setVisible(false)
it.
Finally, note that setting an Action sensitive doesn't always mean it
will be actually sensitive, as you also need to make sensitive the
ActionGroup the Action belongs to (see isSensitive()
).
An Action starts life sensitive (ie, true
).
public void setTooltip(String tooltip)
public void setVisible(boolean visible)
When an Action is not visible, any associated proxies, such as MenuItems or ToolItems are hidden to the used.
In most cases, it's a better idea to setSensitive(false)
an Action instead of make it not visible. That
way, users can see that such operation exists in the application, but
they need to do some operations before it becomes available.
However, when a full Menu is disabled, this could be a good option. For example, on a text edit application, you could hide the "Edit" menu when there's no opened document.
Finally, note that setting an Action visible doesn't always mean it
will be actually displayed, as you also need to make visible the
ActionGroup the Action belongs to (see isVisible()
for details).