/*
 * Copyright 2013 Corpuslinguistic working group Humboldt University Berlin.
 *
 * 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 annis.gui;

import annis.gui.controlpanel.ControlPanel;
import annis.gui.controlpanel.QueryPanel;
import com.vaadin.data.Property;
import com.vaadin.event.ItemClickEvent;
import com.vaadin.ui.Table;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Wraps the auto generated queries.
 *
 * @author Benjamin Weißenfels <b.pixeldrama@gmail.com>
 */
class AutoGeneratedQueries extends Table
{

  // first column
  private final String OPERATOR = "operator";

  // second column String
  private final String EXAMPLE_QUERY = "example query";

  //third column String
  private final String DESCRIPTION = "description";

  //main ui window
  private final SearchUI ui;

  private final static Logger log = LoggerFactory.
    getLogger(AutoGeneratedQueries.class);

  public AutoGeneratedQueries(String caption, SearchUI ui)
  {
    super(caption);
    this.ui = ui;
  }

  /**
   * Sets some layout properties.
   */
  private void setUpTable()
  {

    // expand the table
    setSizeFull();

    // Allow selecting items from the table.
    setSelectable(true);

    // Send changes in selection immediately to server.
    setImmediate(true);

    // set clickhandler for execute example query
    addListener(new ExampleQueryExecuter());
  }

  @Override
  public void attach()
  {
    super.attach();
    addContainerProperty(OPERATOR, String.class, ".");
    addContainerProperty(EXAMPLE_QUERY, String.class,
      "tok=\"in\" & tok=\"Zossen\" & #1 .1 #2");
    addContainerProperty(DESCRIPTION, String.class,
      "For non-terminal nodes, precedence is determined by the right most and left most terminal children");

    // add default item
    addItem();

    setUpTable();
  }

  /**
   * Catches click events on the example query column
   */
  private class ExampleQueryExecuter implements ItemClickEvent.ItemClickListener
  {

    @Override
    public void itemClick(ItemClickEvent event)
    {
      if (event.getButton() == ItemClickEvent.BUTTON_LEFT)
      {
        String column = (String) event.getPropertyId();
        ControlPanel controlPanel = ui.getControlPanel();
        QueryPanel queryPanel;

        if (controlPanel == null)
        {
          log.error("controlPanel is not initialized");
          return;
        }

        queryPanel = controlPanel.getQueryPanel();
        if (queryPanel == null)
        {
          log.error("queryPanel is not initialized");
          return;
        }

        if (EXAMPLE_QUERY.equals(column))
        {
          Property query = event.getItem().getItemProperty(column);
          queryPanel.setQuery(query.toString());
        }
      }
    }
  }
}

@@ -2179,104 +2179,46 @@
    %22
-For non-terminal nodes, precedence is determined by the right most and left most terminal childr
+searches for token in followed by Zoss
 en%22)

change default description