aboutsummaryrefslogtreecommitdiffstats
path: root/src/com/vaadin/demo/reservation/simple/AdminView.java
blob: 59d772b80a23a8b920c92f34bbf5e80e01d3f5b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package com.vaadin.demo.reservation.simple;

import com.vaadin.data.Item;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.ui.Button;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.AbstractSelect.NewItemHandler;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;

@SuppressWarnings("serial")
public class AdminView extends VerticalLayout {

    private ComboBox resources = new ComboBox(
            "Select for editing or type new resource");
    private SimpleReserver application;
    private VerticalLayout form = new VerticalLayout();
    private Button save = new Button("Save resource");

    private TextField name = new TextField("Name:");
    private TextField desc = new TextField("Description:");
    protected Item editedItem;

    AdminView(SimpleReserver app) {
        setWidth("250px");

        application = app;

        resources.setImmediate(true);
        resources.setFilteringMode(ComboBox.FILTERINGMODE_CONTAINS);
        refreshList();
        resources.setItemCaptionMode(ComboBox.ITEM_CAPTION_MODE_PROPERTY);
        resources.setItemCaptionPropertyId(SampleDB.Resource.PROPERTY_ID_NAME);
        resources.setNewItemsAllowed(true);
        resources.setNewItemHandler(new NewItemHandler() {
            public void addNewItem(String newItemCaption) {
                name.setValue(newItemCaption);
                desc.setValue("");
                form.setVisible(true);
            }
        });

        resources.addListener(new ComboBox.ValueChangeListener() {
            public void valueChange(ValueChangeEvent event) {
                if (resources.getValue() != null) {
                    editedItem = resources.getItem(resources.getValue());

                    name
                            .setPropertyDataSource(editedItem
                                    .getItemProperty(SampleDB.Resource.PROPERTY_ID_NAME));
                    desc
                            .setPropertyDataSource(editedItem
                                    .getItemProperty(SampleDB.Resource.PROPERTY_ID_DESCRIPTION));

                    form.setVisible(true);

                } else {
                    form.setVisible(false);
                    editedItem = null;
                }

            }
        });
        addComponent(resources);

        form.setVisible(false);
        addComponent(form);
        form.addComponent(name);
        form.addComponent(desc);
        name.setWidth("100%");
        desc.setWidth("100%");
        form.addComponent(save);
        save.addListener(new ClickListener() {
            public void buttonClick(ClickEvent event) {
                if (editedItem == null) {
                    // save
                    int addResource = application.getDb().addResource(
                            name.getValue().toString(),
                            desc.getValue().toString());
                } else {
                    // update
                    application.getDb().updateResource(editedItem,
                            name.getValue().toString(),
                            desc.getValue().toString());
                }
                resources.setValue(null);
                refreshList();
            }
        });

    }

    private void refreshList() {
        resources
                .setContainerDataSource(application.getDb().getResources(null));
    }
}