/*
 * 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.visualizers.component.rst;

import annis.gui.visualizers.VisualizerInput;
import com.vaadin.ui.Button;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Panel;
import java.util.Properties;
import org.vaadin.csstools.RenderInfo;
import org.vaadin.csstools.client.VRenderInfoFetcher.CssProperty;

/**
 *
 *
 * @author Benjamin Weißenfels <b.pixeldrama@gmail.com>
 */
public class RSTPanel extends Panel
{

  RSTPanel(VisualizerInput visInput)
  {
    String btWidth = "30px";
    HorizontalLayout grid = new HorizontalLayout();
    final int scrollStep = 200;
    final Panel rstView = new RSTImpl(visInput);

    this.setHeight("-1px");
    this.setWidth("100%");
    grid.setHeight("-1px");
    grid.setWidth("100%");


    Button buttonLeft = new Button();
    buttonLeft.setWidth(btWidth);
    buttonLeft.setHeight("100%");
    buttonLeft.addStyleName("left-button");

    buttonLeft.addListener(new Button.ClickListener()
    {
      @Override
      public void buttonClick(Button.ClickEvent event)
      {
        getScrollLeft();

        if (rstView.getScrollLeft() < scrollStep)
        {
          rstView.setScrollLeft(0);
        }
        else
        {
          rstView.setScrollLeft(rstView.getScrollLeft() - scrollStep);
        }
      }
    });

    Button buttonRight = new Button();
    buttonRight.setWidth(btWidth);
    buttonRight.setHeight("100%");
    buttonRight.addStyleName("right-button");

    buttonRight.addListener(new Button.ClickListener()
    {
      @Override
      public void buttonClick(Button.ClickEvent event)
      {
        final Properties props = new Properties();
        RenderInfo.get(rstView, new RenderInfo.Callback()
        {
          @Override
          public void infoReceived(RenderInfo info)
          {
            props.put("width", info.getProperty(CssProperty.width));
            String width = ((String) props.get("width")).replaceAll("px", "");
            int maxWidth = Integer.parseInt(width);
            if (maxWidth > rstView.getScrollLeft())
            {
              rstView.setScrollLeft(rstView.getScrollLeft() + scrollStep);
            }
          }
        });
      }
    });

    grid.addComponent(buttonLeft);
    grid.addComponent(rstView);
    grid.addComponent(buttonRight);
    setContent(grid);
    grid.setExpandRatio(rstView, 1.0f);
  }
}

@@ -1384,24 +1384,30 @@
 0%25%22);%0A%0A%0A    
+final 
 Button butto
@@ -1531,32 +1531,228 @@
 (%22left-button%22);
+%0A    buttonLeft.setEnabled(false);%0A%0A    final Button buttonRight = new Button();%0A    buttonRight.setWidth(btWidth);%0A    buttonRight.setHeight(%22100%25%22);%0A    buttonRight.addStyleName(%22right-button%22);
 %0A%0A    buttonLeft
@@ -1880,34 +1880,8 @@
   %7B%0A
-        getScrollLeft();%0A%0A
     
@@ -1928,32 +1928,72 @@
 Step)%0A        %7B%0A
+          buttonLeft.setEnabled(false);%0A
           rstVie
@@ -2037,32 +2037,92 @@
  else%0A        %7B%0A
+          //if the right button was deactivated set it back%0A
           rstVie
@@ -2190,177 +2190,60 @@
   %7D%0A
-      %7D%0A    %7D);%0A%0A    Button buttonRight = new Button();%0A    buttonRight.setWidth(btWidth);%0A    buttonRight.setHeight(%22100%25%22);%0A    buttonRight.addStyleName(%22right-button%22
+%0A        buttonRight.setEnabled(true);%0A      %7D%0A    %7D
 );%0A%0A
@@ -2781,24 +2781,25 @@
 Int(width);%0A
+%0A
             
@@ -2811,17 +2811,17 @@
 axWidth 
-%3E
+-
  rstView
@@ -2836,16 +2836,29 @@
 llLeft()
+ %3E scrollStep
 )%0A      
@@ -2857,32 +2857,75 @@
 )%0A            %7B%0A
+              buttonLeft.setEnabled(true);%0A
               rs
@@ -2989,32 +2989,301 @@
 ;%0A            %7D%0A
+            else%0A            %7B%0A              rstView.%0A                setScrollLeft(%0A                rstView.getScrollLeft() - (maxWidth - rstView.getScrollLeft()));%0A%0A              buttonLeft.setEnabled(true);%0A              buttonRight.setEnabled(false);%0A            %7D%0A
           %7D%0A    

Disable rst scroll buttons when left or right border is reached.