/*
 * Copyright 2011 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.controlpanel;

import annis.gui.ExampleQueriesPanel;
import annis.gui.SearchUI;
import com.vaadin.ui.*;
import com.vaadin.ui.TabSheet.Tab;
import com.vaadin.ui.themes.ChameleonTheme;
import java.util.HashMap;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * This panel allows the user to control and execute queries.
 *
 * @author Thomas Krause <thomas.krause@alumni.hu-berlin.de>
 */
public class ControlPanel extends VerticalLayout implements TabSheet.SelectedTabChangeListener
{

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

  private static final long serialVersionUID = -2220211539424865671L;

  private QueryPanel queryPanel;

  private CorpusListPanel corpusList;

  private SearchOptionsPanel searchOptions;
  
  private Tab optionTab;
  private Map<Component, Component> optionComponentRegistry;
  private Map<Component, String> optionComponentCaptions;
  private Accordion accordion;
  private SearchUI ui;

  public ControlPanel(SearchUI ui,
    ExampleQueriesPanel autoGenQueries)
  {
    this.ui = ui;
    
    setSizeFull();

    setStyleName(ChameleonTheme.PANEL_BORDERLESS);
    addStyleName("control");

    accordion = new Accordion();
    accordion.setHeight(100f, Layout.UNITS_PERCENTAGE);
    accordion.setWidth(100f, Layout.UNITS_PERCENTAGE);

    corpusList = new CorpusListPanel(ui.getQueryController(), ui.getInstanceConfig(), autoGenQueries);

    searchOptions = new SearchOptionsPanel();

    queryPanel = new QueryPanel(ui);
    queryPanel.setHeight("-1px");
    queryPanel.setWidth("100%");

    accordion.addTab(corpusList, "Corpus List", null);
    optionTab = accordion.addTab(searchOptions, "Search Options", null);
    
    ui.getMainTab().addSelectedTabChangeListener(this);

    addComponent(queryPanel);
    addComponent(accordion);

    setExpandRatio(accordion, 1.0f);
    
    optionComponentRegistry = new HashMap<Component, Component>();
    optionComponentCaptions = new HashMap<Component, String>();
  }

  
  
  public CorpusListPanel getCorpusList()
  {
    return corpusList;
  }

  public QueryPanel getQueryPanel()
  {
    return queryPanel;
  }

  public SearchOptionsPanel getSearchOptions()
  {
    return searchOptions;
  }
  
  public void registerOptionComponent(String caption, Component optionComponent, Component tabComponent)
  {
    optionComponentRegistry.put(tabComponent, optionComponent);
    optionComponentCaptions.put(tabComponent, caption);
  }

  @Override
  public void selectedTabChange(TabSheet.SelectedTabChangeEvent event)
  {
    if(event.getTabSheet() != ui.getMainTab())
    {
      return;
    }
    
    boolean wasSelected = optionTab != null 
      && accordion.getSelectedTab() == optionTab.getComponent();
    
    Component selection = ui.getMainTab().getSelectedTab();
    if(selection != null && optionComponentRegistry.get(selection) != null)
    {
      accordion.removeTab(optionTab);
      optionTab = accordion.addTab(optionComponentRegistry.get(selection), optionComponentCaptions.get(selection));
    }
    else
    {
      // replace/leave the default search options
      if(optionTab != null)
      {
        accordion.removeTab(optionTab);
      }
      
      optionTab = accordion.addTab(searchOptions, "Search Options", null);
    }
    
    if(wasSelected)
    {
      accordion.setSelectedTab(optionTab.getComponent());
    }
  }
}

@@ -1606,27 +1606,27 @@
 ate 
-Accordion accordion
+TabSheet optionsTab
 ;%0A  
@@ -1858,50 +1858,51 @@
     
-accordion = new Accordion();%0A    accordion
+optionsTab = new TabSheet();%0A    optionsTab
 .set
@@ -1940,33 +1940,34 @@
 NTAGE);%0A    
-accordion
+optionsTab
 .setWidth(10
@@ -2257,25 +2257,26 @@
 );%0A%0A    
-accordion
+optionsTab
 .addTab(
@@ -2321,33 +2321,34 @@
 optionTab = 
-accordion
+optionsTab
 .addTab(sear
@@ -2492,25 +2492,26 @@
 mponent(
-accordion
+optionsTab
 );%0A%0A    
@@ -2525,25 +2525,26 @@
 ndRatio(
-accordion
+optionsTab
 , 1.0f);
@@ -3369,25 +3369,26 @@
      && 
-accordion
+optionsTab
 .getSele
@@ -3575,33 +3575,34 @@
     %7B%0A      
-accordion
+optionsTab
 .removeTab(o
@@ -3626,33 +3626,34 @@
 optionTab = 
-accordion
+optionsTab
 .addTab(opti
@@ -3844,25 +3844,26 @@
         
-accordion
+optionsTab
 .removeT
@@ -3910,25 +3910,26 @@
 onTab = 
-accordion
+optionsTab
 .addTab(
@@ -4015,17 +4015,18 @@
     
-accordion
+optionsTab
 .set

use a tab sheet instead of an accordion