Hello world

A first example

The following code refers the "Components and Event Flow" example (click on "Demo A" of the

First look at SimpleDemoController.java



This demo (which is a lot more than just Hello World) does the following



1. In the constructor, it creates a velocitycontainer. The content of this container is described in the file -place-of-javacode-/_content/index.html and looks as follows:

## -- translate the title and the intro --
<h1>$r.translate("title")</h1>
$r.translate("intro")
<br><br>
## render the first link
$r.render("simpleLink")
<br><br>
<table><tr style="height:100px">
<td style="width:200px; border:1px solid black;">
## -- render the first panel --
$r.render("content1")
</td>
<td style="width:200px; border:1px solid black;">
## -- render the second panel --
$r.render("content2")
</td>
</tr></table>

					

$r.translate(...) translates a string, $r.render(..) renders a component.



2. In the constructor of the java code, two yet empty panels are created and added to the velocity container:

   // create a yet empty panel and put it into the main template
32     content_A = new Panel("content_A");
33     // 'content1' is the name how we like to reference it from within the template
34     mainVC.put("content1", content_A);
35 
36     // create a yet empty panel and put it into the main template
37     content_B = new Panel("content_B");
38     // 'content2' is the name how we like to reference it from within the template
39     mainVC.put("content2", content_B);
40 									
					


3. A link is also added, and the initial component (the main visual part) of the controller is set, which is our velocitycontainer "mainVC"

				
  // create a link (button)
43     simpleLink = LinkFactory.createButtonSmall("simpleLink", mainVC, this);
44     // init main template
45     putInitialPanel(mainVC);
					


4. upon a click with the mouse, we check if the source is our link (source == simpleLink) and then update the first and the second demo panel with a) another velocitycontainer with another button, and b) with a dialogcontroller's initial component

				
48   protected void event(UserRequest ureq, Component source, Event event) {
49     if (source == simpleLink) {
50       VelocityContainer sampleVC = createVelocityContainer("sample");
51       anotherButton = LinkFactory.createButtonSmall("anotherButton", sampleVC, this);
52       content_A.setContent(sampleVC);
53       dialogC = DialogController.createOkCancelDialogController(ureq.getLocale(), translate("question"), this);
54       content_B.setContent(dialogC.getInitialComponent());
55     } else if (source == anotherButton) {
56       content_A.setContent(null);
57     }
58   }						
					


5. Here we listen to subcontrollers (see the "this" in
dialogC = DialogController.createOkCancelDialogController(ureq.getLocale(), translate("question"), this);
)

if the event comes from the subcontroller and the user clicked the first button, we then show an info message. Afterwards the panel is cleared.

				
60   protected void event(UserRequest ureq, Controller source, Event event) {
61     // if we get the answer from the dialogcontroller, we inform the user
62     if (source == dialogC) {
63       if (event == DialogController.EVENT_FIRSTBUTTON) {
64         // ok, since ok/cancel dialog
65         showInfo("dialog.ok.chosen", null);
66       } // else cancelled, show nothing
67       // in all cases, emtpy the panel holding the dialog
68       content_B.setContent(null);
69     }
70   }
						
					


6. When our controller is disposed, we need to dispose of all childcontrollers, too.

				
72   protected void doDispose() {
73     if (dialogC != null) dialogC.dispose();
74   }