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 // This function is the standard way to fire events using our
support object (see above) |
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: |
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"}; } }); |