de.decidr.ui.view.SearchPanel.java Source code

Java tutorial

Introduction

Here is the source code for de.decidr.ui.view.SearchPanel.java

Source

/*
 * The DecidR Development Team licenses this file to you 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 de.decidr.ui.view;

import org.vaadin.henrik.superimmediatetextfield.SuperImmediateTextField;
import org.vaadin.henrik.superimmediatetextfield.SuperImmediateTextField.KeyPressEvent;
import org.vaadin.henrik.superimmediatetextfield.SuperImmediateTextField.KeyPressListener;

import com.vaadin.ui.ComboBox;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Panel;
import com.vaadin.ui.Table;
import com.vaadin.data.Property;
import com.vaadin.data.Property.ValueChangeEvent;
import de.decidr.model.annotations.Reviewed;
import de.decidr.model.annotations.Reviewed.State;
import de.decidr.ui.controller.SearchAction;

/**
 * This is a panel containing a text field and a button. This should represent a
 * search widget.
 * 
 * @author AT
 */
@Reviewed(reviewers = { "unknown" }, lastRevision = "0", currentReviewState = State.NeedsReview)
public class SearchPanel extends Panel implements Property.ValueChangeListener {

    private static final long serialVersionUID = 8703352734047305920L;

    private HorizontalLayout searchHorizontalLayout = null;

    private SuperImmediateTextField searchTextField = null;

    //    private Button searchButton = null;

    private ComboBox searchModeSelector = null;

    private SearchAction searchAction = null;

    /**
     * Requires a table to know in which table the keyword should be searched
     * for.
     * 
     * @param table
     *            TODO document
     */
    public SearchPanel(Table table) {
        init(table);
    }

    /**
     * Returns the horizontal layout.
     * 
     * @return searchHorizontalLayout
     */
    public HorizontalLayout getSearchHorizontalLayout() {
        return searchHorizontalLayout;
    }

    /**
     * Change the refered table from what was passed in the constructor.
     *
     * @param newTable New Table that the panel should search in.
     */
    public void setTable(Table newTable) {
        searchAction = new SearchAction(newTable, searchTextField, searchModeSelector);
        valueChange(null);
    }

    /**
     * This method initializes the components of the {@link SearchPanel}
     * component.
     */
    @SuppressWarnings("serial")
    private void init(Table table) {
        searchHorizontalLayout = new HorizontalLayout();

        searchTextField = new SuperImmediateTextField();
        searchTextField.setInputPrompt("Enter search term");
        searchTextField.setImmediate(true);
        searchTextField.addListener(this);
        searchTextField.addListener(new KeyPressListener() {
            @Override
            public void keyPressed(KeyPressEvent event) {
                valueChange(null);
            }
        });

        searchModeSelector = new ComboBox();

        searchAction = new SearchAction(table, searchTextField, searchModeSelector);
        //        searchButton = new Button("Search", searchAction);

        /* Set up combo box so that its items have the table's
         * column property ids as index and the column name as caption.
         */
        String[] headers = table.getColumnHeaders();
        Object[] properties = table.getVisibleColumns();
        for (int i = 0; i < headers.length; i++) {
            searchModeSelector.addItem(properties[i]);
            searchModeSelector.setItemCaption(properties[i], headers[i]);
        }
        searchModeSelector.setNullSelectionAllowed(false);
        searchModeSelector.setValue(properties[0]);
        searchModeSelector.setImmediate(true);
        searchModeSelector.addListener(this);

        addComponent(searchHorizontalLayout);

        searchHorizontalLayout.setSpacing(true);
        searchHorizontalLayout.addComponent(searchTextField);
        //        searchHorizontalLayout.addComponent(searchButton);
        searchHorizontalLayout.addComponent(searchModeSelector);
    }

    /* (non-Javadoc)
     * @see com.vaadin.data.Property.ValueChangeListener#valueChange(com.vaadin.data.Property.ValueChangeEvent)
     */
    @Override
    public void valueChange(ValueChangeEvent event) {
        if (searchAction != null) {
            searchAction.buttonClick(null);
        }
    }
}