Examples

1.The example of HTMLPage Visual Pattern Bean


public class HTMLPageStub extends VisualPatternStub implements HTMLPage, InitApplicationEventListener,
                                                                RefreshPageEventListener {

    protected technion.dbui.util.PatternEventSourceSupport support; // this class helps to register listeners and fire events

    // Properties
    private Color bgColor = Color.white;
   
    public Color getBackgroundColor() { return bgColor; }
    public void setBackgroundColor(Color c) {
        bgColor = c;
    }

   
   
    public HTMLPageStub() throws technion.dbui.DBUIException {
        super();
    }
       

// Since we are the Listener of the InitApplicationEvent we have to implement this function:
    public void initApplication(InitApplicationEvent e) {
        fireEvent(new PageEvent(this,e));
    }
   

  // The same for the RefreshPageEvent listener function.
  // (This is the main function gathering the HTML representations of all the components).

    public void refreshPage(RefreshPageEvent e) {
       
        // Get HTML output from all Visual Patterns residing in the page.
        Environment env = e.getEnvironment();
       
        StringBuffer sb = new StringBuffer();
        sb.append(
"<BODY><table margins=0 width=100% height=100%" +
                       
"bgcolor=\"#"+technion.dbui.util.RGBColorFORMField.createStringFromObject(bgColor)+"\"><tr><td valign=top>");
        sb.append(
"<b><i>HTML Page</i></b>\n<hr>\n");
        sb.append(env.requestHTMLFromPattern(
Tables.class));        
        sb.append(
"<HR>");
        sb.append(env.requestHTMLFromPattern(
Info.class));        
        sb.append(
"<HR>");
        sb.append(env.requestHTMLFromPattern(
FormEditor.class));        
        sb.append(
"</td></tr></table></BODY>");

        html = sb.toString();
        fireEvent(new PageReadyEvent(this,html));
    }


   private String html = "<BODY></BODY>";   // Default HTML representation of the Page

   // This function returns the HTML representation of the Visual Pattern Bean
    public String getHTML() { return html; }

  // This function is the standard way to fire events using our support object (see above)
   public void fireEvent(DBUIEvent ev) {
     try {
         support.fireEvent(ev);
    } catch (DBUIException e) {
        Output.error(e,"Cannot fire event");
    }
  }


   // Since we are the Event Source of two events: PageEvent and PageReadyEvent,
 // Since we are the Event Source of two events: PageEvent and PageReadyEvent,
   // there is a need to take care of the listeners

    public void addPageEventListener(PageEventListener el) {    
        support.addListener(PageEvent.class, el);     }
    public void removePageEventListener(PageEventListener el) {
        support.removeListener(PageEvent.class, el);
    }
    public void addPageReadyEventListener(PageReadyEventListener el) {    
        support.addListener(PageReadyEvent.class, el);     }
    public void removePageReadyEventListener(PageReadyEventListener el) {
        support.removeListener(PageReadyEvent.class, el);
    }
    public void addPageEventListener(PageEventListener el) {    
        support.addListener(PageEvent.class, el);     }
    public void removePageEventListener(PageEventListener el) {
        support.removeListener(PageEvent.class, el);
    }
    public void addPageReadyEventListener(PageReadyEventListener el) {    
        support.addListener(PageReadyEvent.class, el);     }
    public void removePageReadyEventListener(PageReadyEventListener el) {
        support.removeListener(PageReadyEvent.class, el);
    }

}

2. The example of HTMLPageStub BeanInfo file

