Пример #1
0
  /** Generates a stub controller for a view. */
  public static void generateCode(
      java.io.PrintWriter p, Hashtable hashFields, JspController cont, String controllerName) {
    try {
      PrintWriter pw = p;
      println(pw, "<BR>");
      println(pw, "");
      String cName = controllerName;
      String packageName = "";
      if (cName == null || cName.equals("") || cName.equals("com.salmonllc.jsp.JspController"))
        cName = "GeneratedController";
      int pos = cName.lastIndexOf(".");
      if (pos > 0) {
        packageName = cName.substring(0, pos);
        cName = cName.substring(pos + 1);
      }

      // create the package statement
      println(pw, "//package statement");
      if (packageName != null) println(pw, PACKAGE + SPACE + packageName + ";");

      // create some standard import statements
      println(pw, "");
      println(pw, "//Salmon import statements");
      println(pw, IMPORT + SPACE + "com.salmonllc.jsp.*;");
      println(pw, IMPORT + SPACE + "com.salmonllc.html.events.*;");
      println(pw, "");
      println(pw, "");

      // Start the Class definition
      println(pw, "/**");
      println(pw, " * " + cName + ": a SOFIA generated controller");
      println(pw, " */");
      pw.print(CLASSDEF + SPACE + cName + SPACE);
      pw.print(EXTENDS + SPACE + BASECLASS + SPACE);

      if (INTERFACES != null && INTERFACES.size() > 0) {
        pw.print(IMPLEMENTS + SPACE);
        pw.print(INTERFACES.elementAt(0));
        for (int i = 1; i < INTERFACES.size(); i++) {
          pw.print(COMMA + SPACE);
          pw.print(INTERFACES.elementAt(i));
        }
      }
      println(pw, " {");

      // Add the variables
      cont.printVars(pw);

      // add the Initialize Method
      println(pw, SPACE);
      println(pw, "/**");
      println(
          pw,
          " * Initialize the page. Set up listeners and perform other initialization activities.");
      println(pw, " */");
      println(pw, INITMETHOD + "{");

      // For Page Listeners
      if (INTERFACES.contains(new String("PageListener")))
        println(pw, TAB + "addPageListener(this);");

      if (hashFields != null) {
        Enumeration en = hashFields.keys();
        while (en.hasMoreElements() && INTERFACES != null) {
          String key = (String) en.nextElement();
          Object obj = hashFields.get(key);

          // For Buttons
          if (INTERFACES.contains(new String("SubmitListener"))) {
            if (obj instanceof com.salmonllc.html.HtmlSubmitButton
                || obj instanceof com.salmonllc.html.HtmlSubmitImage)
              println(pw, TAB + computeFieldName(key) + ".addSubmitListener(this);");
          }

          // For Value Change Listeners
          if (INTERFACES.contains(new String("ValueChangedListener"))) {
            if (obj instanceof com.salmonllc.html.HtmlCheckBox
                || obj instanceof com.salmonllc.html.HtmlRadioButton
                || obj instanceof com.salmonllc.html.HtmlDropDownList)
              println(pw, TAB + computeFieldName(key) + ".addValueChangeListener(this);");
          }

          // For Tree Listeners
          if (INTERFACES.contains(new String("TreeListener"))) {
            if (obj instanceof com.salmonllc.html.HtmlTreeControl)
              println(pw, TAB + computeFieldName(key) + ".addTreeListener(this);");
          }
        } // while loop
      } // hashatable not null

      println(pw, "}");

      // add the listeners
      addListeners(pw);

      // End the class definition
      println(pw, "");
      println(pw, "}");

      pw.flush();

    } catch (java.io.IOException e) {
      System.out.println("Cannot create Java File.");
      e.printStackTrace();
    }
  }