concept group in category griffon

appears as: group, group, groups, The group, groups
Griffon in Action

This is an excerpt from Manning's book Griffon in Action.

You named the group filePanel because you’ll use a panel container, but you could have chosen FileTab or some other name that indicates this group is related to files and tabs. Again you end up with three files that follow the MVC pattern, each organized in a specific folder.

Where do you register a group? In the Application.groovy file that lives in griffonapp/conf. We won’t look at the whole file here; the following code shows just the parts relating to MVC groups.

Listing 6.1. MVC group declarations in Application.groovy
. . . mvcGroups { // MVC Group for "FilePanel" 'filePanel' { model = 'FilePanelModel' view = 'FilePanelView' controller = 'FilePanelController' } // MVC Group for "GroovyEdit" 'groovyEdit' { model = 'GroovyEditModel' view = 'GroovyEditView' controller = 'GroovyEditController' } } . . .

The declaration of the MVC group is fairly straightforward: in the mvcGroups section of the configuration, you declare the name of the MVC group and open a block describing all the portions that compose the group. The group portions are assigned to the names of the classes representing that portion of the MVC group. Note that the class names are defined as strings, for the sole reason of avoiding eager class resolution when the group configuration is loaded and parsed. The MVC group type itself is also in single quotes; this enables you to create groups that may not be groovy identifiers, possibly including spaces, dots, and any other characters you feel like adding. A key aspect of group names is that they must be unique within the application. If a duplicate name is encountered, the last group registered will win over the previous ones.

A group created with this method will be automatically destroyed as soon as it’s no longer of use. The typical case is a modal dialog that requires a few customizations before displaying itself, perhaps capturing some input and returning back to the original caller. Here’s an example of what we just described. Suppose a group named query is responsible for capturing a user selection in a model property named choice. The group’s controller also has an action named show that takes care of displaying the dialog:

def output = null
withMVCGroup('query') { m, v, c ->
    c.show()
    output = m.choice
}

Using this construct liberates you from explicitly destroying the group instance after it has been put to use. Destroying a group will be covered later in this chapter.

When creating the group, the first task the framework accomplishes is loading the Java class for the MVC group members. This is the plain Java part. What happens next is Groovy.

If the artifact or its superclass implements the griffon.core.GriffonArtifact interface and the superclass is Object, then the Griffon compiler switches the superclass to one that contains the app object and four method definitions (createMVC-Group(), buildMVCGroup(), withMVCGroup(), and destroyMVCGroup()). If the artifact or its superclass does not implement the griffon.core.GriffonArtifact interface, the app and the four method definitions are injected directly into the artifact’s byte code. The app field is a reference to the GriffonApplication object that serves as the central touchstone for the whole application, where all of the magic pixie dust is stored. The four injected methods are required for managing MVC groups. The first three methods exist for creating groups and the last one for destroying groups (more on that in the next section). These injections for an MVC member are compulsory; every member gets them. And they will overwrite any existing field or methods by the same name, so don’t even try!