public class HTMLPageStubBeanInfo extends SimpleBeanInfo {
   
public BeanDescriptor getBeanDescriptor() {
    BeanDescriptor bd = new BeanDescriptor(technion.dbui.ui.stub.HTMLPageStubBeanInfo.class);

    bd.setDisplayName("HTML Page");
        bd.setName("HTML Page"); // may be omitted because is set automatically

        bd.setShortDescription("This bean represents the output HTML page");

    return bd;
}
   
   
public PropertyDescriptor[] getPropertyDescriptors() {

        PropertyDescriptor result[] = new PropertyDescriptor[1];

try {
    result[0] =
    new PropertyDescriptor("bgColor",HTMLPageStub.class,"getBackgroundColor","setBackgroundColor");
    result[0].setDisplayName("Background color");
    result[0].setShortDescription("The background color of the page");
}
catch (IntrospectionException e) {
Output.error(e,"Property construction problem " + e);
}
    return result;
}


public EventSetDescriptor[] getEventSetDescriptors() {
   
   
    EventSetDescriptor result[] = new EventSetDescriptor[2];
   
    { // PageReady event
       
Class[] aListenerMethod = { PageReadyEvent.class };
Class[] aAddListenerMethod = { PageReadyEventListener.class };
Class[] aRemoveListenerMethod = { PageReadyEventListener.class };

Method mListenerMethod;
Method mAddListenerMethod, mRemoveListenerMethod;

try {
mListenerMethod = PageReadyEventListener.class.getMethod("pageReady", aListenerMethod);
mAddListenerMethod = HTMLPageStub.class.getMethod("addPageReadyEventListener",aAddListenerMethod);
mRemoveListenerMethod = HTMLPageStub.class.getMethod("removePageReadyEventListener",aRemoveListenerMethod);
} catch (Exception ex) {
// "should never happen"
throw new Error("Missing method " + ex);
}

Method[] mListenerMethods = { mListenerMethod };


try {
result[0] =
            new EventSetDescriptor( mListenerMethod.getName()+" method call", // event set name
            PageReadyEventListener.class, // listener class
            mListenerMethods, // listener method names
            mAddListenerMethod,         // add listener method
            mRemoveListenerMethod);     // remove listener method
}
catch (IntrospectionException e) {
System.err.println("Property construction problem "+e);
}
    }
   
    { // Page event
       
Class[] aListenerMethod = { PageEvent.class };
Class[] aAddListenerMethod = { PageEventListener.class };
Class[] aRemoveListenerMethod = { PageEventListener.class };

Method mListenerMethod;
Method mAddListenerMethod, mRemoveListenerMethod;

try {
mListenerMethod = PageEventListener.class.getMethod("initPage", aListenerMethod);
mAddListenerMethod = HTMLPageStub.class.getMethod("addPageEventListener",aAddListenerMethod);
mRemoveListenerMethod = HTMLPageStub.class.getMethod("removePageEventListener",aRemoveListenerMethod);
} catch (Exception ex) {
// "should never happen"
throw new Error("Missing method " + ex);
}

Method[] mListenerMethods = { mListenerMethod };


try {
result[1] =
            new EventSetDescriptor( mListenerMethod.getName()+" method call", // event set name
            PageEventListener.class, // listener class
            mListenerMethods, // listener method names
            mAddListenerMethod,         // add listener method
            mRemoveListenerMethod);     // remove listener method
}
catch (IntrospectionException e) {
System.err.println("Property construction problem "+e);
}
    }
   
return result;
}


}

3. The example of Tables Visual Pattern Bean


public class TablesStub extends VisualPatternStub implements Tables, PageEventListener {   
   
    public TablesStub() throws technion.dbui.DBUIException {
        super();
    }
   
String[] names;
   
PageEvent pageEvent = null;

// Since we are the Listener of the PageEvent we have to implement this function:
public void initPage(PageEvent e) {
    pageEvent = e;
    DatabaseProxy dp = e.getEnvironment().getDatabaseProxy();
    try {
        names = dp.getTableNames();
    } catch (DBException e1) {
        Output.error(e1,"DB exception");
    }
}   

   
// This function returns the HTML representation of the Visual Pattern Bean

public String getHTML() {
    StringBuffer sb = new StringBuffer();
    sb.append("<b><i>Tables:</i></b>\n");
    sb.append("<UL>\n");
    for (int i = 0; i < names.length; i++) {                    
        sb.append("<LI><a href=" + pageEvent.getEnvironment().getHREF(new TablesEvent(this,pageEvent,names[i])) + ">" + names[i]+"</a>\n");
    }
    sb.append("</UL>\n");
    return sb.toString();
}

 // Since we are the Event Source of the TablesEvent,
// there is a need to take care of the listeners
// there is a need to take care of the listeners
public void addTablesEventListener(technion.dbui.ui.event.TablesEventListener el) {
    support.addListener(TablesEvent.class, el);
}
public void removeTablesEventListener(technion.dbui.ui.event.TablesEventListener el) {
    support.removeListener(TablesEvent.class, el);
}


}

 

