next up previous contents index
Next: A Standard Main Program Up: Project Rotate Previous: The Rotate Add-Method

The Rotate Init-Method

 

It was not possible to create the editor in the setup-method because the "get-editor" method -- which is not available at that time -- is needed. The editor is created in the init-method instead, a static variable is used to make sure that it is created only once:

PROJECT *project_rotate_init(int batch)
{
  PROJECT *self;
  static GROUP *editor = NULL;

  self = (PROJECT *)START_METHOD(G_INSTANCE);
  ASSURE(self, "", END_METHOD(NULL));

  /*
   * The init method is called when the project is used for the first
   * time. Here all classes are initialized and all methods added,
   * now we can create the editor (just once, therefore it has to be
   * static). The project might be called in batch mode, in this
   * case no interface is needed and we don't create it.
   */
  if(!batch && !editor) {
    /*
     * Again we need the manager, but not to add the editor to it...
     */
    MANAGER *mgr = (MANAGER *)GRAPE(Manager, "get-stdmgr")();

    /*
     * We create the editor, might fail...
     */
    ASSURE(editor = (GROUP *)GRAPE(Rot2d, "get-editor")(),
           "project_rotate_init: can't get editor", END_METHOD(NULL));

    /*
     * This time we add the editor to the project instead of the
     * manager. The project then takes care of adding the editor
     * to or removing it from the manager when the project is added
     * or removed.
     */
    GRAPE(self, "add-item")(editor);

    /*
     * We tell the manager that it should send the method "update-
     * editor" to the class Rot2d everytime its current object is
     * changed. If the new current object has class Rot2d then the
     * editor is bound to that instance, otherwise the editor is
     * deactivated.
     */
    GRAPE(mgr, "add-cross-depend")(Rot2d, "update-editor");
  }

  END_METHOD(self);
}

The editor is given to the project instead of the manager, it will be added and removed with the project.

You might have wondered how the "update-editor" method is called. Since our editor depends on the current instance of the manager we have to react when the current instance is changed. Therefore we ask the manager to include our "update-editor" method to its ``cross-dependency list''. Every time the current object is changed the manager send these methods to the given objects, in our case the class Rot2d, with the new current instance as parameter. This instance then is checked by "update-editor" and the editor is updated.



SFB 256 Universität Bonn and IAM Universität Freiburg

Copyright © by the Sonderforschungsbereich 256 at the Institut für Angewandte Mathematik, Universität Bonn.