/*
 * ARX: Powerful Data Anonymization
 * Copyright (C) 2012 - 2014 Florian Kohlmayer, Fabian Prasser
 * 
 * This program 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.
 * 
 * This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
 */

package org.deidentifier.arx.gui.view.impl.common.table;

import org.eclipse.nebula.widgets.nattable.command.ILayerCommand;
import org.eclipse.nebula.widgets.nattable.command.StructuralRefreshCommand;
import org.eclipse.nebula.widgets.nattable.grid.command.ClientAreaResizeCommand;
import org.eclipse.nebula.widgets.nattable.layer.IUniqueIndexLayer;
import org.eclipse.nebula.widgets.nattable.selection.ScrollSelectionCommandHandler;
import org.eclipse.nebula.widgets.nattable.viewport.ViewportLayer;
import org.eclipse.nebula.widgets.nattable.viewport.command.RecalculateScrollBarsCommand;
import org.eclipse.nebula.widgets.nattable.viewport.command.RecalculateScrollBarsCommandHandler;
import org.eclipse.nebula.widgets.nattable.viewport.command.ViewportDragCommandHandler;
import org.eclipse.nebula.widgets.nattable.viewport.command.ViewportSelectColumnCommandHandler;
import org.eclipse.nebula.widgets.nattable.viewport.command.ViewportSelectRowCommandHandler;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Listener;

public class LayerViewport extends ViewportLayer{

    private CTContext context;
    
    public LayerViewport(IUniqueIndexLayer underlyingLayer, CTContext context) {
        super(underlyingLayer);
        this.context = context;
    }
    

    @Override
    public boolean doCommand(ILayerCommand command) {
        
        context.getTable().getHorizontalBar().getParent().setRedraw(false);
        context.getTable().getVerticalBar().getParent().setRedraw(false);
        
        boolean result = super.doCommand(command);
        
        if (command instanceof ClientAreaResizeCommand) {
            checkScrollBars();
        } else if (command instanceof RecalculateScrollBarsCommand) {
            checkScrollBars();
        } else if (command instanceof StructuralRefreshCommand) {
            checkScrollBars();
        }
        
        context.getTable().getHorizontalBar().getParent().setRedraw(true);
        context.getTable().getVerticalBar().getParent().setRedraw(true);
        
        return result;
    }

    @Override
    public void moveCellPositionIntoViewport(int scrollableColumnPosition, int scrollableRowPosition) {
        if (!(context.isRowExpanded() && context.isColumnExpanded())) {
            super.moveCellPositionIntoViewport(scrollableColumnPosition, scrollableRowPosition);
        }
    }

    @Override
    public void moveColumnPositionIntoViewport(int scrollableColumnPosition) {
        if (!context.isColumnExpanded()) {
            super.moveColumnPositionIntoViewport(scrollableColumnPosition);
        }
    }


    @Override
    public void moveRowPositionIntoViewport(int scrollableRowPosition) {
        if (!context.isRowExpanded()) {
            super.moveRowPositionIntoViewport(scrollableRowPosition);
        }
    }

    private void checkScrollBars() {
        
        if (context.getTable().isDisposed()) {
            return;
        }

        Listener[] listeners = context.getTable().getListeners(SWT.Resize);
        for (Listener listener : listeners) {
            context.getTable().removeListener(SWT.Resize, listener);
        }
        
        if (context.isColumnExpanded()) {
            context.getTable().getHorizontalBar().setVisible(true);
            context.getTable().getHorizontalBar().setValues(0, 0, 1, 1, 1, 1);
            context.getTable().getHorizontalBar().setEnabled(false);
        }

        if (context.isRowExpanded()) {
            context.getTable().getVerticalBar().setVisible(true);
            context.getTable().getVerticalBar().setValues(0, 0, 1, 1, 1, 1);
            context.getTable().getVerticalBar().setEnabled(false);
        }
        
        for (Listener listener : listeners) {
            context.getTable().addListener(SWT.Resize, listener);
        }
    }

    @Override
    protected boolean isLastColumnCompletelyDisplayed() {
        if (context.isColumnExpanded()) return true;
        else return super.isLastColumnCompletelyDisplayed();
    }

    @Override
    protected boolean isLastRowCompletelyDisplayed() {
        if (context.isRowExpanded()) return true;
        else return super.isLastRowCompletelyDisplayed();
    }


    @Override
    protected void registerCommandHandlers() {
        registerCommandHandler(new RecalculateScrollBarsCommandHandler(this));
        registerCommandHandler(new ScrollSelectionCommandHandler(this));
        registerCommandHandler(new ViewportSelectColumnCommandHandler(this));
        registerCommandHandler(new ViewportSelectRowCommandHandler(this));
        registerCommandHandler(new ViewportDragCommandHandler(this));
    }
}

@@ -2098,32 +2098,114 @@
 and) %7B%0A        %0A
+        if (context.getTable() != null && !context.getTable().isDisposed()) %7B%0A    
         context.
@@ -2256,32 +2256,36 @@
 tRedraw(false);%0A
+    
         context.
@@ -2334,32 +2334,42 @@
 tRedraw(false);%0A
+        %7D%0A
         %0A       
@@ -2719,32 +2719,114 @@
      %7D%0A        %0A
+        if (context.getTable() != null && !context.getTable().isDisposed()) %7B%0A    
         context.
@@ -2876,32 +2876,36 @@
 etRedraw(true);%0A
+    
         context.
@@ -2953,32 +2953,42 @@
 etRedraw(true);%0A
+        %7D%0A
         %0A       
@@ -3912,16 +3912,124 @@
 teners =
+ null;%0A        if (context.getTable() != null && !context.getTable().isDisposed()) %7B%0A            listeners =
  context
@@ -4062,24 +4062,28 @@
 WT.Resize);%0A
+    
         for 
@@ -4120,32 +4120,36 @@
 ) %7B%0A            
+  
+  
 context.getTable
@@ -4181,32 +4181,46 @@
 ize, listener);%0A
+            %7D%0A
         %7D%0A      
@@ -4751,32 +4751,114 @@
      %7D%0A        %0A
+        if (context.getTable() != null && !context.getTable().isDisposed()) %7B%0A    
         for (Lis
@@ -4879,32 +4879,36 @@
  : listeners) %7B%0A
+    
             cont
@@ -4949,32 +4949,46 @@
 ize, listener);%0A
+            %7D%0A
         %7D%0A    %7D%0A

check if table is disposed or null