4. The example of event hyperlink usage

Environment environment = event.getEnvironment();
String linkHTML =

"<a href="
"<a href=" + environment.getHREF(new MyPatternEvent(this,event,param)) + ">" + text+"</a>\n";

5. The example of event FORM usage

        Environment environment = event.getEnvironment();
       
        // Fill in the FORM Structure
        FORMStructure fs = new FORMStructure();
        fs.addField(new technion.dbui.util.StringFORMField("test"));
       String formHTML =
            environment.getFORM_TAG(new MyPatternEvent(this,event),"form_name",fs) +
           "<input type=text name=test>" +
            "<input type=submit name=submit>" +
            "</FORM>"
; // the FORM must be closed explicitly
       

6. The example HTML using the JavaScript to simulate
FORM field editing by Applet (see example 7)

<script language=JavaScript>
function doSubmit() {
      document.formname.color.value=document.ceditor.getStringValue();
<!-- the applet must have getStringValue() method -->
      document.formname.submit();
}
</script>
<body>
<applet name=ceditor 
        code="technion.dbui.teamname.somepackage.ColorEditorApplet.class" width=400 height=400
        archive="http://ginit.cs.techniton.ac.il/javalab/resources/teamname/ColorEditorApplet.jar">
<param name=CURRENT_COLOR value=000000>
</applet>
<form name="formname">
<input type=hidden name=color value=unknown>
<input type=button value="Update" onClick="doSubmit();">
</form>          

7. The example of Applet-edited Form Field
(see example 6)

FORMStructure fs = new FORMStructure();
StringBuffer sb = new StringBuffer();
sb.append(env.getFORM_TAG(new MyPatternEvent(this,event,param),"formname",fs));

       ... // iterate through form fields     ... // iterate through form fields

            fs.addField(new technion.dbui.util.RGBColorFORMField(propertyName));            
            sb.append("<script language=JavaSript> function doSubmit() { "+
                        "document.formname."+propertyName+".value=document.ceditor.getStringValue();"+
                       "document.formname.submit();"+
                        "}</script>"
);
            sb.append(prop.getShortDescription() + "<br> :"+
                    "<applet name=ceditor code=\"technion.dbui.teamname.somepackage.ColorEditorApplet.class\" width=150 height=300 "+
                    "archive=\""
+environment.getResourcesURLBase()+"/ColorEditorApplet.jar\">"+
                    "<param name=CURRENT_COLOR value=000000>"+   
                    "</applet>"+
                    "<input type=hidden name="
+propertyName+ " value=unknown>"+
                    "<br><input type=button value=\"Submit\" onClick=\"doSubmit();\">"

                );
            }            
        sb.append("</FORM>");

8. The example of analysing the submitted FORM
and changing the edited bean property

        Object bean = formEvent.getEditedBean();
  
        try {
            java.beans.BeanInfo bi = java.beans.Introspector.getBeanInfo(bean.getClass(),Object.class);            
           
            FORMStructure fs = e.getFORM();
            Hashtable fields = fs.getFields();
           
            java.beans.PropertyDescriptor[] pd = bi.getPropertyDescriptors();
            for (int i = 0; i < pd.length; i++) {               

                 java.beans.PropertyDescriptor prop = pd[i];
                FORMField field = (FORMField)fields.get(prop.getName());

                if(field != null) {
                    Method setter = prop.getWriteMethod();
                    setter.invoke(bean,new Object[] {field.getValue()}); // setting the new property value
                }
            }
        }
        catch (Exception ex) {
            Output.error(ex,"Introspection problem " + ex.toString());
        }
       

9. The example of setting custom property editor

result[0] =
    new PropertyDescriptor("bgImage",HTMLPageStub.class,"getBackgroundImage","setBackgroundImage");
    result[0].setDisplayName("Background image");
    result[0].setShortDescription("The background image of the page");

    result[0].setPropertyEditor(new java.beans.PropertyEditorSupport() {
                                                 public String getAsText() { if (getValue().equals("bg1.gif") return "Funny background"; // ... etc. }
                                                 public void setAsText(String tag) { if (tag.equals("Funny background") setValue("bg1.gif"); // ... etc. }
                                                public String[] getTags() { return new String[]{"Funny background", "Forest background", "Industrial background"}; }
                                           });