Permalink
Browse files

Merge pull request #74 from enthought/action-schema

Schema for Pyface Actions.
  • Loading branch information...
2 parents c472fde + dda7317 commit 017353363e8e5891048879a4a102c0e7d78027ae nmichaud committed Dec 27, 2012
Showing with 30 additions and 3 deletions.
  1. +2 −2 pyface/tasks/action/api.py
  2. +28 −1 pyface/tasks/action/schema.py
@@ -1,7 +1,7 @@
# Local imports.
from dock_pane_toggle_group import DockPaneToggleGroup
-from schema import GroupSchema, MenuSchema, MenuBarSchema, ToolBarSchema, \
- SGroup, SMenu, SMenuBar, SToolBar
+from schema import ActionSchema, GroupSchema, MenuSchema, MenuBarSchema, \
+ ToolBarSchema, SGroup, SMenu, SMenuBar, SToolBar
from schema_addition import SchemaAddition
from task_action import CentralPaneAction, DockPaneAction, EditorAction, \
TaskAction, TaskWindowAction
@@ -2,7 +2,7 @@
from pyface.action.api import Action, ActionItem, Group, \
MenuManager, MenuBarManager, ToolBarManager
from traits.api import Bool, Callable, Enum, HasTraits, Instance, \
- List, Str, Trait, Tuple, Unicode
+ List, Property, Str, Trait, Tuple, Unicode
# Trait definitions.
SubSchema = Trait(None, Action, ActionItem, Group, MenuManager,
@@ -35,6 +35,33 @@ def create(self, children):
raise NotImplementedError
+class ActionSchema(Schema):
+ """ Action schema for Pyface Actions.
+
+ An action schema cannot have children. It is used as an action factory
+ to make sure a larger schema (e.g., a menu schema) can be used multiple
+ times. Without using an ActionSchema, a reference to the action is added
+ to every menu created from the schema. When one of the menus is destroyed,
+ the action is also destroyed and is made unusable.
+
+ """
+
+ #: A factory for the Action instance.
+ action_factory = Callable(Action)
+
+ #: Items is overwritten to be empty and read-only to avoid assigning to
+ #: it by mistake.
+ items = Property()
+ def _get_items(self):
+ return []
+
+ def create(self, children):
+ """ Create the appropriate PyFace Action instance. """
+
+ traits = dict(id=self.id)
+ return self.action_factory(**traits)
+
+
class GroupSchema(Schema):
""" A schema for a Pyface Group.
"""

0 comments on commit 0173533

Please sign in to comment.