songstock.web.extensions.searching.basic.BasicSearchForm.java Source code

Java tutorial

Introduction

Here is the source code for songstock.web.extensions.searching.basic.BasicSearchForm.java

Source

/**
* Copyright  2013 Universidad Icesi
* 
* This file is part of SongStock.
* 
* SongStock is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* 
* SongStock is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with SongStock.  If not, see <http://www.gnu.org/licenses/>.
**/
package songstock.web.extensions.searching.basic;

import com.vaadin.annotations.AutoGenerated;
import com.vaadin.server.AbstractErrorMessage.ContentMode;
import com.vaadin.server.ErrorMessage.ErrorLevel;
import com.vaadin.server.UserError;
import com.vaadin.ui.AbsoluteLayout;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.Label;
import com.vaadin.ui.Notification;
import com.vaadin.ui.Notification.Type;
import com.vaadin.ui.TextField;

/**
 * 
 * @author Andrs Paz
 *
 */
public class BasicSearchForm extends CustomComponent {

    /*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */

    @AutoGenerated
    private AbsoluteLayout mainLayout;

    @AutoGenerated
    private Label labelError;

    @AutoGenerated
    private Button buttonSearch;

    @AutoGenerated
    private TextField textFieldQuery;

    @AutoGenerated
    private Label labelTitle;

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    public static final String BASIC_SEARCH_FORM = "basicSearchForm";

    /**
     * The constructor should first build the main layout, set the
     * composition root and then do any custom initialization.
     *
     * The constructor will not be automatically regenerated by the
     * visual editor.
     */
    public BasicSearchForm() {
        buildMainLayout();
        setCompositionRoot(mainLayout);

        // User code

        this.labelError.setValue("");
        this.labelError.setEnabled(false);

        this.buttonSearch.addClickListener(new ClickListener() {

            private static final long serialVersionUID = 1L;

            @Override
            public void buttonClick(ClickEvent event) {
                doBasicSearch();
            }
        });
    }

    protected void doBasicSearch() {
        String query = textFieldQuery.getValue();
        if (!query.equals("")) {
            SearchController.getInstance().doBasicSearch(query);
        } else {
            setErrorMessage("Query field cannot be empty.");
        }
    }

    public void setErrorMessage(String message) {
        Notification.show("SongStock", message, Type.TRAY_NOTIFICATION);
        this.labelError.setValue("<i>" + message + "</i>");
        this.buttonSearch.setComponentError(new UserError(message, ContentMode.TEXT, ErrorLevel.WARNING));
    }

    public void clear() {
        this.textFieldQuery.setValue("");
        this.labelError.setValue("");
        this.buttonSearch.setComponentError(null);
    }

    @AutoGenerated
    private AbsoluteLayout buildMainLayout() {
        // common part: create layout
        mainLayout = new AbsoluteLayout();
        mainLayout.setImmediate(false);
        mainLayout.setWidth("100%");
        mainLayout.setHeight("100%");

        // top-level component properties
        setWidth("100.0%");
        setHeight("100.0%");

        // labelTitle
        labelTitle = new Label();
        labelTitle.setImmediate(false);
        labelTitle.setWidth("-1px");
        labelTitle.setHeight("-1px");
        labelTitle.setValue("<b>Search Catalog</b>");
        labelTitle.setContentMode(com.vaadin.shared.ui.label.ContentMode.HTML);
        mainLayout.addComponent(labelTitle, "top:20.0px;left:20.0px;");

        // textFieldQuery
        textFieldQuery = new TextField();
        textFieldQuery.setImmediate(false);
        textFieldQuery.setWidth("380px");
        textFieldQuery.setHeight("-1px");
        mainLayout.addComponent(textFieldQuery, "top:60.0px;left:20.0px;");

        // buttonSearch
        buttonSearch = new Button();
        buttonSearch.setCaption("Search");
        buttonSearch.setImmediate(false);
        buttonSearch.setWidth("-1px");
        buttonSearch.setHeight("-1px");
        mainLayout.addComponent(buttonSearch, "top:100.0px;left:20.0px;");

        // labelError
        labelError = new Label();
        labelError.setImmediate(false);
        labelError.setWidth("-1px");
        labelError.setHeight("-1px");
        labelError.setValue("<i>Label</i>");
        labelError.setContentMode(com.vaadin.shared.ui.label.ContentMode.HTML);
        mainLayout.addComponent(labelError, "top:104.0px;left:100.0px;");

        return mainLayout;
    }

}