net.gvcc.jgoffice.searchcomponent.SearchComponent.java Source code

Java tutorial

Introduction

Here is the source code for net.gvcc.jgoffice.searchcomponent.SearchComponent.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package net.gvcc.jgoffice.searchcomponent;

import com.vaadin.data.Property;
import com.vaadin.event.ShortcutAction.KeyCode;
import com.vaadin.ui.AbstractField;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Field.ValueChangeEvent;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.ListSelect;
import com.vaadin.ui.TextField;
import java.util.ArrayList;
import java.util.StringTokenizer;
import net.gvcc.jgoffice.searchcomponent.interfaces.ISearchFormObserver;
import net.gvcc.jgoffice.searchcomponent.interfaces.ISearchForm;
import net.gvcc.jgoffice.templates.interfaces.IGlobalConst;

/**
 *
 * @author ernst_sgv
 */
public class SearchComponent extends HorizontalLayout implements ISearchFormObserver, IGlobalConst {

    private TextField searchField;
    private Button searchButton;
    private ArrayList<String> tokens = new ArrayList<String>();

    public SearchComponent() {

        //this.setStyleName("g-commandBar");
        this.setImmediate(true);
        this.setWidth("100.0%");
        this.setHeight("-1px");
        this.setMargin(false);

        // searchField
        searchField = new TextField();
        searchField.setImmediate(true);

        searchField.setInputPrompt("Press F2 to activate...");
        searchField.addValueChangeListener(new Property.ValueChangeListener() {
            @Override
            public void valueChange(Property.ValueChangeEvent event) {
                String value = (String) searchField.getValue();

                if (value.equals("#")) {
                    searchField.setValue("#START:");
                }
            }
        });

        searchField.setWidth("100.0%");
        searchField.setHeight("-1px");
        this.addComponent(searchField);
        this.setExpandRatio(searchField, 1.0f);
        this.setComponentAlignment(searchField, new Alignment(48));

        searchField.addShortcutListener(new AbstractField.FocusShortcut(searchField, KeyCode.F2));

        searchButton = new Button();
        searchButton.setCaption("Start");
        searchButton.setImmediate(true);
        searchButton.setHeight("-1px");

        searchButton.addClickListener(new Button.ClickListener() {

            @Override
            public void buttonClick(Button.ClickEvent event) {
                searchButtonHandler(event);
            }
        });

        this.addComponent(searchButton);
    }

    public void searchButtonHandler(Button.ClickEvent event) {
        StringTokenizer tokenizer = new StringTokenizer(searchField.getValue(), ";");
        tokens.clear();

        while (tokenizer.hasMoreTokens()) {
            String s = tokenizer.nextToken();
            tokens.add(s);
        }

        searchField.setValue("");
        notifyForm(tokens);
    }

    @Override
    public void addForm(ISearchForm form) {
        searchFormList.add(form);
    }

    @Override
    public void removeForm(ISearchForm form) {
        searchFormList.remove(form);
    }

    @Override
    public void notifyForm(ArrayList<String> searchTokens) {
        for (ISearchForm searchForm : searchFormList) {
            searchForm.performSearch(searchTokens);
        }
    }

}