at.meikel.nentis.Nentis.java Source code

Java tutorial

Introduction

Here is the source code for at.meikel.nentis.Nentis.java

Source

/*
 * Copyright 2009 IT Mill Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package at.meikel.nentis;

import at.meikel.nentis.at.meikel.nentis.model.NentisApi;
import at.meikel.nentis.at.meikel.nentis.model.Ticket;
import com.vaadin.Application;
import com.vaadin.ui.*;
import com.vaadin.ui.Button.ClickEvent;

import java.util.Date;
import java.util.List;

/**
 * The Application's "main" class
 */
@SuppressWarnings("serial")
public class Nentis extends Application {
    private NentisApi nentisApi;
    private Window mainWindow;

    @Override
    public void init() {
        nentisApi = NentisApi.getInstance();
        mainWindow = new Window("Nentis");
        setMainWindow(mainWindow);
        TabSheet tabSheet = new TabSheet();
        mainWindow.setContent(tabSheet);

        initInfo(tabSheet);
        initMain(tabSheet);
    }

    private void initInfo(TabSheet tabSheet) {
        VerticalLayout infoTab = new VerticalLayout();
        tabSheet.addTab(infoTab);
        tabSheet.getTab(infoTab).setCaption("Info");
        infoTab.addComponent(new Label("temp.dir:" + System.getProperty("temp.dir")));
    }

    private void initMain(TabSheet tabSheet) {
        final VerticalLayout mainTab = new VerticalLayout();
        tabSheet.addTab(mainTab);
        tabSheet.getTab(mainTab).setCaption("Main");

        final HorizontalLayout buttons = new HorizontalLayout();
        mainTab.addComponent(buttons);

        final VerticalLayout state = new VerticalLayout();
        mainTab.addComponent(state);

        final VerticalLayout info = new VerticalLayout();
        mainTab.addComponent(info);

        Button createTicket = new Button("Create a new ticket");
        createTicket.addListener(new Button.ClickListener() {
            public void buttonClick(ClickEvent clickEvent) {
                state.removeAllComponents();
                Ticket ticket = nentisApi.createTicket("Ticket (" + new Date() + ")");
                state.addComponent(
                        new Label("Ticket #" + ticket.getId() + " [" + ticket.getDescription() + "] created."));
            }
        });
        buttons.addComponent(createTicket);

        Button listAllTickets = new Button("List all existing tickets");
        listAllTickets.addListener(new Button.ClickListener() {
            public void buttonClick(ClickEvent clickEvent) {
                state.removeAllComponents();
                List<Ticket> allTickets = nentisApi.getAllTickets();
                info.removeAllComponents();
                if (allTickets != null) {
                    for (Ticket ticket : allTickets) {
                        info.addComponent(new Label(
                                "Ticket #" + ticket.getId() + " [" + ticket.getDescription() + "] created."));
                    }
                }
            }
        });
        buttons.addComponent(listAllTickets);

        final Button openSubWindow = new Button("Open a sub window");
        openSubWindow.addListener(new Button.ClickListener() {
            public void buttonClick(ClickEvent clickEvent) {
                buttons.removeComponent(openSubWindow);
                Window subWindow = new Window("Sub window");
                mainWindow.addWindow(subWindow);
            }
        });
        buttons.addComponent(openSubWindow);
    }

}