Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
book-of-vaadin.pdf
Скачиваний:
88
Добавлен:
24.03.2015
Размер:
13.43 Mб
Скачать

Writing a Server-Side Web Application

Figure 4.4. Three-Layer Architecture

An application layer (or service layer) is often distinguished from the domain layer, offering the domain logic as a service, which can be used by the user interface layer, as well as for other uses. In Java EE development, Enterprise JavaBeans (EJBs) are typically used for building this layer.

An infrastructure layer (or data access layer) is often distinguished from the data store layer, with a purpose to abstract the data store. For example, it could involve a persistence solution such as JPA and an EJB container. This layer becomes relevant with Vaadin when binding Vaadin components to data with the JPAContainer, as described in Chapter 21, Vaadin JPAContainer.

4.2.5. Accessing UI, Page, Session, and Service

You can get the UI and the page to which a component is attached to with getUI() and getPage().

However, the values are null until the component is attached to the UI, and typically, when you need it in constructors, it is not. It is therefore preferable to access the current UI, page, session, and service objects from anywhere in the application using the static getCurrent() methods in the respective UI, Page, VaadinSession, and VaadinService classes.

//Set the default locale of the UI UI.getCurrent().setLocale(new Locale("en"));

//Set the page title (window or tab caption) Page.getCurrent().setTitle("My Page");

//Set a session attribute

VaadinSession.getCurrent().setAttribute("myattrib", "hello");

// Access the HTTP service parameters

File baseDir = VaadinService.getCurrent().getBaseDirectory();

You can get the page and the session also from a UI with getPage() and getSession() and the service from VaadinSession with getService().

The static methods use the built-in ThreadLocal support in the classes. The pattern is described in Section 11.14.3, “ThreadLocal Pattern”.

4.3. Handling Events with Listeners

Let us put into practice what we learned of event handling in Section 3.4, “Events and Listeners”. You can implement listener interfaces in a regular class, but it brings the problem with differentiating between different event sources. Using anonymous class for listeners is recommended in most cases.

68

Accessing UI, Page, Session, and Service

Writing a Server-Side Web Application

4.3.1. Implementing a Listener in a Regular Class

The following example follows a typical pattern where you have a Button component and a listener that handles user interaction (clicks) communicated to the application as events. Here we define a class that listens to click events.

public class MyComposite extends CustomComponent implements Button.ClickListener {

Button button; // Defined here for access

public MyComposite() {

Layout layout = new HorizontalLayout();

// Just a single component in this composition button = new Button("Do not push this"); button.addClickListener(this); layout.addComponent(button);

setCompositionRoot(layout);

}

// The listener method implementation public void buttonClick(ClickEvent event) {

button.setCaption("Do not push this again");

}

}

4.3.2. Differentiating Between Event Sources

If an application receives events of the same type from multiple sources, such as multiple buttons, it has to be able to distinguish between the sources. If using a regular class listener, distinguishing between the components can be done by comparing the source of the event with each of the components. The method for identifying the source depends on the event type.

public class TheButtons extends CustomComponent implements Button.ClickListener {

Button onebutton; Button toobutton;

public TheButtons() {

onebutton = new Button("Button One", this); toobutton = new Button("A Button Too", this);

// Put them in some layout

Layout root = new HorizontalLayout(); root.addComponent(onebutton); root.addComponent(toobutton); setCompositionRoot(root);

}

@Override

public void buttonClick(ClickEvent event) { // Differentiate targets by event source if (event.getButton() == onebutton)

onebutton.setCaption ("Pushed one"); else if (event.getButton() == toobutton) toobutton.setCaption ("Pushed too");

}

}

Other techniques exist for separating between event sources, such as using object properties, names, or captions to separate between them. Using captions or any other visible text is generally

Implementing a Listener in a Regular Class

69

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]